1 /* Copyright (C) 2003 Aleksey Krivoshey <voodoo@foss.kharkov.ua>
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 AsyncDNSMemPool::PoolChunk::PoolChunk(size_t _size):
22 pool(NULL), pos(0), size(_size)
24 pool = ::malloc(size);
27 AsyncDNSMemPool::PoolChunk::~PoolChunk()
32 AsyncDNSMemPool::AsyncDNSMemPool(size_t _defaultSize):
33 chunks(NULL), chunksCount(0), defaultSize(_defaultSize),
34 poolUsage(0), poolUsageCounter(0)
38 AsyncDNSMemPool::~AsyncDNSMemPool()
40 for(size_t i = 0; i<chunksCount; i++){
46 int AsyncDNSMemPool::initialize()
49 chunks = (PoolChunk**)::malloc(sizeof(PoolChunk*));
53 chunks[chunksCount-1] = new PoolChunk(defaultSize);
58 void AsyncDNSMemPool::addNewChunk(size_t size)
61 chunks = (PoolChunk**)::realloc(chunks, chunksCount*sizeof(PoolChunk*));
62 if(size <= defaultSize)
63 chunks[chunksCount-1] = new PoolChunk(defaultSize);
65 chunks[chunksCount-1] = new PoolChunk(size);
68 void * AsyncDNSMemPool::alloc(size_t size)
70 PoolChunk * chunk = NULL;
71 for(size_t i = 0; i<chunksCount; i++){
73 if((chunk->size - chunk->pos) >= size){
75 return ((u_int8_t*)chunk->pool) + chunk->pos - size;
79 chunks[chunksCount-1]->pos = size;
80 return chunks[chunksCount-1]->pool;
83 void AsyncDNSMemPool::free()
89 for(size_t i = 0; i<chunksCount; i++){
91 psz += chunks[i]->size;
94 poolUsage=(poolUsage>pu)?poolUsage:pu;
96 if(poolUsageCounter >= 10 && chunksCount > 1){
97 psz -= chunks[chunksCount-1]->size;
100 delete chunks[chunksCount];
103 poolUsageCounter = 0;
107 void * AsyncDNSMemPool::calloc(size_t size)
109 return ::memset(this->alloc(size), 0, size);
112 char * AsyncDNSMemPool::strdup(const char *str)
114 return ::strcpy((char*)this->alloc(strlen(str)+1), str);