diff options
Diffstat (limited to 'src/common/malloc.c')
-rw-r--r-- | src/common/malloc.c | 86 |
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
|