2008-02-07 06:31:47 +01:00
|
|
|
/* Copyright (c) 2007-2008, The Tor Project, Inc. */
|
2007-04-11 02:30:22 +02:00
|
|
|
/* See LICENSE for licensing information */
|
2007-04-11 02:30:34 +02:00
|
|
|
/* $Id$ */
|
2007-06-17 20:22:35 +02:00
|
|
|
#if 1
|
|
|
|
/* Tor dependencies */
|
|
|
|
#include "orconfig.h"
|
|
|
|
#endif
|
|
|
|
|
2007-04-11 02:30:22 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2008-02-08 22:13:08 +01:00
|
|
|
#include "torint.h"
|
2007-04-11 02:30:22 +02:00
|
|
|
#define MEMPOOL_PRIVATE
|
|
|
|
#include "mempool.h"
|
|
|
|
|
2008-02-12 21:20:52 +01:00
|
|
|
#define LAZY_CHUNK_SORT
|
2008-02-08 22:13:08 +01:00
|
|
|
|
2007-04-11 02:30:29 +02:00
|
|
|
/* OVERVIEW:
|
2007-04-11 02:30:34 +02:00
|
|
|
*
|
|
|
|
* This is an implementation of memory pools for Tor cells. It may be
|
|
|
|
* useful for you too.
|
|
|
|
*
|
|
|
|
* Generally, a memory pool is an allocation strategy optimized for large
|
|
|
|
* numbers of identically-sized objects. Rather than the elaborate arena
|
2007-05-25 21:41:31 +02:00
|
|
|
* and coalescing strategies you need to get good performance for a
|
2007-04-11 02:30:34 +02:00
|
|
|
* general-purpose malloc(), pools use a series of large memory "chunks",
|
|
|
|
* each of which is carved into a bunch of smaller "items" or
|
|
|
|
* "allocations".
|
|
|
|
*
|
|
|
|
* To get decent performance, you need to:
|
|
|
|
* - Minimize the number of times you hit the underlying allocator.
|
|
|
|
* - Try to keep accesses as local in memory as possible.
|
|
|
|
* - Try to keep the common case fast.
|
|
|
|
*
|
|
|
|
* Our implementation uses three lists of chunks per pool. Each chunk can
|
|
|
|
* be either "full" (no more room for items); "empty" (no items); or
|
|
|
|
* "used" (not full, not empty). There are independent doubly-linked
|
|
|
|
* lists for each state.
|
|
|
|
*
|
|
|
|
* CREDIT:
|
|
|
|
*
|
|
|
|
* I wrote this after looking at 3 or 4 other pooling allocators, but
|
|
|
|
* without copying. The strategy this most resembles (which is funny,
|
2007-05-25 21:41:31 +02:00
|
|
|
* since that's the one I looked at longest ago) is the pool allocator
|
2007-04-11 02:30:34 +02:00
|
|
|
* underlying Python's obmalloc code. Major differences from obmalloc's
|
|
|
|
* pools are:
|
|
|
|
* - We don't even try to be threadsafe.
|
|
|
|
* - We only handle objects of one size.
|
|
|
|
* - Our list of empty chunks is doubly-linked, not singly-linked.
|
|
|
|
* (This could change pretty easily; it's only doubly-linked for
|
|
|
|
* consistency.)
|
|
|
|
* - We keep a list of full chunks (so we can have a "nuke everything"
|
|
|
|
* function). Obmalloc's pools leave full chunks to float unanchored.
|
|
|
|
*
|
|
|
|
* LIMITATIONS:
|
2007-04-11 02:30:22 +02:00
|
|
|
* - Not even slightly threadsafe.
|
|
|
|
* - Likes to have lots of items per chunks.
|
|
|
|
* - One pointer overhead per allocated thing. (The alternative is
|
|
|
|
* something like glib's use of an RB-tree to keep track of what
|
|
|
|
* chunk any given piece of memory is in.)
|
|
|
|
* - Only aligns allocated things to void* level: redefign ALIGNMENT_TYPE
|
|
|
|
* if you need doubles.
|
|
|
|
* - Could probably be optimized a bit; the representation contains
|
|
|
|
* a bit more info than it really needs to have.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if 1
|
|
|
|
/* Tor dependencies */
|
|
|
|
#include "orconfig.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "compat.h"
|
|
|
|
#include "log.h"
|
|
|
|
#define ALLOC(x) tor_malloc(x)
|
|
|
|
#define FREE(x) tor_free(x)
|
|
|
|
#define ASSERT(x) tor_assert(x)
|
2007-04-11 02:30:34 +02:00
|
|
|
#undef ALLOC_CAN_RETURN_NULL
|
2007-04-19 20:47:04 +02:00
|
|
|
#define TOR
|
2007-12-26 01:12:01 +01:00
|
|
|
//#define ALLOC_ROUNDUP(p) tor_malloc_roundup(p)
|
2007-04-11 02:30:22 +02:00
|
|
|
/* End Tor dependencies */
|
|
|
|
#else
|
2007-04-11 02:30:34 +02:00
|
|
|
/* If you're not building this as part of Tor, you'll want to define the
|
|
|
|
* following macros. For now, these should do as defaults.
|
|
|
|
*/
|
2007-04-11 02:30:22 +02:00
|
|
|
#include <assert.h>
|
|
|
|
#define PREDICT_UNLIKELY(x) (x)
|
|
|
|
#define PREDICT_LIKELY(x) (x)
|
|
|
|
#define ALLOC(x) malloc(x)
|
|
|
|
#define FREE(x) free(x)
|
|
|
|
#define STRUCT_OFFSET(tp, member) \
|
|
|
|
((off_t) (((char*)&((tp*)0)->member)-(char*)0))
|
|
|
|
#define ASSERT(x) assert(x)
|
2007-04-11 02:30:34 +02:00
|
|
|
#define ALLOC_CAN_RETURN_NULL
|
2007-04-11 02:30:22 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Tuning parameters */
|
2007-04-11 02:30:34 +02:00
|
|
|
/** Largest type that we need to ensure returned memory items are aligned to.
|
|
|
|
* Change this to "double" if we need to be safe for structs with doubles. */
|
2007-04-11 02:30:22 +02:00
|
|
|
#define ALIGNMENT_TYPE void *
|
2007-05-25 21:41:31 +02:00
|
|
|
/** Increment that we need to align allocated. */
|
2007-04-11 02:30:34 +02:00
|
|
|
#define ALIGNMENT sizeof(ALIGNMENT_TYPE)
|
|
|
|
/** Largest memory chunk that we should allocate. */
|
2007-04-11 02:30:22 +02:00
|
|
|
#define MAX_CHUNK (8*(1L<<20))
|
2007-04-11 02:30:34 +02:00
|
|
|
/** Smallest memory chunk size that we should allocate. */
|
2007-04-11 02:30:22 +02:00
|
|
|
#define MIN_CHUNK 4096
|
|
|
|
|
2007-04-11 02:30:25 +02:00
|
|
|
typedef struct mp_allocated_t mp_allocated_t;
|
2007-04-11 02:30:34 +02:00
|
|
|
typedef struct mp_chunk_t mp_chunk_t;
|
2007-04-11 02:30:25 +02:00
|
|
|
|
2007-04-11 02:30:34 +02:00
|
|
|
/** Holds a single allocated item, allocated as part of a chunk. */
|
2007-04-11 02:30:22 +02:00
|
|
|
struct mp_allocated_t {
|
2007-04-11 02:30:34 +02:00
|
|
|
/** The chunk that this item is allocated in. This adds overhead to each
|
|
|
|
* allocated item, thus making this implementation inappropriate for
|
|
|
|
* very small items. */
|
2007-04-11 02:30:22 +02:00
|
|
|
mp_chunk_t *in_chunk;
|
|
|
|
union {
|
2007-04-11 02:30:34 +02:00
|
|
|
/** If this item is free, the next item on the free list. */
|
2007-04-11 02:30:22 +02:00
|
|
|
mp_allocated_t *next_free;
|
2007-04-11 02:30:34 +02:00
|
|
|
/** If this item is not free, the actual memory contents of this item.
|
|
|
|
* (Not actual size.) */
|
2007-04-11 02:30:22 +02:00
|
|
|
char mem[1];
|
2007-04-11 02:30:34 +02:00
|
|
|
/** An extra element to the union to insure correct alignment. */
|
2007-04-11 02:30:22 +02:00
|
|
|
ALIGNMENT_TYPE _dummy;
|
2007-04-11 21:58:51 +02:00
|
|
|
} u;
|
2007-04-11 02:30:22 +02:00
|
|
|
};
|
|
|
|
|
2007-04-11 02:30:34 +02:00
|
|
|
/** 'Magic' value used to detect memory corruption. */
|
|
|
|
#define MP_CHUNK_MAGIC 0x09870123
|
|
|
|
|
|
|
|
/** A chunk of memory. Chunks come from malloc; we use them */
|
2007-04-11 02:30:25 +02:00
|
|
|
struct mp_chunk_t {
|
2007-04-11 02:30:34 +02:00
|
|
|
unsigned long magic; /**< Must be MP_CHUNK_MAGIC if this chunk is valid. */
|
|
|
|
mp_chunk_t *next; /**< The next free, used, or full chunk in sequence. */
|
|
|
|
mp_chunk_t *prev; /**< The previous free, used, or full chunk in sequence. */
|
2007-05-25 21:41:31 +02:00
|
|
|
mp_pool_t *pool; /**< The pool that this chunk is part of. */
|
2007-04-11 02:30:34 +02:00
|
|
|
/** First free item in the freelist for this chunk. Note that this may be
|
|
|
|
* NULL even if this chunk is not at capacity: if so, the free memory at
|
|
|
|
* next_mem has not yet been carved into items.
|
|
|
|
*/
|
2007-04-11 02:30:25 +02:00
|
|
|
mp_allocated_t *first_free;
|
2007-05-25 21:41:31 +02:00
|
|
|
int n_allocated; /**< Number of currently allocated items in this chunk. */
|
2007-05-31 21:03:49 +02:00
|
|
|
int capacity; /**< Number of items that can be fit into this chunk. */
|
2007-04-11 02:30:34 +02:00
|
|
|
size_t mem_size; /**< Number of usable bytes in mem. */
|
|
|
|
char *next_mem; /**< Pointer into part of <b>mem</b> not yet carved up. */
|
|
|
|
char mem[1]; /**< Storage for this chunk. (Not actual size.) */
|
2007-04-11 02:30:25 +02:00
|
|
|
};
|
|
|
|
|
2007-04-11 02:30:34 +02:00
|
|
|
/** Number of extra bytes needed beyond mem_size to allocate a chunk. */
|
2007-04-11 02:30:22 +02:00
|
|
|
#define CHUNK_OVERHEAD (sizeof(mp_chunk_t)-1)
|
|
|
|
|
2007-04-11 02:30:34 +02:00
|
|
|
/** Given a pointer to a mp_allocated_t, return a pointer to the memory
|
|
|
|
* item it holds. */
|
2007-04-11 21:58:51 +02:00
|
|
|
#define A2M(a) (&(a)->u.mem)
|
2007-04-11 02:30:34 +02:00
|
|
|
/** Given a pointer to a memory_item_t, return a pointer to its enclosing
|
|
|
|
* mp_allocated_t. */
|
2007-04-11 21:58:51 +02:00
|
|
|
#define M2A(p) ( ((char*)p) - STRUCT_OFFSET(mp_allocated_t, u.mem) )
|
2007-04-11 02:30:22 +02:00
|
|
|
|
2007-04-11 02:30:34 +02:00
|
|
|
#ifdef ALLOC_CAN_RETURN_NULL
|
|
|
|
/** If our ALLOC() macro can return NULL, check whether <b>x</b> is NULL,
|
|
|
|
* and if so, return NULL. */
|
|
|
|
#define CHECK_ALLOC(x) \
|
|
|
|
if (PREDICT_UNLIKELY(!x)) { return NULL; }
|
|
|
|
#else
|
|
|
|
/** If our ALLOC() macro can't return NULL, do nothing. */
|
|
|
|
#define CHECK_ALLOC(x)
|
|
|
|
#endif
|
2007-04-11 02:30:22 +02:00
|
|
|
|
2007-04-11 02:30:34 +02:00
|
|
|
/** Helper: Allocate and return a new memory chunk for <b>pool</b>. Does not
|
|
|
|
* link the chunk into any list. */
|
2007-04-11 02:30:22 +02:00
|
|
|
static mp_chunk_t *
|
|
|
|
mp_chunk_new(mp_pool_t *pool)
|
|
|
|
{
|
|
|
|
size_t sz = pool->new_chunk_capacity * pool->item_alloc_size;
|
2007-12-26 01:12:01 +01:00
|
|
|
#ifdef ALLOC_ROUNDUP
|
|
|
|
size_t alloc_size = CHUNK_OVERHEAD + sz;
|
|
|
|
mp_chunk_t *chunk = ALLOC_ROUNDUP(&alloc_size);
|
|
|
|
#else
|
2007-04-11 02:30:22 +02:00
|
|
|
mp_chunk_t *chunk = ALLOC(CHUNK_OVERHEAD + sz);
|
2008-02-08 22:13:08 +01:00
|
|
|
#endif
|
|
|
|
#ifdef MEMPOOL_STATS
|
|
|
|
++pool->total_chunks_allocated;
|
2007-12-26 01:12:01 +01:00
|
|
|
#endif
|
2007-04-11 02:30:34 +02:00
|
|
|
CHECK_ALLOC(chunk);
|
2007-04-11 02:30:22 +02:00
|
|
|
memset(chunk, 0, sizeof(mp_chunk_t)); /* Doesn't clear the whole thing. */
|
|
|
|
chunk->magic = MP_CHUNK_MAGIC;
|
2007-12-26 01:12:01 +01:00
|
|
|
#ifdef ALLOC_ROUNDUP
|
|
|
|
chunk->mem_size = alloc_size - CHUNK_OVERHEAD;
|
|
|
|
chunk->capacity = chunk->mem_size / pool->item_alloc_size;
|
|
|
|
#else
|
2007-04-11 02:30:22 +02:00
|
|
|
chunk->capacity = pool->new_chunk_capacity;
|
|
|
|
chunk->mem_size = sz;
|
2007-12-26 01:12:01 +01:00
|
|
|
#endif
|
2007-04-11 02:30:22 +02:00
|
|
|
chunk->next_mem = chunk->mem;
|
|
|
|
chunk->pool = pool;
|
|
|
|
return chunk;
|
|
|
|
}
|
|
|
|
|
2008-02-12 21:20:52 +01:00
|
|
|
/** Take a <b>chunk</b> that has just been allocated or removed from
|
|
|
|
* <b>pool</b>'s empty chunk list, and add it to the head of the used chunk
|
|
|
|
* list. */
|
2008-02-08 22:13:08 +01:00
|
|
|
static INLINE void
|
|
|
|
add_newly_used_chunk_to_used_list(mp_pool_t *pool, mp_chunk_t *chunk)
|
|
|
|
{
|
|
|
|
chunk->next = pool->used_chunks;
|
|
|
|
if (chunk->next)
|
|
|
|
chunk->next->prev = chunk;
|
|
|
|
pool->used_chunks = chunk;
|
|
|
|
ASSERT(!chunk->prev);
|
|
|
|
}
|
|
|
|
|
2008-02-09 04:11:10 +01:00
|
|
|
/** Return a newly allocated item from <b>pool</b>. */
|
2007-04-11 02:30:22 +02:00
|
|
|
void *
|
|
|
|
mp_pool_get(mp_pool_t *pool)
|
|
|
|
{
|
|
|
|
mp_chunk_t *chunk;
|
|
|
|
mp_allocated_t *allocated;
|
2007-04-11 02:30:34 +02:00
|
|
|
|
2007-04-11 02:30:22 +02:00
|
|
|
if (PREDICT_LIKELY(pool->used_chunks != NULL)) {
|
2007-04-11 02:30:34 +02:00
|
|
|
/* Common case: there is some chunk that is neither full nor empty. Use
|
|
|
|
* that one. (We can't use the full ones, obviously, and we should fill
|
|
|
|
* up the used ones before we start on any empty ones. */
|
2007-04-11 02:30:22 +02:00
|
|
|
chunk = pool->used_chunks;
|
2007-04-11 02:30:34 +02:00
|
|
|
|
2007-04-11 02:30:22 +02:00
|
|
|
} else if (pool->empty_chunks) {
|
2007-04-11 02:30:34 +02:00
|
|
|
/* We have no used chunks, but we have an empty chunk that we haven't
|
|
|
|
* freed yet: use that. (We pull from the front of the list, which should
|
|
|
|
* get us the most recently emptied chunk.) */
|
2007-04-11 02:30:22 +02:00
|
|
|
chunk = pool->empty_chunks;
|
2007-04-11 02:30:34 +02:00
|
|
|
|
|
|
|
/* Remove the chunk from the empty list. */
|
2007-04-11 02:30:22 +02:00
|
|
|
pool->empty_chunks = chunk->next;
|
|
|
|
if (chunk->next)
|
|
|
|
chunk->next->prev = NULL;
|
2007-04-11 02:30:34 +02:00
|
|
|
|
|
|
|
/* Put the chunk on the 'used' list*/
|
2008-02-08 22:13:08 +01:00
|
|
|
add_newly_used_chunk_to_used_list(pool, chunk);
|
2007-04-11 02:30:34 +02:00
|
|
|
|
2007-04-11 02:30:22 +02:00
|
|
|
ASSERT(!chunk->prev);
|
2007-04-11 02:30:25 +02:00
|
|
|
--pool->n_empty_chunks;
|
2007-04-11 15:18:25 +02:00
|
|
|
if (pool->n_empty_chunks < pool->min_empty_chunks)
|
|
|
|
pool->min_empty_chunks = pool->n_empty_chunks;
|
2007-04-11 02:30:22 +02:00
|
|
|
} else {
|
2007-04-11 02:30:34 +02:00
|
|
|
/* We have no used or empty chunks: allocate a new chunk. */
|
2007-04-11 02:30:22 +02:00
|
|
|
chunk = mp_chunk_new(pool);
|
2007-04-11 02:30:34 +02:00
|
|
|
CHECK_ALLOC(chunk);
|
|
|
|
|
|
|
|
/* Add the new chunk to the used list. */
|
2008-02-08 22:13:08 +01:00
|
|
|
add_newly_used_chunk_to_used_list(pool, chunk);
|
2007-04-11 02:30:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT(chunk->n_allocated < chunk->capacity);
|
|
|
|
|
|
|
|
if (chunk->first_free) {
|
2007-04-11 02:30:34 +02:00
|
|
|
/* If there's anything on the chunk's freelist, unlink it and use it. */
|
2007-04-11 02:30:22 +02:00
|
|
|
allocated = chunk->first_free;
|
2007-04-11 21:58:51 +02:00
|
|
|
chunk->first_free = allocated->u.next_free;
|
|
|
|
allocated->u.next_free = NULL; /* For debugging; not really needed. */
|
2007-04-11 02:30:34 +02:00
|
|
|
ASSERT(allocated->in_chunk == chunk);
|
2007-04-11 02:30:22 +02:00
|
|
|
} else {
|
2007-04-11 02:30:34 +02:00
|
|
|
/* Otherwise, the chunk had better have some free space left on it. */
|
2007-04-11 02:30:25 +02:00
|
|
|
ASSERT(chunk->next_mem + pool->item_alloc_size <=
|
2007-04-11 02:30:22 +02:00
|
|
|
chunk->mem + chunk->mem_size);
|
2007-04-11 02:30:34 +02:00
|
|
|
|
|
|
|
/* Good, it did. Let's carve off a bit of that free space, and use
|
|
|
|
* that. */
|
2007-04-11 02:30:22 +02:00
|
|
|
allocated = (void*)chunk->next_mem;
|
|
|
|
chunk->next_mem += pool->item_alloc_size;
|
|
|
|
allocated->in_chunk = chunk;
|
2007-04-11 21:58:51 +02:00
|
|
|
allocated->u.next_free = NULL; /* For debugging; not really needed. */
|
2007-04-11 02:30:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
++chunk->n_allocated;
|
2008-02-08 22:13:08 +01:00
|
|
|
#ifdef MEMPOOL_STATS
|
|
|
|
++pool->total_items_allocated;
|
|
|
|
#endif
|
2007-04-11 02:30:34 +02:00
|
|
|
|
2007-04-11 02:30:22 +02:00
|
|
|
if (PREDICT_UNLIKELY(chunk->n_allocated == chunk->capacity)) {
|
2007-04-11 02:30:34 +02:00
|
|
|
/* This chunk just became full. */
|
2007-04-11 02:30:22 +02:00
|
|
|
ASSERT(chunk == pool->used_chunks);
|
|
|
|
ASSERT(chunk->prev == NULL);
|
2007-04-11 02:30:34 +02:00
|
|
|
|
|
|
|
/* Take it off the used list. */
|
2007-04-11 02:30:22 +02:00
|
|
|
pool->used_chunks = chunk->next;
|
|
|
|
if (chunk->next)
|
|
|
|
chunk->next->prev = NULL;
|
|
|
|
|
2007-04-11 02:30:34 +02:00
|
|
|
/* Put it on the full list. */
|
2007-04-11 02:30:22 +02:00
|
|
|
chunk->next = pool->full_chunks;
|
2007-04-11 02:30:25 +02:00
|
|
|
if (chunk->next)
|
|
|
|
chunk->next->prev = chunk;
|
2007-04-11 02:30:22 +02:00
|
|
|
pool->full_chunks = chunk;
|
|
|
|
}
|
2007-04-11 02:30:34 +02:00
|
|
|
/* And return the memory portion of the mp_allocated_t. */
|
2007-04-11 02:30:22 +02:00
|
|
|
return A2M(allocated);
|
|
|
|
}
|
|
|
|
|
2007-04-11 02:30:34 +02:00
|
|
|
/** Return an allocated memory item to its memory pool. */
|
2007-04-11 02:30:22 +02:00
|
|
|
void
|
2007-04-11 02:30:34 +02:00
|
|
|
mp_pool_release(void *item)
|
2007-04-11 02:30:22 +02:00
|
|
|
{
|
2007-04-11 02:30:34 +02:00
|
|
|
mp_allocated_t *allocated = (void*) M2A(item);
|
2007-04-11 02:30:22 +02:00
|
|
|
mp_chunk_t *chunk = allocated->in_chunk;
|
|
|
|
|
|
|
|
ASSERT(chunk);
|
|
|
|
ASSERT(chunk->magic == MP_CHUNK_MAGIC);
|
|
|
|
ASSERT(chunk->n_allocated > 0);
|
|
|
|
|
2007-04-11 21:58:51 +02:00
|
|
|
allocated->u.next_free = chunk->first_free;
|
2007-04-11 02:30:22 +02:00
|
|
|
chunk->first_free = allocated;
|
|
|
|
|
|
|
|
if (PREDICT_UNLIKELY(chunk->n_allocated == chunk->capacity)) {
|
|
|
|
/* This chunk was full and is about to be used. */
|
|
|
|
mp_pool_t *pool = chunk->pool;
|
2007-04-11 02:30:34 +02:00
|
|
|
/* unlink from the full list */
|
2007-04-11 02:30:22 +02:00
|
|
|
if (chunk->prev)
|
|
|
|
chunk->prev->next = chunk->next;
|
|
|
|
if (chunk->next)
|
|
|
|
chunk->next->prev = chunk->prev;
|
|
|
|
if (chunk == pool->full_chunks)
|
|
|
|
pool->full_chunks = chunk->next;
|
|
|
|
|
2007-04-11 02:30:34 +02:00
|
|
|
/* link to the used list. */
|
2007-04-11 02:30:22 +02:00
|
|
|
chunk->next = pool->used_chunks;
|
|
|
|
chunk->prev = NULL;
|
|
|
|
if (chunk->next)
|
|
|
|
chunk->next->prev = chunk;
|
|
|
|
pool->used_chunks = chunk;
|
|
|
|
} else if (PREDICT_UNLIKELY(chunk->n_allocated == 1)) {
|
|
|
|
/* This was used and is about to be empty. */
|
|
|
|
mp_pool_t *pool = chunk->pool;
|
2007-04-11 02:30:34 +02:00
|
|
|
|
|
|
|
/* Unlink from the used list */
|
2007-04-11 02:30:22 +02:00
|
|
|
if (chunk->prev)
|
|
|
|
chunk->prev->next = chunk->next;
|
|
|
|
if (chunk->next)
|
|
|
|
chunk->next->prev = chunk->prev;
|
|
|
|
if (chunk == pool->used_chunks)
|
|
|
|
pool->used_chunks = chunk->next;
|
|
|
|
|
2007-04-11 02:30:34 +02:00
|
|
|
/* Link to the empty list */
|
2007-04-11 02:30:22 +02:00
|
|
|
chunk->next = pool->empty_chunks;
|
|
|
|
chunk->prev = NULL;
|
|
|
|
if (chunk->next)
|
|
|
|
chunk->next->prev = chunk;
|
|
|
|
pool->empty_chunks = chunk;
|
|
|
|
|
2007-04-11 02:30:34 +02:00
|
|
|
/* Reset the guts of this chunk to defragment it, in case it gets
|
|
|
|
* used again. */
|
2007-04-11 02:30:22 +02:00
|
|
|
chunk->first_free = NULL;
|
|
|
|
chunk->next_mem = chunk->mem;
|
|
|
|
|
|
|
|
++pool->n_empty_chunks;
|
|
|
|
}
|
|
|
|
--chunk->n_allocated;
|
|
|
|
}
|
|
|
|
|
2007-04-11 02:30:34 +02:00
|
|
|
/** Allocate a new memory pool to hold items of size <b>item_size</b>. We'll
|
2007-04-11 03:27:33 +02:00
|
|
|
* try to fit about <b>chunk_capacity</b> bytes in each chunk. */
|
2007-04-11 02:30:22 +02:00
|
|
|
mp_pool_t *
|
|
|
|
mp_pool_new(size_t item_size, size_t chunk_capacity)
|
|
|
|
{
|
|
|
|
mp_pool_t *pool;
|
2008-02-21 22:57:47 +01:00
|
|
|
size_t alloc_size, new_chunk_cap;
|
2007-04-11 02:30:22 +02:00
|
|
|
|
|
|
|
pool = ALLOC(sizeof(mp_pool_t));
|
2007-04-11 02:30:34 +02:00
|
|
|
CHECK_ALLOC(pool);
|
2007-04-11 02:30:22 +02:00
|
|
|
memset(pool, 0, sizeof(mp_pool_t));
|
|
|
|
|
2007-04-11 02:30:34 +02:00
|
|
|
/* First, we figure out how much space to allow per item. We'll want to
|
|
|
|
* use make sure we have enough for the overhead plus the item size. */
|
2007-06-02 14:44:54 +02:00
|
|
|
alloc_size = (size_t)(STRUCT_OFFSET(mp_allocated_t, u.mem) + item_size);
|
2007-04-11 02:30:34 +02:00
|
|
|
/* If the item_size is less than sizeof(next_free), we need to make
|
|
|
|
* the allocation bigger. */
|
2007-04-11 02:30:22 +02:00
|
|
|
if (alloc_size < sizeof(mp_allocated_t))
|
|
|
|
alloc_size = sizeof(mp_allocated_t);
|
|
|
|
|
2007-04-11 02:30:34 +02:00
|
|
|
/* If we're not an even multiple of ALIGNMENT, round up. */
|
2007-04-11 02:30:22 +02:00
|
|
|
if (alloc_size % ALIGNMENT) {
|
|
|
|
alloc_size = alloc_size + ALIGNMENT - (alloc_size % ALIGNMENT);
|
|
|
|
}
|
|
|
|
if (alloc_size < ALIGNMENT)
|
|
|
|
alloc_size = ALIGNMENT;
|
|
|
|
ASSERT((alloc_size % ALIGNMENT) == 0);
|
|
|
|
|
2007-04-11 02:30:34 +02:00
|
|
|
/* Now we figure out how many items fit in each chunk. We need to fit at
|
|
|
|
* least 2 items per chunk. No chunk can be more than MAX_CHUNK bytes long,
|
|
|
|
* or less than MIN_CHUNK. */
|
2007-04-11 02:30:22 +02:00
|
|
|
if (chunk_capacity > MAX_CHUNK)
|
|
|
|
chunk_capacity = MAX_CHUNK;
|
2007-05-24 19:12:57 +02:00
|
|
|
/* Try to be around a power of 2 in size, since that's what allocators like
|
|
|
|
* handing out. 512K-1 byte is a lot better than 512K+1 byte. */
|
|
|
|
chunk_capacity = (size_t) round_to_power_of_2(chunk_capacity);
|
|
|
|
while (chunk_capacity < alloc_size * 2 + CHUNK_OVERHEAD)
|
|
|
|
chunk_capacity *= 2;
|
2007-04-11 02:30:34 +02:00
|
|
|
if (chunk_capacity < MIN_CHUNK)
|
2007-04-11 02:30:22 +02:00
|
|
|
chunk_capacity = MIN_CHUNK;
|
|
|
|
|
2008-02-21 22:57:47 +01:00
|
|
|
new_chunk_cap = (chunk_capacity-CHUNK_OVERHEAD) / alloc_size;
|
|
|
|
tor_assert(new_chunk_cap < INT_MAX);
|
|
|
|
pool->new_chunk_capacity = (int)new_chunk_cap;
|
|
|
|
|
2007-04-11 02:30:22 +02:00
|
|
|
pool->item_alloc_size = alloc_size;
|
|
|
|
|
2007-05-24 19:12:57 +02:00
|
|
|
log_debug(LD_MM, "Capacity is %lu, item size is %lu, alloc size is %lu",
|
|
|
|
(unsigned long)pool->new_chunk_capacity,
|
|
|
|
(unsigned long)pool->item_alloc_size,
|
|
|
|
(unsigned long)(pool->new_chunk_capacity*pool->item_alloc_size));
|
|
|
|
|
2007-04-11 02:30:22 +02:00
|
|
|
return pool;
|
|
|
|
}
|
|
|
|
|
2008-02-08 22:13:08 +01:00
|
|
|
#ifdef LAZY_CHUNK_SORT
|
2008-02-12 21:20:52 +01:00
|
|
|
/** Helper function for qsort: used to sort pointers to mp_chunk_t into
|
|
|
|
* descending order of fullness. */
|
2008-02-08 22:13:08 +01:00
|
|
|
static int
|
|
|
|
mp_pool_sort_used_chunks_helper(const void *_a, const void *_b)
|
|
|
|
{
|
|
|
|
mp_chunk_t *a = *(mp_chunk_t**)_a;
|
|
|
|
mp_chunk_t *b = *(mp_chunk_t**)_b;
|
|
|
|
return b->n_allocated - a->n_allocated;
|
|
|
|
}
|
|
|
|
|
2008-02-12 21:20:52 +01:00
|
|
|
/** Sort the used chunks in <b>pool</b> into descending order of fullness,
|
|
|
|
* so that we preferentially fill up mostly full chunks before we make
|
|
|
|
* nearly empty chunks less nearly empty. */
|
2008-02-08 22:13:08 +01:00
|
|
|
static void
|
|
|
|
mp_pool_sort_used_chunks(mp_pool_t *pool)
|
|
|
|
{
|
|
|
|
int i, n=0, inverted=0;
|
|
|
|
mp_chunk_t **chunks, *chunk;
|
|
|
|
for (chunk = pool->used_chunks; chunk; chunk = chunk->next) {
|
|
|
|
++n;
|
|
|
|
if (chunk->next && chunk->next->n_allocated > chunk->n_allocated)
|
|
|
|
++inverted;
|
|
|
|
}
|
|
|
|
if (!inverted)
|
|
|
|
return;
|
|
|
|
//printf("Sort %d/%d\n",inverted,n);
|
|
|
|
chunks = ALLOC(sizeof(mp_chunk_t *)*n);
|
|
|
|
#ifdef ALLOC_CAN_RETURN_NULL
|
|
|
|
if (PREDICT_UNLIKELY(!chunks)) return;
|
|
|
|
#endif
|
|
|
|
for (i=0,chunk = pool->used_chunks; chunk; chunk = chunk->next)
|
|
|
|
chunks[i++] = chunk;
|
|
|
|
qsort(chunks, n, sizeof(mp_chunk_t *), mp_pool_sort_used_chunks_helper);
|
|
|
|
pool->used_chunks = chunks[0];
|
|
|
|
chunks[0]->prev = NULL;
|
|
|
|
for (i=1;i<n;++i) {
|
|
|
|
chunks[i-1]->next = chunks[i];
|
|
|
|
chunks[i]->prev = chunks[i-1];
|
|
|
|
}
|
|
|
|
chunks[n-1]->next = NULL;
|
|
|
|
FREE(chunks);
|
|
|
|
#if 0
|
|
|
|
inverted = 0;
|
|
|
|
for (chunk = pool->used_chunks; chunk; chunk = chunk->next) {
|
|
|
|
if (chunk->next) {
|
|
|
|
ASSERT(chunk->next->n_allocated <= chunk->n_allocated);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
mp_pool_assert_ok(pool);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-04-11 02:30:34 +02:00
|
|
|
/** If there are more than <b>n</b> empty chunks in <b>pool</b>, free the
|
2008-02-12 21:20:52 +01:00
|
|
|
* excess ones that have been empty for the longest. If
|
|
|
|
* <b>keep_recently_used</b> is true, do not free chunks unless they have been
|
|
|
|
* empty since the last call to this function.
|
2008-02-08 22:13:08 +01:00
|
|
|
**/
|
2007-04-11 02:30:22 +02:00
|
|
|
void
|
2008-02-08 22:13:08 +01:00
|
|
|
mp_pool_clean(mp_pool_t *pool, int n_to_keep, int keep_recently_used)
|
2007-04-11 02:30:22 +02:00
|
|
|
{
|
2007-04-11 02:30:34 +02:00
|
|
|
mp_chunk_t *chunk, **first_to_free;
|
2008-02-08 22:13:08 +01:00
|
|
|
|
|
|
|
#ifdef LAZY_CHUNK_SORT
|
|
|
|
mp_pool_sort_used_chunks(pool);
|
|
|
|
#endif
|
2008-02-12 21:20:52 +01:00
|
|
|
ASSERT(n_to_keep >= 0);
|
2008-02-08 22:13:08 +01:00
|
|
|
|
|
|
|
if (keep_recently_used) {
|
|
|
|
int n_recently_used = pool->n_empty_chunks - pool->min_empty_chunks;
|
|
|
|
if (n_to_keep < n_recently_used)
|
|
|
|
n_to_keep = n_recently_used;
|
2007-04-11 15:18:25 +02:00
|
|
|
}
|
2008-02-08 22:13:08 +01:00
|
|
|
|
|
|
|
ASSERT(n_to_keep >= 0);
|
2007-04-11 15:18:25 +02:00
|
|
|
|
2007-04-11 02:30:34 +02:00
|
|
|
first_to_free = &pool->empty_chunks;
|
2008-02-08 22:13:08 +01:00
|
|
|
while (*first_to_free && n_to_keep > 0) {
|
2007-04-11 02:30:34 +02:00
|
|
|
first_to_free = &(*first_to_free)->next;
|
2008-02-08 22:13:08 +01:00
|
|
|
--n_to_keep;
|
2007-04-11 02:30:34 +02:00
|
|
|
}
|
2008-02-08 22:13:08 +01:00
|
|
|
if (!*first_to_free) {
|
|
|
|
pool->min_empty_chunks = pool->n_empty_chunks;
|
2007-04-11 02:30:34 +02:00
|
|
|
return;
|
2008-02-08 22:13:08 +01:00
|
|
|
}
|
2007-04-11 02:30:34 +02:00
|
|
|
|
|
|
|
chunk = *first_to_free;
|
|
|
|
while (chunk) {
|
|
|
|
mp_chunk_t *next = chunk->next;
|
|
|
|
chunk->magic = 0xdeadbeef;
|
|
|
|
FREE(chunk);
|
2008-02-08 22:13:08 +01:00
|
|
|
#ifdef MEMPOOL_STATS
|
|
|
|
++pool->total_chunks_freed;
|
|
|
|
#endif
|
2007-04-11 02:30:34 +02:00
|
|
|
--pool->n_empty_chunks;
|
|
|
|
chunk = next;
|
2007-04-11 02:30:22 +02:00
|
|
|
}
|
2007-04-11 02:30:34 +02:00
|
|
|
|
2008-02-08 22:13:08 +01:00
|
|
|
pool->min_empty_chunks = pool->n_empty_chunks;
|
2007-04-11 02:30:34 +02:00
|
|
|
*first_to_free = NULL;
|
2007-04-11 02:30:22 +02:00
|
|
|
}
|
|
|
|
|
2007-04-11 02:30:34 +02:00
|
|
|
/** Helper: Given a list of chunks, free all the chunks in the list. */
|
2007-04-11 02:30:22 +02:00
|
|
|
static void
|
|
|
|
destroy_chunks(mp_chunk_t *chunk)
|
|
|
|
{
|
|
|
|
mp_chunk_t *next;
|
|
|
|
while (chunk) {
|
|
|
|
chunk->magic = 0xd3adb33f;
|
|
|
|
next = chunk->next;
|
|
|
|
FREE(chunk);
|
|
|
|
chunk = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-11 02:30:34 +02:00
|
|
|
/** Free all space held in <b>pool</b> This makes all pointers returned from
|
|
|
|
* mp_pool_get(<b>pool</b>) invalid. */
|
2007-04-11 02:30:22 +02:00
|
|
|
void
|
|
|
|
mp_pool_destroy(mp_pool_t *pool)
|
|
|
|
{
|
|
|
|
destroy_chunks(pool->empty_chunks);
|
|
|
|
destroy_chunks(pool->used_chunks);
|
|
|
|
destroy_chunks(pool->full_chunks);
|
|
|
|
memset(pool, 0xe0, sizeof(mp_pool_t));
|
|
|
|
FREE(pool);
|
|
|
|
}
|
|
|
|
|
2007-04-11 02:30:34 +02:00
|
|
|
/** Helper: make sure that a given chunk list is not corrupt. */
|
2007-04-11 02:30:25 +02:00
|
|
|
static int
|
|
|
|
assert_chunks_ok(mp_pool_t *pool, mp_chunk_t *chunk, int empty, int full)
|
|
|
|
{
|
|
|
|
mp_allocated_t *allocated;
|
|
|
|
int n = 0;
|
|
|
|
if (chunk)
|
|
|
|
ASSERT(chunk->prev == NULL);
|
|
|
|
|
|
|
|
while (chunk) {
|
|
|
|
n++;
|
|
|
|
ASSERT(chunk->magic == MP_CHUNK_MAGIC);
|
|
|
|
ASSERT(chunk->pool == pool);
|
|
|
|
for (allocated = chunk->first_free; allocated;
|
2007-04-11 21:58:51 +02:00
|
|
|
allocated = allocated->u.next_free) {
|
2007-04-11 02:30:25 +02:00
|
|
|
ASSERT(allocated->in_chunk == chunk);
|
|
|
|
}
|
|
|
|
if (empty)
|
|
|
|
ASSERT(chunk->n_allocated == 0);
|
|
|
|
else if (full)
|
|
|
|
ASSERT(chunk->n_allocated == chunk->capacity);
|
|
|
|
else
|
|
|
|
ASSERT(chunk->n_allocated > 0 && chunk->n_allocated < chunk->capacity);
|
|
|
|
|
|
|
|
ASSERT(chunk->capacity == pool->new_chunk_capacity);
|
|
|
|
|
|
|
|
ASSERT(chunk->mem_size ==
|
|
|
|
pool->new_chunk_capacity * pool->item_alloc_size);
|
|
|
|
|
|
|
|
ASSERT(chunk->next_mem >= chunk->mem &&
|
|
|
|
chunk->next_mem <= chunk->mem + chunk->mem_size);
|
|
|
|
|
|
|
|
if (chunk->next)
|
|
|
|
ASSERT(chunk->next->prev == chunk);
|
|
|
|
|
|
|
|
chunk = chunk->next;
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2007-04-11 02:30:34 +02:00
|
|
|
/** Fail with an assertion if <b>pool</b> is not internally consistent. */
|
2007-04-11 02:30:25 +02:00
|
|
|
void
|
|
|
|
mp_pool_assert_ok(mp_pool_t *pool)
|
|
|
|
{
|
|
|
|
int n_empty;
|
|
|
|
|
|
|
|
n_empty = assert_chunks_ok(pool, pool->empty_chunks, 1, 0);
|
|
|
|
assert_chunks_ok(pool, pool->full_chunks, 0, 1);
|
|
|
|
assert_chunks_ok(pool, pool->used_chunks, 0, 0);
|
|
|
|
|
|
|
|
ASSERT(pool->n_empty_chunks == n_empty);
|
|
|
|
}
|
|
|
|
|
2007-04-19 20:47:04 +02:00
|
|
|
#ifdef TOR
|
2007-05-29 19:31:13 +02:00
|
|
|
/** Dump information about <b>pool</b>'s memory usage to the Tor log at level
|
|
|
|
* <b>severity</b>. */
|
2007-04-19 20:47:04 +02:00
|
|
|
/*FFFF uses Tor logging functions. */
|
|
|
|
void
|
|
|
|
mp_pool_log_status(mp_pool_t *pool, int severity)
|
|
|
|
{
|
|
|
|
uint64_t bytes_used = 0;
|
|
|
|
uint64_t bytes_allocated = 0;
|
|
|
|
uint64_t bu = 0, ba = 0;
|
|
|
|
mp_chunk_t *chunk;
|
|
|
|
int n_full = 0, n_used = 0;
|
|
|
|
|
|
|
|
ASSERT(pool);
|
|
|
|
|
|
|
|
for (chunk = pool->empty_chunks; chunk; chunk = chunk->next) {
|
|
|
|
bytes_allocated += chunk->mem_size;
|
|
|
|
}
|
|
|
|
log_fn(severity, LD_MM, U64_FORMAT" bytes in %d empty chunks",
|
2008-02-11 02:52:24 +01:00
|
|
|
U64_PRINTF_ARG(bytes_allocated), pool->n_empty_chunks);
|
2007-04-19 20:47:04 +02:00
|
|
|
for (chunk = pool->used_chunks; chunk; chunk = chunk->next) {
|
|
|
|
++n_used;
|
|
|
|
bu += chunk->n_allocated * pool->item_alloc_size;
|
|
|
|
ba += chunk->mem_size;
|
2008-02-08 22:13:08 +01:00
|
|
|
log_fn(severity, LD_MM, " used chunk: %d items allocated",
|
|
|
|
chunk->n_allocated);
|
2007-04-19 20:47:04 +02:00
|
|
|
}
|
|
|
|
log_fn(severity, LD_MM, U64_FORMAT"/"U64_FORMAT
|
|
|
|
" bytes in %d partially full chunks",
|
|
|
|
U64_PRINTF_ARG(bu), U64_PRINTF_ARG(ba), n_used);
|
|
|
|
bytes_used += bu;
|
2007-04-19 21:52:30 +02:00
|
|
|
bytes_allocated += ba;
|
2007-04-19 20:47:04 +02:00
|
|
|
bu = ba = 0;
|
|
|
|
for (chunk = pool->full_chunks; chunk; chunk = chunk->next) {
|
|
|
|
++n_full;
|
|
|
|
bu += chunk->n_allocated * pool->item_alloc_size;
|
|
|
|
ba += chunk->mem_size;
|
|
|
|
}
|
|
|
|
log_fn(severity, LD_MM, U64_FORMAT"/"U64_FORMAT
|
|
|
|
" bytes in %d full chunks",
|
|
|
|
U64_PRINTF_ARG(bu), U64_PRINTF_ARG(ba), n_full);
|
|
|
|
bytes_used += bu;
|
2007-04-19 21:52:30 +02:00
|
|
|
bytes_allocated += ba;
|
2007-04-19 20:47:04 +02:00
|
|
|
|
|
|
|
log_fn(severity, LD_MM, "Total: "U64_FORMAT"/"U64_FORMAT" bytes allocated "
|
|
|
|
"for cell pools are full.",
|
|
|
|
U64_PRINTF_ARG(bytes_used), U64_PRINTF_ARG(bytes_allocated));
|
2008-02-08 22:13:08 +01:00
|
|
|
|
|
|
|
#ifdef MEMPOOL_STATS
|
|
|
|
log_fn(severity, LD_MM, U64_FORMAT" cell allocations ever; "
|
|
|
|
U64_FORMAT" chunk allocations ever; "
|
|
|
|
U64_FORMAT" chunk frees ever.",
|
|
|
|
U64_PRINTF_ARG(pool->total_items_allocated),
|
|
|
|
U64_PRINTF_ARG(pool->total_chunks_allocated),
|
|
|
|
U64_PRINTF_ARG(pool->total_chunks_freed));
|
|
|
|
#endif
|
2007-04-19 20:47:04 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|