summaryrefslogtreecommitdiff
path: root/src/common/malloc.c
diff options
context:
space:
mode:
authorLance <Lance@54d463be-8e91-2dee-dedb-b68131a5f0ec>2006-08-27 06:38:17 +0000
committerLance <Lance@54d463be-8e91-2dee-dedb-b68131a5f0ec>2006-08-27 06:38:17 +0000
commit3c8999edce9e1f0d5c0dee3ff8311e781d64c684 (patch)
treed066567a720e5fad02e6f857d55bc165d9a3809a /src/common/malloc.c
parentea320701794515d4ffa6a8e8ff8f3b8fdfe09860 (diff)
downloadhercules-3c8999edce9e1f0d5c0dee3ff8311e781d64c684.tar.gz
hercules-3c8999edce9e1f0d5c0dee3ff8311e781d64c684.tar.bz2
hercules-3c8999edce9e1f0d5c0dee3ff8311e781d64c684.tar.xz
hercules-3c8999edce9e1f0d5c0dee3ff8311e781d64c684.zip
* Optional macro MEMSET_TURBO for faster low-level memory initializations.
git-svn-id: https://rathena.svn.sourceforge.net/svnroot/rathena/trunk@8499 54d463be-8e91-2dee-dedb-b68131a5f0ec
Diffstat (limited to 'src/common/malloc.c')
-rw-r--r--src/common/malloc.c86
1 files changed, 82 insertions, 4 deletions
diff --git a/src/common/malloc.c b/src/common/malloc.c
index 6eaa994d8..700da0f88 100644
--- a/src/common/malloc.c
+++ b/src/common/malloc.c
@@ -4,7 +4,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include "malloc.h"
+#include "../common/malloc.h"
#include "../common/core.h"
#include "../common/showmsg.h"
@@ -116,13 +116,15 @@ void aFree_ (void *p, const char *file, int line, const char *func)
void* _bcallocA(size_t size, size_t cnt)
{
void *ret = MALLOCA(size * cnt);
- if (ret) memset(ret, 0, size * cnt);
+ if (ret) //memset(ret, 0, size * cnt);
+ malloc_set(ret, 0, size*cnt);
return ret;
}
void* _bcalloc(size_t size, size_t cnt)
{
void *ret = MALLOC(size * cnt);
- if (ret) memset(ret, 0, size * cnt);
+ if (ret) //memset(ret, 0, size * cnt);
+ malloc_set(ret, 0, size*cnt);
return ret;
}
char* _bstrdup(const char *chr)
@@ -325,7 +327,8 @@ 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);
- memset(p,0,num * size);
+ //memset(p,0,num * size);
+ malloc_set(p,0,num*size);
return p;
}
@@ -683,6 +686,81 @@ static void memmgr_init (void)
}
#endif
+#ifdef MEMSET_TURBO
+ // This function is practically useless if the the size of the data is
+ // static. It decides whether to use setword or setdword.
+ void malloc_set(void *dest, int value, int size)
+ {
+ if(size%4 == 0)
+ malloc_tsetdword(dest, value, size);
+ else if(size%2 == 0)
+ malloc_tsetword(dest, (short)value, size);
+ else
+ memset(dest, value, (size_t) size);
+ }
+
+ // Sets 32-bit aligned memory.
+ void malloc_tsetdword(void *dest, int value, int count){
+#ifdef _WIN32
+ _asm
+ {
+ mov edx, 0
+ mov eax, count
+ mov ebx, 4
+ idiv ebx
+ mov edi, dest
+ mov ecx, eax
+ mov eax, value
+ rep stosd
+ }
+#else
+ __asm__("movl $0, %%edx;
+ movl %1, %%eax;
+ movl $4, %%ebx;
+ idivl %%ebx;
+ movl %0, %%edi;
+ movl %%eax, %%ecx;
+ movl %2, %%eax;
+ rep;
+ stosd;"
+ :
+ : "g" (dest), "g" (count), "g" (value)
+ : "edx", "eax", "ebx", "edi", "ecx"
+ );
+#endif
+ }
+
+ // Sets 16-bit aligned memory.
+ void malloc_tsetword(void *dest, short value, int count){
+#ifdef _WIN32
+ _asm
+ {
+ mov edx, 0
+ mov eax, count
+ mov ebx, 2
+ idiv ebx
+ mov edi, dest
+ mov ecx, eax
+ mov ax, value
+ rep stosw
+ }
+#else
+ __asm__("movl $0, %%edx;
+ movl %1, %%eax;
+ movl $2, %%ebx;
+ idivl %%ebx;
+ movl %0, %%edi;
+ movl %%eax, %%ecx;
+ movw %2, %%ax;
+ rep;
+ stosw;"
+ :
+ : "g" (dest), "g" (count), "g" (value)
+ : "edx", "eax", "ebx", "edi", "ecx", "ax"
+ );
+#endif
+ }
+#endif
/*======================================
* Initialise