view src/lib/mempool-alloconly.c @ 896:21ffcce83c70 HEAD

Rewrote rfc822-tokenize.c to work one token at a time so it won't uselessly take memory, maybe also a bit faster. This caused pretty large changes all around. Also moved all string (un)escaping code to lib/strescape.c.
author Timo Sirainen <tss@iki.fi>
date Fri, 03 Jan 2003 17:57:12 +0200
parents 3d437b1e5257
children fd8888f6f037
line wrap: on
line source

/*
 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,
    distribute, sublicense, and/or sell copies of the Software, and to
    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

typedef struct _PoolBlock PoolBlock;

typedef struct {
	struct Pool pool;
	int refcount;

	PoolBlock *block;

	char name[MEM_ALIGN_SIZE]; /* variable size */
} AlloconlyPool;
#define SIZEOF_ALLOCONLYPOOL (sizeof(AlloconlyPool)-MEM_ALIGN_SIZE)

struct _PoolBlock {
	PoolBlock *prev;

	size_t size;
	size_t left;
	size_t last_alloc_size;

	/* unsigned char data[]; */
};
#define SIZEOF_POOLBLOCK (MEM_ALIGN(sizeof(PoolBlock)))

#define POOL_BLOCK_DATA(block) \
	((char *) (block) + SIZEOF_POOLBLOCK)

typedef struct {
	union {
		size_t size;
		unsigned char alignment[MEM_ALIGN_SIZE];
	} size;
	unsigned char data[MEM_ALIGN_SIZE]; /* variable size */
} PoolAlloc;
#define SIZEOF_POOLALLOC (sizeof(PoolAlloc)-MEM_ALIGN_SIZE)

static void pool_alloconly_ref(Pool pool);
static void pool_alloconly_unref(Pool pool);
static void *pool_alloconly_malloc(Pool pool, size_t size);
static void pool_alloconly_free(Pool pool, void *mem);
static void *pool_alloconly_realloc(Pool pool, void *mem, size_t size);
static void pool_alloconly_clear(Pool pool);

static void block_alloc(AlloconlyPool *pool, size_t size);

static struct Pool static_alloconly_pool = {
	pool_alloconly_ref,
	pool_alloconly_unref,

	pool_alloconly_malloc,
	pool_alloconly_free,

	pool_alloconly_realloc,

	pool_alloconly_clear
};

Pool pool_alloconly_create(const char *name, size_t size)
{
	AlloconlyPool *apool;
	int len;

	len = strlen(name);

	apool = calloc(SIZEOF_ALLOCONLYPOOL + len+1, 1);
	if (apool == NULL)
		i_panic("pool_alloconly_create(): Out of memory");
	apool->pool = static_alloconly_pool;
	apool->refcount = 1;
	memcpy(apool->name, name, len+1);

	block_alloc(apool, size);
	return (Pool) apool;
}

static void pool_alloconly_destroy(AlloconlyPool *apool)
{
	/* destroy all but the last block */
	pool_alloconly_clear(&apool->pool);

	/* destroy the last block */
	free(apool->block);
	free(apool);
}

static void pool_alloconly_ref(Pool pool)
{
	AlloconlyPool *apool = (AlloconlyPool *) pool;

	apool->refcount++;
}

static void pool_alloconly_unref(Pool pool)
{
	AlloconlyPool *apool = (AlloconlyPool *) pool;

	if (--apool->refcount == 0)
		pool_alloconly_destroy(apool);
}

static void block_alloc(AlloconlyPool *apool, size_t size)
{
	PoolBlock *block;

	/* each block is at least twice the size of the previous one */
	if (apool->block != NULL && size <= apool->block->size)
		size += apool->block->size;

	size = nearest_power(size + SIZEOF_POOLBLOCK);

#ifdef DEBUG
	if (apool->block != NULL) {
		i_warning("Growing pool '%s' with: %"PRIuSIZE_T,
			  apool->name, size);
	}
#endif

	block = calloc(size, 1);
	if (block == NULL)
		i_panic("block_alloc(): Out of memory");
	block->prev = apool->block;
	apool->block = block;

	block->size = size - SIZEOF_POOLBLOCK;
	block->left = block->size;
}

static void *pool_alloconly_malloc(Pool pool, size_t size)
{
	AlloconlyPool *apool = (AlloconlyPool *) pool;
	PoolAlloc *alloc;

	if (size == 0 || size > SSIZE_T_MAX)
		i_panic("Trying to allocate %"PRIuSIZE_T" bytes", size);

	size = MEM_ALIGN(size);

	if (apool->block->left < size + SIZEOF_POOLALLOC) {
		/* we need a new block */
		block_alloc(apool, size);
	}

	alloc = (PoolAlloc *) (POOL_BLOCK_DATA(apool->block) +
			       apool->block->size - apool->block->left);
	alloc->size.size = size;

	apool->block->left -= size + SIZEOF_POOLALLOC;
	apool->block->last_alloc_size = size;
	return alloc->data;
}

static void pool_alloconly_free(Pool pool __attr_unused__,
				void *mem __attr_unused__)
{
	/* ignore */
}

static int pool_try_grow(AlloconlyPool *apool, void *mem, size_t size)
{
	/* see if we want to grow the memory we allocated last */
	if (POOL_BLOCK_DATA(apool->block) +
	    (apool->block->size - apool->block->left -
	     apool->block->last_alloc_size) == mem) {
		/* yeah, see if we can grow */
		if (apool->block->left >= size-apool->block->last_alloc_size) {
			/* just shrink the available size */
			apool->block->left -=
				size - apool->block->last_alloc_size;
			apool->block->last_alloc_size = size;
			return TRUE;
		}
	}

	return FALSE;
}

static void *pool_alloconly_realloc(Pool pool, void *mem, size_t size)
{
	AlloconlyPool *apool = (AlloconlyPool *) pool;
	PoolAlloc *alloc;
	unsigned char *new_mem;
	size_t old_size;

	if (size == 0 || size > SSIZE_T_MAX)
		i_panic("Trying to allocate %"PRIuSIZE_T" bytes", size);

	if (mem == NULL)
		return pool_alloconly_malloc(pool, size);

	/* get old size */
	alloc = (PoolAlloc *) ((char *) mem - SIZEOF_POOLALLOC);
	old_size = alloc->size.size;

	if (size <= old_size)
		return mem;

	size = MEM_ALIGN(size);

	/* see if we can directly grow it */
	if (!pool_try_grow(apool, mem, size)) {
		/* slow way - allocate + copy */
		new_mem = pool_alloconly_malloc(pool, size);
		memcpy(new_mem, mem, old_size);
		mem = new_mem;
	}

	if (size > old_size) {
                /* clear new data */
		memset((char *) mem + old_size, 0, size - old_size);
	}

        return mem;
}

static void pool_alloconly_clear(Pool pool)
{
	AlloconlyPool *apool = (AlloconlyPool *) pool;
	PoolBlock *block;

	/* destroy all blocks but the last, which is the largest */
	while (apool->block->prev != NULL) {
		block = apool->block;
		apool->block = block->prev;

		free(block);
	}

	/* clear the last block */
	memset(POOL_BLOCK_DATA(apool->block), 0,
	       apool->block->size - apool->block->left);
	apool->block->left = apool->block->size;
	apool->block->last_alloc_size = 0;
}