diff options
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/HPM.c | 4 | ||||
-rw-r--r-- | src/common/HPMi.h | 4 | ||||
-rw-r--r-- | src/common/console.c | 2 | ||||
-rw-r--r-- | src/common/core.c | 10 | ||||
-rw-r--r-- | src/common/db.c | 32 | ||||
-rw-r--r-- | src/common/db.h | 123 | ||||
-rw-r--r-- | src/common/malloc.c | 79 | ||||
-rw-r--r-- | src/common/malloc.h | 74 |
8 files changed, 187 insertions, 141 deletions
diff --git a/src/common/HPM.c b/src/common/HPM.c index af06811a5..28ea8f413 100644 --- a/src/common/HPM.c +++ b/src/common/HPM.c @@ -264,6 +264,8 @@ void hplugins_share_defaults(void) { HPM->share(&SERVER_TYPE,"SERVER_TYPE"); HPM->share((void*)get_svn_revision,"get_svn_revision"); HPM->share((void*)get_git_hash,"get_git_hash"); + HPM->share(DB, "DB"); + HPM->share(malloclib, "malloclib"); /* socket */ HPM->share(RFIFOSKIP,"RFIFOSKIP"); HPM->share(WFIFOSET,"WFIFOSET"); @@ -284,7 +286,7 @@ void hplugins_share_defaults(void) { HPM->share(add_timer_interval,"add_timer_interval"); HPM->share(add_timer_func_list,"add_timer_func_list"); HPM->share(delete_timer,"delete_timer"); - HPM->share(get_uptime,"get_uptime"); + HPM->share(get_uptime,"get_uptime"); } CPCMD(plugins) { if( HPM->plugin_count == 0 ) { diff --git a/src/common/HPMi.h b/src/common/HPMi.h index a58eeed38..3cdb804e0 100644 --- a/src/common/HPMi.h +++ b/src/common/HPMi.h @@ -57,14 +57,14 @@ enum hp_event_types { }; /* Hercules Plugin Mananger Include Interface */ -struct HPMi_interface { +HPExport struct HPMi_interface { void (*event[HPET_MAX]) (void); bool (*addCommand) (char *name, bool (*func)(const int fd, struct map_session_data* sd, const char* command, const char* message,struct AtCommandInfo *info)); bool (*addScript) (char *name, char *args, bool (*func)(struct script_state *st)); void (*addCPCommand) (char *name, CParseFunc func); } HPMi_s; #ifndef _HPM_H_ - struct HPMi_interface *HPMi; + HPExport struct HPMi_interface *HPMi; #endif #endif /* _HPMi_H_ */ diff --git a/src/common/console.c b/src/common/console.c index 248dd7b45..ba93b8e09 100644 --- a/src/common/console.c +++ b/src/common/console.c @@ -98,7 +98,7 @@ CPCMD(help) { } /* [Ind/Hercules] */ CPCMD(malloc_usage) { - unsigned int val = (unsigned int)malloc_usage(); + unsigned int val = (unsigned int)malloclib->usage(); ShowInfo("malloc_usage: %.2f MB\n",(double)(val)/1024); } #define CP_DEF_C(x) { #x , NULL , NULL, NULL } diff --git a/src/common/core.c b/src/common/core.c index 984815bc2..0959e6fc9 100644 --- a/src/common/core.c +++ b/src/common/core.c @@ -284,9 +284,11 @@ void core_defaults(void) { #endif console_defaults(); strlib_defaults(); + malloc_defaults(); #ifndef MINICORE sql_defaults(); timer_defaults(); + db_defaults(); #endif } /*====================================== @@ -305,7 +307,7 @@ int main (int argc, char **argv) { } core_defaults(); - malloc_init();// needed for Show* in display_title() [FlavioJS] + malloclib->init();// needed for Show* in display_title() [FlavioJS] console->display_title(); @@ -320,7 +322,7 @@ int main (int argc, char **argv) { Sql_Init(); rathread_init(); mempool_init(); - db_init(); + DB->init(); signals_init(); #ifdef _WIN32 @@ -354,12 +356,12 @@ int main (int argc, char **argv) { #endif timer_final(); socket_final(); - db_final(); + DB->final(); mempool_final(); rathread_final(); #endif - malloc_final(); + malloclib->final(); return 0; } diff --git a/src/common/db.c b/src/common/db.c index f2e69abbb..561371787 100644 --- a/src/common/db.c +++ b/src/common/db.c @@ -510,7 +510,7 @@ static void db_rebalance_erase(DBNode node, DBNode *root) // put the right of 'node' in 'y' y->right = node->right; node->right->parent = y; - // 'y' is a direct child of 'node' + // 'y' is a direct child of 'node' } else { x_parent = y; } @@ -2349,7 +2349,7 @@ DBHasher db_default_hash(DBType type) DBReleaser db_default_release(DBType type, DBOptions options) { DB_COUNTSTAT(db_default_release); - options = db_fix_options(type, options); + 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)) return &db_release_both; // Release both key and data @@ -2416,7 +2416,7 @@ DBMap* db_alloc(const char *file, const char *func, int line, DBType type, DBOpt #endif /* DB_ENABLE_STATS */ db = ers_alloc(db_alloc_ers, struct DBMap_impl); - options = db_fix_options(type, options); + options = DB->fix_options(type, options); /* Interface of the database */ db->vtable.iterator = db_obj_iterator; db->vtable.exists = db_obj_exists; @@ -2447,9 +2447,9 @@ DBMap* db_alloc(const char *file, const char *func, int line, DBType type, DBOpt /* Other */ snprintf(ers_name, 50, "db_alloc:nodes:%s:%s:%d",func,file,line); db->nodes = ers_new(sizeof(struct dbn),ers_name,ERS_OPT_WAIT|ERS_OPT_FREE_NAME); - db->cmp = db_default_cmp(type); - db->hash = db_default_hash(type); - db->release = db_default_release(type, options); + db->cmp = DB->default_cmp(type); + db->hash = DB->default_hash(type); + db->release = DB->default_release(type, options); for (i = 0; i < HASH_SIZE; i++) db->ht[i] = NULL; db->cache = NULL; @@ -2829,3 +2829,23 @@ void linkdb_final( struct linkdb_node** head ) } *head = NULL; } +void db_defaults(void) { + DB = &DB_s; + DB->alloc = db_alloc; + DB->custom_release = db_custom_release; + DB->data2i = db_data2i; + DB->data2ptr = db_data2ptr; + DB->data2ui = db_data2ui; + DB->default_cmp = db_default_cmp; + DB->default_hash = db_default_hash; + DB->default_release = db_default_release; + DB->final = db_final; + DB->fix_options = db_fix_options; + DB->i2data = db_i2data; + DB->i2key = db_i2key; + DB->init = db_init; + DB->ptr2data = db_ptr2data; + DB->str2key = db_str2key; + DB->ui2data = db_ui2data; + DB->ui2key = db_ui2key; +} diff --git a/src/common/db.h b/src/common/db.h index 45babed78..5a555b2fa 100644 --- a/src/common/db.h +++ b/src/common/db.h @@ -599,73 +599,73 @@ struct DBMap { // 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 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)) ) // 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))) ) +#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))) ) +#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))) ) +#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) ) +#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),NULL) ) -#define idb_iput(db,k,d) ( (db)->put((db),db_i2key(k),db_i2data(d),NULL) ) -#define uidb_iput(db,k,d) ( (db)->put((db),db_ui2key(k),db_i2data(d),NULL) ) -#define strdb_iput(db,k,d) ( (db)->put((db),db_str2key(k),db_i2data(d),NULL) ) +#define db_iput(db,k,d) ( (db)->put((db),(k),DB->i2data(d),NULL) ) +#define idb_iput(db,k,d) ( (db)->put((db),DB->i2key(k),DB->i2data(d),NULL) ) +#define uidb_iput(db,k,d) ( (db)->put((db),DB->ui2key(k),DB->i2data(d),NULL) ) +#define strdb_iput(db,k,d) ( (db)->put((db),DB->str2key(k),DB->i2data(d),NULL) ) // Put uint-type data into DBMaps of various key types -#define db_uiput(db,k,d) ( (db)->put((db),(k),db_ui2data(d),NULL) ) -#define idb_uiput(db,k,d) ( (db)->put((db),db_i2key(k),db_ui2data(d),NULL) ) -#define uidb_uiput(db,k,d) ( (db)->put((db),db_ui2key(k),db_ui2data(d),NULL) ) -#define strdb_uiput(db,k,d) ( (db)->put((db),db_str2key(k),db_ui2data(d),NULL) ) +#define db_uiput(db,k,d) ( (db)->put((db),(k),DB->ui2data(d),NULL) ) +#define idb_uiput(db,k,d) ( (db)->put((db),DB->i2key(k),DB->ui2data(d),NULL) ) +#define uidb_uiput(db,k,d) ( (db)->put((db),DB->ui2key(k),DB->ui2data(d),NULL) ) +#define strdb_uiput(db,k,d) ( (db)->put((db),DB->str2key(k),DB->ui2data(d),NULL) ) // 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) ) +#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_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))) ) +#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__,__func__,__LINE__,DB_INT,(opt),sizeof(int)) -#define uidb_alloc(opt) db_alloc(__FILE__,__func__,__LINE__,DB_UINT,(opt),sizeof(unsigned int)) -#define strdb_alloc(opt,maxlen) db_alloc(__FILE__,__func__,__LINE__,DB_STRING,(opt),(maxlen)) -#define stridb_alloc(opt,maxlen) db_alloc(__FILE__,__func__,__LINE__,DB_ISTRING,(opt),(maxlen)) +#define idb_alloc(opt) DB->alloc(__FILE__,__func__,__LINE__,DB_INT,(opt),sizeof(int)) +#define uidb_alloc(opt) DB->alloc(__FILE__,__func__,__LINE__,DB_UINT,(opt),sizeof(unsigned int)) +#define strdb_alloc(opt,maxlen) DB->alloc(__FILE__,__func__,__LINE__,DB_STRING,(opt),(maxlen)) +#define stridb_alloc(opt,maxlen) DB->alloc(__FILE__,__func__,__LINE__,DB_ISTRING,(opt),(maxlen)) #define db_destroy(db) ( (db)->destroy((db),NULL) ) // Other macros #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) ( 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_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) ) @@ -692,6 +692,7 @@ struct DBMap { * db_final - Finalizes the database system. * \*****************************************************************************/ +struct db_interface { /** * Returns the fixed options according to the database type. * Sets required options and unsets unsupported options. @@ -704,7 +705,7 @@ struct DBMap { * @see #DBOptions * @see #db_default_release(DBType,DBOptions) */ -DBOptions db_fix_options(DBType type, DBOptions options); +DBOptions (*fix_options) (DBType type, DBOptions options); /** * Returns the default comparator for the type of database. @@ -714,7 +715,7 @@ DBOptions db_fix_options(DBType type, DBOptions options); * @see #DBType * @see #DBComparator */ -DBComparator db_default_cmp(DBType type); +DBComparator (*default_cmp) (DBType type); /** * Returns the default hasher for the specified type of database. @@ -724,7 +725,7 @@ DBComparator db_default_cmp(DBType type); * @see #DBType * @see #DBHasher */ -DBHasher db_default_hash(DBType type); +DBHasher (*default_hash) (DBType type); /** * Returns the default releaser for the specified type of database with the @@ -741,7 +742,7 @@ DBHasher db_default_hash(DBType type); * @see #db_fix_options(DBType,DBOptions) * @see #db_custom_release(DBRelease) */ -DBReleaser db_default_release(DBType type, DBOptions options); +DBReleaser (*default_release) (DBType type, DBOptions options); /** * Returns the releaser that behaves as <code>which</code> specifies. @@ -752,7 +753,7 @@ DBReleaser db_default_release(DBType type, DBOptions options); * @see #DBReleaser * @see #db_default_release(DBType,DBOptions) */ -DBReleaser db_custom_release(DBRelease which); +DBReleaser (*custom_release) (DBRelease which); /** * Allocate a new database of the specified type. @@ -775,7 +776,7 @@ DBReleaser db_custom_release(DBRelease which); * @see #db_default_release(DBType,DBOptions) * @see #db_fix_options(DBType,DBOptions) */ -DBMap* db_alloc(const char *file, const char *func, int line, DBType type, DBOptions options, unsigned short maxlen); +DBMap* (*alloc) (const char *file, const char *func, int line, DBType type, DBOptions options, unsigned short maxlen); /** * Manual cast from 'int' to the union DBKey. @@ -783,7 +784,7 @@ DBMap* db_alloc(const char *file, const char *func, int line, DBType type, DBOpt * @return The key as a DBKey union * @public */ -DBKey db_i2key(int key); +DBKey (*i2key) (int key); /** * Manual cast from 'unsigned int' to the union DBKey. @@ -791,7 +792,7 @@ DBKey db_i2key(int key); * @return The key as a DBKey union * @public */ -DBKey db_ui2key(unsigned int key); +DBKey (*ui2key) (unsigned int key); /** * Manual cast from 'unsigned char *' to the union DBKey. @@ -799,7 +800,7 @@ DBKey db_ui2key(unsigned int key); * @return The key as a DBKey union * @public */ -DBKey db_str2key(const char *key); +DBKey (*str2key) (const char *key); /** * Manual cast from 'int' to the struct DBData. @@ -807,7 +808,7 @@ DBKey db_str2key(const char *key); * @return The data as a DBData struct * @public */ -DBData db_i2data(int data); +DBData (*i2data) (int data); /** * Manual cast from 'unsigned int' to the struct DBData. @@ -815,7 +816,7 @@ DBData db_i2data(int data); * @return The data as a DBData struct * @public */ -DBData db_ui2data(unsigned int data); +DBData (*ui2data) (unsigned int data); /** * Manual cast from 'void *' to the struct DBData. @@ -823,7 +824,7 @@ DBData db_ui2data(unsigned int data); * @return The data as a DBData struct * @public */ -DBData db_ptr2data(void *data); +DBData (*ptr2data) (void *data); /** * Gets int type data from struct DBData. @@ -832,7 +833,7 @@ DBData db_ptr2data(void *data); * @return Integer value of the data. * @public */ -int db_data2i(DBData *data); +int (*data2i) (DBData *data); /** * Gets unsigned int type data from struct DBData. @@ -841,7 +842,7 @@ int db_data2i(DBData *data); * @return Unsigned int value of the data. * @public */ -unsigned int db_data2ui(DBData *data); +unsigned int (*data2ui) (DBData *data); /** * Gets void* type data from struct DBData. @@ -850,14 +851,14 @@ unsigned int db_data2ui(DBData *data); * @return Void* value of the data. * @public */ -void* db_data2ptr(DBData *data); +void* (*data2ptr) (DBData *data); /** * Initialize the database system. * @public * @see #db_final(void) */ -void db_init(void); +void (*init) (void); /** * Finalize the database system. @@ -865,8 +866,12 @@ void db_init(void); * @public * @see #db_init(void) */ -void db_final(void); +void (*final) (void); +} DB_s; +struct db_interface *DB; + +void db_defaults(void); // Link DB System - From jAthena struct linkdb_node { struct linkdb_node *next; diff --git a/src/common/malloc.c b/src/common/malloc.c index d8799b2a1..4874aa0f4 100644 --- a/src/common/malloc.c +++ b/src/common/malloc.c @@ -130,21 +130,21 @@ void aFree_(void *p, const char *file, int line, const char *func) /* USE_MEMMGR */ /* - * Memory manager - * able to handle malloc and free efficiently - * Since the complex processing, I might be slightly heavier. - * - * (I'm sorry for the poor description ^ ^;) such as data structures - * Divided into "blocks" of a plurality of memory, "unit" of a plurality of blocks further - * I have to divide. Size of the unit, a plurality of distribution equal to the capacity of one block - * That's what you have. For example, if one unit of 32KB, one block 1 Yu 32Byte - * Knit, or are able to gather 1024, gathered 512 units 64Byte - * I can be or have. (Excluding padding, the unit_head) - * - * Lead-linked list (block_prev, block_next) in each other is the same size block - * Linked list (hash_prev, hash_nect) even among such one in the block with the figure - * I like to have. Thus, reuse of memory no longer needed can be performed efficiently. - */ +* Memory manager +* able to handle malloc and free efficiently +* Since the complex processing, I might be slightly heavier. +* +* (I'm sorry for the poor description ^ ^;) such as data structures +* Divided into "blocks" of a plurality of memory, "unit" of a plurality of blocks further +* I have to divide. Size of the unit, a plurality of distribution equal to the capacity of one block +* That's what you have. For example, if one unit of 32KB, one block 1 Yu 32Byte +* Knit, or are able to gather 1024, gathered 512 units 64Byte +* I can be or have. (Excluding padding, the unit_head) +* +* Lead-linked list (block_prev, block_next) in each other is the same size block +* Linked list (hash_prev, hash_nect) even among such one in the block with the figure +* I like to have. Thus, reuse of memory no longer needed can be performed efficiently. +*/ /* Alignment of the block */ #define BLOCK_ALIGNMENT1 16 @@ -210,7 +210,7 @@ static unsigned short size2hash( size_t size ) return (unsigned short)(size + BLOCK_ALIGNMENT1 - 1) / BLOCK_ALIGNMENT1; } else if( size <= BLOCK_DATA_SIZE ){ return (unsigned short)(size - BLOCK_DATA_SIZE1 + BLOCK_ALIGNMENT2 - 1) / BLOCK_ALIGNMENT2 - + BLOCK_DATA_COUNT1; + + BLOCK_DATA_COUNT1; } else { return 0xffff; // If it exceeds the block length hash I do not } @@ -235,7 +235,7 @@ void* _mmalloc(size_t size, const char *file, int line, const char *func ) ShowError("_mmalloc: %d\n", size); return NULL; } - + if(size == 0) { return NULL; } @@ -332,7 +332,7 @@ void* _mmalloc(size_t size, const char *file, int line, const char *func ) void* _mcalloc(size_t num, size_t size, const char *file, int line, const char *func ) { - void *p = _mmalloc(num * size,file,line,func); + void *p = malloclib->malloc(num * size,file,line,func); memset(p,0,num * size); return p; } @@ -341,7 +341,7 @@ void* _mrealloc(void *memblock, size_t size, const char *file, int line, const c { size_t old_size; if(memblock == NULL) { - return _mmalloc(size,file,line,func); + return malloclib->malloc(size,file,line,func); } old_size = ((struct unit_head *)((char *)memblock - sizeof(struct unit_head) + sizeof(long)))->size; @@ -353,11 +353,11 @@ void* _mrealloc(void *memblock, size_t size, const char *file, int line, const c return memblock; } else { // Size Large - void *p = _mmalloc(size,file,line,func); + void *p = malloclib->malloc(size,file,line,func); if(p != NULL) { memcpy(p,memblock,old_size); } - _mfree(memblock,file,line,func); + malloclib->free(memblock,file,line,func); return p; } } @@ -368,7 +368,7 @@ char* _mstrdup(const char *p, const char *file, int line, const char *func ) return NULL; } else { size_t len = strlen(p); - char *string = (char *)_mmalloc(len + 1,file,line,func); + char *string = (char *)malloclib->malloc(len + 1,file,line,func); memcpy(string,p,len+1); return string; } @@ -544,7 +544,7 @@ static void memmgr_log (char *buf) t = localtime(&raw); fprintf(log_fp, "\nMemory manager: Memory leaks found at %d/%02d/%02d %02dh%02dm%02ds (rev %s).\n", (t->tm_year+1900), (t->tm_mon+1), t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec, - git[0] != HERC_UNKNOWN_VER ? git : svn[0] != HERC_UNKNOWN_VER ? svn : "Unknown"); + git[0] != HERC_UNKNOWN_VER ? git : svn[0] != HERC_UNKNOWN_VER ? svn : "Unknown"); } fprintf(log_fp, "%s", buf); return; @@ -621,7 +621,7 @@ static void memmgr_final (void) memmgr_log (buf); #endif /* LOG_MEMMGR */ // get block pointer and free it [celest] - _mfree(ptr, ALC_MARK); + malloclib->free(ptr, ALC_MARK); } } } @@ -663,9 +663,9 @@ static void memmgr_init (void) /*====================================== - * Initialise - *-------------------------------------- - */ +* Initialise +*-------------------------------------- +*/ /// Tests the memory for errors and memory leaks. @@ -719,3 +719,28 @@ void malloc_init (void) memmgr_init (); #endif } + +void malloc_defaults() +{ + malloclib = &malloclib_s; + malloclib->init = malloc_init; + malloclib->final = malloc_final; + malloclib->memory_check = malloc_memory_check; + malloclib->usage = malloc_usage; + malloclib->verify_ptr = malloc_verify_ptr; + +// Athena's built-in Memory Manager +#ifdef USE_MEMMGR + malloclib->malloc = _mmalloc; + malloclib->calloc = _mcalloc; + malloclib->realloc = _mrealloc; + malloclib->astrdup = _mstrdup; + malloclib->free = _mfree; +#else + malloclib->malloc = aMalloc_; + malloclib->calloc = aCalloc_; + malloclib->realloc = aRealloc_; + malloclib->astrdup = aStrdup_; + malloclib->free = aFree_; +#endif +} diff --git a/src/common/malloc.h b/src/common/malloc.h index 6b4e8e5c4..34a26b56e 100644 --- a/src/common/malloc.h +++ b/src/common/malloc.h @@ -22,57 +22,32 @@ ////////////////////////////////////////////////////////////////////// -// Athena's built-in Memory Manager -#ifdef USE_MEMMGR - // Enable memory manager logging by default #define LOG_MEMMGR // no logging for minicore #if defined(MINICORE) && defined(LOG_MEMMGR) -#undef LOG_MEMMGR + #undef LOG_MEMMGR #endif -# define aMalloc(n) _mmalloc(n,ALC_MARK) -# define aCalloc(m,n) _mcalloc(m,n,ALC_MARK) -# define aRealloc(p,n) _mrealloc(p,n,ALC_MARK) -# define aStrdup(p) _mstrdup(p,ALC_MARK) -# define aFree(p) _mfree(p,ALC_MARK) - - void* _mmalloc (size_t size, const char *file, int line, const char *func); - void* _mcalloc (size_t num, size_t size, const char *file, int line, const char *func); - void* _mrealloc (void *p, size_t size, const char *file, int line, const char *func); - char* _mstrdup (const char *p, const char *file, int line, const char *func); - void _mfree (void *p, const char *file, int line, const char *func); - -#else - -# define aMalloc(n) aMalloc_((n),ALC_MARK) -# define aCalloc(m,n) aCalloc_((m),(n),ALC_MARK) -# define aRealloc(p,n) aRealloc_(p,n,ALC_MARK) -# define aStrdup(p) aStrdup_(p,ALC_MARK) -# define aFree(p) aFree_(p,ALC_MARK) - - void* aMalloc_ (size_t size, const char *file, int line, const char *func); - void* aCalloc_ (size_t num, size_t size, const char *file, int line, const char *func); - void* aRealloc_ (void *p, size_t size, const char *file, int line, const char *func); - char* aStrdup_ (const char *p, const char *file, int line, const char *func); - void aFree_ (void *p, const char *file, int line, const char *func); - -#endif +# define aMalloc(n) malloclib->malloc (n,ALC_MARK) +# define aCalloc(m,n) malloclib->calloc (m,n,ALC_MARK) +# define aRealloc(p,n) malloclib->realloc (p,n,ALC_MARK) +# define aStrdup(p) malloclib->astrdup (p,ALC_MARK) +# define aFree(p) malloclib->free (p,ALC_MARK) /////////////// Buffer Creation ///////////////// // Full credit for this goes to Shinomori [Ajarn] #ifdef __GNUC__ // GCC has variable length arrays - #define CREATE_BUFFER(name, type, size) type name[size] - #define DELETE_BUFFER(name) +#define CREATE_BUFFER(name, type, size) type name[size] +#define DELETE_BUFFER(name) #else // others don't, so we emulate them - #define CREATE_BUFFER(name, type, size) type *name = (type *) aCalloc (size, sizeof(type)) - #define DELETE_BUFFER(name) aFree(name) +#define CREATE_BUFFER(name, type, size) type *name = (type *) aCalloc (size, sizeof(type)) +#define DELETE_BUFFER(name) aFree(name) #endif @@ -83,10 +58,27 @@ //////////////////////////////////////////////// -void malloc_memory_check(void); -bool malloc_verify_ptr(void* ptr); -size_t malloc_usage (void); -void malloc_init (void); -void malloc_final (void); - +//void malloc_memory_check(void); +//bool malloc_verify_ptr(void* ptr); +//size_t malloc_usage (void); +//void malloc_init (void); +//void malloc_final (void); + +void malloc_defaults(void); + +struct malloc_interface { + void* (*malloc )(size_t size, const char *file, int line, const char *func); + void* (*calloc )(size_t num, size_t size, const char *file, int line, const char *func); + void* (*realloc )(void *p, size_t size, const char *file, int line, const char *func); + char* (*astrdup )(const char *p, const char *file, int line, const char *func); + void (*free )(void *p, const char *file, int line, const char *func); + + void (*memory_check)(void); + bool (*verify_ptr)(void* ptr); + size_t (*usage) (void); + void (*init) (void); + void (*final) (void); +} malloclib_s; + +struct malloc_interface *malloclib; #endif /* _MALLOC_H_ */ |