Explorar o código

Deduplicate strlib code (#7651)

Follow up to 4e64623
Lemongrass3110 %!s(int64=2) %!d(string=hai) anos
pai
achega
4d9b1a0b83
Modificáronse 3 ficheiros con 19 adicións e 110 borrados
  1. 14 0
      src/common/malloc.hpp
  2. 5 101
      src/common/strlib.cpp
  3. 0 9
      src/common/strlib.hpp

+ 14 - 0
src/common/malloc.hpp

@@ -4,6 +4,8 @@
 #ifndef MALLOC_HPP
 #define MALLOC_HPP
 
+#include <config/core.hpp>
+
 #include "cbasetypes.hpp"
 
 #define ALC_MARK __FILE__, __LINE__, __func__
@@ -39,6 +41,12 @@
 #	define aStrdup(p)		_mstrdup(p,ALC_MARK)
 #	define aFree(p)			_mfree(p,ALC_MARK)
 
+#	define aMalloc2(n,file,line,func)		_mmalloc(n,file,line,func)
+#	define aCalloc2(m,n,file,line,func)		_mcalloc(m,n,file,line,func)
+#	define aRealloc2(p,n,file,line,func)	_mrealloc(p,n,file,line,func)
+#	define aStrdup2(p,file,line,func)		_mstrdup(p,file,line,func)
+#	define aFree2(p,file,line,func)			_mfree(p,file,line,func)
+
 	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);
@@ -53,6 +61,12 @@
 #	define aStrdup(p)		aStrdup_(p,ALC_MARK)
 #	define aFree(p)			aFree_(p,ALC_MARK)
 
+#	define aMalloc2(n,file,line,func)		aMalloc_((n),file,line,func)
+#	define aCalloc2(m,n,file,line,func)		aCalloc_((m),(n),file,line,func)
+#	define aRealloc2(p,n,file,line,func)	aRealloc_(p,n,file,line,func)
+#	define aStrdup2(p,file,line,func)		aStrdup_(p,file,line,func)
+#	define aFree2(p,file,line,func)			aFree_(p,file,line,func)
+
 	void* aMalloc_	(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* aRealloc_	(void *p, size_t size, const char *file, int line, const char *func);

+ 5 - 101
src/common/strlib.cpp

@@ -948,12 +948,11 @@ bool sv_readdb(const char* directory, const char* filename, char delim, int minc
 //
 // @author MouseJstr (original)
 
-#ifdef USE_MEMMGR
 /// Allocates a StringBuf
 StringBuf* _StringBuf_Malloc(const char *file, int line, const char *func)
 {
 	StringBuf* self;
-	self = (StringBuf *)_mcalloc(1, sizeof(StringBuf), file, line, func);
+	self = (StringBuf *)aCalloc2(1, sizeof(StringBuf), file, line, func);
 	_StringBuf_Init(file, line, func, self);
 	return self;
 }
@@ -962,7 +961,7 @@ StringBuf* _StringBuf_Malloc(const char *file, int line, const char *func)
 void _StringBuf_Init(const char *file, int line, const char *func,StringBuf* self)
 {
 	self->max_ = 1024;
-	self->ptr_ = self->buf_ = (char*)_mmalloc(self->max_ + 1, file, line, func);
+	self->ptr_ = self->buf_ = (char*)aMalloc2(self->max_ + 1, file, line, func);
 }
 
 /// Appends the result of printf to the StringBuf
@@ -999,7 +998,7 @@ int _StringBuf_Vprintf(const char *file, int line, const char *func,StringBuf* s
 		/* Else try again with more space. */
 		self->max_ *= 2; // twice the old size
 		off = (int)(self->ptr_ - self->buf_);
-		self->buf_ = (char*)_mrealloc(self->buf_, self->max_ + 1, file, line, func);
+		self->buf_ = (char*)aRealloc2(self->buf_, self->max_ + 1, file, line, func);
 		self->ptr_ = self->buf_ + off;
 	}
 }
@@ -1014,7 +1013,7 @@ int _StringBuf_Append(const char *file, int line, const char *func,StringBuf* se
 	{
 		int off = (int)(self->ptr_ - self->buf_);
 		self->max_ += needed;
-		self->buf_ = (char*)_mrealloc(self->buf_, self->max_ + 1, file, line, func);
+		self->buf_ = (char*)aRealloc2(self->buf_, self->max_ + 1, file, line, func);
 		self->ptr_ = self->buf_ + off;
 	}
 
@@ -1033,7 +1032,7 @@ int _StringBuf_AppendStr(const char *file, int line, const char *func,StringBuf*
 	{// not enough space, expand the buffer (minimum expansion = 1024)
 		int off = (int)(self->ptr_ - self->buf_);
 		self->max_ += max(needed, 1024);
-		self->buf_ = (char*)_mrealloc(self->buf_, self->max_ + 1, file, line, func);
+		self->buf_ = (char*)aRealloc2(self->buf_, self->max_ + 1, file, line, func);
 		self->ptr_ = self->buf_ + off;
 	}
 
@@ -1042,101 +1041,6 @@ int _StringBuf_AppendStr(const char *file, int line, const char *func,StringBuf*
 	return (int)(self->ptr_ - self->buf_);
 }
 
-#else
-/// Allocates a StringBuf
-StringBuf* StringBuf_Malloc(void)
-{
-	StringBuf* self;
-	self = (StringBuf *)aMalloc(sizeof(StringBuf));
-	StringBuf_Init(self);
-	return self;
-}
-
-/// Initializes a previously allocated StringBuf
-void StringBuf_Init(StringBuf* self)
-{
-	self->max_ = 1024;
-	self->ptr_ = self->buf_ = (char*)aMalloc(self->max_ + 1);
-}
-
-/// Appends the result of printf to the StringBuf
-int StringBuf_Printf(StringBuf* self, const char* fmt, ...)
-{
-	int len;
-	va_list ap;
-
-	va_start(ap, fmt);
-	len = StringBuf_Vprintf(self, fmt, ap);
-	va_end(ap);
-
-	return len;
-}
-
-/// Appends the result of vprintf to the StringBuf
-int StringBuf_Vprintf(StringBuf* self, const char* fmt, va_list ap)
-{
-	for(;;)
-	{
-		int n, size, off;
-		va_list apcopy;
-		/* Try to print in the allocated space. */
-		size = self->max_ - (self->ptr_ - self->buf_);
-		va_copy(apcopy, ap);
-		n = vsnprintf(self->ptr_, size, fmt, apcopy);
-		va_end(apcopy);
-		/* If that worked, return the length. */
-		if( n > -1 && n < size )
-		{
-			self->ptr_ += n;
-			return (int)(self->ptr_ - self->buf_);
-		}
-		/* Else try again with more space. */
-		self->max_ *= 2; // twice the old size
-		off = (int)(self->ptr_ - self->buf_);
-		self->buf_ = (char*)aRealloc(self->buf_, self->max_ + 1);
-		self->ptr_ = self->buf_ + off;
-	}
-}
-
-/// Appends the contents of another StringBuf to the StringBuf
-int StringBuf_Append(StringBuf* self, const StringBuf* sbuf)
-{
-	int available = self->max_ - (self->ptr_ - self->buf_);
-	int needed = (int)(sbuf->ptr_ - sbuf->buf_);
-
-	if( needed >= available )
-	{
-		int off = (int)(self->ptr_ - self->buf_);
-		self->max_ += needed;
-		self->buf_ = (char*)aRealloc(self->buf_, self->max_ + 1);
-		self->ptr_ = self->buf_ + off;
-	}
-
-	memcpy(self->ptr_, sbuf->buf_, needed);
-	self->ptr_ += needed;
-	return (int)(self->ptr_ - self->buf_);
-}
-
-// Appends str to the StringBuf
-int StringBuf_AppendStr(StringBuf* self, const char* str)
-{
-	int available = self->max_ - (self->ptr_ - self->buf_);
-	int needed = (int)strlen(str);
-
-	if( needed >= available )
-	{// not enough space, expand the buffer (minimum expansion = 1024)
-		int off = (int)(self->ptr_ - self->buf_);
-		self->max_ += max(needed, 1024);
-		self->buf_ = (char*)aRealloc(self->buf_, self->max_ + 1);
-		self->ptr_ = self->buf_ + off;
-	}
-
-	memcpy(self->ptr_, str, needed);
-	self->ptr_ += needed;
-	return (int)(self->ptr_ - self->buf_);
-}
-#endif
-
 // Returns the length of the data in the Stringbuf
 int StringBuf_Length(StringBuf* self)
 {

+ 0 - 9
src/common/strlib.hpp

@@ -144,7 +144,6 @@ struct StringBuf
 };
 typedef struct StringBuf StringBuf;
 
-#ifdef USE_MEMMGR
 StringBuf* _StringBuf_Malloc(const char *file, int line, const char *func);
 #define StringBuf_Malloc() _StringBuf_Malloc(ALC_MARK)
 void _StringBuf_Init(const char *file, int line, const char *func, StringBuf* self);
@@ -157,14 +156,6 @@ int _StringBuf_Append(const char *file, int line, const char *func, StringBuf* s
 #define StringBuf_Append(self,sbuf) _StringBuf_Append(ALC_MARK,self,sbuf)
 int _StringBuf_AppendStr(const char *file, int line, const char *func, StringBuf* self, const char* str);
 #define StringBuf_AppendStr(self,str) _StringBuf_AppendStr(ALC_MARK,self,str)
-#else
-StringBuf* StringBuf_Malloc();
-void StringBuf_Init(StringBuf* self);
-int StringBuf_Printf(StringBuf* self, const char* fmt, ...);
-int StringBuf_Vprintf(StringBuf* self, const char* fmt, va_list args);
-int StringBuf_Append(StringBuf* self, const StringBuf* sbuf);
-int StringBuf_AppendStr(StringBuf* self, const char* str);
-#endif
 int StringBuf_Length(StringBuf* self);
 char* StringBuf_Value(StringBuf* self);
 void StringBuf_Clear(StringBuf* self);