temp-mempool.c revision d2f5aacd3d549c3d39dae41b6522d585244b016d
/*
temp-mempool.c : Memory pool for temporary memory allocations
Copyright (c) 2001-2002 Timo Sirainen
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "lib.h"
#include "temp-mempool.h"
#include <stdlib.h>
/* #define TEMP_POOL_DISABLE */
#ifndef TEMP_POOL_DISABLE
/* max. number of bytes to even try to allocate. This is done just to avoid
allocating less memory than was actually requested because of integer
overflows. */
#define MAX_ALLOC_SIZE SSIZE_T_MAX
/* Initial pool size - this should be kept in a size that doesn't exceed
in a normal use to keep it fast. */
typedef struct _MemBlockStack MemBlockStack;
struct _MemBlock {
/* unsigned char data[]; */
};
#define MEM_BLOCK_DATA(block) \
((char *) (block) + SIZEOF_MEMBLOCK)
/* current_stack contains last t_push()ed blocks. After that new
MemBlockStack is created and it's ->prev is set to current_stack. */
#define MEM_LIST_BLOCK_COUNT 16
struct _MemBlockStack {
};
static int stack_pos; /* next free position in current_stack->block[] */
static int last_alloc_size;
static MemBlock *last_buffer_block;
static size_t last_buffer_size;
int t_push(void)
{
if (stack_pos == MEM_LIST_BLOCK_COUNT) {
/* stack list full */
stack_pos = 0;
if (unused_stack_list == NULL) {
/* allocate new stack */
i_panic("t_push(): Out of memory");
} else {
/* use existing unused stack */
}
}
/* mark our current position */
return stack_pos++;
}
{
/* free all the blocks, except if any of them is bigger than
unused_block, replace it */
} else {
}
}
}
int t_pop(void)
{
if (stack_pos == 0)
i_panic("t_pop() called with empty stack");
stack_pos--;
/* update the current block */
/* free unused blocks */
}
if (stack_pos == 0) {
/* stack block is now unused, add it to unused list */
}
return stack_pos;
}
{
i_panic("mem_block_alloc(): "
}
return block;
}
{
void *ret;
if (size == 0)
return NULL;
if (size > MAX_ALLOC_SIZE)
i_panic("Trying to allocate too much memory");
/* reset t_buffer_get() mark - not really needed but makes it easier
to notice if t_malloc() is called between t_buffer_get() and
t_buffer_alloc() */
/* allocate only aligned amount of memory so alignment comes
always properly */
/* used for t_try_grow() */
/* enough space in current block, use it */
if (permanent)
return ret;
}
/* current block is full, see if we can use the unused_block */
unused_block = NULL;
} else {
}
if (permanent)
return MEM_BLOCK_DATA(current_block);
}
{
}
{
void *mem;
return mem;
}
{
/* see if we want to grow the memory we allocated last */
if (MEM_BLOCK_DATA(current_block) +
last_alloc_size) == mem) {
/* yeah, see if we can grow */
/* just shrink the available size */
return TRUE;
}
}
return FALSE;
}
{
void *ret;
return ret;
}
{
void *new_buffer;
return buffer;
if (new_buffer != buffer)
return new_buffer;
}
{
/* we've already reserved the space, now we just mark it used */
}
void temp_mempool_init(void)
{
t_push();
last_alloc_size = 0;
last_buffer_size = 0;
}
void temp_mempool_deinit(void)
{
t_pop();
if (stack_pos != MEM_LIST_BLOCK_COUNT)
i_panic("Missing t_pop() call");
while (unused_stack_list != NULL) {
}
}
#else
struct _Stack {
};
struct _Alloc {
void *mem;
};
static int stack_counter;
static Stack *current_stack;
static void *buffer_mem;
int t_push(void)
{
return stack_counter++;
}
int t_pop(void)
{
}
return --stack_counter;
}
{
if (buffer_mem != NULL) {
buffer_mem = NULL;
}
}
{
void *mem;
return mem;
}
{
void *mem;
return mem;
}
{
void *new_mem;
return TRUE;
return FALSE;
}
{
return buffer_mem;
}
{
return buffer_mem;
}
{
void *mem;
mem = buffer_mem;
buffer_mem = NULL;
}
void temp_mempool_init(void)
{
stack_counter = 0;
buffer_mem = NULL;
t_push();
}
void temp_mempool_deinit(void)
{
t_pop();
if (stack_counter != 0)
i_panic("Missing t_pop() call");
}
#endif