taskrambler  v0.1.8
Web server and task management solution.
pool.c
Go to the documentation of this file.
1 /**
2  * \file
3  *
4  * \author Georg Hopp
5  *
6  * \copyright
7  * Copyright © 2012 Georg Hopp
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 // for size_t
24 #include <sys/types.h>
25 
26 // for strlen
27 
28 #include "class.h"
29 #include "asset.h"
30 #include "hash.h"
31 
32 Hash asset_pool = NULL;
33 
34 static
35 inline
36 void
37 freeAsset(const void * _node)
38 {
39  delete(_node);
40 }
41 
42 Asset
43 assetPoolGet(const char * path, size_t npath)
44 {
45  Asset asset = NULL;
46 
47  if (NULL == asset_pool) {
48  asset_pool = new(Hash);
49  } else {
50  asset = hashGet(asset_pool, path, npath);
51  }
52 
53  if (NULL == asset) {
54  asset = new(Asset, path, npath);
55  if (NULL != asset) {
56  hashAdd(asset_pool, asset);
57  }
58  } else {
59  asset->ref_count++;
60  }
61 
62  return asset;
63 }
64 
65 size_t
66 assetPoolRelease(Asset asset)
67 {
68  if (asset->ref_count > 1) {
69  asset->ref_count--;
70  return asset->ref_count;
71  }
72 
73  if (NULL != asset) {
74  Asset found = (Asset)hashDelete(
75  asset_pool, asset->fname, asset->nfname);
76 
77  if (found == asset) {
78  delete(found);
79  } else {
80  // this should never happen....error log...
81  }
82  }
83 
84  return 0;
85 }
86 
87 void
89 {
90  if (NULL != asset_pool) {
92  delete(asset_pool);
93  }
94 }
95 
96 // vim: set ts=4 sw=4:
size_t assetPoolRelease(Asset asset)
Definition: pool.c:66
static void freeAsset(const void *_node)
Definition: pool.c:37
void * hashGet(Hash, const char *, size_t)
Definition: hash/get.c:51
Hash asset_pool
Definition: pool.c:32
Asset assetPoolGet(const char *path, size_t npath)
Definition: pool.c:43
void assetPoolCleanup(void)
Definition: pool.c:88
void * hashDelete(Hash, const char *, size_t)
Definition: hash/delete.c:48
void * hashAdd(Hash, void *)
Definition: add.c:48
void hashEach(Hash, void(*)(const void *))
Definition: each.c:38