/* * Copyright (c) 2004 Teodor Sigaev * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the author nor the names of any co-contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY CONTRIBUTORS ``AS IS'' AND ANY EXPRESS * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL CONTRIBUTORS BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifndef __TMALLOC_H__ #define __TMALLOC_H__ #include #include "tools.h" void * tmalloc(size_t size); void * trealloc(void * ptr, size_t size); void * t0malloc(size_t size); void tfree(void * ptr); char * tstrdup(char * src); char * tnstrdup(char *src, int len); char * strlower(char * src); char * strupper(char * src); int clrspace(char *buf); /* fast allocate */ #define CNTXCHUNK (64*1024) typedef struct MemoryChunk { size_t size; size_t freesize; struct MemoryChunk *next; char data[1]; } MemoryChunk; #define MEMCHNKHDRSZ offsetof(MemoryChunk, data) typedef struct MemoryContext { u_int32_t flags; struct MemoryContext *parent; struct MemoryContext *child; MemoryChunk *chunk; } MemoryContext; #define MC_DEBUG 0x01 typedef struct { size_t size; MemoryContext *cntx; char data[1]; } MCAllocatedSpace; #define MCASHDRSZ offsetof(MCAllocatedSpace, data) #define MCMAGICKNUMBER (0xFE0FBEEF) 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); void mcfree(void * ptr); 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