From 434c36b4267b2665780f1afbee2b7d7118bc3c01 Mon Sep 17 00:00:00 2001 From: FlavioJS Date: Tue, 28 Nov 2006 13:31:22 +0000 Subject: - Changed write to send as suggested by TheUltraMage in: http://www.eathena.ws/board/index.php?showtopic=105417 Hopefully that will take care of the SIGPIPE problem in Debian and cygwin. - Server name in core.c skipping '\\' characters. - Memory allocation functions using file,line,func from the invoking functions. - Other minor changes in malloc git-svn-id: https://rathena.svn.sourceforge.net/svnroot/rathena/trunk@9344 54d463be-8e91-2dee-dedb-b68131a5f0ec --- src/common/core.c | 2 +- src/common/malloc.c | 100 ++++++++++++++++------------------------------- src/common/malloc.h | 109 ++++++++++++++++++++++++++++------------------------ src/common/socket.c | 2 +- 4 files changed, 94 insertions(+), 119 deletions(-) (limited to 'src') diff --git a/src/common/core.c b/src/common/core.c index 3264ac1cc..1f361bd95 100644 --- a/src/common/core.c +++ b/src/common/core.c @@ -228,7 +228,7 @@ int main (int argc, char **argv) // initialise program arguments { char *p = SERVER_NAME = argv[0]; - while ((p = strchr(p, '/')) != NULL) + while ((p = strchr(p, '/')) != NULL || (p = strchr(p, '\\')) != NULL) SERVER_NAME = ++p; arg_c = argc; arg_v = argv; diff --git a/src/common/malloc.c b/src/common/malloc.c index f06fe00ca..1a4729fa2 100644 --- a/src/common/malloc.c +++ b/src/common/malloc.c @@ -12,101 +12,73 @@ #undef LOG_MEMMGR #endif -void* aMalloc_ (size_t size, const char *file, int line, const char *func) +void* aMalloc_(size_t size, const char *file, int line, const char *func) { -#ifndef MEMWATCH - void *ret = MALLOC(size); -#else - void *ret = mwMalloc(size, file, line); -#endif - // ShowMessage("%s:%d: in func %s: malloc %d\n",file,line,func,size); + void *ret = MALLOC(size, file, line, func); + // ShowMessage("%s:%d: in func %s: aMalloc %d\n",file,line,func,size); if (ret == NULL){ - ShowFatalError("%s:%d: in func %s: malloc error out of memory!\n",file,line,func); + ShowFatalError("%s:%d: in func %s: aMalloc error out of memory!\n",file,line,func); exit(1); } return ret; } -void* aMallocA_ (size_t size, const char *file, int line, const char *func) +void* aMallocA_(size_t size, const char *file, int line, const char *func) { -#ifndef MEMWATCH - void *ret = MALLOCA(size); -#else - void *ret = mwMalloc(size, file, line); -#endif - // ShowMessage("%s:%d: in func %s: malloc %d\n",file,line,func,size); + void *ret = MALLOCA(size, file, line, func); + // ShowMessage("%s:%d: in func %s: aMallocA %d\n",file,line,func,size); if (ret == NULL){ - ShowFatalError("%s:%d: in func %s: malloc error out of memory!\n",file,line,func); + ShowFatalError("%s:%d: in func %s: aMallocA error out of memory!\n",file,line,func); exit(1); } return ret; } -void* aCalloc_ (size_t num, size_t size, const char *file, int line, const char *func) +void* aCalloc_(size_t num, size_t size, const char *file, int line, const char *func) { -#ifndef MEMWATCH - void *ret = CALLOC(num, size); -#else - void *ret = mwCalloc(num, size, file, line); -#endif - // ShowMessage("%s:%d: in func %s: calloc %d %d\n",file,line,func,num,size); + void *ret = CALLOC(num, size, file, line, func); + // ShowMessage("%s:%d: in func %s: aCalloc %d %d\n",file,line,func,num,size); if (ret == NULL){ - ShowFatalError("%s:%d: in func %s: calloc error out of memory!\n", file, line, func); + ShowFatalError("%s:%d: in func %s: aCalloc error out of memory!\n", file, line, func); exit(1); } return ret; } -void* aCallocA_ (size_t num, size_t size, const char *file, int line, const char *func) +void* aCallocA_(size_t num, size_t size, const char *file, int line, const char *func) { -#ifndef MEMWATCH - void *ret = CALLOCA(num, size); -#else - void *ret = mwCalloc(num, size, file, line); -#endif - // ShowMessage("%s:%d: in func %s: calloc %d %d\n",file,line,func,num,size); + void *ret = CALLOCA(num, size, file, line, func); + // ShowMessage("%s:%d: in func %s: aCallocA %d %d\n",file,line,func,num,size); if (ret == NULL){ - ShowFatalError("%s:%d: in func %s: calloc error out of memory!\n",file,line,func); + ShowFatalError("%s:%d: in func %s: aCallocA error out of memory!\n",file,line,func); exit(1); } return ret; } -void* aRealloc_ (void *p, size_t size, const char *file, int line, const char *func) +void* aRealloc_(void *p, size_t size, const char *file, int line, const char *func) { -#ifndef MEMWATCH - void *ret = REALLOC(p, size); -#else - void *ret = mwRealloc(p, size, file, line); -#endif - // ShowMessage("%s:%d: in func %s: realloc %p %d\n",file,line,func,p,size); + void *ret = REALLOC(p, size, file, line, func); + // ShowMessage("%s:%d: in func %s: aRealloc %p %d\n",file,line,func,p,size); if (ret == NULL){ - ShowFatalError("%s:%d: in func %s: realloc error out of memory!\n",file,line,func); + ShowFatalError("%s:%d: in func %s: aRealloc error out of memory!\n",file,line,func); exit(1); } return ret; } -char* aStrdup_ (const char *p, const char *file, int line, const char *func) +char* aStrdup_(const char *p, const char *file, int line, const char *func) { -#ifndef MEMWATCH - char *ret = STRDUP(p); -#else - char *ret = mwStrdup(p, file, line); -#endif - // ShowMessage("%s:%d: in func %s: strdup %p\n",file,line,func,p); + char *ret = STRDUP(p, file, line, func); + // ShowMessage("%s:%d: in func %s: aStrdup %p\n",file,line,func,p); if (ret == NULL){ - ShowFatalError("%s:%d: in func %s: strdup error out of memory!\n", file, line, func); + ShowFatalError("%s:%d: in func %s: aStrdup error out of memory!\n", file, line, func); exit(1); } return ret; } -void aFree_ (void *p, const char *file, int line, const char *func) +void aFree_(void *p, const char *file, int line, const char *func) { - // ShowMessage("%s:%d: in func %s: free %p\n",file,line,func,p); + // ShowMessage("%s:%d: in func %s: aFree %p\n",file,line,func,p); if (p) - #ifndef MEMWATCH - FREE(p); - #else - mwFree(p, file, line); - #endif + FREE(p, file, line, func); p = NULL; } @@ -238,11 +210,7 @@ void* _mmalloc(size_t size, const char *file, int line, const char *func ) { /* ブロック長を超える領域の確保には、malloc() を用いる */ /* その際、unit_head.block に NULL を代入して区別する */ if(size_hash * BLOCK_ALIGNMENT > BLOCK_DATA_SIZE - sizeof(struct unit_head)) { -#ifdef MEMWATCH - struct unit_head_large* p = (struct unit_head_large*)mwMalloc(sizeof(struct unit_head_large) + size,file,line); -#else - struct unit_head_large* p = (struct unit_head_large*) MALLOC (sizeof(struct unit_head_large) + size); -#endif + struct unit_head_large* p = (struct unit_head_large*)MALLOC(sizeof(struct unit_head_large)+size,file,line,func); if(p != NULL) { p->unit_head.block = NULL; p->unit_head.size = size; @@ -394,7 +362,7 @@ void _mfree(void *ptr, const char *file, int line, const char *func ) { } head->block = NULL; memmgr_usage_bytes -= head->size; - FREE (head_large); + FREE(head_large,file,line,func); } else { ShowError("Memory manager: args of aFree is freed pointer %s:%d@%s\n", file, line, func); } @@ -510,14 +478,14 @@ static struct block* block_malloc(void) { int block_no; struct block* p; struct chunk* chunk; - char *pb = (char *) CALLOC (sizeof(struct block),BLOCK_ALLOC + 1); + char *pb = (char *)CALLOC(sizeof(struct block),BLOCK_ALLOC+1,file,line,func); if(pb == NULL) { ShowFatalError("Memory manager::block_alloc failed.\n"); exit(1); } // store original block address in chunk - chunk = (struct chunk *) MALLOC (sizeof(struct chunk)); + chunk = (struct chunk *)MALLOC(sizeof(struct chunk),file,line,func); if (chunk == NULL) { ShowFatalError("Memory manager::block_alloc failed.\n"); exit(1); @@ -649,8 +617,8 @@ static void memmgr_final (void) chunk = chunk_first; while (chunk) { chunk2 = chunk->next; - FREE(chunk->block); - FREE(chunk); + FREE(chunk->block,file,line,func); + FREE(chunk,file,line,func); chunk = chunk2; } @@ -662,7 +630,7 @@ static void memmgr_final (void) large->unit_head.file, large->unit_head.line, large->unit_head.size); memmgr_log (buf); #endif - FREE (large); + FREE(large,file,line,func); large = large2; } #ifdef LOG_MEMMGR diff --git a/src/common/malloc.h b/src/common/malloc.h index d75ff548c..f65593eb1 100644 --- a/src/common/malloc.h +++ b/src/common/malloc.h @@ -3,6 +3,12 @@ #ifndef _MALLOC_H_ #define _MALLOC_H_ +// Q: What are the 'a'-variant allocation functions? +// A: They allocate memory from the stack, which is automatically +// freed when the invoking function returns. +// But it's not portable (http://c-faq.com/malloc/alloca.html) +// and I have doubts our implementation works. +// -> They should NOT be used, period. #define MEMSET_TURBO @@ -36,29 +42,29 @@ # define aStrdup(p) _mstrdup(p,ALC_MARK) # define aFree(p) _mfree(p,ALC_MARK) - void* _mmalloc (size_t, const char *, int, const char *); - void* _mcalloc (size_t, size_t, const char *, int, const char *); - void* _mrealloc (void *, size_t, const char *, int, const char *); - char* _mstrdup (const char *, const char *, int, const char *); - void _mfree (void *, const char *, int, const char *); + 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* _mrealloc (void *p, size_t size, const char *file, int line, const char *func); + char* _mstrdup (const char *p, const char *file, int line, const char *func); + void _mfree (void *p, const char *file, int line, const char *func); #else -# define aMalloc(n) aMalloc_(n,ALC_MARK) -# define aMallocA(n) aMallocA_(n,ALC_MARK) -# define aCalloc(m,n) aCalloc_(m,n,ALC_MARK) +# define aMalloc(n) aMalloc_((n),ALC_MARK) +# define aMallocA(n) aMallocA_((n),ALC_MARK) +# define aCalloc(m,n) aCalloc_((m),(n),ALC_MARK) # define aCallocA(m,n) aCallocA_(m,n,ALC_MARK) # define aRealloc(p,n) aRealloc_(p,n,ALC_MARK) # define aStrdup(p) aStrdup_(p,ALC_MARK) # define aFree(p) aFree_(p,ALC_MARK) - void* aMalloc_ (size_t, const char *, int, const char *); - void* aMallocA_ (size_t, const char *, int, const char *); - void* aCalloc_ (size_t, size_t, const char *, int, const char *); - void* aCallocA_ (size_t, size_t, const char *, int, const char *); - void* aRealloc_ (void *, size_t, const char *, int, const char *); - char* aStrdup_ (const char *, const char *, int, const char *); - void aFree_ (void *, const char *, int, const char *); + void* aMalloc_ (size_t size, const char *file, int line, const char *func); + void* aMallocA_ (size_t size, const char *file, int line, const char *func); + void* aCalloc_ (size_t num, size_t size, const char *file, int line, const char *func); + void* aCallocA_ (size_t num, size_t size, const char *file, int line, const char *func); + void* aRealloc_ (void *p, size_t size, const char *file, int line, const char *func); + char* aStrdup_ (const char *p, const char *file, int line, const char *func); + void aFree_ (void *p, const char *file, int line, const char *func); #endif @@ -67,59 +73,60 @@ #ifdef MEMWATCH # include "memwatch.h" -# define MALLOC(n) mwMalloc(n,__FILE__, __LINE__) -# define MALLOCA(n) mwMalloc(n,__FILE__, __LINE__) -# define CALLOC(m,n) mwCalloc(m,n,__FILE__, __LINE__) -# define CALLOCA(m,n) mwCalloc(m,n,__FILE__, __LINE__) -# define REALLOC(p,n) mwRealloc(p,n,__FILE__, __LINE__) -# define STRDUP(p) mwStrdup(p,__FILE__, __LINE__) -# define FREE(p) mwFree(p,__FILE__, __LINE__) +# define MALLOC(n,file,line,func) mwMalloc((n),(file),(line)) +# define MALLOCA(n,file,line,func) mwMalloc((n),(file),(line)) +# define CALLOC(m,n,file,line,func) mwCalloc((m),(n),(file),(line)) +# define CALLOCA(m,n,file,line,func) mwCalloc((m),(n),(file),(line)) +# define REALLOC(p,n,file,line,func) mwRealloc((p),(n),(file),(line)) +# define STRDUP(p,file,line,func) mwStrdup((p),(file),(line)) +# define FREE(p,file,line,func) mwFree((p),(file),(line)) #elif defined(DMALLOC) # include "dmalloc.h" -# define MALLOC(n) dmalloc_malloc(__FILE__, __LINE__, (n), DMALLOC_FUNC_MALLOC, 0, 0) -# define MALLOCA(n) dmalloc_malloc(__FILE__, __LINE__, (n), DMALLOC_FUNC_MALLOC, 0, 0) -# define CALLOC(m,n) dmalloc_malloc(__FILE__, __LINE__, (m)*(n), DMALLOC_FUNC_CALLOC, 0, 0) -# define CALLOCA(m,n) dmalloc_malloc(__FILE__, __LINE__, (m)*(n), DMALLOC_FUNC_CALLOC, 0, 0) -# define REALLOC(p,n) dmalloc_realloc(__FILE__, __LINE__, (p), (n), DMALLOC_FUNC_REALLOC, 0) -# define STRDUP(p) strdup(p) -# define FREE(p) free(p) +# define MALLOC(n,file,line,func) dmalloc_malloc((file),(line),(n),DMALLOC_FUNC_MALLOC,0,0) +# define MALLOCA(n,file,line,func) dmalloc_malloc((file),(line),(n),DMALLOC_FUNC_MALLOC,0,0) +# define CALLOC(m,n,file,line,func) dmalloc_malloc((file),(line),(m)*(n),DMALLOC_FUNC_CALLOC,0,0) +# define CALLOCA(m,n,file,line,func) dmalloc_malloc((file),(line),(m)*(n),DMALLOC_FUNC_CALLOC,0,0) +# define REALLOC(p,n,file,line,func) dmalloc_realloc((file),(line),(p),(n),DMALLOC_FUNC_REALLOC,0) +# define STRDUP(p,file,line,func) strdup(p) +# define FREE(p,file,line,func) free(p) #elif defined(GCOLLECT) # include "gc.h" -# define MALLOC(n) GC_MALLOC(n) -# define MALLOCA(n) GC_MALLOC_ATOMIC(n) -# define CALLOC(m,n) _bcalloc(m,n) -# define CALLOCA(m,n) _bcallocA(m,n) -# define REALLOC(p,n) GC_REALLOC(p,n) -# define STRDUP(p) _bstrdup(p) -# define FREE(p) GC_FREE(p) +# define MALLOC(n,file,line,func) GC_MALLOC(n) +# define MALLOCA(n,file,line,func) GC_MALLOC_ATOMIC(n) +# define CALLOC(m,n,file,line,func) _bcalloc((m),(n)) +# define CALLOCA(m,n,file,line,func) _bcallocA((m),(n)) +# define REALLOC(p,n,file,line,func) GC_REALLOC((p),(n)) +# define STRDUP(p,file,line,func) _bstrdup(p) +# define FREE(p,file,line,func) GC_FREE(p) void * _bcalloc(size_t, size_t); void * _bcallocA(size_t, size_t); char * _bstrdup(const char *); +/* FIXME Why is this the same as #else? [FlavioJS] #elif defined(BCHECK) -# define MALLOC(n) malloc(n) -# define MALLOCA(n) malloc(n) -# define CALLOC(m,n) calloc(m,n) -# define CALLOCA(m,n) calloc(m,n) -# define REALLOC(p,n) realloc(p,n) -# define STRDUP(p) strdup(p) -# define FREE(p) free(p) - +# define MALLOC(n,file,line,func) malloc(n) +# define MALLOCA(n,file,line,func) malloc(n) +# define CALLOC(m,n,file,line,func) calloc((m),(n)) +# define CALLOCA(m,n,file,line,func) calloc((m),(n)) +# define REALLOC(p,n,file,line,func) realloc((p),(n)) +# define STRDUP(p,file,line,func) strdup(p) +# define FREE(p,file,line,func) free(p) +*/ #else -# define MALLOC(n) malloc(n) -# define MALLOCA(n) malloc(n) -# define CALLOC(m,n) calloc(m,n) -# define CALLOCA(m,n) calloc(m,n) -# define REALLOC(p,n) realloc(p,n) -# define STRDUP(p) strdup(p) -# define FREE(p) free(p) +# define MALLOC(n,file,line,func) malloc(n) +# define MALLOCA(n,file,line,func) malloc(n) +# define CALLOC(m,n,file,line,func) calloc((m),(n)) +# define CALLOCA(m,n,file,line,func) calloc((m),(n)) +# define REALLOC(p,n,file,line,func) realloc((p),(n)) +# define STRDUP(p,file,line,func) strdup(p) +# define FREE(p,file,line,func) free(p) #endif diff --git a/src/common/socket.c b/src/common/socket.c index 1273677b6..f9262f424 100644 --- a/src/common/socket.c +++ b/src/common/socket.c @@ -253,7 +253,7 @@ static int send_from_fifo(int fd) return 0; } #else - len=write(fd,session[fd]->wdata,session[fd]->wdata_size); + len=send(fd, session[fd]->wdata, session[fd]->wdata_size, MSG_NOSIGNAL); if (len == -1) { if (errno == ECONNABORTED) -- cgit v1.2.3-70-g09d2