2007-10-12 23:50:05 +00:00
|
|
|
#ifndef _TOKU_MEMPOOL_H
|
|
|
|
#define _TOKU_MEMPOOL_H
|
|
|
|
|
2007-11-29 14:18:54 +00:00
|
|
|
#ident "Copyright (c) 2007 Tokutek Inc. All rights reserved."
|
|
|
|
|
2007-10-10 19:33:31 +00:00
|
|
|
/* a memory pool is a contiguous region of memory that supports single
|
|
|
|
allocations from the pool. these allocated regions are never recycled.
|
|
|
|
when the memory pool no longer has free space, the allocated chunks
|
|
|
|
must be relocated by the application to a new memory pool. */
|
|
|
|
|
2008-04-02 23:40:36 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
2007-10-12 23:50:05 +00:00
|
|
|
struct mempool;
|
|
|
|
|
2007-10-10 19:33:31 +00:00
|
|
|
struct mempool {
|
|
|
|
void *base; /* the base address of the memory */
|
2008-04-02 23:40:36 +00:00
|
|
|
size_t free_offset; /* the offset of the memory pool free space */
|
|
|
|
size_t size; /* the size of the memory */
|
|
|
|
size_t frag_size; /* the size of the fragmented memory */
|
2007-10-10 19:33:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* initialize the memory pool with the base address and size of a
|
|
|
|
contiguous chunk of memory */
|
2008-04-04 18:22:01 +00:00
|
|
|
void toku_mempool_init(struct mempool *mp, void *base, size_t size);
|
2007-10-10 19:33:31 +00:00
|
|
|
|
|
|
|
/* finalize the memory pool */
|
2007-11-28 20:51:16 +00:00
|
|
|
void toku_mempool_fini(struct mempool *mp);
|
2007-10-10 19:33:31 +00:00
|
|
|
|
2007-10-12 23:50:05 +00:00
|
|
|
/* get the base address of the memory pool */
|
2007-11-28 20:51:16 +00:00
|
|
|
void *toku_mempool_get_base(struct mempool *mp);
|
2007-10-12 23:50:05 +00:00
|
|
|
|
|
|
|
/* get the size of the memory pool */
|
2013-04-16 23:57:18 -04:00
|
|
|
size_t toku_mempool_get_size(struct mempool *mp);
|
2007-10-10 19:33:31 +00:00
|
|
|
|
|
|
|
/* get the amount of fragmented space in the memory pool */
|
2013-04-16 23:57:18 -04:00
|
|
|
size_t toku_mempool_get_frag_size(struct mempool *mp);
|
2007-10-10 19:33:31 +00:00
|
|
|
|
|
|
|
/* allocate a chunk of memory from the memory pool suitably aligned */
|
2008-04-02 23:40:36 +00:00
|
|
|
void *toku_mempool_malloc(struct mempool *mp, size_t size, int alignment);
|
2007-10-10 19:33:31 +00:00
|
|
|
|
|
|
|
/* free a previously allocated chunk of memory. the free only updates
|
|
|
|
a count of the amount of free space in the memory pool. the memory
|
|
|
|
pool does not keep track of the locations of the free chunks */
|
2013-04-16 23:57:18 -04:00
|
|
|
void toku_mempool_mfree(struct mempool *mp, void *vp, size_t size);
|
2007-10-12 23:50:05 +00:00
|
|
|
|
2008-07-30 19:36:37 +00:00
|
|
|
/* verify that a memory range is contained within a mempool */
|
2013-04-16 23:57:18 -04:00
|
|
|
static inline int toku_mempool_inrange(struct mempool *mp, void *vp, size_t size) {
|
2013-04-16 23:57:19 -04:00
|
|
|
return (mp->base <= vp) && ((char *)vp + size <= (char *)mp->base + mp->size);
|
2007-11-14 17:58:38 +00:00
|
|
|
}
|
|
|
|
|
2007-10-12 23:50:05 +00:00
|
|
|
#endif
|