Browse Source

- 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://svn.code.sf.net/p/rathena/svn/trunk@9344 54d463be-8e91-2dee-dedb-b68131a5f0ec

FlavioJS 18 years ago
parent
commit
1e8a3bcd3f
5 changed files with 101 additions and 119 deletions
  1. 7 0
      Changelog-Trunk.txt
  2. 1 1
      src/common/core.c
  3. 34 66
      src/common/malloc.c
  4. 58 51
      src/common/malloc.h
  5. 1 1
      src/common/socket.c

+ 7 - 0
Changelog-Trunk.txt

@@ -3,6 +3,13 @@ Date	Added
 AS OF SVN REV. 5091, WE ARE NOW USING TRUNK.  ALL UNTESTED BUGFIXES/FEATURES GO INTO TRUNK.
 AS OF SVN REV. 5091, WE ARE NOW USING TRUNK.  ALL UNTESTED BUGFIXES/FEATURES GO INTO TRUNK.
 IF YOU HAVE A WORKING AND TESTED BUGFIX PUT IT INTO STABLE AS WELL AS TRUNK.
 IF YOU HAVE A WORKING AND TESTED BUGFIX PUT IT INTO STABLE AS WELL AS TRUNK.
 
 
+2006/11/28
+	* 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. [FlavioJS]
+	* Server name in core.c skipping '\\' characters. [FlavioJS]
+	* Memory allocation functions using file,line,func from the invoking functions. [FlavioJS]
+	* Other minor malloc changes. [FlavioJS]
 2006/11/27
 2006/11/27
 	* Now when a player's "attack once" request fails due to range, the client
 	* Now when a player's "attack once" request fails due to range, the client
 	  is told to move to the target to attack it. This sort of fixes the problem
 	  is told to move to the target to attack it. This sort of fixes the problem

+ 1 - 1
src/common/core.c

@@ -228,7 +228,7 @@ int main (int argc, char **argv)
 	// initialise program arguments
 	// initialise program arguments
 	{
 	{
 		char *p = SERVER_NAME = argv[0];
 		char *p = SERVER_NAME = argv[0];
-		while ((p = strchr(p, '/')) != NULL)
+		while ((p = strchr(p, '/')) != NULL || (p = strchr(p, '\\')) != NULL)
 			SERVER_NAME = ++p;
 			SERVER_NAME = ++p;
 		arg_c = argc;
 		arg_c = argc;
 		arg_v = argv;
 		arg_v = argv;

+ 34 - 66
src/common/malloc.c

@@ -12,101 +12,73 @@
 	#undef LOG_MEMMGR
 	#undef LOG_MEMMGR
 #endif
 #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){
 	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);
 		exit(1);
 	}
 	}
 
 
 	return ret;
 	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){
 	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);
 		exit(1);
 	}
 	}
 
 
 	return ret;
 	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){
 	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);
 		exit(1);
 	}
 	}
 	return ret;
 	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){
 	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);
 		exit(1);
 	}
 	}
 	return ret;
 	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){
 	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);
 		exit(1);
 	}
 	}
 	return ret;
 	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){
 	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);
 		exit(1);
 	}
 	}
 	return ret;
 	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)
 	if (p)
-	#ifndef MEMWATCH
-		FREE(p);
-	#else
-		mwFree(p, file, line);
-	#endif
+		FREE(p, file, line, func);
 
 
 	p = NULL;
 	p = NULL;
 }
 }
@@ -238,11 +210,7 @@ void* _mmalloc(size_t size, const char *file, int line, const char *func ) {
 	/* ブロック長を超える領域の確保には、malloc() を用いる */
 	/* ブロック長を超える領域の確保には、malloc() を用いる */
 	/* その際、unit_head.block に NULL を代入して区別する */
 	/* その際、unit_head.block に NULL を代入して区別する */
 	if(size_hash * BLOCK_ALIGNMENT > BLOCK_DATA_SIZE - sizeof(struct unit_head)) {
 	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) {
 		if(p != NULL) {
 			p->unit_head.block = NULL;
 			p->unit_head.block = NULL;
 			p->unit_head.size  = size;
 			p->unit_head.size  = size;
@@ -394,7 +362,7 @@ void _mfree(void *ptr, const char *file, int line, const char *func ) {
 			}
 			}
 			head->block = NULL;
 			head->block = NULL;
 			memmgr_usage_bytes -= head->size;
 			memmgr_usage_bytes -= head->size;
-			FREE (head_large);			
+			FREE(head_large,file,line,func);
 		} else {
 		} else {
 			ShowError("Memory manager: args of aFree is freed pointer %s:%d@%s\n", file, line, func);
 			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;
 		int block_no;
 		struct block* p;
 		struct block* p;
 		struct chunk* chunk;
 		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) {
 		if(pb == NULL) {
 			ShowFatalError("Memory manager::block_alloc failed.\n");
 			ShowFatalError("Memory manager::block_alloc failed.\n");
 			exit(1);
 			exit(1);
 		}
 		}
 
 
 		// store original block address in chunk
 		// 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) {
 		if (chunk == NULL) {
 			ShowFatalError("Memory manager::block_alloc failed.\n");
 			ShowFatalError("Memory manager::block_alloc failed.\n");
 			exit(1);
 			exit(1);
@@ -649,8 +617,8 @@ static void memmgr_final (void)
 	chunk = chunk_first;
 	chunk = chunk_first;
 	while (chunk) {
 	while (chunk) {
 		chunk2 = chunk->next;
 		chunk2 = chunk->next;
-		FREE(chunk->block);
-		FREE(chunk);
+		FREE(chunk->block,file,line,func);
+		FREE(chunk,file,line,func);
 		chunk = chunk2;
 		chunk = chunk2;
 	}
 	}
 
 
@@ -662,7 +630,7 @@ static void memmgr_final (void)
 			large->unit_head.file, large->unit_head.line, large->unit_head.size);
 			large->unit_head.file, large->unit_head.line, large->unit_head.size);
 		memmgr_log (buf);
 		memmgr_log (buf);
 	#endif
 	#endif
-		FREE (large);
+		FREE(large,file,line,func);
 		large = large2;
 		large = large2;
 	}
 	}
 #ifdef LOG_MEMMGR
 #ifdef LOG_MEMMGR

+ 58 - 51
src/common/malloc.h

@@ -3,6 +3,12 @@
 
 
 #ifndef _MALLOC_H_
 #ifndef _MALLOC_H_
 #define _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
 #define MEMSET_TURBO
 
 
@@ -36,29 +42,29 @@
 #	define aStrdup(p)		_mstrdup(p,ALC_MARK)
 #	define aStrdup(p)		_mstrdup(p,ALC_MARK)
 #	define aFree(p)			_mfree(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
 #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 aCallocA(m,n)	aCallocA_(m,n,ALC_MARK)
 #	define aRealloc(p,n)	aRealloc_(p,n,ALC_MARK)
 #	define aRealloc(p,n)	aRealloc_(p,n,ALC_MARK)
 #	define aStrdup(p)		aStrdup_(p,ALC_MARK)
 #	define aStrdup(p)		aStrdup_(p,ALC_MARK)
 #	define aFree(p)			aFree_(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
 #endif
 
 
@@ -67,59 +73,60 @@
 #ifdef MEMWATCH
 #ifdef MEMWATCH
 
 
 #	include "memwatch.h"
 #	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)
 #elif defined(DMALLOC)
 
 
 #	include "dmalloc.h"
 #	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)
 #elif defined(GCOLLECT)
 
 
 #	include "gc.h"
 #	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 * _bcalloc(size_t, size_t);
 	void * _bcallocA(size_t, size_t);
 	void * _bcallocA(size_t, size_t);
 	char * _bstrdup(const char *);
 	char * _bstrdup(const char *);
 
 
+/* FIXME Why is this the same as #else? [FlavioJS]
 #elif defined(BCHECK)
 #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
 #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
 #endif
 
 

+ 1 - 1
src/common/socket.c

@@ -253,7 +253,7 @@ static int send_from_fifo(int fd)
 		return 0;
 		return 0;
 	}
 	}
 #else
 #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 (len == -1)
 	{
 	{
 		if (errno == ECONNABORTED)
 		if (errno == ECONNABORTED)