mempool-alloconly.c revision e4af52c1ff7edb2bdecc3b99520cbf062db3e6af
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen mempool-alloconly.c : Memory pool for fast allocation of memory without
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen need to free it in small blocks
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen Copyright (c) 2002 Timo Sirainen
252db51b6c0a605163326b3ea5d09e9936ca3b29Timo Sirainen Permission is hereby granted, free of charge, to any person obtaining
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen a copy of this software and associated documentation files (the
9511a40d933181045343110c8101b75887062aaeTimo Sirainen "Software"), to deal in the Software without restriction, including
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen without limitation the rights to use, copy, modify, merge, publish,
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen distribute, sublicense, and/or sell copies of the Software, and to
4aa7fe81503a20bc972ae625da4dd9e6996fbdbfTimo Sirainen permit persons to whom the Software is furnished to do so, subject to
4aa7fe81503a20bc972ae625da4dd9e6996fbdbfTimo Sirainen the following conditions:
4aa7fe81503a20bc972ae625da4dd9e6996fbdbfTimo Sirainen The above copyright notice and this permission notice shall be
4aa7fe81503a20bc972ae625da4dd9e6996fbdbfTimo Sirainen included in all copies or substantial portions of the Software.
4aa7fe81503a20bc972ae625da4dd9e6996fbdbfTimo Sirainen THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
4aa7fe81503a20bc972ae625da4dd9e6996fbdbfTimo Sirainen OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
4aa7fe81503a20bc972ae625da4dd9e6996fbdbfTimo Sirainen MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
4aa7fe81503a20bc972ae625da4dd9e6996fbdbfTimo Sirainen IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
4aa7fe81503a20bc972ae625da4dd9e6996fbdbfTimo Sirainen CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
4aa7fe81503a20bc972ae625da4dd9e6996fbdbfTimo Sirainen TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
4aa7fe81503a20bc972ae625da4dd9e6996fbdbfTimo Sirainen SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
8887a9bb6d2e3f664cf741b763643a0e5610fa4dTimo Sirainentypedef struct {
8887a9bb6d2e3f664cf741b763643a0e5610fa4dTimo Sirainen char name[MEM_ALIGN_SIZE]; /* variable size */
8887a9bb6d2e3f664cf741b763643a0e5610fa4dTimo Sirainen#define SIZEOF_ALLOCONLYPOOL (sizeof(AlloconlyPool)-MEM_ALIGN_SIZE)
8887a9bb6d2e3f664cf741b763643a0e5610fa4dTimo Sirainen /* unsigned char data[]; */
8887a9bb6d2e3f664cf741b763643a0e5610fa4dTimo Sirainen#define SIZEOF_POOLBLOCK (MEM_ALIGN(sizeof(PoolBlock)))
8887a9bb6d2e3f664cf741b763643a0e5610fa4dTimo Sirainentypedef struct {
8887a9bb6d2e3f664cf741b763643a0e5610fa4dTimo Sirainen unsigned char data[MEM_ALIGN_SIZE]; /* variable size */
8887a9bb6d2e3f664cf741b763643a0e5610fa4dTimo Sirainen#define SIZEOF_POOLALLOC (sizeof(PoolAlloc)-MEM_ALIGN_SIZE)
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainenstatic void *pool_alloconly_malloc(Pool pool, size_t size);
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainenstatic void pool_alloconly_free(Pool pool, void *mem);
01f4ee4a0243f3fe9af763e1a540cd5cff0d63f5Timo Sirainenstatic void *pool_alloconly_realloc(Pool pool, void *mem, size_t size);
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainenstatic void *pool_alloconly_realloc_min(Pool pool, void *mem, size_t size);
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainenstatic void block_alloc(AlloconlyPool *pool, size_t size);
4b9f99761df5014c659cd87fddaf6854af428cfcTimo SirainenPool pool_alloconly_create(const char *name, size_t size)
4b9f99761df5014c659cd87fddaf6854af428cfcTimo Sirainen apool = calloc(SIZEOF_ALLOCONLYPOOL + len+1, 1);
01f4ee4a0243f3fe9af763e1a540cd5cff0d63f5Timo Sirainen i_panic("pool_alloconly_create(): Out of memory");
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainenstatic void pool_alloconly_destroy(AlloconlyPool *apool)
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen /* destroy all but the last block */
597dba3488c648ffb375ee4a552bd52ac4346979Timo Sirainen /* destroy the last block */
da2aa032ccfa8e7e4a4380ef738014549f4d2c2dTimo Sirainen AlloconlyPool *apool = (AlloconlyPool *) pool;
bdcb00145ad87765e3fd22d4ebc4d2c029a326b9Timo Sirainen AlloconlyPool *apool = (AlloconlyPool *) pool;
bdcb00145ad87765e3fd22d4ebc4d2c029a326b9Timo Sirainenstatic void block_alloc(AlloconlyPool *apool, size_t size)
bdcb00145ad87765e3fd22d4ebc4d2c029a326b9Timo Sirainen /* each block is at least twice the size of the previous one */
return TRUE;
return FALSE;
unsigned char *new_mem;
old_size = 0;
return mem;
return mem;