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-04-11 02:30:22 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \file util.h
|
|
|
|
* \brief Headers for mempool.c
|
|
|
|
**/
|
|
|
|
|
|
|
|
#ifndef MEMPOOL_H
|
|
|
|
#define MEMPOOL_H
|
|
|
|
|
2007-04-11 02:30:34 +02:00
|
|
|
/** A memory pool is a context in which a large number of fixed-sized
|
|
|
|
* objects can be allocated efficiently. See mempool.c for implementation
|
|
|
|
* details. */
|
2007-04-11 02:30:22 +02:00
|
|
|
typedef struct mp_pool_t mp_pool_t;
|
|
|
|
|
|
|
|
void *mp_pool_get(mp_pool_t *pool);
|
|
|
|
void mp_pool_release(void *item);
|
2007-04-11 03:27:33 +02:00
|
|
|
mp_pool_t *mp_pool_new(size_t item_size, size_t chunk_capacity);
|
2008-02-08 22:13:08 +01:00
|
|
|
void mp_pool_clean(mp_pool_t *pool, int n_to_keep, int keep_recently_used);
|
2007-04-11 02:30:22 +02:00
|
|
|
void mp_pool_destroy(mp_pool_t *pool);
|
2007-04-11 02:30:25 +02:00
|
|
|
void mp_pool_assert_ok(mp_pool_t *pool);
|
2007-04-19 20:47:04 +02:00
|
|
|
void mp_pool_log_status(mp_pool_t *pool, int severity);
|
2007-04-11 02:30:22 +02:00
|
|
|
|
2008-02-08 22:13:08 +01:00
|
|
|
#define MEMPOOL_STATS
|
|
|
|
|
2007-04-11 02:30:22 +02:00
|
|
|
#ifdef MEMPOOL_PRIVATE
|
2007-04-11 02:30:34 +02:00
|
|
|
/* These declarations are only used by mempool.c and test.c */
|
2007-04-11 02:30:22 +02:00
|
|
|
|
|
|
|
struct mp_pool_t {
|
2007-04-11 02:30:34 +02:00
|
|
|
/** Doubly-linked list of chunks in which no items have been allocated.
|
|
|
|
* The front of the list is the most recently emptied chunk. */
|
|
|
|
struct mp_chunk_t *empty_chunks;
|
|
|
|
/** Doubly-linked list of chunks in which some items have been allocated,
|
|
|
|
* but which are not yet full. The front of the list is the chunk that has
|
|
|
|
* most recently been modified. */
|
|
|
|
struct mp_chunk_t *used_chunks;
|
|
|
|
/** Doubly-linked list of chunks in which no more items can be allocated.
|
|
|
|
* The front of the list is the chunk that has most recently become full. */
|
|
|
|
struct mp_chunk_t *full_chunks;
|
|
|
|
/** Length of <b>empty_chunks</b>. */
|
2007-04-11 02:30:22 +02:00
|
|
|
int n_empty_chunks;
|
2007-04-11 15:18:25 +02:00
|
|
|
/** Lowest value of <b>empty_chunks</b> since last call to
|
|
|
|
* mp_pool_clean(-1). */
|
|
|
|
int min_empty_chunks;
|
2007-04-11 02:30:34 +02:00
|
|
|
/** Size of each chunk (in items). */
|
2007-04-11 02:30:25 +02:00
|
|
|
int new_chunk_capacity;
|
2007-04-11 02:30:34 +02:00
|
|
|
/** Size to allocate for each item, including overhead and alignment
|
|
|
|
* padding. */
|
2007-04-11 02:30:22 +02:00
|
|
|
size_t item_alloc_size;
|
2008-02-08 22:13:08 +01:00
|
|
|
#ifdef MEMPOOL_STATS
|
2008-02-09 04:11:10 +01:00
|
|
|
/** Total number of items allocated ever. */
|
2008-02-08 22:13:08 +01:00
|
|
|
uint64_t total_items_allocated;
|
2008-02-09 04:11:10 +01:00
|
|
|
/** Total number of chunks allocated ever. */
|
2008-02-08 22:13:08 +01:00
|
|
|
uint64_t total_chunks_allocated;
|
2008-02-09 04:11:10 +01:00
|
|
|
/** Total number of chunks freed ever. */
|
2008-02-08 22:13:08 +01:00
|
|
|
uint64_t total_chunks_freed;
|
|
|
|
#endif
|
2007-04-11 02:30:22 +02:00
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|