From 3c8999edce9e1f0d5c0dee3ff8311e781d64c684 Mon Sep 17 00:00:00 2001 From: Lance Date: Sun, 27 Aug 2006 06:38:17 +0000 Subject: * 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 --- src/common/graph.c | 2 +- src/common/grfio.c | 8 ++--- src/common/malloc.c | 86 ++++++++++++++++++++++++++++++++++++++++++++++++--- src/common/malloc.h | 18 +++++++++++ src/common/mapindex.c | 3 +- src/common/plugins.c | 2 +- src/common/socket.c | 10 +++--- src/common/socket.h | 2 +- src/common/strlib.c | 4 +-- src/common/timer.c | 8 ++--- 10 files changed, 120 insertions(+), 23 deletions(-) (limited to 'src/common') diff --git a/src/common/graph.c b/src/common/graph.c index 0a49fa709..4caa830e6 100644 --- a/src/common/graph.c +++ b/src/common/graph.c @@ -71,7 +71,7 @@ void graph_pallet(struct graph* g, int index,unsigned long c) { if(g == NULL || c >= 256) return; if(g->pallet_count <= index) { - memset(g->png_data + 0x29 + 3 * g->pallet_count,0,(index - g->pallet_count) * 3); + malloc_set(g->png_data + 0x29 + 3 * g->pallet_count,0,(index - g->pallet_count) * 3); g->pallet_count = index + 1; } g->png_data[0x29 + index * 3 ] = (unsigned char)((c >> 16) & 0xFF); // R diff --git a/src/common/grfio.c b/src/common/grfio.c index aa677a0e5..6cfc7eeaa 100644 --- a/src/common/grfio.c +++ b/src/common/grfio.c @@ -169,7 +169,7 @@ static void BitConvert(BYTE *Src,char *BitSwapTable) int lop,prm; BYTE tmp[8]; // *(DWORD*)tmp=*(DWORD*)(tmp+4)=0; - memset(tmp,0,8); + malloc_tsetdword(tmp,0,8); for(lop=0;lop!=64;lop++) { prm = BitSwapTable[lop]-1; if (Src[(prm >> 3) & 7] & BitMaskTable[prm & 7]) { @@ -299,7 +299,7 @@ int decode_zip(unsigned char *dest, unsigned long* destLen, const unsigned char* int encode_zip(unsigned char *dest, unsigned long* destLen, const unsigned char* source, unsigned long sourceLen) { z_stream stream; int err; - memset(&stream, 0, sizeof(stream)); + malloc_tsetdword(&stream, 0, sizeof(stream)); stream.next_in = (Bytef*)source; stream.avail_in = (uInt)sourceLen; /* Check for source > 64K on 16-bit machine: */ @@ -479,7 +479,7 @@ static FILELIST* filelist_add(FILELIST *entry) if (filelist_entrys >= filelist_maxentry) { filelist = (FILELIST *)aRealloc(filelist, (filelist_maxentry + FILELIST_ADDS) * sizeof(FILELIST)); - memset(filelist + filelist_maxentry, '\0', FILELIST_ADDS * sizeof(FILELIST)); + malloc_tsetdword(filelist + filelist_maxentry, '\0', FILELIST_ADDS * sizeof(FILELIST)); filelist_maxentry += FILELIST_ADDS; } @@ -949,7 +949,7 @@ char *grfio_alloc_ptr(char *fname) if (gentry_entrys >= gentry_maxentry) { gentry_maxentry += GENTRY_ADDS; gentry_table = (char**)aRealloc(gentry_table, gentry_maxentry * sizeof(char*)); - memset(gentry_table + (gentry_maxentry - GENTRY_ADDS), 0, sizeof(char*) * GENTRY_ADDS); + malloc_tsetdword(gentry_table + (gentry_maxentry - GENTRY_ADDS), 0, sizeof(char*) * GENTRY_ADDS); } len = strlen( fname ); buf = (char*)aMallocA(len + 1); 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 #include #include -#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 diff --git a/src/common/malloc.h b/src/common/malloc.h index e9dbb9d44..d3df667a6 100644 --- a/src/common/malloc.h +++ b/src/common/malloc.h @@ -4,6 +4,8 @@ #ifndef _MALLOC_H_ #define _MALLOC_H_ +//#define MEMSET_TURBO + #ifndef __NETBSD__ #if __STDC_VERSION__ < 199901L # if __GNUC__ >= 2 @@ -147,6 +149,22 @@ //////////////////////////////////////////////// unsigned int malloc_usage (void); +#ifndef INLINE + #ifdef _WIN32 + #define INLINE + #else + #define INLINE inline + #endif +#endif +#ifdef MEMSET_TURBO + INLINE void malloc_tsetdword(void *dest, int value, int count); + INLINE void malloc_tsetword(void *dest, short value, int count); + INLINE void malloc_set(void *dest, int value, int size); +#else + #define malloc_tsetdword(x,y,z) memset(x,y,z) + #define malloc_tsetword(x,y,z) memset(x,y,z) + #define malloc_set(x,y,z) memset(x,y,z) +#endif void malloc_init (void); void malloc_final (void); diff --git a/src/common/mapindex.c b/src/common/mapindex.c index 8fbf2da12..0c3faec75 100644 --- a/src/common/mapindex.c +++ b/src/common/mapindex.c @@ -3,6 +3,7 @@ #include #include #include "showmsg.h" +#include "../common/malloc.h" #define MAX_MAPINDEX 2000 @@ -70,7 +71,7 @@ void mapindex_init(void) { int index, length; char map_name[1024]; - memset (&indexes, 0, sizeof (indexes)); + malloc_tsetdword (&indexes, 0, sizeof (indexes)); fp=fopen(mapindex_cfgfile,"r"); if(fp==NULL){ ShowFatalError("Unable to read mapindex config file %s!\n", mapindex_cfgfile); diff --git a/src/common/plugins.c b/src/common/plugins.c index fbadca065..a057190b9 100644 --- a/src/common/plugins.c +++ b/src/common/plugins.c @@ -133,7 +133,7 @@ int export_symbol (void *var, int offset) plugin_call_table = (void**)aRealloc(plugin_call_table, max_call_table*sizeof(void*)); // clear the new alloced block - memset(plugin_call_table + call_table_size, 0, (max_call_table-call_table_size)*sizeof(void*)); + malloc_tsetdword(plugin_call_table + call_table_size, 0, (max_call_table-call_table_size)*sizeof(void*)); } // the new table size is delimited by the new element at the end diff --git a/src/common/socket.c b/src/common/socket.c index dc8162ee1..3d7684972 100644 --- a/src/common/socket.c +++ b/src/common/socket.c @@ -424,7 +424,7 @@ int make_listen_port(int port) CREATE(session[fd], struct socket_data, 1); - memset(session[fd],0,sizeof(*session[fd])); + malloc_set(session[fd],0,sizeof(*session[fd])); session[fd]->func_recv = connect_client; return fd; @@ -502,7 +502,7 @@ int make_listen_bind(long ip,int port) CREATE(session[fd], struct socket_data, 1); - memset(session[fd],0,sizeof(*session[fd])); + malloc_set(session[fd],0,sizeof(*session[fd])); session[fd]->func_recv = connect_client; ShowStatus("Open listen port on %d.%d.%d.%d:%i\n", @@ -517,7 +517,7 @@ int console_recieve(int i) { char *buf; CREATE_A(buf, char, 64); - memset(buf,0,sizeof(64)); + malloc_tsetdword(buf,0,sizeof(64)); n = read(0, buf , 64); if ( n < 0 ) @@ -580,7 +580,7 @@ int start_console(void) { if (!session[0]) { // dummy socket already uses fd 0 CREATE(session[0], struct socket_data, 1); } - memset(session[0],0,sizeof(*session[0])); + malloc_set(session[0],0,sizeof(*session[0])); session[0]->func_recv = console_recieve; session[0]->func_console = default_console_parse; @@ -1369,7 +1369,7 @@ void socket_init (void) session[0]->max_rdata = (int)2*rfifo_size; session[0]->max_wdata = (int)2*wfifo_size; - memset (func_parse_table, 0, sizeof(func_parse_table)); + malloc_set (func_parse_table, 0, sizeof(func_parse_table)); func_parse_table[SESSION_RAW].check = default_func_check; func_parse_table[SESSION_RAW].func = default_func_parse; diff --git a/src/common/socket.h b/src/common/socket.h index 158759f9e..ca4b20c91 100644 --- a/src/common/socket.h +++ b/src/common/socket.h @@ -16,7 +16,7 @@ typedef long in_addr_t; #include #endif #include -#include "malloc.h" +#include "../common/malloc.h" #include "cbasetypes.h" extern time_t last_tick; diff --git a/src/common/strlib.c b/src/common/strlib.c index d62bf4f46..8cff2a878 100644 --- a/src/common/strlib.c +++ b/src/common/strlib.c @@ -8,7 +8,7 @@ #include "strlib.h" #include "utils.h" -#include "malloc.h" +#include "../common/malloc.h" //----------------------------------------------- // string lib. @@ -123,7 +123,7 @@ char *trim(char *str, const char *delim) char *strp = strtok(str,delim); char buf[1024]; char *bufp = buf; - memset(buf,0,sizeof buf); + malloc_tsetdword(buf,0,sizeof buf); while(strp) { strcpy(bufp, strp); diff --git a/src/common/timer.c b/src/common/timer.c index 9baa33e08..1e9ff25aa 100644 --- a/src/common/timer.c +++ b/src/common/timer.c @@ -20,7 +20,7 @@ #include #include "timer.h" -#include "malloc.h" +#include "../common/malloc.h" #include "showmsg.h" // タイマー間隔の最小値。モンスターの大量召還時、多数のクライアント接続時に @@ -137,7 +137,7 @@ static void push_timer_heap(int index) } else { timer_heap_max += 256; timer_heap = (int *) aRealloc( timer_heap, sizeof(int) * timer_heap_max); - memset(timer_heap + (timer_heap_max - 256), 0, sizeof(int) * 256); + malloc_tsetdword(timer_heap + (timer_heap_max - 256), 0, sizeof(int) * 256); } } @@ -210,7 +210,7 @@ int acquire_timer (void) } else { timer_data_max += 256; timer_data = (struct TimerData *) aRealloc( timer_data, sizeof(struct TimerData) * timer_data_max); - memset(timer_data + (timer_data_max - 256), 0, sizeof(struct TimerData) * 256); + malloc_tsetdword(timer_data + (timer_data_max - 256), 0, sizeof(struct TimerData) * 256); } } @@ -383,7 +383,7 @@ int do_timer(unsigned int tick) if (free_timer_list_pos >= free_timer_list_max) { free_timer_list_max += 256; free_timer_list = (int *) aRealloc(free_timer_list, sizeof(int) * free_timer_list_max); - memset(free_timer_list + (free_timer_list_max - 256), 0, 256 * sizeof(int)); + malloc_tsetdword(free_timer_list + (free_timer_list_max - 256), 0, 256 * sizeof(int)); } free_timer_list[free_timer_list_pos++] = i; break; -- cgit v1.2.3-70-g09d2