mempool-datastack.c revision f94332a6f35455f112cb7dfccfff369fbc55d8cc
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen/*
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen mempool-data-stack.c : Memory pool wrapper for data stack
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen Copyright (c) 2002 Timo Sirainen
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen Permission is hereby granted, free of charge, to any person obtaining
af49da69a12b5383b89c7b2fa574c2a85b9ca310Timo Sirainen a copy of this software and associated documentation files (the
af49da69a12b5383b89c7b2fa574c2a85b9ca310Timo Sirainen "Software"), to deal in the Software without restriction, including
fea53234804e75bb8f0b87ab0a0b1cc1c9770031Timo Sirainen without limitation the rights to use, copy, modify, merge, publish,
fea53234804e75bb8f0b87ab0a0b1cc1c9770031Timo Sirainen distribute, sublicense, and/or sell copies of the Software, and to
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen permit persons to whom the Software is furnished to do so, subject to
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen the following conditions:
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen The above copyright notice and this permission notice shall be
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen included in all copies or substantial portions of the Software.
518781a0a4ba2403f23dbadda445b107fbb87daaPhil Carmody
33750ba29b605a925af5aed58d3f3735422b1e25Phil Carmody THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
33750ba29b605a925af5aed58d3f3735422b1e25Phil Carmody OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33750ba29b605a925af5aed58d3f3735422b1e25Phil Carmody MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
33750ba29b605a925af5aed58d3f3735422b1e25Phil Carmody IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
40ac30b9267c710f5fcdd4b2f6bcd7718a631843Timo Sirainen CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
518781a0a4ba2403f23dbadda445b107fbb87daaPhil Carmody TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
8562f8de469e87ad4104747cebec6b133fa3a0f5Timo Sirainen SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
8562f8de469e87ad4104747cebec6b133fa3a0f5Timo Sirainen*/
8562f8de469e87ad4104747cebec6b133fa3a0f5Timo Sirainen
8562f8de469e87ad4104747cebec6b133fa3a0f5Timo Sirainen#include "lib.h"
8562f8de469e87ad4104747cebec6b133fa3a0f5Timo Sirainen#include "mempool.h"
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen#include <stdlib.h>
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainenstatic void pool_data_stack_ref(pool_t pool);
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainenstatic void pool_data_stack_unref(pool_t pool);
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainenstatic void *pool_data_stack_malloc(pool_t pool, size_t size);
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainenstatic void pool_data_stack_free(pool_t pool, void *mem);
518781a0a4ba2403f23dbadda445b107fbb87daaPhil Carmodystatic void *pool_data_stack_realloc(pool_t pool, void *mem,
518781a0a4ba2403f23dbadda445b107fbb87daaPhil Carmody size_t old_size, size_t new_size);
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainenstatic void pool_data_stack_clear(pool_t pool);
af49da69a12b5383b89c7b2fa574c2a85b9ca310Timo Sirainen
af49da69a12b5383b89c7b2fa574c2a85b9ca310Timo Sirainenstatic struct pool static_data_stack_pool = {
9625595c47c665f5aee57ebfcb1fcbe9ad1bf3a0Martti Rannanjärvi pool_data_stack_ref,
af49da69a12b5383b89c7b2fa574c2a85b9ca310Timo Sirainen pool_data_stack_unref,
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen
pool_data_stack_malloc,
pool_data_stack_free,
pool_data_stack_realloc,
pool_data_stack_clear,
TRUE
};
pool_t data_stack_pool = &static_data_stack_pool;
static void pool_data_stack_ref(pool_t pool __attr_unused__)
{
}
static void pool_data_stack_unref(pool_t pool __attr_unused__)
{
}
static void *pool_data_stack_malloc(pool_t pool __attr_unused__, size_t size)
{
if (size == 0 || size > SSIZE_T_MAX)
i_panic("Trying to allocate %"PRIuSIZE_T" bytes", size);
return t_malloc0(size);
}
static void pool_data_stack_free(pool_t pool __attr_unused__,
void *mem __attr_unused__)
{
}
static void *pool_data_stack_realloc(pool_t pool __attr_unused__, void *mem,
size_t old_size, size_t new_size)
{
void *new_mem;
/* @UNSAFE */
if (new_size == 0 || new_size > SSIZE_T_MAX)
i_panic("Trying to allocate %"PRIuSIZE_T" bytes", new_size);
if (mem == NULL)
return pool_data_stack_malloc(pool, new_size);
if (old_size >= new_size)
return mem;
if (!t_try_realloc(mem, new_size)) {
new_mem = t_malloc(new_size);
memcpy(new_mem, mem, old_size);
mem = new_mem;
}
memset((char *) mem + old_size, 0, new_size - old_size);
return mem;
}
static void pool_data_stack_clear(pool_t pool __attr_unused__)
{
}