mempool-alloconly.c revision c0435c854a0e7246373b9752d163095cc4fbe985
/*
mempool-alloconly.c : Memory pool for fast allocation of memory without
need to free it in small blocks
Copyright (c) 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.
*/
/* @UNSAFE: whole file */
#include "lib.h"
#include "mempool.h"
#include <stdlib.h>
#define MAX_ALLOC_SIZE SSIZE_T_MAX
struct alloconly_pool {
int refcount;
struct pool_block *block;
};
struct pool_block {
struct pool_block *prev;
/* unsigned char data[]; */
};
#define POOL_BLOCK_DATA(block) \
((char *) (block) + SIZEOF_POOLBLOCK)
struct pool_alloc {
union {
unsigned char alignment[MEM_ALIGN_SIZE];
} size;
};
static struct pool static_alloconly_pool = {
};
{
struct alloconly_pool *apool;
int len;
i_panic("pool_alloconly_create(): Out of memory");
}
{
/* destroy all but the last block */
/* destroy the last block */
}
{
}
{
}
{
struct pool_block *block;
/* each block is at least twice the size of the previous one */
#ifdef DEBUG
}
#endif
i_panic("block_alloc(): Out of memory");
}
{
struct pool_alloc *alloc;
/* we need a new block */
}
}
void *mem __attr_unused__)
{
/* ignore */
}
{
/* see if we want to grow the memory we allocated last */
/* yeah, see if we can grow */
/* just shrink the available size */
return TRUE;
}
}
return FALSE;
}
{
struct pool_alloc *alloc;
unsigned char *new_mem;
/* get old size */
return mem;
/* see if we can directly grow it */
/* slow way - allocate + copy */
}
/* clear new data */
}
return mem;
}
{
struct pool_block *block;
/* destroy all blocks but the last, which is the largest */
}
/* clear the last block */
}