diff options
-rw-r--r-- | src/char/char.c | 58 | ||||
-rw-r--r-- | src/char/int_guild.c | 9 | ||||
-rw-r--r-- | src/char/inter.c | 9 | ||||
-rw-r--r-- | src/common/db.c | 296 | ||||
-rw-r--r-- | src/common/db.h | 257 | ||||
-rw-r--r-- | src/common/showmsg.c | 2 | ||||
-rw-r--r-- | src/login/login.c | 25 | ||||
-rw-r--r-- | src/map/battleground.c | 7 | ||||
-rw-r--r-- | src/map/chrif.c | 25 | ||||
-rw-r--r-- | src/map/guild.c | 84 | ||||
-rw-r--r-- | src/map/itemdb.c | 41 | ||||
-rw-r--r-- | src/map/map.c | 56 | ||||
-rw-r--r-- | src/map/mapreg_sql.c | 6 | ||||
-rw-r--r-- | src/map/npc.c | 45 | ||||
-rw-r--r-- | src/map/script.c | 10 | ||||
-rw-r--r-- | src/map/skill.c | 10 | ||||
-rw-r--r-- | src/map/storage.c | 23 |
17 files changed, 661 insertions, 302 deletions
diff --git a/src/char/char.c b/src/char/char.c index fb413be82..dccadc9fe 100644 --- a/src/char/char.c +++ b/src/char/char.c @@ -195,7 +195,10 @@ static DBMap* online_char_db; // int account_id -> struct online_char_data* static int chardb_waiting_disconnect(int tid, unsigned int tick, int id, intptr_t data); int delete_char_sql(int char_id); -static void* create_online_char_data(DBKey key, va_list args) +/** + * @see DBCreateData + */ +static DBData create_online_char_data(DBKey key, va_list args) { struct online_char_data* character; CREATE(character, struct online_char_data, 1); @@ -204,14 +207,14 @@ static void* create_online_char_data(DBKey key, va_list args) character->server = -1; character->fd = -1; character->waiting_disconnect = INVALID_TIMER; - return character; + return db_ptr2data(character); } void set_char_charselect(int account_id) { struct online_char_data* character; - character = (struct online_char_data*)idb_ensure(online_char_db, account_id, create_online_char_data); + character = idb_ensure(online_char_db, account_id, create_online_char_data); if( character->server > -1 ) if( server[character->server].users > 0 ) // Prevent this value from going negative. @@ -245,7 +248,7 @@ void set_char_online(int map_id, int char_id, int account_id) Sql_ShowDebug(sql_handle); //Check to see for online conflicts - character = (struct online_char_data*)idb_ensure(online_char_db, account_id, create_online_char_data); + character = idb_ensure(online_char_db, account_id, create_online_char_data); if( character->char_id != -1 && character->server > -1 && character->server != map_id ) { ShowNotice("set_char_online: Character %d:%d marked in map server %d, but map server %d claims to have (%d:%d) online!\n", @@ -330,9 +333,12 @@ void set_char_offline(int char_id, int account_id) } } -static int char_db_setoffline(DBKey key, void* data, va_list ap) +/** + * @see DBApply + */ +static int char_db_setoffline(DBKey key, DBData *data, va_list ap) { - struct online_char_data* character = (struct online_char_data*)data; + struct online_char_data* character = db_data2ptr(data); int server = va_arg(ap, int); if (server == -1) { character->char_id = -1; @@ -346,15 +352,18 @@ static int char_db_setoffline(DBKey key, void* data, va_list ap) return 0; } -static int char_db_kickoffline(DBKey key, void* data, va_list ap) +/** + * @see DBApply + */ +static int char_db_kickoffline(DBKey key, DBData *data, va_list ap) { - struct online_char_data* character = (struct online_char_data*)data; + struct online_char_data* character = db_data2ptr(data); int server_id = va_arg(ap, int); if (server_id > -1 && character->server != server_id) return 0; - //Kick out any connected characters, and set them offline as appropiate. + //Kick out any connected characters, and set them offline as appropriate. if (character->server > -1) mapif_disconnectplayer(server[character->server].fd, character->account_id, character->char_id, 1); else if (character->waiting_disconnect == INVALID_TIMER) @@ -392,12 +401,15 @@ void set_all_offline_sql(void) Sql_ShowDebug(sql_handle); } -static void* create_charstatus(DBKey key, va_list args) +/** + * @see DBCreateData + */ +static DBData create_charstatus(DBKey key, va_list args) { struct mmo_charstatus *cp; cp = (struct mmo_charstatus *) aCalloc(1,sizeof(struct mmo_charstatus)); cp->char_id = key.i; - return cp; + return db_ptr2data(cp); } @@ -413,7 +425,7 @@ int mmo_char_tosql(int char_id, struct mmo_charstatus* p) if (char_id!=p->char_id) return 0; - cp = (struct mmo_charstatus*)idb_ensure(char_db_, char_id, create_charstatus); + cp = idb_ensure(char_db_, char_id, create_charstatus); StringBuf_Init(&buf); memset(save_status, 0, sizeof(save_status)); @@ -1184,7 +1196,7 @@ int mmo_char_fromsql(int char_id, struct mmo_charstatus* p, bool load_everything SqlStmt_Free(stmt); StringBuf_Destroy(&buf); - cp = (struct mmo_charstatus*)idb_ensure(char_db_, char_id, create_charstatus); + cp = idb_ensure(char_db_, char_id, create_charstatus); memcpy(cp, p, sizeof(struct mmo_charstatus)); return 1; } @@ -2609,7 +2621,7 @@ int parse_frommap(int fd) for(i = 0; i < server[id].users; i++) { aid = RFIFOL(fd,6+i*8); cid = RFIFOL(fd,6+i*8+4); - character = (struct online_char_data*)idb_ensure(online_char_db, aid, create_online_char_data); + character = idb_ensure(online_char_db, aid, create_online_char_data); if( character->server > -1 && character->server != id ) { ShowNotice("Set map user: Character (%d:%d) marked on map server %d, but map server %d claims to have (%d:%d) online!\n", @@ -2753,7 +2765,7 @@ int parse_frommap(int fd) node->changing_mapservers = 1; idb_put(auth_db, RFIFOL(fd,2), node); - data = (struct online_char_data*)idb_ensure(online_char_db, RFIFOL(fd,2), create_online_char_data); + data = idb_ensure(online_char_db, RFIFOL(fd,2), create_online_char_data); data->char_id = char_data->char_id; data->server = map_id; //Update server where char is. @@ -4060,10 +4072,13 @@ int broadcast_user_count(int tid, unsigned int tick, int id, intptr_t data) return 0; } -/// load this char's account id into the 'online accounts' packet -static int send_accounts_tologin_sub(DBKey key, void* data, va_list ap) +/** + * Load this character's account id into the 'online accounts' packet + * @see DBApply + */ +static int send_accounts_tologin_sub(DBKey key, DBData *data, va_list ap) { - struct online_char_data* character = (struct online_char_data*)data; + struct online_char_data* character = db_data2ptr(data); int* i = va_arg(ap, int*); if(character->server > -1) @@ -4152,9 +4167,12 @@ static int chardb_waiting_disconnect(int tid, unsigned int tick, int id, intptr_ return 0; } -static int online_data_cleanup_sub(DBKey key, void *data, va_list ap) +/** + * @see DBApply + */ +static int online_data_cleanup_sub(DBKey key, DBData *data, va_list ap) { - struct online_char_data *character= (struct online_char_data*)data; + struct online_char_data *character= db_data2ptr(data); if (character->fd != -1) return 0; //Character still connected if (character->server == -2) //Unknown server.. set them offline diff --git a/src/char/int_guild.c b/src/char/int_guild.c index d2e74ee1d..a4d91ecd6 100644 --- a/src/char/int_guild.c +++ b/src/char/int_guild.c @@ -56,7 +56,7 @@ static int guild_save_timer(int tid, unsigned int tick, int id, intptr_t data) if( last_id == 0 ) //Save the first guild in the list. state = 1; - for( g = (struct guild*)iter->first(iter,&key); iter->exists(iter); g = (struct guild*)iter->next(iter,&key) ) + for( g = db_data2ptr(iter->first(iter, &key)); dbi_exists(iter); g = db_data2ptr(iter->next(iter, &key)) ) { if( state == 0 && g->guild_id == last_id ) state++; //Save next guild in the list. @@ -746,9 +746,12 @@ int inter_guild_sql_init(void) return 0; } -static int guild_db_final(DBKey key, void *data, va_list ap) +/** + * @see DBApply + */ +static int guild_db_final(DBKey key, DBData *data, va_list ap) { - struct guild *g = (struct guild*)data; + struct guild *g = db_data2ptr(data); if (g->save_flag&GS_MASK) { inter_guild_tosql(g, g->save_flag&GS_MASK); return 1; diff --git a/src/char/inter.c b/src/char/inter.c index dbff1993f..c72c64108 100644 --- a/src/char/inter.c +++ b/src/char/inter.c @@ -421,11 +421,14 @@ int mapif_disconnectplayer(int fd, int account_id, int char_id, int reason) //-------------------------------------------------------- -// Existence check of WISP data -int check_ttl_wisdata_sub(DBKey key, void *data, va_list ap) +/** + * Existence check of WISP data + * @see DBApply + */ +int check_ttl_wisdata_sub(DBKey key, DBData *data, va_list ap) { unsigned long tick; - struct WisData *wd = (struct WisData *)data; + struct WisData *wd = db_data2ptr(data); tick = va_arg(ap, unsigned long); if (DIFF_TICK(tick, wd->tick) > WISDATA_TTL && wis_delnum < WISDELLIST_MAX) diff --git a/src/common/db.c b/src/common/db.c index 481e9db2a..999deea48 100644 --- a/src/common/db.c +++ b/src/common/db.c @@ -40,7 +40,6 @@ * * TODO: * - create test cases to test the database system thoroughly - * - make data an enumeration * - finish this header describing the database system * - create custom database allocator * - make the system thread friendly @@ -48,6 +47,7 @@ * - create a db that organizes itself by splaying * * HISTORY: + * 2012/03/09 - Added enum for data types (int, uint, void*) * 2008/02/19 - Fixed db_obj_get not handling deleted entries correctly. * 2007/11/09 - Added an iterator to the database. * 2006/12/21 - Added 1-node cache to the database. @@ -135,7 +135,7 @@ typedef struct dbn { struct dbn *right; // Node data DBKey key; - void *data; + DBData data; // Other node_color color; unsigned deleted : 1; @@ -221,7 +221,7 @@ typedef struct DBIterator_impl { #if defined(DB_ENABLE_STATS) /** - * Structure with what is counted when the database estatistics are enabled. + * Structure with what is counted when the database statistics are enabled. * @private * @see #DB_ENABLE_STATS * @see #stats @@ -297,6 +297,12 @@ static struct db_stats { uint32 db_i2key; uint32 db_ui2key; uint32 db_str2key; + uint32 db_i2data; + uint32 db_ui2data; + uint32 db_ptr2data; + uint32 db_data2i; + uint32 db_data2ui; + uint32 db_data2ptr; uint32 db_init; uint32 db_final; } stats = { @@ -305,7 +311,8 @@ static struct db_stats { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0 + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0 }; #define DB_COUNTSTAT(token) if (stats. ## token != UINT32_MAX) ++stats. ## token #else /* !defined(DB_ENABLE_STATS) */ @@ -399,7 +406,7 @@ static void db_rotate_right(DBNode node, DBNode *root) * @private * @see #db_rotate_left(DBNode,DBNode *) * @see #db_rotate_right(DBNode,DBNode *) - * @see #db_obj_put(DBMap*,DBKey,void *) + * @see #db_obj_put(DBMap*,DBKey,DBData) */ static void db_rebalance(DBNode node, DBNode *root) { @@ -595,13 +602,13 @@ static void db_rebalance_erase(DBNode node, DBNode *root) } /** - * Returns not 0 if the key is considerd to be NULL. + * Returns not 0 if the key is considered to be NULL. * @param type Type of database * @param key Key being tested * @return not 0 if considered NULL, 0 otherwise * @private * @see #db_obj_get(DBMap*,DBKey) - * @see #db_obj_put(DBMap*,DBKey,void *) + * @see #db_obj_put(DBMap*,DBKey,DBData) * @see #db_obj_remove(DBMap*,DBKey) */ static int db_is_key_null(DBType type, DBKey key) @@ -731,7 +738,7 @@ static void db_free_add(DBMap_impl* db, DBNode node, DBNode *root) * @see #struct db_free * @see DBMap_impl#free_list * @see DBMap_impl#free_count - * @see #db_obj_put(DBMap*,DBKey,void*) + * @see #db_obj_put(DBMap*,DBKey,DBData) * @see #db_free_add(DBMap_impl*,DBNode*,DBNode) */ static void db_free_remove(DBMap_impl* db, DBNode node) @@ -1006,7 +1013,7 @@ static unsigned int db_istring_hash(DBKey key, unsigned short maxlen) * @see #DBReleaser * @see #db_default_releaser(DBType,DBOptions) */ -static void db_release_nothing(DBKey key, void *data, DBRelease which) +static void db_release_nothing(DBKey key, DBData data, DBRelease which) { (void)key;(void)data;(void)which;//not used DB_COUNTSTAT(db_release_nothing); @@ -1021,7 +1028,7 @@ static void db_release_nothing(DBKey key, void *data, DBRelease which) * @see #DBReleaser * @see #db_default_release(DBType,DBOptions) */ -static void db_release_key(DBKey key, void *data, DBRelease which) +static void db_release_key(DBKey key, DBData data, DBRelease which) { (void)data;//not used DB_COUNTSTAT(db_release_key); @@ -1034,16 +1041,16 @@ 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 #DBKey + * @see #DBData * @see #DBRelease * @see #DBReleaser * @see #db_default_release(DBType,DBOptions) */ -static void db_release_data(DBKey key, void *data, DBRelease which) +static void db_release_data(DBKey key, DBData data, DBRelease which) { (void)key;//not used DB_COUNTSTAT(db_release_data); - if (which&DB_RELEASE_DATA) aFree(data); + if (which&DB_RELEASE_DATA && data.type == DB_DATA_PTR) aFree(data.u.ptr); } /** @@ -1053,15 +1060,16 @@ static void db_release_data(DBKey key, void *data, DBRelease which) * @param which What is being requested to be released * @protected * @see #DBKey + * @see #DBData * @see #DBRelease * @see #DBReleaser * @see #db_default_release(DBType,DBOptions) */ -static void db_release_both(DBKey key, void *data, DBRelease which) +static void db_release_both(DBKey key, DBData data, DBRelease which) { 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); + if (which&DB_RELEASE_DATA && data.type == DB_DATA_PTR) aFree(data.u.ptr); } /*****************************************************************************\ @@ -1075,7 +1083,7 @@ static void db_release_both(DBKey key, void *data, DBRelease which) * dbit_obj_remove - Remove the current entry from the database. * * dbit_obj_destroy - Destroys the iterator, unlocking the database and * * freeing used memory. * - * db_obj_iterator - Return a new databse iterator. * + * db_obj_iterator - Return a new database iterator. * * db_obj_exists - Checks if an entry exists. * * db_obj_get - Get the data identified by the key. * * db_obj_vgetall - Get the data of the matched entries. * @@ -1107,7 +1115,7 @@ static void db_release_both(DBKey key, void *data, DBRelease which) * @protected * @see DBIterator#first */ -void* dbit_obj_first(DBIterator* self, DBKey* out_key) +DBData* dbit_obj_first(DBIterator* self, DBKey* out_key) { DBIterator_impl* it = (DBIterator_impl*)self; @@ -1129,7 +1137,7 @@ void* dbit_obj_first(DBIterator* self, DBKey* out_key) * @protected * @see DBIterator#last */ -void* dbit_obj_last(DBIterator* self, DBKey* out_key) +DBData* dbit_obj_last(DBIterator* self, DBKey* out_key) { DBIterator_impl* it = (DBIterator_impl*)self; @@ -1151,7 +1159,7 @@ void* dbit_obj_last(DBIterator* self, DBKey* out_key) * @protected * @see DBIterator#next */ -void* dbit_obj_next(DBIterator* self, DBKey* out_key) +DBData* dbit_obj_next(DBIterator* self, DBKey* out_key) { DBIterator_impl* it = (DBIterator_impl*)self; DBNode node; @@ -1209,7 +1217,7 @@ void* dbit_obj_next(DBIterator* self, DBKey* out_key) it->node = node; if( out_key ) memcpy(out_key, &node->key, sizeof(DBKey)); - return node->data; + return &node->data; } } } @@ -1227,7 +1235,7 @@ void* dbit_obj_next(DBIterator* self, DBKey* out_key) * @protected * @see DBIterator#prev */ -void* dbit_obj_prev(DBIterator* self, DBKey* out_key) +DBData* dbit_obj_prev(DBIterator* self, DBKey* out_key) { DBIterator_impl* it = (DBIterator_impl*)self; DBNode node; @@ -1286,7 +1294,7 @@ void* dbit_obj_prev(DBIterator* self, DBKey* out_key) it->node = node; if( out_key ) memcpy(out_key, &node->key, sizeof(DBKey)); - return node->data; + return &node->data; } } } @@ -1299,7 +1307,7 @@ void* dbit_obj_prev(DBIterator* self, DBKey* out_key) * The databases entries might have NULL data, so use this to to test if * the iterator is done. * @param self Iterator - * @return true is the entry exists + * @return true if the entry exists * @protected * @see DBIterator#exists */ @@ -1314,19 +1322,20 @@ bool dbit_obj_exists(DBIterator* self) /** * Removes the current entry from the database. * NOTE: {@link DBIterator#exists} will return false until another entry - * is fethed - * Returns the data of the entry. + * is fetched + * Puts data of the removed entry in out_data, if out_data is not NULL. * @param self Iterator - * @return The data of the entry or NULL if not found + * @param out_data Data of the removed entry. + * @return 1 if entry was removed, 0 otherwise * @protected * @see DBMap#remove * @see DBIterator#remove */ -void* dbit_obj_remove(DBIterator* self) +int dbit_obj_remove(DBIterator* self, DBData *out_data) { DBIterator_impl* it = (DBIterator_impl*)self; DBNode node; - void* data = NULL; + int retval = 0; DB_COUNTSTAT(dbit_remove); node = it->node; @@ -1335,11 +1344,13 @@ void* dbit_obj_remove(DBIterator* self) DBMap_impl* db = it->db; if( db->cache == node ) db->cache = NULL; - data = node->data; + if( out_data ) + memcpy(out_data, &node->data, sizeof(DBData)); + retval = 1; db->release(node->key, node->data, DB_RELEASE_DATA); db_free_add(db, node, &db->ht[it->ht_index]); } - return data; + return retval; } /** @@ -1443,19 +1454,19 @@ static bool db_obj_exists(DBMap* self, DBKey key) } /** - * Get the data of the entry identifid by the key. + * Get the data of the entry identified by the key. * @param self Interface of the database * @param key Key that identifies the entry * @return Data of the entry or NULL if not found * @protected * @see DBMap#get */ -static void* db_obj_get(DBMap* self, DBKey key) +static DBData* db_obj_get(DBMap* self, DBKey key) { DBMap_impl* db = (DBMap_impl*)self; DBNode node; int c; - void *data = NULL; + DBData *data = NULL; DB_COUNTSTAT(db_get); if (db == NULL) return NULL; // nullpo candidate @@ -1471,7 +1482,7 @@ static void* db_obj_get(DBMap* self, DBKey key) return NULL; } #endif - return db->cache->data; // cache hit + return &db->cache->data; // cache hit } db_free_lock(db); @@ -1480,7 +1491,7 @@ static void* db_obj_get(DBMap* self, DBKey key) c = db->cmp(key, node->key, db->maxlen); if (c == 0) { if (!(node->deleted)) { - data = node->data; + data = &node->data; db->cache = node; } break; @@ -1510,7 +1521,7 @@ static void* db_obj_get(DBMap* self, DBKey key) * @protected * @see DBMap#vgetall */ -static unsigned int db_obj_vgetall(DBMap* self, void **buf, unsigned int max, DBMatcher match, va_list args) +static unsigned int db_obj_vgetall(DBMap* self, DBData **buf, unsigned int max, DBMatcher match, va_list args) { DBMap_impl* db = (DBMap_impl*)self; unsigned int i; @@ -1534,7 +1545,7 @@ static unsigned int db_obj_vgetall(DBMap* self, void **buf, unsigned int max, DB va_copy(argscopy, args); if (match(node->key, node->data, argscopy) == 0) { if (buf && ret < max) - buf[ret] = node->data; + buf[ret] = &node->data; ret++; } va_end(argscopy); @@ -1579,7 +1590,7 @@ static unsigned int db_obj_vgetall(DBMap* self, void **buf, unsigned int max, DB * @see DBMap#vgetall * @see DBMap#getall */ -static unsigned int db_obj_getall(DBMap* self, void **buf, unsigned int max, DBMatcher match, ...) +static unsigned int db_obj_getall(DBMap* self, DBData **buf, unsigned int max, DBMatcher match, ...) { va_list args; unsigned int ret; @@ -1605,14 +1616,14 @@ static unsigned int db_obj_getall(DBMap* self, void **buf, unsigned int max, DBM * @protected * @see DBMap#vensure */ -static void *db_obj_vensure(DBMap* self, DBKey key, DBCreateData create, va_list args) +static DBData* db_obj_vensure(DBMap* self, DBKey key, DBCreateData create, va_list args) { DBMap_impl* db = (DBMap_impl*)self; DBNode node; DBNode parent = NULL; unsigned int hash; int c = 0; - void *data = NULL; + DBData *data = NULL; DB_COUNTSTAT(db_vensure); if (db == NULL) return NULL; // nullpo candidate @@ -1626,7 +1637,7 @@ static void *db_obj_vensure(DBMap* self, DBKey key, DBCreateData create, va_list } if (db->cache && db->cmp(key, db->cache->key, db->maxlen) == 0) - return db->cache->data; // cache hit + return &db->cache->data; // cache hit db_free_lock(db); hash = db->hash(key, db->maxlen)%HASH_SIZE; @@ -1677,7 +1688,7 @@ static void *db_obj_vensure(DBMap* self, DBKey key, DBCreateData create, va_list if (db->options&DB_OPT_DUP_KEY) { node->key = db_dup_key(db, key); if (db->options&DB_OPT_RELEASE_KEY) - db->release(key, data, DB_RELEASE_KEY); + db->release(key, *data, DB_RELEASE_KEY); } else { node->key = key; } @@ -1685,7 +1696,7 @@ static void *db_obj_vensure(DBMap* self, DBKey key, DBCreateData create, va_list node->data = create(key, argscopy); va_end(argscopy); } - data = node->data; + data = &node->data; db->cache = node; db_free_unlock(db); return data; @@ -1705,13 +1716,13 @@ static void *db_obj_vensure(DBMap* self, DBKey key, DBCreateData create, va_list * @see DBMap#vensure * @see DBMap#ensure */ -static void *db_obj_ensure(DBMap* self, DBKey key, DBCreateData create, ...) +static DBData* db_obj_ensure(DBMap* self, DBKey key, DBCreateData create, ...) { va_list args; - void *ret; + DBData *ret = NULL; DB_COUNTSTAT(db_ensure); - if (self == NULL) return 0; // nullpo candidate + if (self == NULL) return NULL; // nullpo candidate va_start(args, create); ret = self->vensure(self, key, create, args); @@ -1721,47 +1732,47 @@ static void *db_obj_ensure(DBMap* self, DBKey key, DBCreateData create, ...) /** * Put the data identified by the key in the database. - * Returns the previous data if the entry exists or NULL. + * Puts the previous data in out_data, if out_data is not NULL. * NOTE: Uses the new key, the old one is released. * @param self Interface of the 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 + * @param out_data Previous data if the entry exists + * @return 1 if if the entry already exists, 0 otherwise * @protected * @see #db_malloc_dbn(void) * @see DBMap#put */ -static void *db_obj_put(DBMap* self, DBKey key, void *data) +static int db_obj_put(DBMap* self, DBKey key, DBData data, DBData *out_data) { DBMap_impl* db = (DBMap_impl*)self; DBNode node; DBNode parent = NULL; - int c = 0; + int c = 0, retval = 0; unsigned int hash; - void *old_data = NULL; DB_COUNTSTAT(db_put); - if (db == NULL) return NULL; // nullpo candidate + if (db == NULL) return 0; // nullpo candidate if (db->global_lock) { ShowError("db_put: Database is being destroyed, aborting entry insertion.\n" "Database allocated at %s:%d\n", db->alloc_file, db->alloc_line); - return NULL; // nullpo candidate + return 0; // nullpo candidate } if (!(db->options&DB_OPT_ALLOW_NULL_KEY) && db_is_key_null(db->type, key)) { ShowError("db_put: Attempted to use non-allowed NULL key for db allocated at %s:%d\n",db->alloc_file, db->alloc_line); - return NULL; // nullpo candidate + return 0; // nullpo candidate } - if (!(data || db->options&DB_OPT_ALLOW_NULL_DATA)) { + if (!(db->options&DB_OPT_ALLOW_NULL_DATA) && (data.type == DB_DATA_PTR && data.u.ptr == NULL)) { ShowError("db_put: Attempted to use non-allowed NULL data for db allocated at %s:%d\n",db->alloc_file, db->alloc_line); - return NULL; // nullpo candidate + return 0; // nullpo candidate } if (db->item_count == UINT32_MAX) { ShowError("db_put: item_count overflow, aborting item insertion.\n" "Database allocated at %s:%d", db->alloc_file, db->alloc_line); - return NULL; + return 0; } // search for an equal node db_free_lock(db); @@ -1773,8 +1784,10 @@ static void *db_obj_put(DBMap* self, DBKey key, void *data) db_free_remove(db, node); } else { db->release(node->key, node->data, DB_RELEASE_BOTH); + if (out_data) + memcpy(out_data, &node->data, sizeof(*out_data)); + retval = 1; } - old_data = node->data; break; } parent = node; @@ -1820,39 +1833,39 @@ static void *db_obj_put(DBMap* self, DBKey key, void *data) node->data = data; db->cache = node; db_free_unlock(db); - return old_data; + return retval; } /** * Remove an entry from the database. - * Returns the data of the entry. + * Puts the previous data in out_data, if out_data is not NULL. * 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 + * @param out_data Previous data if the entry exists + * @return 1 if if the entry already exists, 0 otherwise * @protected * @see #db_free_add(DBMap_impl*,DBNode,DBNode *) * @see DBMap#remove */ -static void *db_obj_remove(DBMap* self, DBKey key) +static int db_obj_remove(DBMap* self, DBKey key, DBData *out_data) { DBMap_impl* db = (DBMap_impl*)self; - void *data = NULL; DBNode node; unsigned int hash; - int c = 0; + int c = 0, retval = 0; DB_COUNTSTAT(db_remove); - if (db == NULL) return NULL; // nullpo candidate + if (db == NULL) return 0; // nullpo candidate if (db->global_lock) { ShowError("db_remove: Database is being destroyed. Aborting entry deletion.\n" "Database allocated at %s:%d\n", db->alloc_file, db->alloc_line); - return NULL; // nullpo candidate + return 0; // nullpo candidate } if (!(db->options&DB_OPT_ALLOW_NULL_KEY) && db_is_key_null(db->type, key)) { ShowError("db_remove: Attempted to use non-allowed NULL key for db allocated at %s:%d\n",db->alloc_file, db->alloc_line); - return NULL; // nullpo candidate + return 0; // nullpo candidate } db_free_lock(db); @@ -1863,7 +1876,9 @@ static void *db_obj_remove(DBMap* self, DBKey key) if (!(node->deleted)) { if (db->cache == node) db->cache = NULL; - data = node->data; + if (out_data) + memcpy(out_data, &node->data, sizeof(*out_data)); + retval = 1; db->release(node->key, node->data, DB_RELEASE_DATA); db_free_add(db, node, &db->ht[hash]); } @@ -1875,14 +1890,14 @@ static void *db_obj_remove(DBMap* self, DBKey key) node = node->right; } db_free_unlock(db); - return data; + return retval; } /** * Apply <code>func</code> to every entry in the database. * Returns the sum of values returned by func. * @param self Interface of the database - * @param func Function to be applyed + * @param func Function to be applied * @param args Extra arguments for func * @return Sum of the values returned by func * @protected @@ -1913,7 +1928,7 @@ static int db_obj_vforeach(DBMap* self, DBApply func, va_list args) { va_list argscopy; va_copy(argscopy, args); - sum += func(node->key, node->data, argscopy); + sum += func(node->key, &node->data, argscopy); va_end(argscopy); } if (node->left) { @@ -1966,11 +1981,11 @@ static int db_obj_foreach(DBMap* self, DBApply func, ...) /** * Removes all entries from the database. - * Before deleting an entry, func is applyed to it. + * Before deleting an entry, func is applied to it. * Releases the key and the data. * Returns the sum of values returned by func, if it exists. * @param self Interface of the database - * @param func Function to be applyed to every entry before deleting + * @param func Function to be applied to every entry before deleting * @param args Extra arguments for func * @return Sum of values returned by func * @protected @@ -2010,7 +2025,7 @@ static int db_obj_vclear(DBMap* self, DBApply func, va_list args) { va_list argscopy; va_copy(argscopy, args); - sum += func(node->key, node->data, argscopy); + sum += func(node->key, &node->data, argscopy); va_end(argscopy); } db->release(node->key, node->data, DB_RELEASE_BOTH); @@ -2037,13 +2052,13 @@ static int db_obj_vclear(DBMap* self, DBApply func, va_list args) /** * Just calls {@link DBMap#vclear}. * Removes all entries from the database. - * Before deleting an entry, func is applyed to it. + * Before deleting an entry, func is applied 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 self Interface of the database - * @param func Function to be applyed to every entry before deleting + * @param func Function to be applied to every entry before deleting * @param ... Extra arguments for func * @return Sum of values returned by func * @protected @@ -2066,12 +2081,12 @@ static int db_obj_clear(DBMap* self, DBApply func, ...) /** * Finalize the database, feeing all the memory it uses. - * Before deleting an entry, func is applyed to it. + * Before deleting an entry, func is applied to it. * 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 self Interface of the database - * @param func Function to be applyed to every entry before deleting + * @param func Function to be applied to every entry before deleting * @param args Extra arguments for func * @return Sum of values returned by func * @protected @@ -2118,13 +2133,13 @@ static int db_obj_vdestroy(DBMap* self, DBApply func, va_list args) /** * Just calls {@link DBMap#db_vdestroy}. * Finalize the database, feeing all the memory it uses. - * Before deleting an entry, func is applyed to it. + * Before deleting an entry, func is applied 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 Database - * @param func Function to be applyed to every entry before deleting + * @param func Function to be applied to every entry before deleting * @param ... Extra arguments for func * @return Sum of values returned by func * @protected @@ -2225,6 +2240,12 @@ static DBOptions db_obj_options(DBMap* self) * db_i2key - Manual cast from 'int' to 'DBKey'. * db_ui2key - Manual cast from 'unsigned int' to 'DBKey'. * db_str2key - Manual cast from 'unsigned char *' to 'DBKey'. + * db_i2data - Manual cast from 'int' to 'DBData'. + * db_ui2data - Manual cast from 'unsigned int' to 'DBData'. + * db_ptr2data - Manual cast from 'void*' to 'DBData'. + * db_data2i - Gets 'int' value from 'DBData'. + * db_data2ui - Gets 'unsigned int' value from 'DBData'. + * db_data2ptr - Gets 'void*' value from 'DBData'. * db_init - Initializes the database system. * db_final - Finalizes the database system. \*****************************************************************************/ @@ -2313,10 +2334,10 @@ DBHasher db_default_hash(DBType type) * @param options Options of the database * @return Default releaser for the type of database with the specified options * @public - * @see #db_release_nothing(DBKey,void *,DBRelease) - * @see #db_release_key(DBKey,void *,DBRelease) - * @see #db_release_data(DBKey,void *,DBRelease) - * @see #db_release_both(DBKey,void *,DBRelease) + * @see #db_release_nothing(DBKey,DBData,DBRelease) + * @see #db_release_key(DBKey,DBData,DBRelease) + * @see #db_release_data(DBKey,DBData,DBRelease) + * @see #db_release_both(DBKey,DBData,DBRelease) * @see #db_custom_release(DBRelease) */ DBReleaser db_default_release(DBType type, DBOptions options) @@ -2338,10 +2359,10 @@ DBReleaser db_default_release(DBType type, DBOptions options) * @param which Options that specified what the releaser releases * @return Releaser for the specified release options * @public - * @see #db_release_nothing(DBKey,void *,DBRelease) - * @see #db_release_key(DBKey,void *,DBRelease) - * @see #db_release_data(DBKey,void *,DBRelease) - * @see #db_release_both(DBKey,void *,DBRelease) + * @see #db_release_nothing(DBKey,DBData,DBRelease) + * @see #db_release_key(DBKey,DBData,DBRelease) + * @see #db_release_data(DBKey,DBData,DBRelease) + * @see #db_release_both(DBKey,DBData,DBRelease) * @see #db_default_release(DBType,DBOptions) */ DBReleaser db_custom_release(DBRelease which) @@ -2483,6 +2504,99 @@ DBKey db_str2key(const char *key) } /** + * Manual cast from 'int' to the struct DBData. + * @param data Data to be casted + * @return The data as a DBData struct + * @public + */ +DBData db_i2data(int data) +{ + DBData ret; + + DB_COUNTSTAT(db_i2data); + ret.type = DB_DATA_INT; + ret.u.i = data; + return ret; +} + +/** + * Manual cast from 'unsigned int' to the struct DBData. + * @param data Data to be casted + * @return The data as a DBData struct + * @public + */ +DBData db_ui2data(unsigned int data) +{ + DBData ret; + + DB_COUNTSTAT(db_ui2data); + ret.type = DB_DATA_UINT; + ret.u.ui = data; + return ret; +} + +/** + * Manual cast from 'void *' to the struct DBData. + * @param data Data to be casted + * @return The data as a DBData struct + * @public + */ +DBData db_ptr2data(void *data) +{ + DBData ret; + + DB_COUNTSTAT(db_ptr2data); + ret.type = DB_DATA_PTR; + ret.u.ptr = data; + return ret; +} + +/** + * Gets int type data from struct DBData. + * If data is not int type, returns 0. + * @param data Data + * @return Integer value of the data. + * @public + */ +int db_data2i(DBData *data) +{ + DB_COUNTSTAT(db_data2i); + if (data && DB_DATA_INT == data->type) + return data->u.i; + return 0; +} + +/** + * Gets unsigned int type data from struct DBData. + * If data is not unsigned int type, returns 0. + * @param data Data + * @return Unsigned int value of the data. + * @public + */ +unsigned int db_data2ui(DBData *data) +{ + DB_COUNTSTAT(db_data2ui); + if (data && DB_DATA_UINT == data->type) + return data->u.ui; + return 0; +} + +/** + * Gets void* type data from struct DBData. + * If data is not void* type, returns NULL. + * @param data Data + * @return Void* value of the data. + * @public + */ +void* db_data2ptr(DBData *data) +{ + DB_COUNTSTAT(db_data2ptr); + if (data && DB_DATA_PTR == data->type) + return data->u.ptr; + return NULL; +} + +/** * Initializes the database system. * @public * @see #db_final(void) @@ -2543,6 +2657,9 @@ void db_final(void) "db_default_release %10u, db_custom_release %10u,\n" "db_alloc %10u, db_i2key %10u,\n" "db_ui2key %10u, db_str2key %10u,\n" + "db_i2data %10u, db_ui2data %10u,\n" + "db_ptr2data %10u, db_data2i %10u,\n" + "db_data2ui %10u, db_data2ptr %10u,\n" "db_init %10u, db_final %10u\n", stats.db_rotate_left, stats.db_rotate_right, stats.db_rebalance, stats.db_rebalance_erase, @@ -2573,6 +2690,9 @@ void db_final(void) stats.db_default_release, stats.db_custom_release, stats.db_alloc, stats.db_i2key, stats.db_ui2key, stats.db_str2key, + stats.db_i2data, stats.db_ui2data, + stats.db_ptr2data, stats.db_data2i, + stats.db_data2ui, stats.db_data2ptr, stats.db_init, stats.db_final); #endif /* DB_ENABLE_STATS */ } diff --git a/src/common/db.h b/src/common/db.h index e53ace0e6..49a95a135 100644 --- a/src/common/db.h +++ b/src/common/db.h @@ -16,12 +16,12 @@ * released. * * * * TODO: * - * - create an enum for the data (with int, unsigned int and void *) * * - create a custom database allocator * * - see what functions need or should be added to the database interface * * * * HISTORY: * - * 2007/11/09 - Added an iterator to the database. + * 2012/03/09 - Added enum for data types (int, uint, void*) * + * 2007/11/09 - Added an iterator to the database. * * 2.1 (Athena build #???#) - Portability fix * * - Fixed the portability of casting to union and added the functions * * {@link DBMap#ensure(DBMap,DBKey,DBCreateData,...)} and * @@ -50,7 +50,9 @@ * DBType - Enumeration of database types. * * DBOptions - Bitfield enumeration of database options. * * DBKey - Union of used key types. * - * DBApply - Format of functions applyed to the databases. * + * DBDataType - Enumeration of data types. * + * DBData - Struct for used data types. * + * DBApply - Format of functions applied to the databases. * * 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. * @@ -146,33 +148,65 @@ typedef union DBKey { } DBKey; /** - * Format of funtions that create the data for the key when the entry doesn't + * Supported types of database data. + * @param DB_DATA_INT Uses ints for data. + * @param DB_DATA_UINT Uses unsigned ints for data. + * @param DB_DATA_PTR Uses void pointers for data. + * @public + * @see #DBData + */ +typedef enum DBDataType { + DB_DATA_INT, + DB_DATA_UINT, + DB_DATA_PTR +} DBDataType; + +/** + * Struct for data types used by the database. + * @param type Type of data + * @param u Union of available data types + * @param u.i Data of int type + * @param u.ui Data of unsigned int type + * @param u.ptr Data of void* type + * @public + */ +typedef struct DBData { + DBDataType type; + union { + int i; + unsigned int ui; + void *ptr; + } u; +} DBData; + +/** + * Format of functions that create the data for the key when the entry doesn't * exist in the database yet. * @param key Key of the database entry - * @param args Extra arguments of the funtion + * @param args Extra arguments of the function * @return Data identified by the key to be put in the database * @public * @see DBMap#vensure * @see DBMap#ensure */ -typedef void* (*DBCreateData)(DBKey key, va_list args); +typedef DBData (*DBCreateData)(DBKey key, va_list args); /** - * Format of functions to be applyed to an unspecified quantity of entries of + * Format of functions to be applied to an unspecified quantity of entries of * a database. * 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 + * @param args Extra arguments of the function + * @return Value to be added up by the function that is applying this * @public * @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, DBData *data, va_list args); /** * Format of functions that match database entries. @@ -185,7 +219,7 @@ typedef int (*DBApply)(DBKey key, void* data, va_list args); * @public * @see DBMap#getall */ -typedef int (*DBMatcher)(DBKey key, void* data, va_list args); +typedef int (*DBMatcher)(DBKey key, DBData data, va_list args); /** * Format of the comparators used internally by the database system. @@ -225,7 +259,7 @@ 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, DBData data, DBRelease which); @@ -255,7 +289,7 @@ struct DBIterator * @return Data of the entry * @protected */ - void* (*first)(DBIterator* self, DBKey* out_key); + DBData* (*first)(DBIterator* self, DBKey* out_key); /** * Fetches the last entry in the database. @@ -266,7 +300,7 @@ struct DBIterator * @return Data of the entry * @protected */ - void* (*last)(DBIterator* self, DBKey* out_key); + DBData* (*last)(DBIterator* self, DBKey* out_key); /** * Fetches the next entry in the database. @@ -277,7 +311,7 @@ struct DBIterator * @return Data of the entry * @protected */ - void* (*next)(DBIterator* self, DBKey* out_key); + DBData* (*next)(DBIterator* self, DBKey* out_key); /** * Fetches the previous entry in the database. @@ -288,7 +322,7 @@ struct DBIterator * @return Data of the entry * @protected */ - void* (*prev)(DBIterator* self, DBKey* out_key); + DBData* (*prev)(DBIterator* self, DBKey* out_key); /** * Returns true if the fetched entry exists. @@ -303,14 +337,15 @@ struct DBIterator /** * Removes the current entry from the database. * NOTE: {@link DBIterator#exists} will return false until another entry - * is fethed - * Returns the data of the entry. + * is fetched + * Puts data of the removed entry in out_data, if out_data is not NULL. * @param self Iterator - * @return The data of the entry or NULL if not found + * @param out_data Data of the removed entry. + * @return 1 if entry was removed, 0 otherwise * @protected * @see DBMap#remove */ - void* (*remove)(DBIterator* self); + int (*remove)(DBIterator* self, DBData *out_data); /** * Destroys this iterator and unlocks the database. @@ -350,13 +385,13 @@ struct DBMap { bool (*exists)(DBMap* self, DBKey key); /** - * Get the data of the entry identifid by the key. + * Get the data of the entry identified by the key. * @param self Database * @param key Key that identifies the entry * @return Data of the entry or NULL if not found * @protected */ - void* (*get)(DBMap* self, DBKey key); + DBData* (*get)(DBMap* self, DBKey key); /** * Just calls {@link DBMap#vgetall}. @@ -375,7 +410,7 @@ struct DBMap { * @protected * @see DBMap#vgetall(DBMap*,void **,unsigned int,DBMatcher,va_list) */ - unsigned int (*getall)(DBMap* self, void** buf, unsigned int max, DBMatcher match, ...); + unsigned int (*getall)(DBMap* self, DBData** buf, unsigned int max, DBMatcher match, ...); /** * Get the data of the entries matched by <code>match</code>. @@ -393,7 +428,7 @@ struct DBMap { * @protected * @see DBMap#getall(DBMap*,void **,unsigned int,DBMatcher,...) */ - unsigned int (*vgetall)(DBMap* self, void** buf, unsigned int max, DBMatcher match, va_list args); + unsigned int (*vgetall)(DBMap* self, DBData** buf, unsigned int max, DBMatcher match, va_list args); /** * Just calls {@link DBMap#vensure}. @@ -408,7 +443,7 @@ struct DBMap { * @protected * @see DBMap#vensure(DBMap*,DBKey,DBCreateData,va_list) */ - void* (*ensure)(DBMap* self, DBKey key, DBCreateData create, ...); + DBData* (*ensure)(DBMap* self, DBKey key, DBCreateData create, ...); /** * Get the data of the entry identified by the key. @@ -422,37 +457,39 @@ struct DBMap { * @protected * @see DBMap#ensure(DBMap*,DBKey,DBCreateData,...) */ - void* (*vensure)(DBMap* self, DBKey key, DBCreateData create, va_list args); + DBData* (*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. + * Puts the previous data in out_data, if out_data is not NULL. * NOTE: Uses the new key, the old one is released. * @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 + * @param out_data Previous data if the entry exists + * @return 1 if if the entry already exists, 0 otherwise * @protected */ - void* (*put)(DBMap* self, DBKey key, void* data); + int (*put)(DBMap* self, DBKey key, DBData data, DBData *out_data); /** * Remove an entry from the database. - * Returns the data of the entry. + * Puts the previous data in out_data, if out_data is not NULL. * NOTE: The key (of the database) is released. * @param self Database * @param key Key that identifies the entry - * @return The data of the entry or NULL if not found + * @param out_data Previous data if the entry exists + * @return 1 if if the entry already exists, 0 otherwise * @protected */ - void* (*remove)(DBMap* self, DBKey key); + int (*remove)(DBMap* self, DBKey key, DBData *out_data); /** * Just calls {@link DBMap#vforeach}. * Apply <code>func</code> to every entry in the database. * Returns the sum of values returned by func. * @param self Database - * @param func Function to be applyed + * @param func Function to be applied * @param ... Extra arguments for func * @return Sum of the values returned by func * @protected @@ -464,7 +501,7 @@ struct DBMap { * Apply <code>func</code> to every entry in the database. * Returns the sum of values returned by func. * @param self Database - * @param func Function to be applyed + * @param func Function to be applied * @param args Extra arguments for func * @return Sum of the values returned by func * @protected @@ -475,11 +512,11 @@ struct DBMap { /** * Just calls {@link DBMap#vclear}. * Removes all entries from the database. - * Before deleting an entry, func is applyed to it. + * Before deleting an entry, func is applied to it. * Releases the key and the data. * Returns the sum of values returned by func, if it exists. * @param self Database - * @param func Function to be applyed to every entry before deleting + * @param func Function to be applied to every entry before deleting * @param ... Extra arguments for func * @return Sum of values returned by func * @protected @@ -489,11 +526,11 @@ struct DBMap { /** * Removes all entries from the database. - * Before deleting an entry, func is applyed to it. + * Before deleting an entry, func is applied to it. * Releases the key and the data. * Returns the sum of values returned by func, if it exists. * @param self Database - * @param func Function to be applyed to every entry before deleting + * @param func Function to be applied to every entry before deleting * @param args Extra arguments for func * @return Sum of values returned by func * @protected @@ -504,13 +541,13 @@ struct DBMap { /** * Just calls {@link DBMap#vdestroy}. * Finalize the database, feeing all the memory it uses. - * Before deleting an entry, func is applyed to it. + * Before deleting an entry, func is applied 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 self Database - * @param func Function to be applyed to every entry before deleting + * @param func Function to be applied to every entry before deleting * @param ... Extra arguments for func * @return Sum of values returned by func * @protected @@ -520,12 +557,12 @@ struct DBMap { /** * Finalize the database, feeing all the memory it uses. - * Before deleting an entry, func is applyed to it. + * Before deleting an entry, func is applied to it. * 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 self Database - * @param func Function to be applyed to every entry before deleting + * @param func Function to be applied to every entry before deleting * @param args Extra arguments for func * @return Sum of values returned by func * @protected @@ -559,33 +596,61 @@ struct DBMap { }; -//For easy access to the common functions. +// For easy access to the common functions. + #define db_exists(db,k) ( (db)->exists((db),(k)) ) #define idb_exists(db,k) ( (db)->exists((db),db_i2key(k)) ) #define uidb_exists(db,k) ( (db)->exists((db),db_ui2key(k)) ) #define strdb_exists(db,k) ( (db)->exists((db),db_str2key(k)) ) -#define db_get(db,k) ( (db)->get((db),(k)) ) -#define idb_get(db,k) ( (db)->get((db),db_i2key(k)) ) -#define uidb_get(db,k) ( (db)->get((db),db_ui2key(k)) ) -#define strdb_get(db,k) ( (db)->get((db),db_str2key(k)) ) - -#define db_put(db,k,d) ( (db)->put((db),(k),(d)) ) -#define idb_put(db,k,d) ( (db)->put((db),db_i2key(k),(d)) ) -#define uidb_put(db,k,d) ( (db)->put((db),db_ui2key(k),(d)) ) -#define strdb_put(db,k,d) ( (db)->put((db),db_str2key(k),(d)) ) - -#define db_remove(db,k) ( (db)->remove((db),(k)) ) -#define idb_remove(db,k) ( (db)->remove((db),db_i2key(k)) ) -#define uidb_remove(db,k) ( (db)->remove((db),db_ui2key(k)) ) -#define strdb_remove(db,k) ( (db)->remove((db),db_str2key(k)) ) +// Get pointer-type data from DBMaps of various key types +#define db_get(db,k) ( db_data2ptr((db)->get((db),(k))) ) +#define idb_get(db,k) ( db_data2ptr((db)->get((db),db_i2key(k))) ) +#define uidb_get(db,k) ( db_data2ptr((db)->get((db),db_ui2key(k))) ) +#define strdb_get(db,k) ( db_data2ptr((db)->get((db),db_str2key(k))) ) + +// Get int-type data from DBMaps of various key types +#define db_iget(db,k) ( db_data2i((db)->get((db),(k))) ) +#define idb_iget(db,k) ( db_data2i((db)->get((db),db_i2key(k))) ) +#define uidb_iget(db,k) ( db_data2i((db)->get((db),db_ui2key(k))) ) +#define strdb_iget(db,k) ( db_data2i((db)->get((db),db_str2key(k))) ) + +// Get uint-type data from DBMaps of various key types +#define db_uiget(db,k) ( db_data2ui((db)->get((db),(k))) ) +#define idb_uiget(db,k) ( db_data2ui((db)->get((db),db_i2key(k))) ) +#define uidb_uiget(db,k) ( db_data2ui((db)->get((db),db_ui2key(k))) ) +#define strdb_uiget(db,k) ( db_data2ui((db)->get((db),db_str2key(k))) ) + +// Put pointer-type data into DBMaps of various key types +#define db_put(db,k,d) ( (db)->put((db),(k),db_ptr2data(d),NULL) ) +#define idb_put(db,k,d) ( (db)->put((db),db_i2key(k),db_ptr2data(d),NULL) ) +#define uidb_put(db,k,d) ( (db)->put((db),db_ui2key(k),db_ptr2data(d),NULL) ) +#define strdb_put(db,k,d) ( (db)->put((db),db_str2key(k),db_ptr2data(d),NULL) ) + +// Put int-type data into DBMaps of various key types +#define db_iput(db,k,d) ( (db)->put((db),(k),db_i2data(d)) ) +#define idb_iput(db,k,d) ( (db)->put((db),db_i2key(k),db_i2data(d)) ) +#define uidb_iput(db,k,d) ( (db)->put((db),db_ui2key(k),db_i2data(d)) ) +#define strdb_iput(db,k,d) ( (db)->put((db),db_str2key(k),db_i2data(d)) ) + +// Put uint-type data into DBMaps of various key types +#define db_uiput(db,k,d) ( (db)->put((db),(k),db_ui2data(d)) ) +#define idb_uiput(db,k,d) ( (db)->put((db),db_i2key(k),db_ui2data(d)) ) +#define uidb_uiput(db,k,d) ( (db)->put((db),db_ui2key(k),db_ui2data(d)) ) +#define strdb_uiput(db,k,d) ( (db)->put((db),db_str2key(k),db_ui2data(d)) ) + +// Remove entry from DBMaps of various key types +#define db_remove(db,k) ( (db)->remove((db),(k),NULL) ) +#define idb_remove(db,k) ( (db)->remove((db),db_i2key(k),NULL) ) +#define uidb_remove(db,k) ( (db)->remove((db),db_ui2key(k),NULL) ) +#define strdb_remove(db,k) ( (db)->remove((db),db_str2key(k),NULL) ) //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),db_i2key(k),(f)) ) -#define uidb_ensure(db,k,f) ( (db)->ensure((db),db_ui2key(k),(f)) ) -#define strdb_ensure(db,k,f) ( (db)->ensure((db),db_str2key(k),(f)) ) +#define db_ensure(db,k,f) ( db_data2ptr((db)->ensure((db),(k),(f))) ) +#define idb_ensure(db,k,f) ( db_data2ptr((db)->ensure((db),db_i2key(k),(f))) ) +#define uidb_ensure(db,k,f) ( db_data2ptr((db)->ensure((db),db_ui2key(k),(f))) ) +#define strdb_ensure(db,k,f) ( db_data2ptr((db)->ensure((db),db_str2key(k),(f))) ) // Database creation and destruction macros #define idb_alloc(opt) db_alloc(__FILE__,__LINE__,DB_INT,(opt),sizeof(int)) @@ -597,10 +662,11 @@ struct DBMap { #define db_clear(db) ( (db)->clear(db,NULL) ) #define db_size(db) ( (db)->size(db) ) #define db_iterator(db) ( (db)->iterator(db) ) -#define dbi_first(dbi) ( (dbi)->first(dbi,NULL) ) -#define dbi_last(dbi) ( (dbi)->last(dbi,NULL) ) -#define dbi_next(dbi) ( (dbi)->next(dbi,NULL) ) -#define dbi_prev(dbi) ( (dbi)->prev(dbi,NULL) ) +#define dbi_first(dbi) ( db_data2ptr((dbi)->first(dbi,NULL)) ) +#define dbi_last(dbi) ( db_data2ptr((dbi)->last(dbi,NULL)) ) +#define dbi_next(dbi) ( db_data2ptr((dbi)->next(dbi,NULL)) ) +#define dbi_prev(dbi) ( db_data2ptr((dbi)->prev(dbi,NULL)) ) +#define dbi_remove(dbi) ( (dbi)->remove(dbi,NULL) ) #define dbi_exists(dbi) ( (dbi)->exists(dbi) ) #define dbi_destroy(dbi) ( (dbi)->destroy(dbi) ) @@ -616,8 +682,14 @@ struct DBMap { * db_i2key - Manual cast from 'int' to 'DBKey'. * * db_ui2key - Manual cast from 'unsigned int' to 'DBKey'. * * db_str2key - Manual cast from 'unsigned char *' to 'DBKey'. * - * db_init - Initialise the database system. * - * db_final - Finalise the database system. * + * db_i2data - Manual cast from 'int' to 'DBData'. * + * db_ui2data - Manual cast from 'unsigned int' to 'DBData'. * + * db_ptr2data - Manual cast from 'void*' to 'DBData'. * + * db_data2i - Gets 'int' value from 'DBData'. * + * db_data2ui - Gets 'unsigned int' value from 'DBData'. * + * db_data2ptr - Gets 'void*' value from 'DBData'. * + * db_init - Initializes the database system. * + * db_final - Finalizes the database system. * \*****************************************************************************/ /** @@ -730,6 +802,57 @@ DBKey db_ui2key(unsigned int key); DBKey db_str2key(const char *key); /** + * Manual cast from 'int' to the struct DBData. + * @param data Data to be casted + * @return The data as a DBData struct + * @public + */ +DBData db_i2data(int data); + +/** + * Manual cast from 'unsigned int' to the struct DBData. + * @param data Data to be casted + * @return The data as a DBData struct + * @public + */ +DBData db_ui2data(unsigned int data); + +/** + * Manual cast from 'void *' to the struct DBData. + * @param data Data to be casted + * @return The data as a DBData struct + * @public + */ +DBData db_ptr2data(void *data); + +/** + * Gets int type data from struct DBData. + * If data is not int type, returns 0. + * @param data Data + * @return Integer value of the data. + * @public + */ +int db_data2i(DBData *data); + +/** + * Gets unsigned int type data from struct DBData. + * If data is not unsigned int type, returns 0. + * @param data Data + * @return Unsigned int value of the data. + * @public + */ +unsigned int db_data2ui(DBData *data); + +/** + * Gets void* type data from struct DBData. + * If data is not void* type, returns NULL. + * @param data Data + * @return Void* value of the data. + * @public + */ +void* db_data2ptr(DBData *data); + +/** * Initialize the database system. * @public * @see #db_final(void) diff --git a/src/common/showmsg.c b/src/common/showmsg.c index 4d09e2c53..dce0a8dbb 100644 --- a/src/common/showmsg.c +++ b/src/common/showmsg.c @@ -59,7 +59,7 @@ int console_msg_log = 0;//[Ind] msg error logging /////////////////////////////////////////////////////////////////////////////// /// static/dynamic buffer for the messages -#define SBUF_SIZE 2048 // never put less that what's required for the debug message +#define SBUF_SIZE 2054 // never put less that what's required for the debug message #define NEWBUF(buf) \ struct { \ diff --git a/src/login/login.c b/src/login/login.c index 78211dc64..14bd9fb9b 100644 --- a/src/login/login.c +++ b/src/login/login.c @@ -98,20 +98,23 @@ struct online_login_data { static DBMap* online_db; // int account_id -> struct online_login_data* static int waiting_disconnect_timer(int tid, unsigned int tick, int id, intptr_t data); -static void* create_online_user(DBKey key, va_list args) +/** + * @see DBCreateData + */ +static DBData create_online_user(DBKey key, va_list args) { struct online_login_data* p; CREATE(p, struct online_login_data, 1); p->account_id = key.i; p->char_server = -1; p->waiting_disconnect = INVALID_TIMER; - return p; + return db_ptr2data(p); } struct online_login_data* add_online_user(int char_server, int account_id) { struct online_login_data* p; - p = (struct online_login_data*)idb_ensure(online_db, account_id, create_online_user); + p = idb_ensure(online_db, account_id, create_online_user); p->char_server = char_server; if( p->waiting_disconnect != INVALID_TIMER ) { @@ -145,9 +148,12 @@ static int waiting_disconnect_timer(int tid, unsigned int tick, int id, intptr_t return 0; } -static int online_db_setoffline(DBKey key, void* data, va_list ap) +/** + * @see DBApply + */ +static int online_db_setoffline(DBKey key, DBData *data, va_list ap) { - struct online_login_data* p = (struct online_login_data*)data; + struct online_login_data* p = db_data2ptr(data); int server = va_arg(ap, int); if( server == -1 ) { @@ -163,9 +169,12 @@ static int online_db_setoffline(DBKey key, void* data, va_list ap) return 0; } -static int online_data_cleanup_sub(DBKey key, void *data, va_list ap) +/** + * @see DBApply + */ +static int online_data_cleanup_sub(DBKey key, DBData *data, va_list ap) { - struct online_login_data *character= (struct online_login_data*)data; + struct online_login_data *character= db_data2ptr(data); if (character->char_server == -2) //Unknown server.. set them offline remove_online_user(character->account_id); return 0; @@ -835,7 +844,7 @@ int parse_fromchar(int fd) users = RFIFOW(fd,4); for (i = 0; i < users; i++) { aid = RFIFOL(fd,6+i*4); - p = (struct online_login_data*)idb_ensure(online_db, aid, create_online_user); + p = idb_ensure(online_db, aid, create_online_user); p->char_server = id; if (p->waiting_disconnect != INVALID_TIMER) { diff --git a/src/map/battleground.c b/src/map/battleground.c index b456a868a..7b605066d 100644 --- a/src/map/battleground.c +++ b/src/map/battleground.c @@ -216,9 +216,12 @@ int bg_send_message(struct map_session_data *sd, const char *mes, int len) return 0; } -int bg_send_xy_timer_sub(DBKey key, void *data, va_list ap) +/** + * @see DBApply + */ +int bg_send_xy_timer_sub(DBKey key, DBData *data, va_list ap) { - struct battleground_data *bg = (struct battleground_data *)data; + struct battleground_data *bg = db_data2ptr(data); struct map_session_data *sd; int i; nullpo_ret(bg); diff --git a/src/map/chrif.c b/src/map/chrif.c index 26c7b10cc..c4ff4b662 100644 --- a/src/map/chrif.c +++ b/src/map/chrif.c @@ -463,9 +463,13 @@ int chrif_connectack(int fd) return 0; } -static int chrif_reconnect(DBKey key,void *data,va_list ap) + +/** + * @see DBApply + */ +static int chrif_reconnect(DBKey key, DBData *data, va_list ap) { - struct auth_node *node=(struct auth_node*)data; + struct auth_node *node = db_data2ptr(data); switch (node->state) { case ST_LOGIN: if (node->sd && node->char_dat == NULL) @@ -669,10 +673,13 @@ void chrif_authfail(int fd) } -//This can still happen (client times out while waiting for char to confirm auth data) -int auth_db_cleanup_sub(DBKey key,void *data,va_list ap) +/** + * This can still happen (client times out while waiting for char to confirm auth data) + * @see DBApply + */ +int auth_db_cleanup_sub(DBKey key, DBData *data, va_list ap) { - struct auth_node *node=(struct auth_node*)data; + struct auth_node *node = db_data2ptr(data); const char* states[] = { "Login", "Logout", "Map change" }; if(DIFF_TICK(gettick(),node->node_created)>60000) { switch (node->state) @@ -1558,10 +1565,12 @@ int chrif_removefriend(int char_id, int friend_id) { return 0; } - -int auth_db_final(DBKey k,void *d,va_list ap) +/** + * @see DBApply + */ +int auth_db_final(DBKey key, DBData *data, va_list ap) { - struct auth_node *node=(struct auth_node*)d; + struct auth_node *node = db_data2ptr(data); if (node->char_dat) aFree(node->char_dat); if (node->sd) diff --git a/src/map/guild.c b/src/map/guild.c index deb603473..528560ee8 100644 --- a/src/map/guild.c +++ b/src/map/guild.c @@ -282,14 +282,17 @@ void guild_makemember(struct guild_member *m,struct map_session_data *sd) return; } -// ギルドのEXPキャッシュをinter鯖にフラッシュする -int guild_payexp_timer_sub(DBKey dataid, void *data, va_list ap) +/** + * ギルドのEXPキャッシュをinter鯖にフラッシュする + * @see DBApply + */ +int guild_payexp_timer_sub(DBKey key, DBData *data, va_list ap) { int i; struct guild_expcache *c; struct guild *g; - c = (struct guild_expcache *)data; + c = db_data2ptr(data); if ( (g = guild_search(c->guild_id)) == NULL || @@ -318,10 +321,13 @@ int guild_payexp_timer(int tid, unsigned int tick, int id, intptr_t data) return 0; } -//Taken from party_send_xy_timer_sub. [Skotlex] -int guild_send_xy_timer_sub(DBKey key,void *data,va_list ap) +/** + * Taken from party_send_xy_timer_sub. [Skotlex] + * @see DBApply + */ +int guild_send_xy_timer_sub(DBKey key, DBData *data, va_list ap) { - struct guild *g=(struct guild *)data; + struct guild *g = db_data2ptr(data); int i; nullpo_ret(g); @@ -425,11 +431,13 @@ int guild_npc_request_info(int guild_id,const char *event) if( event && *event ) { - struct eventlist* ev; + struct eventlist *ev; + DBData prev; ev=(struct eventlist *)aCalloc(sizeof(struct eventlist),1); memcpy(ev->name,event,strlen(event)); - //The one in the db becomes the next event from this. - ev->next = (struct eventlist*)idb_put(guild_infoevent_db,guild_id,ev); + //The one in the db (if present) becomes the next event from this. + if (guild_infoevent_db->put(guild_infoevent_db, db_i2key(guild_id), db_ptr2data(ev), &prev)) + ev->next = db_data2ptr(&prev); } return guild_request_info(guild_id); @@ -484,7 +492,7 @@ int guild_recv_info(struct guild *sg) { struct guild *g,before; int i,bm,m; - struct eventlist *ev,*ev2; + DBData data; struct map_session_data *sd; bool guild_new = false; @@ -558,8 +566,9 @@ int guild_recv_info(struct guild *sg) } // イベントの発生 - if( (ev = (struct eventlist*)idb_remove(guild_infoevent_db,sg->guild_id))!=NULL ) + if (guild_infoevent_db->remove(guild_infoevent_db, db_i2key(sg->guild_id), &data)) { + struct eventlist *ev = db_data2ptr(&data), *ev2; while(ev){ npc_event_do(ev->name); ev2=ev->next; @@ -1122,7 +1131,10 @@ int guild_emblem_changed(int len,int guild_id,int emblem_id,const char *data) return 0; } -static void* create_expcache(DBKey key, va_list args) +/** + * @see DBCreateData + */ +static DBData create_expcache(DBKey key, va_list args) { struct guild_expcache *c; struct map_session_data *sd = va_arg(args, struct map_session_data*); @@ -1132,7 +1144,7 @@ static void* create_expcache(DBKey key, va_list args) c->account_id = sd->status.account_id; c->char_id = sd->status.char_id; c->exp = 0; - return c; + return db_ptr2data(c); } // ギルドのEXP上納 @@ -1157,7 +1169,7 @@ unsigned int guild_payexp(struct map_session_data *sd,unsigned int exp) exp = exp * per / 100; //Otherwise tax everything. - c = (struct guild_expcache*)guild_expcache_db->ensure(guild_expcache_db, db_i2key(sd->status.char_id), create_expcache, sd); + c = db_data2ptr(guild_expcache_db->ensure(guild_expcache_db, db_i2key(sd->status.char_id), create_expcache, sd)); if (c->exp > UINT64_MAX - exp) c->exp = UINT64_MAX; @@ -1176,7 +1188,7 @@ int guild_getexp(struct map_session_data *sd,int exp) if (sd->status.guild_id == 0 || guild_search(sd->status.guild_id) == NULL) return 0; - c = (struct guild_expcache*)guild_expcache_db->ensure(guild_expcache_db, db_i2key(sd->status.char_id), create_expcache, sd); + c = db_data2ptr(guild_expcache_db->ensure(guild_expcache_db, db_i2key(sd->status.char_id), create_expcache, sd)); if (c->exp > UINT64_MAX - exp) c->exp = UINT64_MAX; else @@ -1507,10 +1519,14 @@ int guild_allianceack(int guild_id1,int guild_id2,int account_id1,int account_id } return 0; } -// ギルド解散通知用 -int guild_broken_sub(DBKey key,void *data,va_list ap) + +/** + * ギルド解散通知用 + * @see DBApply + */ +int guild_broken_sub(DBKey key, DBData *data, va_list ap) { - struct guild *g=(struct guild *)data; + struct guild *g = db_data2ptr(data); int guild_id=va_arg(ap,int); int i,j; struct map_session_data *sd=NULL; @@ -1529,11 +1545,14 @@ int guild_broken_sub(DBKey key,void *data,va_list ap) return 0; } -//Invoked on Castles when a guild is broken. [Skotlex] -int castle_guild_broken_sub(DBKey key, void *data, va_list ap) +/** + * Invoked on Castles when a guild is broken. [Skotlex] + * @see DBApply + */ +int castle_guild_broken_sub(DBKey key, DBData *data, va_list ap) { char name[EVENT_NAME_LENGTH]; - struct guild_castle *gc = data; + struct guild_castle *gc = db_data2ptr(data); int guild_id = va_arg(ap, int); nullpo_ret(gc); @@ -1900,10 +1919,13 @@ bool guild_isallied(int guild_id, int guild_id2) return( i < MAX_GUILDALLIANCE && g->alliance[i].opposition == 0 ); } -static int eventlist_db_final(DBKey key,void *data,va_list ap) +/** + * @see DBApply + */ +static int eventlist_db_final(DBKey key, DBData *data, va_list ap) { struct eventlist *next = NULL; - struct eventlist *current = data; + struct eventlist *current = db_data2ptr(data); while (current != NULL) { next = current->next; aFree(current); @@ -1912,18 +1934,24 @@ static int eventlist_db_final(DBKey key,void *data,va_list ap) return 0; } -static int guild_expcache_db_final(DBKey key,void *data,va_list ap) +/** + * @see DBApply + */ +static int guild_expcache_db_final(DBKey key, DBData *data, va_list ap) { - ers_free(expcache_ers, data); + ers_free(expcache_ers, db_data2ptr(data)); return 0; } -static int guild_castle_db_final(DBKey key, void* data,va_list ap) +/** + * @see DBApply + */ +static int guild_castle_db_final(DBKey key, DBData *data, va_list ap) { - struct guild_castle* gc = (struct guild_castle*)data; + struct guild_castle* gc = db_data2ptr(data); if( gc->temp_guardians ) aFree(gc->temp_guardians); - aFree(data); + aFree(gc); return 0; } diff --git a/src/map/itemdb.c b/src/map/itemdb.c index e25c50a40..8da198c59 100644 --- a/src/map/itemdb.c +++ b/src/map/itemdb.c @@ -23,13 +23,14 @@ static struct item_group itemgroup_db[MAX_ITEMGROUP]; struct item_data dummy_item; //This is the default dummy item used for non-existant items. [Skotlex] -/*========================================== +/** * 名前で検索用 - *------------------------------------------*/ -// name = item alias, so we should find items aliases first. if not found then look for "jname" (full name) -static int itemdb_searchname_sub(DBKey key,void *data,va_list ap) + * name = item alias, so we should find items aliases first. if not found then look for "jname" (full name) + * @see DBApply + */ +static int itemdb_searchname_sub(DBKey key, DBData *data, va_list ap) { - struct item_data *item=(struct item_data *)data,**dst,**dst2; + struct item_data *item = db_data2ptr(data), **dst, **dst2; char *str; str=va_arg(ap,char *); dst=va_arg(ap,struct item_data **); @@ -77,9 +78,12 @@ struct item_data* itemdb_searchname(const char *str) return item?item:item2; } -static int itemdb_searchname_array_sub(DBKey key,void * data,va_list ap) +/** + * @see DBMatcher + */ +static int itemdb_searchname_array_sub(DBKey key, DBData data, va_list ap) { - struct item_data *item=(struct item_data *)data; + struct item_data *item = db_data2ptr(&data); char *str; str=va_arg(ap,char *); if (item == &dummy_item) @@ -116,17 +120,17 @@ int itemdb_searchname_array(struct item_data** data, int size, const char *str) } // search in the db - if( count >= size ) + if( count < size ) { - data = NULL; - size = 0; - } - else - { - data -= count; + DBData *db_data[MAX_SEARCH]; + int db_count = 0; size -= count; + db_count = itemdb_other->getall(itemdb_other, (DBData**)&db_data, size, itemdb_searchname_array_sub, str); + for (i = 0; i < db_count; i++) + data[count++] = db_data2ptr(db_data[i]); + count += db_count; } - return count + itemdb_other->getall(itemdb_other,(void**)data,size,itemdb_searchname_array_sub,str); + return count; } @@ -1075,9 +1079,12 @@ static void destroy_item_data(struct item_data* self, int free_self) aFree(self); } -static int itemdb_final_sub(DBKey key,void *data,va_list ap) +/** + * @see DBApply + */ +static int itemdb_final_sub(DBKey key, DBData *data, va_list ap) { - struct item_data *id = (struct item_data *)data; + struct item_data *id = db_data2ptr(data); if( id != &dummy_item ) destroy_item_data(id, 1); diff --git a/src/map/map.c b/src/map/map.c index 73a9445ef..172f921ea 100644 --- a/src/map/map.c +++ b/src/map/map.c @@ -1472,11 +1472,14 @@ int map_addflooritem(struct item *item_data,int amount,int m,int x,int y,int fir return fitem->bl.id; } -static void* create_charid2nick(DBKey key, va_list args) +/** + * @see DBCreateData + */ +static DBData create_charid2nick(DBKey key, va_list args) { struct charid2nick *p; CREATE(p, struct charid2nick, 1); - return p; + return db_ptr2data(p); } /// Adds(or replaces) the nick of charid to nick_db and fullfils pending requests. @@ -1490,7 +1493,7 @@ void map_addnickdb(int charid, const char* nick) if( map_charid2sd(charid) ) return;// already online - p = (struct charid2nick*)idb_ensure(nick_db, charid, create_charid2nick); + p = idb_ensure(nick_db, charid, create_charid2nick); safestrncpy(p->nick, nick, sizeof(p->nick)); while( p->requests ) @@ -1511,8 +1514,10 @@ void map_delnickdb(int charid, const char* name) struct charid2nick* p; struct charid_request* req; struct map_session_data* sd; + DBData data; - p = (struct charid2nick*)idb_remove(nick_db, charid); + nick_db->remove(nick_db, db_i2key(charid), &data); + p = db_data2ptr(&data); if( p == NULL ) return; @@ -1546,7 +1551,7 @@ void map_reqnickdb(struct map_session_data * sd, int charid) return; } - p = (struct charid2nick*)idb_ensure(nick_db, charid, create_charid2nick); + p = idb_ensure(nick_db, charid, create_charid2nick); if( *p->nick ) { clif_solved_charname(sd->fd, charid, p->nick); @@ -1765,7 +1770,7 @@ const char* map_charid2nick(int charid) if( sd ) return sd->status.name;// character is online, return it's name - p = (struct charid2nick*)idb_ensure(nick_db, charid, create_charid2nick); + p = idb_ensure(nick_db, charid, create_charid2nick); if( *p->nick ) return p->nick;// name in nick_db @@ -2662,14 +2667,17 @@ void map_iwall_remove(const char *wall_name) strdb_remove(iwall_db, iwall->wall_name); } -static void* create_map_data_other_server(DBKey key, va_list args) +/** + * @see DBCreateData + */ +static DBData create_map_data_other_server(DBKey key, va_list args) { struct map_data_other_server *mdos; unsigned short mapindex = (unsigned short)key.ui; mdos=(struct map_data_other_server *)aCalloc(1,sizeof(struct map_data_other_server)); mdos->index = mapindex; memcpy(mdos->name, mapindex_id2name(mapindex), MAP_NAME_LENGTH); - return mdos; + return db_ptr2data(mdos); } /*========================================== @@ -2679,7 +2687,7 @@ int map_setipport(unsigned short mapindex, uint32 ip, uint16 port) { struct map_data_other_server *mdos=NULL; - mdos=(struct map_data_other_server *)uidb_ensure(map_db,(unsigned int)mapindex, create_map_data_other_server); + mdos= uidb_ensure(map_db,(unsigned int)mapindex, create_map_data_other_server); if(mdos->cell) //Local map,Do nothing. Give priority to our own local maps over ones from another server. [Skotlex] return 0; @@ -2693,12 +2701,13 @@ int map_setipport(unsigned short mapindex, uint32 ip, uint16 port) return 1; } -/*========================================== +/** * 他鯖管理のマップを全て削除 - *------------------------------------------*/ -int map_eraseallipport_sub(DBKey key,void *data,va_list va) + * @see DBApply + */ +int map_eraseallipport_sub(DBKey key, DBData *data, va_list va) { - struct map_data_other_server *mdos = (struct map_data_other_server*)data; + struct map_data_other_server *mdos = db_data2ptr(data); if(mdos->cell == NULL) { db_remove(map_db,key); aFree(mdos); @@ -3425,17 +3434,23 @@ int log_sql_init(void) return 0; } -int map_db_final(DBKey k,void *d,va_list ap) +/** + * @see DBApply + */ +int map_db_final(DBKey key, DBData *data, va_list ap) { - struct map_data_other_server *mdos = (struct map_data_other_server*)d; + struct map_data_other_server *mdos = db_data2ptr(data); if(mdos && mdos->cell == NULL) aFree(mdos); return 0; } -int nick_db_final(DBKey key, void *data, va_list args) +/** + * @see DBApply + */ +int nick_db_final(DBKey key, DBData *data, va_list args) { - struct charid2nick* p = (struct charid2nick*)data; + struct charid2nick* p = db_data2ptr(data); struct charid_request* req; if( p == NULL ) @@ -3478,9 +3493,12 @@ int cleanup_sub(struct block_list *bl, va_list ap) return 1; } -static int cleanup_db_sub(DBKey key,void *data,va_list va) +/** + * @see DBApply + */ +static int cleanup_db_sub(DBKey key, DBData *data, va_list va) { - return cleanup_sub((struct block_list*)data, va); + return cleanup_sub(db_data2ptr(data), va); } /*========================================== diff --git a/src/map/mapreg_sql.c b/src/map/mapreg_sql.c index 365fdc8ce..c8b177c7e 100644 --- a/src/map/mapreg_sql.c +++ b/src/map/mapreg_sql.c @@ -146,7 +146,7 @@ static void script_load_mapreg(void) static void script_save_mapreg(void) { DBIterator* iter; - void* data; + DBData *data; DBKey key; iter = db_iterator(mapreg_db); @@ -159,7 +159,7 @@ static void script_save_mapreg(void) if( name[1] == '@' ) continue; - if( SQL_ERROR == Sql_Query(mmysql_handle, "UPDATE `%s` SET `value`='%d' WHERE `varname`='%s' AND `index`='%d'", mapreg_table, (int)data, name, i) ) + if( SQL_ERROR == Sql_Query(mmysql_handle, "UPDATE `%s` SET `value`='%d' WHERE `varname`='%s' AND `index`='%d'", mapreg_table, (int)db_data2ptr(data), name, i) ) Sql_ShowDebug(mmysql_handle); } dbi_destroy(iter); @@ -175,7 +175,7 @@ static void script_save_mapreg(void) if( name[1] == '@' ) continue; - Sql_EscapeStringLen(mmysql_handle, tmp_str2, (char*)data, safestrnlen((char*)data, 255)); + Sql_EscapeStringLen(mmysql_handle, tmp_str2, db_data2ptr(data), safestrnlen(db_data2ptr(data), 255)); if( SQL_ERROR == Sql_Query(mmysql_handle, "UPDATE `%s` SET `value`='%s' WHERE `varname`='%s' AND `index`='%d'", mapreg_table, tmp_str2, name, i) ) Sql_ShowDebug(mmysql_handle); } diff --git a/src/map/npc.c b/src/map/npc.c index 86d649cde..fb806cdd2 100644 --- a/src/map/npc.c +++ b/src/map/npc.c @@ -284,17 +284,19 @@ static int npc_event_export(struct npc_data *nd, int i) CREATE(ev, struct event_data, 1); ev->nd = nd; ev->pos = pos; - if (strdb_put(ev_db, buf, ev) != NULL) // There was already another event of the same name? + if (strdb_put(ev_db, buf, ev)) // There was already another event of the same name? return 1; } return 0; } int npc_event_sub(struct map_session_data* sd, struct event_data* ev, const char* eventname); //[Lance] -/*========================================== + +/** * 全てのNPCのOn*イベント実行 - *------------------------------------------*/ -int npc_event_doall_sub(DBKey key, void* data, va_list ap) + * @see DBApply + */ +int npc_event_doall_sub(DBKey key, DBData *data, va_list ap) { const char* p = key.str; struct event_data* ev; @@ -302,7 +304,7 @@ int npc_event_doall_sub(DBKey key, void* data, va_list ap) const char* name; int rid; - nullpo_ret(ev = (struct event_data *)data); + nullpo_ret(ev = db_data2ptr(data)); nullpo_ret(c = va_arg(ap, int *)); nullpo_ret(name = va_arg(ap, const char *)); rid = va_arg(ap, int); @@ -320,14 +322,17 @@ int npc_event_doall_sub(DBKey key, void* data, va_list ap) return 0; } -static int npc_event_do_sub(DBKey key, void* data, va_list ap) +/** + * @see DBApply + */ +static int npc_event_do_sub(DBKey key, DBData *data, va_list ap) { const char* p = key.str; struct event_data* ev; int* c; const char* name; - nullpo_ret(ev = (struct event_data *)data); + nullpo_ret(ev = db_data2ptr(data)); nullpo_ret(c = va_arg(ap, int *)); nullpo_ret(name = va_arg(ap, const char *)); @@ -1675,9 +1680,12 @@ int npc_remove_map(struct npc_data* nd) return 0; } -static int npc_unload_ev(DBKey key, void* data, va_list ap) +/** + * @see DBApply + */ +static int npc_unload_ev(DBKey key, DBData *data, va_list ap) { - struct event_data* ev = (struct event_data *)data; + struct event_data* ev = db_data2ptr(data); char* npcname = va_arg(ap, char *); if(strcmp(ev->nd->exname,npcname)==0){ @@ -2133,13 +2141,14 @@ static const char* npc_parse_shop(char* w1, char* w2, char* w3, char* w4, const return strchr(start,'\n');// continue } -/*========================================== +/** * NPCのラベルデータコンバート - *------------------------------------------*/ -int npc_convertlabel_db(DBKey key, void* data, va_list ap) + * @see DBApply + */ +int npc_convertlabel_db(DBKey key, DBData *data, va_list ap) { const char* lname = (const char*)key.str; - int lpos = (int)data; + int lpos = (int)db_data2ptr(data); struct npc_label_list** label_list; int* label_list_num; const char* filepath; @@ -2709,8 +2718,8 @@ void npc_setclass(struct npc_data* nd, short class_) static const char* npc_parse_function(char* w1, char* w2, char* w3, char* w4, const char* start, const char* buffer, const char* filepath) { DBMap* func_db; + DBData old_data; struct script_code *script; - struct script_code *oldscript; const char* end; const char* script_start; @@ -2732,9 +2741,9 @@ static const char* npc_parse_function(char* w1, char* w2, char* w3, char* w4, co return end; func_db = script_get_userfunc_db(); - oldscript = (struct script_code*)strdb_put(func_db, w3, script); - if( oldscript != NULL ) + if (func_db->put(func_db, db_str2key(w3), db_ptr2data(script), &old_data)) { + struct script_code *oldscript = db_data2ptr(&old_data); ShowInfo("npc_parse_function: Overwriting user function [%s] (%s:%d)\n", w3, filepath, strline(buffer,start-buffer)); script_free_vars(&oldscript->script_vars); aFree(oldscript->script_buf); @@ -3358,7 +3367,7 @@ void npc_read_event_script(void) { DBIterator* iter; DBKey key; - void* data; + DBData *data; char name[64]="::"; strncpy(name+2,config[i].event_name,62); @@ -3368,7 +3377,7 @@ void npc_read_event_script(void) for( data = iter->first(iter,&key); iter->exists(iter); data = iter->next(iter,&key) ) { const char* p = key.str; - struct event_data* ed = (struct event_data*) data; + struct event_data* ed = db_data2ptr(data); unsigned char count = script_event[i].event_count; if( count >= ARRAYLENGTH(script_event[i].event) ) diff --git a/src/map/script.c b/src/map/script.c index 3fa32d681..d2ff334bf 100644 --- a/src/map/script.c +++ b/src/map/script.c @@ -3634,10 +3634,14 @@ int script_config_read(char *cfgName) return 0; } -static int db_script_free_code_sub(DBKey key, void *data, va_list ap) +/** + * @see DBApply + */ +static int db_script_free_code_sub(DBKey key, DBData *data, va_list ap) { - if (data) - script_free_code(data); + struct script_code *code = db_data2ptr(data); + if (code) + script_free_code(code); return 0; } diff --git a/src/map/skill.c b/src/map/skill.c index b06a6f84c..1f18d2474 100644 --- a/src/map/skill.c +++ b/src/map/skill.c @@ -13579,12 +13579,12 @@ int skill_unit_timer_sub_onplace (struct block_list* bl, va_list ap) return 1; } -/*========================================== - * - *------------------------------------------*/ -static int skill_unit_timer_sub (DBKey key, void* data, va_list ap) +/** + * @see DBApply + */ +static int skill_unit_timer_sub(DBKey key, DBData *data, va_list ap) { - struct skill_unit* unit = (struct skill_unit*)data; + struct skill_unit* unit = db_data2ptr(data); struct skill_unit_group* group = unit->group; unsigned int tick = va_arg(ap,unsigned int); bool dissonance; diff --git a/src/map/storage.c b/src/map/storage.c index eed454c7c..d9ab0312f 100644 --- a/src/map/storage.c +++ b/src/map/storage.c @@ -66,11 +66,13 @@ void do_final_storage(void) // by [MC Cameri] guild_storage_db->destroy(guild_storage_db,NULL); } - -static int storage_reconnect_sub(DBKey key,void *data,va_list ap) -{ //Parses storage and saves 'dirty' ones upon reconnect. [Skotlex] - - struct guild_storage* stor = (struct guild_storage*) data; +/** + * Parses storage and saves 'dirty' ones upon reconnect. [Skotlex] + * @see DBApply + */ +static int storage_reconnect_sub(DBKey key, DBData *data, va_list ap) +{ + struct guild_storage *stor = db_data2ptr(data); if (stor->dirty && stor->storage_status == 0) //Save closed storages. storage_guild_storagesave(0, stor->guild_id,0); @@ -318,19 +320,22 @@ void storage_storage_quit(struct map_session_data* sd, int flag) sd->state.storage_flag = 0; } - -static void* create_guildstorage(DBKey key, va_list args) +/** + * @see DBCreateData + */ +static DBData create_guildstorage(DBKey key, va_list args) { struct guild_storage *gs = NULL; gs = (struct guild_storage *) aCalloc(sizeof(struct guild_storage), 1); gs->guild_id=key.i; - return gs; + return db_ptr2data(gs); } + struct guild_storage *guild2storage(int guild_id) { struct guild_storage *gs = NULL; if(guild_search(guild_id) != NULL) - gs=(struct guild_storage *) idb_ensure(guild_storage_db,guild_id,create_guildstorage); + gs = idb_ensure(guild_storage_db,guild_id,create_guildstorage); return gs; } |