diff options
author | FlavioJS <FlavioJS@54d463be-8e91-2dee-dedb-b68131a5f0ec> | 2007-11-08 14:08:32 +0000 |
---|---|---|
committer | FlavioJS <FlavioJS@54d463be-8e91-2dee-dedb-b68131a5f0ec> | 2007-11-08 14:08:32 +0000 |
commit | b5be01c5baca2d51da4ec9a138e3bb57eea7cbb4 (patch) | |
tree | b7f079f5317d736e49fdf8b12f6d3f18b2a98daf /src/common/db.c | |
parent | ad2a3a12e2d603e8ef2011d1121271b7ed2f05eb (diff) | |
download | hercules-b5be01c5baca2d51da4ec9a138e3bb57eea7cbb4.tar.gz hercules-b5be01c5baca2d51da4ec9a138e3bb57eea7cbb4.tar.bz2 hercules-b5be01c5baca2d51da4ec9a138e3bb57eea7cbb4.tar.xz hercules-b5be01c5baca2d51da4ec9a138e3bb57eea7cbb4.zip |
* Changed EXIT_SUCCESS back to 0 in console.c to avoid an unnecessary include.
* Fixed gm_account_db not being deallocated in login-converter.c.
* Refactoring names and documentation in db.h/db.c:
- changed 'struct dbt' to 'struct DBMap' and 'DB' to 'DBMap*'
- changed 'struct db' to 'struct DBMap_impl' and 'DB_impl' to 'DBMap_impl*'
- changed COUNT to DB_COUNTSTAT and made it's existence not depend on DB_ENABLE_STATS
- removed some @see links and corrected small typos in the documentation
git-svn-id: https://rathena.svn.sourceforge.net/svnroot/rathena/trunk@11698 54d463be-8e91-2dee-dedb-b68131a5f0ec
Diffstat (limited to 'src/common/db.c')
-rw-r--r-- | src/common/db.c | 457 |
1 files changed, 173 insertions, 284 deletions
diff --git a/src/common/db.c b/src/common/db.c index 476c5a0c9..27aa6776a 100644 --- a/src/common/db.c +++ b/src/common/db.c @@ -82,7 +82,7 @@ * DBNColor - Enumeration of colors of the nodes. * * DBNode - Structure of a node in RED-BLACK trees. * * struct db_free - Structure that holds a deleted node to be freed. * - * DB_impl - Struture of the database. * + * DBMap_impl - Struture of the database. * * stats - Statistics about the database system. * \*****************************************************************************/ @@ -101,7 +101,7 @@ /** * Size of the hashtable in the database. * @private - * @see DB_impl#ht + * @see DBMap_impl#ht */ #define HASH_SIZE (256+27) @@ -115,7 +115,7 @@ * @param deleted If the node is deleted * @param color Color of the node * @private - * @see DB_impl#ht + * @see DBMap_impl#ht */ typedef struct dbn { // Tree structure @@ -135,7 +135,7 @@ typedef struct dbn { * @param node Deleted node * @param root Address to the root of the tree * @private - * @see DB_impl#free_list + * @see DBMap_impl#free_list */ struct db_free { DBNode node; @@ -144,7 +144,7 @@ struct db_free { /** * Complete database structure. - * @param dbi Interface of the database + * @param vtable Interface of the database * @param alloc_file File where the database was allocated * @param alloc_line Line in the file where the database was allocated * @param free_list Array of deleted nodes to be freed @@ -162,11 +162,11 @@ struct db_free { * @param maxlen Maximum length of strings in DB_STRING and DB_ISTRING databases * @param global_lock Global lock of the database * @private - * @see #db_alloc(const char *,int,DBOptions,DBType,...) + * @see #db_alloc(const char*,int,DBType,DBOptions,unsigned short) */ -typedef struct db { +typedef struct DBMap_impl { // Database interface - struct dbt vtable; + struct DBMap vtable; // File and line of allocation const char *alloc_file; int alloc_line; @@ -187,9 +187,9 @@ typedef struct db { uint32 item_count; unsigned short maxlen; unsigned global_lock : 1; -} *DB_impl; +} DBMap_impl; -#ifdef DB_ENABLE_STATS +#if defined(DB_ENABLE_STATS) /** * Structure with what is counted when the database estatistics are enabled. * @private @@ -268,8 +268,10 @@ static struct db_stats { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; -#define COUNT(func) if (stats. ## func != UINT32_MAX) ++stats. ## func -#endif /* DB_ENABLE_STATS */ +#define DB_COUNTSTAT(token) if (stats. ## token != UINT32_MAX) ++stats. ## token +#else /* !defined(DB_ENABLE_STATS) */ +#define DB_COUNTSTAT(token) +#endif /* !defined(DB_ENABLE_STATS) */ /*****************************************************************************\ * (2) Section of private functions used by the database system. * @@ -300,9 +302,7 @@ static void db_rotate_left(DBNode node, DBNode *root) { DBNode y = node->right; -#ifdef DB_ENABLE_STATS - COUNT(db_rotate_left); -#endif /* DB_ENABLE_STATS */ + DB_COUNTSTAT(db_rotate_left); // put the left of y at the right of node node->right = y->left; if (y->left) @@ -333,9 +333,7 @@ static void db_rotate_right(DBNode node, DBNode *root) { DBNode y = node->left; -#ifdef DB_ENABLE_STATS - COUNT(db_rotate_right); -#endif /* DB_ENABLE_STATS */ + DB_COUNTSTAT(db_rotate_right); // put the right of y at the left of node node->left = y->right; if (y->right != 0) @@ -362,15 +360,13 @@ static void db_rotate_right(DBNode node, DBNode *root) * @private * @see #db_rotate_left(DBNode,DBNode *) * @see #db_rotate_right(DBNode,DBNode *) - * @see #db_put(DB,DBKey,void *) + * @see #db_obj_put(DBMap*,DBKey,void *) */ static void db_rebalance(DBNode node, DBNode *root) { DBNode y; -#ifdef DB_ENABLE_STATS - COUNT(db_rebalance); -#endif /* DB_ENABLE_STATS */ + DB_COUNTSTAT(db_rebalance); // Restore the RED-BLACK properties node->color = RED; while (node != *root && node->parent->color == RED) { @@ -426,7 +422,7 @@ static void db_rebalance(DBNode node, DBNode *root) * @private * @see #db_rotate_left(DBNode,DBNode *) * @see #db_rotate_right(DBNode,DBNode *) - * @see #db_free_unlock(DB_impl) + * @see #db_free_unlock(DBMap_impl*) */ static void db_rebalance_erase(DBNode node, DBNode *root) { @@ -435,9 +431,7 @@ static void db_rebalance_erase(DBNode node, DBNode *root) DBNode x_parent = NULL; DBNode w; -#ifdef DB_ENABLE_STATS - COUNT(db_rebalance_erase); -#endif /* DB_ENABLE_STATS */ + DB_COUNTSTAT(db_rebalance_erase); // Select where to change the tree if (y->left == NULL) { // no left x = y->right; @@ -567,15 +561,13 @@ static void db_rebalance_erase(DBNode node, DBNode *root) * @param key Key being tested * @return not 0 if considered NULL, 0 otherwise * @private - * @see #db_get(DB,DBKey) - * @see #db_put(DB,DBKey,void *) - * @see #db_remove(DB,DBKey) + * @see #db_obj_get(DBMap*,DBKey) + * @see #db_obj_put(DBMap*,DBKey,void *) + * @see #db_obj_remove(DBMap*,DBKey) */ static int db_is_key_null(DBType type, DBKey key) { -#ifdef DB_ENABLE_STATS - COUNT(db_is_key_null); -#endif /* DB_ENABLE_STATS */ + DB_COUNTSTAT(db_is_key_null); switch (type) { case DB_STRING: case DB_ISTRING: @@ -592,18 +584,16 @@ static int db_is_key_null(DBType type, DBKey key) * @param key Key to be duplicated * @param Duplicated key * @private - * @see #db_free_add(DB_impl,DBNode,DBNode *) - * @see #db_free_remove(DB_impl,DBNode) - * @see #db_put(DB,DBKey,void *) - * @see #db_dup_key_free(DB_impl,DBKey) + * @see #db_free_add(DBMap_impl*,DBNode,DBNode *) + * @see #db_free_remove(DBMap_impl*,DBNode) + * @see #db_obj_put(DBMap*,DBKey,void *) + * @see #db_dup_key_free(DBMap_impl*,DBKey) */ -static DBKey db_dup_key(DB_impl db, DBKey key) +static DBKey db_dup_key(DBMap_impl* db, DBKey key) { char *str; -#ifdef DB_ENABLE_STATS - COUNT(db_dup_key); -#endif /* DB_ENABLE_STATS */ + DB_COUNTSTAT(db_dup_key); switch (db->type) { case DB_STRING: case DB_ISTRING: @@ -627,13 +617,11 @@ static DBKey db_dup_key(DB_impl db, DBKey key) * @param db Database the key is being used in * @param key Key to be freed * @private - * @see #db_dup_key(DB_impl,DBKey) + * @see #db_dup_key(DBMap_impl*,DBKey) */ -static void db_dup_key_free(DB_impl db, DBKey key) +static void db_dup_key_free(DBMap_impl* db, DBKey key) { -#ifdef DB_ENABLE_STATS - COUNT(db_dup_key_free); -#endif /* DB_ENABLE_STATS */ + DB_COUNTSTAT(db_dup_key_free); switch (db->type) { case DB_STRING: case DB_ISTRING: @@ -654,19 +642,17 @@ static void db_dup_key_free(DB_impl db, DBKey key) * @param node Target node * @private * @see #struct db_free - * @see DB_impl#free_list - * @see DB_impl#free_count - * @see DB_impl#free_max - * @see #db_remove(DB,DBKey) - * @see #db_free_remove(DB_impl,DBNode) + * @see DBMap_impl#free_list + * @see DBMap_impl#free_count + * @see DBMap_impl#free_max + * @see #db_obj_remove(DBMap*,DBKey) + * @see #db_free_remove(DBMap_impl*,DBNode) */ -static void db_free_add(DB_impl db, DBNode node, DBNode *root) +static void db_free_add(DBMap_impl* db, DBNode node, DBNode *root) { DBKey old_key; -#ifdef DB_ENABLE_STATS - COUNT(db_free_add); -#endif /* DB_ENABLE_STATS */ + DB_COUNTSTAT(db_free_add); if (db->free_lock == (unsigned int)~0) { ShowFatalError("db_free_add: free_lock overflow\n" "Database allocated at %s:%d\n", @@ -706,18 +692,16 @@ static void db_free_add(DB_impl db, DBNode node, DBNode *root) * @param node Node being removed from free_list * @private * @see #struct db_free - * @see DB_impl#free_list - * @see DB_impl#free_count - * @see #db_put(DB,DBKey,void *) - * @see #db_free_add(DB_impl,DBNode *,DBNode) + * @see DBMap_impl#free_list + * @see DBMap_impl#free_count + * @see #db_obj_put(DBMap*,DBKey,void*) + * @see #db_free_add(DBMap_impl*,DBNode*,DBNode) */ -static void db_free_remove(DB_impl db, DBNode node) +static void db_free_remove(DBMap_impl* db, DBNode node) { unsigned int i; -#ifdef DB_ENABLE_STATS - COUNT(db_free_remove); -#endif /* DB_ENABLE_STATS */ + DB_COUNTSTAT(db_free_remove); for (i = 0; i < db->free_count; i++) { if (db->free_list[i].node == node) { if (i < db->free_count -1) // copy the last item to where the removed one was @@ -739,14 +723,12 @@ static void db_free_remove(DB_impl db, DBNode node) * Increment the free_lock of the database. * @param db Target database * @private - * @see DB_impl#free_lock - * @see #db_unlock(DB_impl) + * @see DBMap_impl#free_lock + * @see #db_unlock(DBMap_impl*) */ -static void db_free_lock(DB_impl db) +static void db_free_lock(DBMap_impl* db) { -#ifdef DB_ENABLE_STATS - COUNT(db_free_lock); -#endif /* DB_ENABLE_STATS */ + DB_COUNTSTAT(db_free_lock); if (db->free_lock == (unsigned int)~0) { ShowFatalError("db_free_lock: free_lock overflow\n" "Database allocated at %s:%d\n", @@ -763,17 +745,15 @@ static void db_free_lock(DB_impl db) * NOTE: Frees the duplicated keys of the nodes * @param db Target database * @private - * @see DB_impl#free_lock + * @see DBMap_impl#free_lock * @see #db_free_dbn(DBNode) - * @see #db_lock(DB_impl) + * @see #db_lock(DBMap_impl*) */ -static void db_free_unlock(DB_impl db) +static void db_free_unlock(DBMap_impl* db) { unsigned int i; -#ifdef DB_ENABLE_STATS - COUNT(db_free_unlock); -#endif /* DB_ENABLE_STATS */ + DB_COUNTSTAT(db_free_unlock); if (db->free_lock == 0) { ShowWarning("db_free_unlock: free_lock was already 0\n" "Database allocated at %s:%d\n", @@ -787,9 +767,7 @@ static void db_free_unlock(DB_impl db) for (i = 0; i < db->free_count ; i++) { db_rebalance_erase(db->free_list[i].node, db->free_list[i].root); db_dup_key_free(db, db->free_list[i].node->key); -#ifdef DB_ENABLE_STATS - if (stats.db_node_free != (unsigned int)~0) stats.db_node_free++; -#endif /* DB_ENABLE_STATS */ + DB_COUNTSTAT(db_node_free); ers_free(db->nodes, db->free_list[i].node); } db->free_count = 0; @@ -829,9 +807,7 @@ static void db_free_unlock(DB_impl db) static int db_int_cmp(DBKey key1, DBKey key2, unsigned short maxlen) { (void)maxlen;//not used -#ifdef DB_ENABLE_STATS - COUNT(db_int_cmp); -#endif /* DB_ENABLE_STATS */ + DB_COUNTSTAT(db_int_cmp); if (key1.i < key2.i) return -1; if (key1.i > key2.i) return 1; return 0; @@ -853,9 +829,7 @@ static int db_int_cmp(DBKey key1, DBKey key2, unsigned short maxlen) static int db_uint_cmp(DBKey key1, DBKey key2, unsigned short maxlen) { (void)maxlen;//not used -#ifdef DB_ENABLE_STATS - COUNT(db_uint_cmp); -#endif /* DB_ENABLE_STATS */ + DB_COUNTSTAT(db_uint_cmp); if (key1.ui < key2.ui) return -1; if (key1.ui > key2.ui) return 1; return 0; @@ -875,9 +849,7 @@ static int db_uint_cmp(DBKey key1, DBKey key2, unsigned short maxlen) */ static int db_string_cmp(DBKey key1, DBKey key2, unsigned short maxlen) { -#ifdef DB_ENABLE_STATS - COUNT(db_string_cmp); -#endif /* DB_ENABLE_STATS */ + DB_COUNTSTAT(db_string_cmp); if (maxlen == 0) maxlen = UINT16_MAX; return strncmp((const char *)key1.str, (const char *)key2.str, maxlen); @@ -897,9 +869,7 @@ static int db_string_cmp(DBKey key1, DBKey key2, unsigned short maxlen) */ static int db_istring_cmp(DBKey key1, DBKey key2, unsigned short maxlen) { -#ifdef DB_ENABLE_STATS - COUNT(db_istring_cmp); -#endif /* DB_ENABLE_STATS */ + DB_COUNTSTAT(db_istring_cmp); if (maxlen == 0) maxlen = UINT16_MAX; return strncasecmp((const char *)key1.str, (const char *)key2.str, maxlen); @@ -919,9 +889,7 @@ static int db_istring_cmp(DBKey key1, DBKey key2, unsigned short maxlen) static unsigned int db_int_hash(DBKey key, unsigned short maxlen) { (void)maxlen;//not used -#ifdef DB_ENABLE_STATS - COUNT(db_int_hash); -#endif /* DB_ENABLE_STATS */ + DB_COUNTSTAT(db_int_hash); return (unsigned int)key.i; } @@ -939,9 +907,7 @@ static unsigned int db_int_hash(DBKey key, unsigned short maxlen) static unsigned int db_uint_hash(DBKey key, unsigned short maxlen) { (void)maxlen;//not used -#ifdef DB_ENABLE_STATS - COUNT(db_uint_hash); -#endif /* DB_ENABLE_STATS */ + DB_COUNTSTAT(db_uint_hash); return key.ui; } @@ -961,9 +927,7 @@ static unsigned int db_string_hash(DBKey key, unsigned short maxlen) unsigned int hash = 0; unsigned short i; -#ifdef DB_ENABLE_STATS - COUNT(db_string_hash); -#endif /* DB_ENABLE_STATS */ + DB_COUNTSTAT(db_string_hash); if (maxlen == 0) maxlen = UINT16_MAX; @@ -992,9 +956,7 @@ static unsigned int db_istring_hash(DBKey key, unsigned short maxlen) unsigned int hash = 0; unsigned short i; -#ifdef DB_ENABLE_STATS - COUNT(db_istring_hash); -#endif /* DB_ENABLE_STATS */ + DB_COUNTSTAT(db_istring_hash); if (maxlen == 0) maxlen = UINT16_MAX; @@ -1020,9 +982,7 @@ static unsigned int db_istring_hash(DBKey key, unsigned short maxlen) static void db_release_nothing(DBKey key, void *data, DBRelease which) { (void)key;(void)data;(void)which;//not used -#ifdef DB_ENABLE_STATS - COUNT(db_release_nothing); -#endif /* DB_ENABLE_STATS */ + DB_COUNTSTAT(db_release_nothing); } /** @@ -1037,9 +997,7 @@ static void db_release_nothing(DBKey key, void *data, DBRelease which) static void db_release_key(DBKey key, void *data, DBRelease which) { (void)data;//not used -#ifdef DB_ENABLE_STATS - COUNT(db_release_key); -#endif /* DB_ENABLE_STATS */ + DB_COUNTSTAT(db_release_key); if (which&DB_RELEASE_KEY) aFree((char*)key.str); // needs to be a pointer } @@ -1049,17 +1007,15 @@ static void db_release_key(DBKey key, void *data, DBRelease which) * @param data Data of the database entry * @param which What is being requested to be released * @protected - * @see common\db.h#DBKey - * @see common\db.h#DBRelease - * @see common\db.h#DBReleaser + * @see #DBKey + * @see #DBRelease + * @see #DBReleaser * @see #db_default_release(DBType,DBOptions) */ static void db_release_data(DBKey key, void *data, DBRelease which) { (void)key;//not used -#ifdef DB_ENABLE_STATS - COUNT(db_release_data); -#endif /* DB_ENABLE_STATS */ + DB_COUNTSTAT(db_release_data); if (which&DB_RELEASE_DATA) aFree(data); } @@ -1069,16 +1025,14 @@ static void db_release_data(DBKey key, void *data, DBRelease which) * @param data Data of the database entry * @param which What is being requested to be released * @protected - * @see common\db.h#DBKey - * @see common\db.h#DBRelease - * @see common\db.h#DBReleaser + * @see #DBKey + * @see #DBRelease + * @see #DBReleaser * @see #db_default_release(DBType,DBOptions) */ static void db_release_both(DBKey key, void *data, DBRelease which) { -#ifdef DB_ENABLE_STATS - COUNT(db_release_both); -#endif /* DB_ENABLE_STATS */ + DB_COUNTSTAT(db_release_both); if (which&DB_RELEASE_KEY) aFree((char*)key.str); // needs to be a pointer if (which&DB_RELEASE_DATA) aFree(data); } @@ -1112,18 +1066,16 @@ static void db_release_both(DBKey key, void *data, DBRelease which) * @param key Key that identifies the entry * @return Data of the entry or NULL if not found * @protected - * @see DB_impl::vtable#get + * @see DBMap#get */ -static void *db_obj_get(DB self, DBKey key) +static void* db_obj_get(DBMap* self, DBKey key) { - DB_impl db = (DB_impl)self; + DBMap_impl* db = (DBMap_impl*)self; DBNode node; int c; void *data = NULL; -#ifdef DB_ENABLE_STATS - COUNT(db_get); -#endif /* DB_ENABLE_STATS */ + DB_COUNTSTAT(db_get); if (db == NULL) return NULL; // nullpo candidate if (!(db->options&DB_OPT_ALLOW_NULL_KEY) && db_is_key_null(db->type, key)) { ShowError("db_get: Attempted to retrieve non-allowed NULL key for db allocated at %s:%d\n",db->alloc_file, db->alloc_line); @@ -1165,19 +1117,17 @@ static void *db_obj_get(DB self, DBKey key) * @param ... Extra arguments for match * @return The number of entries that matched * @protected - * @see DB_impl::vtable#vgetall + * @see DBMap#vgetall */ -static unsigned int db_obj_vgetall(DB self, void **buf, unsigned int max, DBMatcher match, va_list args) +static unsigned int db_obj_vgetall(DBMap* self, void **buf, unsigned int max, DBMatcher match, va_list args) { - DB_impl db = (DB_impl)self; + DBMap_impl* db = (DBMap_impl*)self; unsigned int i; DBNode node; DBNode parent; unsigned int ret = 0; -#ifdef DB_ENABLE_STATS - COUNT(db_vgetall); -#endif /* DB_ENABLE_STATS */ + DB_COUNTSTAT(db_vgetall); if (db == NULL) return 0; // nullpo candidate if (match == NULL) return 0; // nullpo candidate @@ -1215,7 +1165,7 @@ static unsigned int db_obj_vgetall(DB self, void **buf, unsigned int max, DBMatc } /** - * Just calls {@link DB#vgetall(DB,void **,unsigned int,DBMatch,va_list)}. + * Just calls {@link DBMap#vgetall}. * Get the data of the entries matched by <code>match</code>. * It puts a maximum of <code>max</code> entries into <code>buf</code>. * If <code>buf</code> is NULL, it only counts the matches. @@ -1229,17 +1179,15 @@ static unsigned int db_obj_vgetall(DB self, void **buf, unsigned int max, DBMatc * @param ... Extra arguments for match * @return The number of entries that matched * @protected - * @see DB_impl::vtable#vgetall - * @see DB_impl::vtable#getall + * @see DBMap#vgetall + * @see DBMap#getall */ -static unsigned int db_obj_getall(DB self, void **buf, unsigned int max, DBMatcher match, ...) +static unsigned int db_obj_getall(DBMap* self, void **buf, unsigned int max, DBMatcher match, ...) { va_list args; unsigned int ret; -#ifdef DB_ENABLE_STATS - COUNT(db_getall); -#endif /* DB_ENABLE_STATS */ + DB_COUNTSTAT(db_getall); if (self == NULL) return 0; // nullpo candidate va_start(args, match); @@ -1258,20 +1206,18 @@ static unsigned int db_obj_getall(DB self, void **buf, unsigned int max, DBMatch * @param args Extra arguments for create * @return Data of the entry * @protected - * @see DB_impl::vtable#vensure + * @see DBMap#vensure */ -static void *db_obj_vensure(DB self, DBKey key, DBCreateData create, va_list args) +static void *db_obj_vensure(DBMap* self, DBKey key, DBCreateData create, va_list args) { - DB_impl db = (DB_impl)self; + DBMap_impl* db = (DBMap_impl*)self; DBNode node; DBNode parent = NULL; unsigned int hash; int c = 0; void *data = NULL; -#ifdef DB_ENABLE_STATS - COUNT(db_vensure); -#endif /* DB_ENABLE_STATS */ + DB_COUNTSTAT(db_vensure); if (db == NULL) return NULL; // nullpo candidate if (create == NULL) { ShowError("db_ensure: Create function is NULL for db allocated at %s:%d\n",db->alloc_file, db->alloc_line); @@ -1307,9 +1253,7 @@ static void *db_obj_vensure(DB self, DBKey key, DBCreateData create, va_list arg db->alloc_file, db->alloc_line); return NULL; } -#ifdef DB_ENABLE_STATS - COUNT(db_node_alloc); -#endif /* DB_ENABLE_STATS */ + DB_COUNTSTAT(db_node_alloc); node = ers_alloc(db->nodes, struct dbn); node->left = NULL; node->right = NULL; @@ -1348,7 +1292,7 @@ static void *db_obj_vensure(DB self, DBKey key, DBCreateData create, va_list arg } /** - * Just calls {@link DB#vensure(DB,DBKey,DBCreateData,va_list)}. + * Just calls {@link DBMap#vensure}. * Get the data of the entry identified by the key. * If the entry does not exist, an entry is added with the data returned by * <code>create</code>. @@ -1358,17 +1302,15 @@ static void *db_obj_vensure(DB self, DBKey key, DBCreateData create, va_list arg * @param ... Extra arguments for create * @return Data of the entry * @protected - * @see DB_impl::vtable#vensure - * @see DB_impl::vtable#ensure + * @see DBMap#vensure + * @see DBMap#ensure */ -static void *db_obj_ensure(DB self, DBKey key, DBCreateData create, ...) +static void *db_obj_ensure(DBMap* self, DBKey key, DBCreateData create, ...) { va_list args; void *ret; -#ifdef DB_ENABLE_STATS - COUNT(db_ensure); -#endif /* DB_ENABLE_STATS */ + DB_COUNTSTAT(db_ensure); if (self == NULL) return 0; // nullpo candidate va_start(args, create); @@ -1387,20 +1329,18 @@ static void *db_obj_ensure(DB self, DBKey key, DBCreateData create, ...) * @return The previous data if the entry exists or NULL * @protected * @see #db_malloc_dbn(void) - * @see DB_impl::vtable#put + * @see DBMap#put */ -static void *db_obj_put(DB self, DBKey key, void *data) +static void *db_obj_put(DBMap* self, DBKey key, void *data) { - DB_impl db = (DB_impl)self; + DBMap_impl* db = (DBMap_impl*)self; DBNode node; DBNode parent = NULL; int c = 0; unsigned int hash; void *old_data = NULL; -#ifdef DB_ENABLE_STATS - COUNT(db_put); -#endif /* DB_ENABLE_STATS */ + DB_COUNTSTAT(db_put); if (db == NULL) return NULL; // nullpo candidate if (db->global_lock) { ShowError("db_put: Database is being destroyed, aborting entry insertion.\n" @@ -1446,9 +1386,7 @@ static void *db_obj_put(DB self, DBKey key, void *data) } // allocate a new node if necessary if (node == NULL) { -#ifdef DB_ENABLE_STATS - COUNT(db_node_alloc); -#endif /* DB_ENABLE_STATS */ + DB_COUNTSTAT(db_node_alloc); node = ers_alloc(db->nodes, struct dbn); node->left = NULL; node->right = NULL; @@ -1488,25 +1426,23 @@ static void *db_obj_put(DB self, DBKey key, void *data) /** * Remove an entry from the database. * Returns the data of the entry. - * NOTE: The key (of the database) is released in {@link #db_free_add(DB_impl,DBNode,DBNode *)}. + * NOTE: The key (of the database) is released in {@link #db_free_add(DBMap_impl*,DBNode,DBNode *)}. * @param self Interface of the database * @param key Key that identifies the entry * @return The data of the entry or NULL if not found * @protected - * @see #db_free_add(DB_impl,DBNode,DBNode *) - * @see DB_impl::vtable#remove + * @see #db_free_add(DBMap_impl*,DBNode,DBNode *) + * @see DBMap#remove */ -static void *db_obj_remove(DB self, DBKey key) +static void *db_obj_remove(DBMap* self, DBKey key) { - DB_impl db = (DB_impl)self; + DBMap_impl* db = (DBMap_impl*)self; void *data = NULL; DBNode node; unsigned int hash; int c = 0; -#ifdef DB_ENABLE_STATS - COUNT(db_remove); -#endif /* DB_ENABLE_STATS */ + DB_COUNTSTAT(db_remove); if (db == NULL) return NULL; // nullpo candidate if (db->global_lock) { ShowError("db_remove: Database is being destroyed. Aborting entry deletion.\n" @@ -1550,19 +1486,17 @@ static void *db_obj_remove(DB self, DBKey key) * @param args Extra arguments for func * @return Sum of the values returned by func * @protected - * @see DB_impl::vtable#vforeach + * @see DBMap#vforeach */ -static int db_obj_vforeach(DB self, DBApply func, va_list args) +static int db_obj_vforeach(DBMap* self, DBApply func, va_list args) { - DB_impl db = (DB_impl)self; + DBMap_impl* db = (DBMap_impl*)self; unsigned int i; int sum = 0; DBNode node; DBNode parent; -#ifdef DB_ENABLE_STATS - COUNT(db_vforeach); -#endif /* DB_ENABLE_STATS */ + DB_COUNTSTAT(db_vforeach); if (db == NULL) return 0; // nullpo candidate if (func == NULL) { ShowError("db_foreach: Passed function is NULL for db allocated at %s:%d\n",db->alloc_file, db->alloc_line); @@ -1608,17 +1542,15 @@ static int db_obj_vforeach(DB self, DBApply func, va_list args) * @param ... Extra arguments for func * @return Sum of the values returned by func * @protected - * @see DB_impl::vtable#vforeach - * @see DB_impl::vtable#foreach + * @see DBMap#vforeach + * @see DBMap#foreach */ -static int db_obj_foreach(DB self, DBApply func, ...) +static int db_obj_foreach(DBMap* self, DBApply func, ...) { va_list args; int ret; -#ifdef DB_ENABLE_STATS - COUNT(db_foreach); -#endif /* DB_ENABLE_STATS */ + DB_COUNTSTAT(db_foreach); if (self == NULL) return 0; // nullpo candidate va_start(args, func); @@ -1637,19 +1569,17 @@ static int db_obj_foreach(DB self, DBApply func, ...) * @param args Extra arguments for func * @return Sum of values returned by func * @protected - * @see DB_impl::vtable#vclear + * @see DBMap#vclear */ -static int db_obj_vclear(DB self, DBApply func, va_list args) +static int db_obj_vclear(DBMap* self, DBApply func, va_list args) { - DB_impl db = (DB_impl)self; + DBMap_impl* db = (DBMap_impl*)self; int sum = 0; unsigned int i; DBNode node; DBNode parent; -#ifdef DB_ENABLE_STATS - COUNT(db_vclear); -#endif /* DB_ENABLE_STATS */ + DB_COUNTSTAT(db_vclear); if (db == NULL) return 0; // nullpo candidate db_free_lock(db); @@ -1676,9 +1606,7 @@ static int db_obj_vclear(DB self, DBApply func, va_list args) db->release(node->key, node->data, DB_RELEASE_BOTH); node->deleted = 1; } -#ifdef DB_ENABLE_STATS - COUNT(db_node_free); -#endif /* DB_ENABLE_STATS */ + DB_COUNTSTAT(db_node_free); ers_free(db->nodes, node); if (parent) { if (parent->left == node) @@ -1697,7 +1625,7 @@ static int db_obj_vclear(DB self, DBApply func, va_list args) } /** - * Just calls {@link common\db.h\DB#vclear(DB,DBApply,va_list)}. + * Just calls {@link DBMap#vclear}. * Removes all entries from the database. * Before deleting an entry, func is applyed to it. * Releases the key and the data. @@ -1709,17 +1637,15 @@ static int db_obj_vclear(DB self, DBApply func, va_list args) * @param ... Extra arguments for func * @return Sum of values returned by func * @protected - * @see DB_impl::vtable#vclear - * @see DB_impl::vtable#clear + * @see DBMap#vclear + * @see DBMap#clear */ -static int db_obj_clear(DB self, DBApply func, ...) +static int db_obj_clear(DBMap* self, DBApply func, ...) { va_list args; int ret; -#ifdef DB_ENABLE_STATS - COUNT(db_clear); -#endif /* DB_ENABLE_STATS */ + DB_COUNTSTAT(db_clear); if (self == NULL) return 0; // nullpo candidate va_start(args, func); @@ -1739,16 +1665,14 @@ static int db_obj_clear(DB self, DBApply func, ...) * @param args Extra arguments for func * @return Sum of values returned by func * @protected - * @see DB_impl::vtable#vdestroy + * @see DBMap#vdestroy */ -static int db_obj_vdestroy(DB self, DBApply func, va_list args) +static int db_obj_vdestroy(DBMap* self, DBApply func, va_list args) { - DB_impl db = (DB_impl)self; + DBMap_impl* db = (DBMap_impl*)self; int sum; -#ifdef DB_ENABLE_STATS - COUNT(db_vdestroy); -#endif /* DB_ENABLE_STATS */ + DB_COUNTSTAT(db_vdestroy); if (db == NULL) return 0; // nullpo candidate if (db->global_lock) { ShowError("db_vdestroy: Database is already locked for destruction. Aborting second database destruction.\n" @@ -1763,10 +1687,10 @@ static int db_obj_vdestroy(DB self, DBApply func, va_list args) #ifdef DB_ENABLE_STATS switch (db->type) { - case DB_INT: COUNT(db_int_destroy); break; - case DB_UINT: COUNT(db_uint_destroy); break; - case DB_STRING: COUNT(db_string_destroy); break; - case DB_ISTRING: COUNT(db_istring_destroy); break; + case DB_INT: DB_COUNTSTAT(db_int_destroy); break; + case DB_UINT: DB_COUNTSTAT(db_uint_destroy); break; + case DB_STRING: DB_COUNTSTAT(db_string_destroy); break; + case DB_ISTRING: DB_COUNTSTAT(db_istring_destroy); break; } #endif /* DB_ENABLE_STATS */ db_free_lock(db); @@ -1782,29 +1706,27 @@ static int db_obj_vdestroy(DB self, DBApply func, va_list args) } /** - * Just calls {@link DB#db_vdestroy(DB,DBApply,va_list)}. + * Just calls {@link DBMap#db_vdestroy}. * Finalize the database, feeing all the memory it uses. * Before deleting an entry, func is applyed to it. * Releases the key and the data. * Returns the sum of values returned by func, if it exists. * NOTE: This locks the database globally. Any attempt to insert or remove * a database entry will give an error and be aborted. - * @param self Interface of the database + * @param self Database * @param func Function to be applyed to every entry before deleting * @param ... Extra arguments for func * @return Sum of values returned by func * @protected - * @see DB_impl::vtable#vdestroy - * @see DB_impl::vtable#destroy + * @see DBMap#vdestroy + * @see DBMap#destroy */ -static int db_obj_destroy(DB self, DBApply func, ...) +static int db_obj_destroy(DBMap* self, DBApply func, ...) { va_list args; int ret; -#ifdef DB_ENABLE_STATS - COUNT(db_destroy); -#endif /* DB_ENABLE_STATS */ + DB_COUNTSTAT(db_destroy); if (self == NULL) return 0; // nullpo candidate va_start(args, func); @@ -1818,17 +1740,15 @@ static int db_obj_destroy(DB self, DBApply func, ...) * @param self Interface of the database * @return Size of the database * @protected - * @see DB_impl#item_count - * @see DB_impl::vtable#size + * @see DBMap_impl#item_count + * @see DBMap#size */ -static unsigned int db_obj_size(DB self) +static unsigned int db_obj_size(DBMap* self) { - DB_impl db = (DB_impl)self; + DBMap_impl* db = (DBMap_impl*)self; unsigned int item_count; -#ifdef DB_ENABLE_STATS - COUNT(db_size); -#endif /* DB_ENABLE_STATS */ + DB_COUNTSTAT(db_size); if (db == NULL) return 0; // nullpo candidate db_free_lock(db); @@ -1843,17 +1763,15 @@ static unsigned int db_obj_size(DB self) * @param self Interface of the database * @return Type of the database * @protected - * @see DB_impl#type - * @see DB_impl::vtable#type + * @see DBMap_impl#type + * @see DBMap#type */ -static DBType db_obj_type(DB self) +static DBType db_obj_type(DBMap* self) { - DB_impl db = (DB_impl)self; + DBMap_impl* db = (DBMap_impl*)self; DBType type; -#ifdef DB_ENABLE_STATS - COUNT(db_type); -#endif /* DB_ENABLE_STATS */ + DB_COUNTSTAT(db_type); if (db == NULL) return -1; // nullpo candidate - TODO what should this return? db_free_lock(db); @@ -1868,17 +1786,15 @@ static DBType db_obj_type(DB self) * @param self Interface of the database * @return Options of the database * @protected - * @see DB_impl#options - * @see DB_impl::vtable#options + * @see DBMap_impl#options + * @see DBMap#options */ -static DBOptions db_obj_options(DB self) +static DBOptions db_obj_options(DBMap* self) { - DB_impl db = (DB_impl)self; + DBMap_impl* db = (DBMap_impl*)self; DBOptions options; -#ifdef DB_ENABLE_STATS - COUNT(db_options); -#endif /* DB_ENABLE_STATS */ + DB_COUNTSTAT(db_options); if (db == NULL) return DB_OPT_BASE; // nullpo candidate - TODO what should this return? db_free_lock(db); @@ -1916,9 +1832,7 @@ static DBOptions db_obj_options(DB self) */ DBOptions db_fix_options(DBType type, DBOptions options) { -#ifdef DB_ENABLE_STATS - COUNT(db_fix_options); -#endif /* DB_ENABLE_STATS */ + DB_COUNTSTAT(db_fix_options); switch (type) { case DB_INT: case DB_UINT: // Numeric database, do nothing with the keys @@ -1944,9 +1858,7 @@ DBOptions db_fix_options(DBType type, DBOptions options) */ DBComparator db_default_cmp(DBType type) { -#ifdef DB_ENABLE_STATS - COUNT(db_default_cmp); -#endif /* DB_ENABLE_STATS */ + DB_COUNTSTAT(db_default_cmp); switch (type) { case DB_INT: return db_int_cmp; case DB_UINT: return db_uint_cmp; @@ -1970,9 +1882,7 @@ DBComparator db_default_cmp(DBType type) */ DBHasher db_default_hash(DBType type) { -#ifdef DB_ENABLE_STATS - COUNT(db_default_hash); -#endif /* DB_ENABLE_STATS */ + DB_COUNTSTAT(db_default_hash); switch (type) { case DB_INT: return db_int_hash; case DB_UINT: return db_uint_hash; @@ -2001,9 +1911,7 @@ DBHasher db_default_hash(DBType type) */ DBReleaser db_default_release(DBType type, DBOptions options) { -#ifdef DB_ENABLE_STATS - COUNT(db_default_release); -#endif /* DB_ENABLE_STATS */ + DB_COUNTSTAT(db_default_release); options = db_fix_options(type, options); if (options&DB_OPT_RELEASE_DATA) { // Release data, what about the key? if (options&(DB_OPT_DUP_KEY|DB_OPT_RELEASE_KEY)) @@ -2028,9 +1936,7 @@ DBReleaser db_default_release(DBType type, DBOptions options) */ DBReleaser db_custom_release(DBRelease which) { -#ifdef DB_ENABLE_STATS - COUNT(db_custom_release); -#endif /* DB_ENABLE_STATS */ + DB_COUNTSTAT(db_custom_release); switch (which) { case DB_RELEASE_NOTHING: return db_release_nothing; case DB_RELEASE_KEY: return db_release_key; @@ -2054,32 +1960,24 @@ DBReleaser db_custom_release(DBRelease which) * databases * @return The interface of the database * @public - * @see #DB_impl + * @see #DBMap_impl * @see #db_fix_options(DBType,DBOptions) */ -DB db_alloc(const char *file, int line, DBType type, DBOptions options, unsigned short maxlen) +DBMap* db_alloc(const char *file, int line, DBType type, DBOptions options, unsigned short maxlen) { - DB_impl db; + DBMap_impl* db; unsigned int i; #ifdef DB_ENABLE_STATS - if (stats.db_alloc != (unsigned int)~0) stats.db_alloc++; + DB_COUNTSTAT(db_alloc); switch (type) { - case DB_INT: - COUNT(db_int_alloc); - break; - case DB_UINT: - COUNT(db_uint_alloc); - break; - case DB_STRING: - COUNT(db_string_alloc); - break; - case DB_ISTRING: - COUNT(db_istring_alloc); - break; + case DB_INT: DB_COUNTSTAT(db_int_alloc); break; + case DB_UINT: DB_COUNTSTAT(db_uint_alloc); break; + case DB_STRING: DB_COUNTSTAT(db_string_alloc); break; + case DB_ISTRING: DB_COUNTSTAT(db_istring_alloc); break; } #endif /* DB_ENABLE_STATS */ - CREATE(db, struct db, 1); + CREATE(db, struct DBMap_impl, 1); options = db_fix_options(type, options); /* Interface of the database */ @@ -2137,9 +2035,7 @@ DBKey db_i2key(int key) { DBKey ret; -#ifdef DB_ENABLE_STATS - COUNT(db_i2key); -#endif /* DB_ENABLE_STATS */ + DB_COUNTSTAT(db_i2key); ret.i = key; return ret; } @@ -2156,9 +2052,7 @@ DBKey db_ui2key(unsigned int key) { DBKey ret; -#ifdef DB_ENABLE_STATS - COUNT(db_ui2key); -#endif /* DB_ENABLE_STATS */ + DB_COUNTSTAT(db_ui2key); ret.ui = key; return ret; } @@ -2175,9 +2069,7 @@ DBKey db_str2key(const char *key) { DBKey ret; -#ifdef DB_ENABLE_STATS - COUNT(db_str2key); -#endif /* DB_ENABLE_STATS */ + DB_COUNTSTAT(db_str2key); ret.str = key; return ret; } @@ -2190,9 +2082,7 @@ DBKey db_str2key(const char *key) */ void db_init(void) { -#ifdef DB_ENABLE_STATS - COUNT(db_init); -#endif /* DB_ENABLE_STATS */ + DB_COUNTSTAT(db_init); } /** @@ -2203,8 +2093,7 @@ void db_init(void) void db_final(void) { #ifdef DB_ENABLE_STATS - if (stats.db_final != (unsigned int)~0) - stats.db_final++; + DB_COUNTSTAT(db_final); ShowInfo(CL_WHITE"Database nodes"CL_RESET":\n" "allocated %u, freed %u\n", stats.db_node_alloc, stats.db_node_free); |