summaryrefslogtreecommitdiff
path: root/src/common
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
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')
-rw-r--r--src/common/graph.c2
-rw-r--r--src/common/grfio.c8
-rw-r--r--src/common/malloc.c86
-rw-r--r--src/common/malloc.h18
-rw-r--r--src/common/mapindex.c3
-rw-r--r--src/common/plugins.c2
-rw-r--r--src/common/socket.c10
-rw-r--r--src/common/socket.h2
-rw-r--r--src/common/strlib.c4
-rw-r--r--src/common/timer.c8
10 files changed, 120 insertions, 23 deletions
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 <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
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 <stdio.h>
#include <stdlib.h>
#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 <netinet/in.h>
#endif
#include <time.h>
-#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 <time.h>
#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;