summaryrefslogtreecommitdiff
path: root/src/common/malloc.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/common/malloc.c')
-rw-r--r--src/common/malloc.c79
1 files changed, 52 insertions, 27 deletions
diff --git a/src/common/malloc.c b/src/common/malloc.c
index d8799b2a1..77eef2508 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->strdup = _mstrdup;
+ malloclib->free = _mfree;
+#else
+ malloclib->malloc = aMalloc_;
+ malloclib->calloc = aCalloc_;
+ malloclib->realloc = aRealloc_;
+ malloclib->strdup = aStrdup_;
+ malloclib->free = aFree_;
+#endif
+}