diff --git a/include/asset.h b/include/asset.h index 7feb65f..6d8b8cb 100644 --- a/include/asset.h +++ b/include/asset.h @@ -32,6 +32,8 @@ CLASS(Asset) { + unsigned long hash; + char fname[2048]; char etag[200]; char mtime[200]; diff --git a/src/asset/asset.c b/src/asset/asset.c index 2aa1a6a..2f9628f 100644 --- a/src/asset/asset.c +++ b/src/asset/asset.c @@ -39,8 +39,11 @@ #include "class.h" #include "asset.h" +#include "hash.h" #include "utils/mime_type.h" +#include "utils/hash.h" + static int @@ -58,6 +61,10 @@ assetCtor(void * _this, va_list * params) strncpy(this->fname, fname, 2047); this->fname[2048] = '\0'; + this->hash = sdbm( + (unsigned char *)this->fname, + this->nfname); + if (-1 == access(this->fname, O_RDONLY)) { return -1; } else { @@ -110,7 +117,23 @@ static void assetDtor(void * _this) { } } +static +unsigned long +assetGetHash(void * _this) +{ + Asset this = _this; + + return this->hash; +} + +static +void +assetHandleDouble(void * _this, void * _doub) +{ +} + INIT_IFACE(Class, assetCtor, assetDtor, NULL); -CREATE_CLASS(Asset, NULL, IFACE(Class)); +INIT_IFACE(Hashable, assetGetHash, assetHandleDouble); +CREATE_CLASS(Asset, NULL, IFACE(Class), IFACE(Hashable)); // vim: set ts=4 sw=4: diff --git a/src/asset/pool.c b/src/asset/pool.c index a7b768d..0d4fc19 100644 --- a/src/asset/pool.c +++ b/src/asset/pool.c @@ -36,9 +36,7 @@ inline void freeAsset(const void * _node) { - Asset node = (Asset)_node; - - delete(node); + delete(_node); } Asset @@ -54,9 +52,7 @@ assetPoolGet(const char * path, size_t npath) if (NULL == asset) { asset = new(Asset, path, npath); - - hashAdd(asset_pool, - new(HashValue, path, npath, asset, sizeof(Asset))); + hashAdd(asset_pool, asset); } else { asset->ref_count++; } @@ -68,8 +64,11 @@ size_t assetPoolRelease(Asset asset) { if (asset->ref_count <= 1) { - hashDelete(asset_pool, asset->fname, asset->nfname); - delete(asset); + hashDelete( asset_pool, asset->fname, asset->nfname); + + if (NULL != asset) { + delete(asset); + } return 0; } else { @@ -83,6 +82,7 @@ void assetPoolCleanup(void) { hashEach(asset_pool, freeAsset); + delete(asset_pool); } // vim: set ts=4 sw=4: diff --git a/src/hash/delete.c b/src/hash/delete.c index 17c83ea..a14c70c 100644 --- a/src/hash/delete.c +++ b/src/hash/delete.c @@ -37,10 +37,19 @@ hashDeleteComp(const void * a, const void * b) void * hashDelete(Hash this, const char * search, size_t nsearch) { - unsigned long hash = sdbm((const unsigned char *)search, nsearch); - void * found = tdelete(&hash, &(this->root), hashDeleteComp); + unsigned long hash = sdbm((const unsigned char *)search, nsearch); + void ** _found = tfind(&hash, &(this->root), hashDeleteComp); + void * found; - return (NULL != found)? *(void**)found : NULL; + if (NULL != _found) { + found = *_found; + } else { + found = NULL; + } + + tdelete(&hash, &(this->root), hashDeleteComp); + + return (NULL != found)? found : NULL; } // vim: set ts=4 sw=4: