2 * Copyright (c) 2004 Teodor Sigaev <teodor@sigaev.ru>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the author nor the names of any co-contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY CONTRIBUTORS ``AS IS'' AND ANY EXPRESS
18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 tmalloc(size_t size) {
40 void *ptr = malloc(size);
42 tlog(TL_CRIT|TL_EXIT, "Can't allocate %d bytes", size);
47 t0malloc(size_t size) {
48 void *ptr = tmalloc(size);
54 trealloc(void * ptr, size_t size) {
56 ptr = realloc(ptr,size);
58 tlog(TL_CRIT|TL_EXIT, "Can't reallocate to %d bytes", size);
71 char * dest = strdup(src);
73 tlog(TL_CRIT|TL_EXIT, "Can't strdup %d bytes", strlen(src)+1);
78 tnstrdup(char *src, int len) {
79 char *dest=(char*)tmalloc(len+1);
80 memcpy(dest, src, len);
86 strlower(char * src) {
90 *ptr = tolower(*(unsigned char *) ptr);
97 strupper(char * src) {
101 *ptr = toupper(*(unsigned char *) ptr);
108 clrspace(char *buf) {
109 char *ptr=buf, *ptrc=buf;
111 if (!isspace(*ptr)) {
123 allocMemoryContext(MemoryContext* parent, int flags) {
126 res = (MemoryContext*)tmalloc( sizeof(MemoryContext) );
128 res->parent = parent;
130 res->child = parent->child;
135 res->chunk = (MemoryChunk*)tmalloc( MEMCHNKHDRSZ + CNTXCHUNK );
136 res->chunk->size = res->chunk->freesize = CNTXCHUNK;
137 res->chunk->next = NULL;
143 freeMemoryContext(MemoryContext *cntx) {
144 MemoryContext *ptr=cntx;
145 MemoryChunk *chunk, *chunkptr;
148 cntx->parent->child=NULL;
155 chunk=chunkptr->next;
166 resetMemoryContext(MemoryContext *cntx) {
167 MemoryChunk *chunk, *chunkptr;
170 chunkptr = cntx->chunk;
171 chunkptr->freesize = chunkptr->size;
172 chunkptr = chunkptr->next;
173 cntx->chunk->next = NULL;
176 chunk=chunkptr->next;
185 mcalloc(MemoryContext *cntx, size_t size) {
186 MemoryChunk *chunk = cntx->chunk;
187 MCAllocatedSpace *alloc;
189 size = PTRALIGN(size) + MCASHDRSZ;
190 if ( cntx->flags & MC_DEBUG )
191 size += sizeof(u_int32_t);
193 if ( chunk->freesize < size ) {
194 MemoryChunk *newchunk;
196 if ( size >= CNTXCHUNK ) {
197 newchunk = (MemoryChunk*)tmalloc(MEMCHNKHDRSZ + size);
198 newchunk->size = newchunk->freesize = size;
199 newchunk->next = chunk->next;
200 chunk->next = newchunk;
202 newchunk = (MemoryChunk*)tmalloc(MEMCHNKHDRSZ + CNTXCHUNK);
203 newchunk->size = newchunk->freesize = CNTXCHUNK;
204 newchunk->next = chunk;
205 cntx->chunk = newchunk;
211 alloc = (MCAllocatedSpace*)( chunk->data + chunk->size - chunk->freesize );
214 chunk->freesize -= size;
216 if ( cntx->flags & MC_DEBUG ) {
217 *(u_int32_t*)((char*)alloc + alloc->size - sizeof(u_int32_t) ) = MCMAGICKNUMBER;
218 memset( alloc->data, 0xc3, size - MCASHDRSZ - sizeof(u_int32_t) );
221 return (void*)(alloc->data);
225 mc0alloc(MemoryContext *cntx, size_t size) {
226 void *res = mcalloc(cntx, size);
227 memset( res, 0, size );
232 mcrealloc(void * ptr, size_t size) {
233 MCAllocatedSpace *alloc = (MCAllocatedSpace*)( (char*)ptr - MCASHDRSZ );
237 tlog(TL_CRIT|TL_EXIT, "mcrealloc: realloc null pointer");
239 if ( alloc->cntx->flags & MC_DEBUG ) {
240 tassert( *(u_int32_t*)( (char*)alloc + alloc->size - sizeof(u_int32_t) ) == MCMAGICKNUMBER );
241 realsize = alloc->size - MCASHDRSZ - sizeof(u_int32_t);
243 realsize = alloc->size - MCASHDRSZ;
245 if ( size > realsize ) {
246 MemoryChunk *chunk = alloc->cntx->chunk;
247 if ( (char*)alloc == chunk->data + chunk->size - chunk->freesize - alloc->size &&
248 PTRALIGN(size)-realsize <= alloc->cntx->chunk->freesize ) {
250 alloc->cntx->chunk->freesize -= PTRALIGN(size)-realsize;
251 alloc->size+=PTRALIGN(size)-realsize;
252 if ( alloc->cntx->flags & MC_DEBUG ) {
253 memset( (char*)(alloc->data) + realsize, 0xc3, PTRALIGN(size)-realsize );
254 *(u_int32_t*)((char*)alloc + alloc->size - sizeof(u_int32_t) ) = MCMAGICKNUMBER;
257 void *newptr = mcalloc(alloc->cntx, size);
258 memcpy( newptr, ptr, realsize );
260 if ( alloc->cntx->flags & MC_DEBUG )
261 memset( (char*)ptr + realsize, 0xc3, PTRALIGN(size)-realsize );
271 MCAllocatedSpace *alloc = (MCAllocatedSpace*)( (char*)ptr - MCASHDRSZ );
275 tlog(TL_CRIT|TL_EXIT, "mcfree: free null pointer");
277 if ( alloc->cntx->flags & MC_DEBUG )
278 tassert( *(u_int32_t*)((char*)alloc + alloc->size - sizeof(u_int32_t) ) == MCMAGICKNUMBER );
280 chunk = alloc->cntx->chunk;
281 if ( (char*)alloc == chunk->data + chunk->size - chunk->freesize - alloc->size ) /* last allocated value */
282 alloc->cntx->chunk->freesize+=alloc->size;
288 mcstrdup(MemoryContext *cntx, char * src) {
289 return mcnstrdup(cntx, src, strlen(src));
293 mcnstrdup(MemoryContext *cntx, char *src, int len) {
294 char *dest=(char*)mcalloc(cntx, len+1);
295 memcpy(dest, src, len);
300 /*********StringBuffer********/
302 #define SBALLOC(s) ( ( (s)->mc ) ? mcalloc((s)->mc, (s)->len) : tmalloc((s)->len) )
303 #define SBREALLOC(s) ( ( (s)->mc ) ? mcrealloc((s)->buf, (s)->len) : trealloc((s)->buf, (s)->len) )
306 initStringBuffer(StringBuffer* state, MemoryContext *mc, int initsize) {
307 state->len = (initsize>0) ? initsize : 1024;
309 state->ptr = state->buf = (char*)SBALLOC(state);
316 appendStringBuffer( StringBuffer *state, char *string, int stringlen) {
318 if ( stringlen <= 0 )
319 stringlen = strlen(string);
323 if ( stringlen == 0 )
326 while ( state->len - ( state->ptr - state->buf ) < stringlen + 1 ) {
327 int diff = state->ptr - state->buf;
330 state->buf = (char*)SBREALLOC(state);
331 state->ptr = state->buf + diff;
334 memcpy(state->ptr, string, stringlen);
335 state->ptr += stringlen;
341 printStringBuffer( StringBuffer *state, const char *format, ...) {
346 buffreelen = state->len - ( state->ptr - state->buf ) - 1;
348 va_start(args, format);
349 printedlen = vsnprintf(state->ptr, buffreelen, format, args);
352 * if buffer too short, resize buffer and
355 if ( buffreelen<=printedlen ) {
356 u_int32_t curlen = state->ptr - state->buf;
359 } while( state->ptr - state->buf + printedlen >= state->len );
361 state->buf = (char*)SBREALLOC(state);
362 state->ptr = state->buf + curlen;
363 va_start(args, format);
364 printedlen = vsnprintf(state->ptr, printedlen+1, format, args);
367 state->ptr += printedlen;