#define __TMALLOC_H__
#include <sys/types.h>
+#include "tools.h"
void * tmalloc(size_t size);
void * trealloc(void * ptr, size_t size);
/* fast allocate */
-#define CNTXCHUNK (1024*1024)
+#define CNTXCHUNK (64*1024)
typedef struct MemoryChunk {
size_t size;
char data[1];
} MemoryChunk;
-#define MEMCHNKHDRSZ ( sizeof(u_int32_t)*2 + sizeof(MemoryChunk*) )
+#define MEMCHNKHDRSZ offsetof(MemoryChunk, data)
typedef struct MemoryContext {
u_int32_t flags;
char data[1];
} MCAllocatedSpace;
-#define MCASHDRSZ ( sizeof(size_t) + sizeof(MemoryContext*) )
+#define MCASHDRSZ offsetof(MCAllocatedSpace, data)
#define MCMAGICKNUMBER (0xFE0FBEEF)
-#define TYPEALIGN(ALIGNVAL,LEN) \
- (((long) (LEN) + (ALIGNVAL-1)) & ~((long) (ALIGNVAL-1)))
-
-#define PTRALIGN(LEN) TYPEALIGN(sizeof(void*), (LEN))
-
MemoryContext *allocMemoryContext(MemoryContext* parent, int flags);
void freeMemoryContext(MemoryContext* cntx);
+void resetMemoryContext(MemoryContext* cntx);
void* mcalloc(MemoryContext *cntx, size_t size);
void* mc0alloc(MemoryContext *cntx, size_t size);
void* mcrealloc(void * ptr, size_t size);
char * mcstrdup(MemoryContext *cntx, char * src);
char * mcnstrdup(MemoryContext *cntx, char *src, int len);
+typedef void* (*MemAlloc)(void *ptr, size_t size);
+
+typedef struct {
+ char *buf;
+ char *ptr;
+ u_int32_t len;
+ MemoryContext *mc;
+ MemAlloc memalloc;
+} StringBuffer;
+
+StringBuffer* initStringBuffer(StringBuffer* state, int initsize);
+StringBuffer* initStringBufferMC(StringBuffer* state, MemoryContext *mc, int initsize);
+StringBuffer* initStringBufferMA(StringBuffer* state, MemAlloc memalloc, int initsize);
+StringBuffer* appendStringBuffer( StringBuffer *state, char *string, int stringlen);
+StringBuffer* printStringBuffer( StringBuffer *state, const char *format, ...);
#endif