diff options
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/db.c | 457 | ||||
-rw-r--r-- | src/common/db.h | 231 |
2 files changed, 278 insertions, 410 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); diff --git a/src/common/db.h b/src/common/db.h index ec6539bc6..45034c141 100644 --- a/src/common/db.h +++ b/src/common/db.h @@ -23,8 +23,8 @@ * HISTORY: * * 2.1 (Athena build #???#) - Portability fix * * - Fixed the portability of casting to union and added the functions * - * {@link DB#ensure(DB,DBKey,DBCreateData,...)} and * - * {@link DB#clear(DB,DBApply,...)}. * + * {@link DBMap#ensure(DBMap,DBKey,DBCreateData,...)} and * + * {@link DBMap#clear(DBMap,DBApply,...)}. * * 2.0 (Athena build 4859) - Transition version * * - Almost everything recoded with a strategy similar to objects, * * database structure is maintained. * @@ -40,6 +40,7 @@ #ifndef _DB_H_ #define _DB_H_ +#include "../common/cbasetypes.h" #include <stdarg.h> /*****************************************************************************\ @@ -51,19 +52,19 @@ * DBOptions - Bitfield enumeration of database options. * * DBKey - Union of used key types. * * DBApply - Format of functions applyed to the databases. * - * DBMatcher - Format of matchers used in DB::getall. * + * DBMatcher - Format of matchers used in DBMap::getall. * * DBComparator - Format of the comparators used by the databases. * * DBHasher - Format of the hashers used by the databases. * * DBReleaser - Format of the releasers used by the databases. * - * DB - Database interface. * + * DBMap - Database interface. * \*****************************************************************************/ /** * Define this to enable the functions that cast to unions. * Required when the compiler doesn't support casting to unions. * NOTE: It is recommened that the conditional tests to determine if this - * should be defined be located in a makefile or a header file specific for - * of compatibility and portability issues. + * should be defined be located in the configure script or a header file + * specific for compatibility and portability issues. * @public * @see #db_i2key(int) * @see #db_ui2key(unsigned int) @@ -120,7 +121,7 @@ typedef enum DBType { * @param DB_OPT_RELEASE_KEY Releases the key. * @param DB_OPT_RELEASE_DATA Releases the data whenever an entry is removed * from the database. - * WARNING: for funtions that return the data (like DB::remove), + * WARNING: for funtions that return the data (like DBMap::remove), * a dangling pointer will be returned. * @param DB_OPT_RELEASE_BOTH Releases both key and data. * @param DB_OPT_ALLOW_NULL_KEY Allow NULL keys in the database. @@ -130,7 +131,7 @@ typedef enum DBType { * @see #db_default_release(DBType,DBOptions) * @see #db_alloc(const char *,int,DBType,DBOptions,unsigned short) */ -typedef enum db_opt { +typedef enum DBOptions { DB_OPT_BASE = 0, DB_OPT_DUP_KEY = 1, DB_OPT_RELEASE_KEY = 2, @@ -147,11 +148,11 @@ typedef enum db_opt { * @param str Type of key for DB_STRING and DB_ISTRING databases * @public * @see #DBType - * @see DB#get - * @see DB#put - * @see DB#remove + * @see DBMap#get + * @see DBMap#put + * @see DBMap#remove */ -typedef union dbkey { +typedef union DBKey { int i; unsigned int ui; const char *str; @@ -164,27 +165,27 @@ typedef union dbkey { * @param args Extra arguments of the funtion * @return Data identified by the key to be put in the database * @public - * @see DB#vensure - * @see DB#ensure + * @see DBMap#vensure + * @see DBMap#ensure */ -typedef void *(*DBCreateData)(DBKey key, va_list args); +typedef void* (*DBCreateData)(DBKey key, va_list args); /** * Format of functions to be applyed to an unspecified quantity of entries of * a database. - * Any function that applyes this function to the database will return the sum + * Any function that applies this function to the database will return the sum * of values returned by this function. * @param key Key of the database entry * @param data Data of the database entry * @param args Extra arguments of the funtion * @return Value to be added up by the funtion that is applying this * @public - * @see DB#vforeach - * @see DB#foreach - * @see DB#vdestroy - * @see DB#destroy + * @see DBMap#vforeach + * @see DBMap#foreach + * @see DBMap#vdestroy + * @see DBMap#destroy */ -typedef int (*DBApply)(DBKey key, void *data, va_list args); +typedef int (*DBApply)(DBKey key, void* data, va_list args); /** * Format of functions that match database entries. @@ -195,9 +196,9 @@ typedef int (*DBApply)(DBKey key, void *data, va_list args); * @param args Extra arguments of the function * @return 0 if a match, another number otherwise * @public - * @see DB#getall + * @see DBMap#getall */ -typedef int (*DBMatcher)(DBKey key, void *data, va_list args); +typedef int (*DBMatcher)(DBKey key, void* data, va_list args); /** * Format of the comparators used internally by the database system. @@ -241,58 +242,49 @@ typedef unsigned int (*DBHasher)(DBKey key, unsigned short maxlen); * @see #db_default_releaser(DBType,DBOptions) * @see #db_custom_release(DBRelease) */ -typedef void (*DBReleaser)(DBKey key, void *data, DBRelease which); +typedef void (*DBReleaser)(DBKey key, void* data, DBRelease which); + + + +typedef struct DBMap DBMap; + + /** * Public interface of a database. Only contains funtions. * All the functions take the interface as the first argument. * @public - * @see DB#get(DB,DBKey) - * @see DB#getall(DB,void **,unsigned int,DBMatch,...) - * @see DB#vgetall(DB,void **,unsigned int,DBMatch,va_list) - * @see DB#put(DB,DBKey,void *) - * @see DB#remove(DB,DBKey) - * @see DB#foreach(DB,DBApply,...) - * @see DB#vforeach(DB,DBApply,va_list) - * @see DB#destroy(DB,DBApply,...) - * @see DB#destroy(DB,DBApply,va_list) - * @see DB#size(DB) - * @see DB#type(DB) - * @see DB#options(DB) - * @see #db_alloc(const char *,int,DBType,DBOptions,unsigned short) + * @see #db_alloc(const char*,int,DBType,DBOptions,unsigned short) */ -typedef struct dbt *DB; -struct dbt { +struct DBMap { /** * Get the data of the entry identifid by the key. - * @param dbi Interface of the database + * @param self Database * @param key Key that identifies the entry * @return Data of the entry or NULL if not found * @protected - * @see #db_get(DB,DBKey) */ - void *(*get)(DB self, DBKey key); + void* (*get)(DBMap* self, DBKey key); /** - * 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. * Returns the number of entries that matched. * NOTE: if the value returned is greater than <code>max</code>, only the * first <code>max</code> entries found are put into the buffer. - * @param dbi Interface of the database + * @param self Database * @param buf Buffer to put the data of the matched entries * @param max Maximum number of data entries to be put into buf * @param match Function that matches the database entries * @param ... Extra arguments for match * @return The number of entries that matched * @protected - * @see DB#vgetall - * @see #db_getall(DB,void **,unsigned int,DBMatch,...) + * @see DBMap#vgetall(DBMap*,void **,unsigned int,DBMatcher,va_list) */ - unsigned int (*getall)(DB self, void **buf, unsigned int max, DBMatcher match, ...); + unsigned int (*getall)(DBMap* self, void** buf, unsigned int max, DBMatcher match, ...); /** * Get the data of the entries matched by <code>match</code>. @@ -301,149 +293,139 @@ struct dbt { * Returns the number of entries that matched. * NOTE: if the value returned is greater than <code>max</code>, only the * first <code>max</code> entries found are put into the buffer. - * @param dbi Interface of the database + * @param self Database * @param buf Buffer to put the data of the matched entries * @param max Maximum number of data entries to be put into buf * @param match Function that matches the database entries * @param ... Extra arguments for match * @return The number of entries that matched * @protected - * @see DB#getall - * @see #db_vgetall(DB,void **,unsigned int,DBMatch,va_list) + * @see DBMap#getall(DBMap*,void **,unsigned int,DBMatcher,...) */ - unsigned int (*vgetall)(DB self, void **buf, unsigned int max, DBMatcher match, va_list args); + unsigned int (*vgetall)(DBMap* self, void** buf, unsigned int max, DBMatcher match, va_list args); /** - * Just calls {@link common\db.h\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>. - * @param dbi Interface of the database + * @param self Database * @param key Key that identifies the entry * @param create Function used to create the data if the entry doesn't exist * @param ... Extra arguments for create * @return Data of the entry * @protected - * @see DB#vensure(DB,DBKey,DBCreateData,va_list) - * @see #db_ensure(DB,DBKey,DBCreateData,...) + * @see DBMap#vensure(DBMap*,DBKey,DBCreateData,va_list) */ - void *(*ensure)(DB self, DBKey key, DBCreateData create, ...); + void* (*ensure)(DBMap* self, DBKey key, DBCreateData create, ...); /** * 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>. - * @param dbi Interface of the database + * @param self Database * @param key Key that identifies the entry * @param create Function used to create the data if the entry doesn't exist * @param args Extra arguments for create * @return Data of the entry * @protected - * @see DB#ensure(DB,DBKey,DBCreateData,...) - * @see #db_vensure(DB,DBKey,DBCreateData,va_list) + * @see DBMap#ensure(DBMap*,DBKey,DBCreateData,...) */ - void *(*vensure)(DB self, DBKey key, DBCreateData create, va_list args); + void* (*vensure)(DBMap* self, DBKey key, DBCreateData create, va_list args); /** * Put the data identified by the key in the database. * Returns the previous data if the entry exists or NULL. * NOTE: Uses the new key, the old one is released. - * @param dbi Interface of the database + * @param self Database * @param key Key that identifies the data * @param data Data to be put in the database * @return The previous data if the entry exists or NULL * @protected - * @see #db_put(DB,DBKey,void *) */ - void *(*put)(DB self, DBKey key, void *data); + void* (*put)(DBMap* 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. - * @param dbi Interface of the database + * @param self Database * @param key Key that identifies the entry * @return The data of the entry or NULL if not found * @protected - * @see #db_remove(DB,DBKey) */ - void *(*remove)(DB self, DBKey key); + void* (*remove)(DBMap* self, DBKey key); /** - * Just calls {@link DB#vforeach(DB,DBApply,va_list)}. + * Just calls {@link DBMap#vforeach}. * Apply <code>func</code> to every entry in the database. * Returns the sum of values returned by func. - * @param dbi Interface of the database + * @param self Database * @param func Function to be applyed * @param ... Extra arguments for func * @return Sum of the values returned by func * @protected - * @see DB#vforeach - * @see #db_foreach(DB,DBApply,...) + * @see DBMap#vforeach(DBMap*,DBApply,va_list) */ - int (*foreach)(DB self, DBApply func, ...); + int (*foreach)(DBMap* self, DBApply func, ...); /** * Apply <code>func</code> to every entry in the database. * Returns the sum of values returned by func. - * @param dbi Interface of the database + * @param self Database * @param func Function to be applyed * @param args Extra arguments for func * @return Sum of the values returned by func * @protected - * @see DB#foreach - * @see #db_vforeach(DB,DBApply,va_list) + * @see DBMap#foreach(DBMap*,DBApply,...) */ - int (*vforeach)(DB self, DBApply func, va_list args); + int (*vforeach)(DBMap* self, DBApply func, va_list args); /** - * Just calls {@link 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. * Returns the sum of values returned by func, if it exists. - * @param dbi 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#vclear - * @see #db_clear(DB,DBApply,...) + * @see DBMap#vclear(DBMap*,DBApply,va_list) */ - int (*clear)(DB self, DBApply func, ...); + int (*clear)(DBMap* self, DBApply func, ...); /** * Removes all entries from the database. * 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. - * @param dbi Interface of the database + * @param self Database * @param func Function to be applyed to every entry before deleting * @param args Extra arguments for func * @return Sum of values returned by func * @protected - * @see DB#clear - * @see #vclear(DB,DBApply,va_list) + * @see DBMap#clear(DBMap*,DBApply,...) */ - int (*vclear)(DB self, DBApply func, va_list args); + int (*vclear)(DBMap* self, DBApply func, va_list args); /** - * Just calls {@link DB#vdestroy(DB,DBApply,va_list)}. + * Just calls {@link DBMap#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 (except for clearing). - * @param dbi 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#vdestroy - * @see #db_destroy(DB,DBApply,...) + * @see DBMap#vdestroy(DBMap*,DBApply,va_list) */ - int (*destroy)(DB self, DBApply func, ...); + int (*destroy)(DBMap* self, DBApply func, ...); /** * Finalize the database, feeing all the memory it uses. @@ -451,42 +433,38 @@ struct dbt { * 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 (except for clearing). - * @param dbi Interface of the database + * @param self Database * @param func Function to be applyed to every entry before deleting * @param args Extra arguments for func * @return Sum of values returned by func * @protected - * @see DB#destroy - * @see #db_vdestroy(DB,DBApply,va_list) + * @see DBMap#destroy(DBMap*,DBApply,...) */ - int (*vdestroy)(DB self, DBApply func, va_list args); + int (*vdestroy)(DBMap* self, DBApply func, va_list args); /** * Return the size of the database (number of items in the database). - * @param dbi Interface of the database + * @param self Database * @return Size of the database * @protected - * @see #db_size(DB) */ - unsigned int (*size)(DB self); + unsigned int (*size)(DBMap* self); /** * Return the type of the database. - * @param dbi Interface of the database + * @param self Database * @return Type of the database * @protected - * @see #db_type(DB) */ - DBType (*type)(DB self); + DBType (*type)(DBMap* self); /** * Return the options of the database. - * @param dbi Interface of the database + * @param self Database * @return Options of the database * @protected - * @see #db_options(DB) */ - DBOptions (*options)(DB self); + DBOptions (*options)(DBMap* self); }; @@ -501,27 +479,34 @@ struct dbt { # define str2key(k) ((DBKey)(const char *)(k)) #endif /* not DB_MANUAL_CAST_TO_UNION */ -#define db_get(db,k) (db)->get((db),(k)) -#define idb_get(db,k) (db)->get((db),i2key(k)) -#define uidb_get(db,k) (db)->get((db),ui2key(k)) -#define strdb_get(db,k) (db)->get((db),str2key(k)) +#define db_get(db,k) ( (db)->get((db),(k)) ) +#define idb_get(db,k) ( (db)->get((db),i2key(k)) ) +#define uidb_get(db,k) ( (db)->get((db),ui2key(k)) ) +#define strdb_get(db,k) ( (db)->get((db),str2key(k)) ) -#define db_put(db,k,d) (db)->put((db),(k),(d)) -#define idb_put(db,k,d) (db)->put((db),i2key(k),(d)) -#define uidb_put(db,k,d) (db)->put((db),ui2key(k),(d)) -#define strdb_put(db,k,d) (db)->put((db),str2key(k),(d)) +#define db_put(db,k,d) ( (db)->put((db),(k),(d)) ) +#define idb_put(db,k,d) ( (db)->put((db),i2key(k),(d)) ) +#define uidb_put(db,k,d) ( (db)->put((db),ui2key(k),(d)) ) +#define strdb_put(db,k,d) ( (db)->put((db),str2key(k),(d)) ) -#define db_remove(db,k) (db)->remove((db),(k)) -#define idb_remove(db,k) (db)->remove((db),i2key(k)) -#define uidb_remove(db,k) (db)->remove((db),ui2key(k)) -#define strdb_remove(db,k) (db)->remove((db),str2key(k)) +#define db_remove(db,k) ( (db)->remove((db),(k)) ) +#define idb_remove(db,k) ( (db)->remove((db),i2key(k)) ) +#define uidb_remove(db,k) ( (db)->remove((db),ui2key(k)) ) +#define strdb_remove(db,k) ( (db)->remove((db),str2key(k)) ) //These are discarding the possible vargs you could send to the function, so those //that require vargs must not use these defines. -#define db_ensure(db,k,f) (db)->ensure((db),(k),f) -#define idb_ensure(db,k,f) (db)->ensure((db),i2key(k),f) -#define uidb_ensure(db,k,f) (db)->ensure((db),ui2key(k),f) -#define strdb_ensure(db,k,f) (db)->ensure((db),str2key(k),f) +#define db_ensure(db,k,f) ( (db)->ensure((db),(k),(f)) ) +#define idb_ensure(db,k,f) ( (db)->ensure((db),i2key(k),(f)) ) +#define uidb_ensure(db,k,f) ( (db)->ensure((db),ui2key(k),(f)) ) +#define strdb_ensure(db,k,f) ( (db)->ensure((db),str2key(k),(f)) ) + +// Database creation and destruction macros +#define idb_alloc(opt) db_alloc(__FILE__,__LINE__,DB_INT,(opt),sizeof(int)) +#define uidb_alloc(opt) db_alloc(__FILE__,__LINE__,DB_UINT,(opt),sizeof(unsigned int)) +#define strdb_alloc(opt,maxlen) db_alloc(__FILE__,__LINE__,DB_STRING,(opt),(maxlen)) +#define stridb_alloc(opt,maxlen) db_alloc(__FILE__,__LINE__,DB_ISTRING,(opt),(maxlen)) +#define db_destroy(db) ( (db)->destroy((db),NULL) ) /*****************************************************************************\ * (2) Section with public functions. * @@ -550,7 +535,6 @@ struct dbt { * @see #DBType * @see #DBOptions * @see #db_default_release(DBType,DBOptions) - * @see common\db.c#db_fix_options(DBType,DBOptions) */ DBOptions db_fix_options(DBType type, DBOptions options); @@ -561,7 +545,6 @@ DBOptions db_fix_options(DBType type, DBOptions options); * @public * @see #DBType * @see #DBComparator - * @see common\db.c#db_default_cmp(DBType) */ DBComparator db_default_cmp(DBType type); @@ -572,7 +555,6 @@ DBComparator db_default_cmp(DBType type); * @public * @see #DBType * @see #DBHasher - * @see common\db.c#db_default_hash(DBType) */ DBHasher db_default_hash(DBType type); @@ -590,7 +572,6 @@ DBHasher db_default_hash(DBType type); * @see #DBReleaser * @see #db_fix_options(DBType,DBOptions) * @see #db_custom_release(DBRelease) - * @see common\db.c#db_default_release(DBType,DBOptions) */ DBReleaser db_default_release(DBType type, DBOptions options); @@ -602,7 +583,6 @@ DBReleaser db_default_release(DBType type, DBOptions options); * @see #DBRelease * @see #DBReleaser * @see #db_default_release(DBType,DBOptions) - * @see common\db.c#db_custom_release(DBRelease) */ DBReleaser db_custom_release(DBRelease which); @@ -621,14 +601,13 @@ DBReleaser db_custom_release(DBRelease which); * @return The interface of the database * @public * @see #DBType - * @see #DB + * @see #DBMap * @see #db_default_cmp(DBType) * @see #db_default_hash(DBType) * @see #db_default_release(DBType,DBOptions) * @see #db_fix_options(DBType,DBOptions) - * @see common\db.c#db_alloc(const char *,int,DBType,DBOptions,unsigned short) */ -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); #ifdef DB_MANUAL_CAST_TO_UNION /** |