gwion-util
utilities for the Gwion project
Loading...
Searching...
No Matches
Data Structures | Macros | Typedefs | Functions
mpool.h File Reference

memory pool More...

Go to the source code of this file.

Data Structures

struct  MemPool_
 

Macros

#define mp_malloc(p, name)   _mp_malloc(p, sizeof(struct name##_))
 
#define mp_calloc(p, name)   _mp_calloc(p, sizeof(struct name##_))
 
#define mp_free(p, name, a)   _mp_free(p, sizeof(struct name##_), (a))
 
#define mp_free2(p, sz, a)   _mp_free(p, sz, (a))
 
#define mp_calloc2(p, sz)   _mp_calloc(p, sz)
 
#define mp_malloc2(p, sz)   _mp_calloc(p, sz)
 

Typedefs

typedef struct MemPool_MemPool
 

Functions

ANN struct poolnew_pool (const uint32_t elementSize)
 
ANN struct poolmp_ini (MemPool p, const uint32_t elementSize)
 
ANN void mp_end (struct pool *)
 
ANEW ANN void * _mp_calloc2 (struct pool *, const bool) __attribute__((hot))
 
ANEW ANN void * _mp_calloc (MemPool, const m_uint) __attribute__((hot))
 
ANEW ANN void * _mp_malloc (MemPool, const m_uint) __attribute__((hot))
 
ANN void _mp_free (MemPool, const m_uint, void *)
 
ANN void _mp_free2 (struct pool *, void *)
 
void * mp_realloc (MemPool mp, void *ptr, const m_uint curr, const m_uint next)
 
MemPool mempool_ini (const size_t sz)
 
void mempool_end (MemPool mp)
 

Detailed Description

memory pool

Definition in file mpool.h.

Macro Definition Documentation

◆ mp_calloc

#define mp_calloc ( p,
name )   _mp_calloc(p, sizeof(struct name##_))

Definition at line 26 of file mpool.h.

◆ mp_calloc2

#define mp_calloc2 ( p,
sz )   _mp_calloc(p, sz)

Definition at line 29 of file mpool.h.

◆ mp_free

#define mp_free ( p,
name,
a )   _mp_free(p, sizeof(struct name##_), (a))

Definition at line 27 of file mpool.h.

◆ mp_free2

#define mp_free2 ( p,
sz,
a )   _mp_free(p, sz, (a))

Definition at line 28 of file mpool.h.

◆ mp_malloc

#define mp_malloc ( p,
name )   _mp_malloc(p, sizeof(struct name##_))

Definition at line 25 of file mpool.h.

◆ mp_malloc2

#define mp_malloc2 ( p,
sz )   _mp_calloc(p, sz)

Definition at line 30 of file mpool.h.

Typedef Documentation

◆ MemPool

typedef struct MemPool_ * MemPool

Function Documentation

◆ _mp_calloc()

ANEW ANN void * _mp_calloc ( MemPool ,
const m_uint  )

◆ _mp_calloc2()

ANEW ANN void * _mp_calloc2 ( struct pool * p,
const bool zero )

Definition at line 106 of file mpool.c.

106 {
107 gwt_lock(&p->mutex);
108 void *ret = __mp_calloc2(p, zero);
109 gwt_unlock(&p->mutex);
110 return ret;
111}
static void * __mp_calloc2(struct pool *p, const bool zero)
Definition mpool.c:92
gwtlock_t mutex
Definition mpool.c:17
static ANN int gwt_unlock(gwtlock_t *lock)
Definition threadpool.h:74
static ANN int gwt_lock(gwtlock_t *lock)
Definition threadpool.h:71

◆ _mp_free()

ANN void _mp_free ( MemPool mp,
const m_uint size,
void * ptr )

Definition at line 124 of file mpool.c.

124 {
125 struct pool *p = mp_ini(mp, size);
126 if (p)
127 _mp_free2(p, ptr);
128 else
129 xfree(ptr);
130}
ANN struct pool * mp_ini(MemPool mp, const uint32_t obj_sz)
Definition mpool.c:66
void _mp_free2(struct pool *p, void *ptr)
Definition mpool.c:113
Definition mpool.c:15
#define xfree(a)
Definition xalloc.h:28

◆ _mp_free2()

ANN void _mp_free2 ( struct pool * p,
void * ptr )

Definition at line 113 of file mpool.c.

113 {
114 gwt_lock(&p->mutex);
115 volatile struct Recycle *next = p->next;
116#ifdef POOL_CHECK
117 memset(ptr, 0, p->obj_sz);
118#endif
119 p->next = ptr;
120 p->next->next = next;
121 gwt_unlock(&p->mutex);
122}
volatile struct Recycle * next
Definition mpool.c:12
uint32_t obj_sz
Definition mpool.c:19
volatile struct Recycle * next
Definition mpool.c:18

◆ _mp_malloc()

ANEW ANN void * _mp_malloc ( MemPool ,
const m_uint  )

◆ mempool_end()

void mempool_end ( MemPool mp)

Definition at line 45 of file mpool.c.

45 {
47 for (m_uint i = mp->sz + 1; --i;) {
48 struct pool *p = mp->pools[i - 1];
49 if (p) mp_end(p);
50 }
51 xfree(mp->sizes);
53 xfree(mp->master_pool);
54 xfree(mp->pools);
55 xfree(mp);
56}
#define LOOP_OPTIM
Definition defs.h:34
uintptr_t m_uint
Definition gwcommon.h:11
void mp_end(struct pool *p)
Definition mpool.c:74
size_t * sizes
Definition mpool.h:11
struct pool ** pools
Definition mpool.h:10
struct pool * master_pool
Definition mpool.h:9
size_t sz
Definition mpool.h:12

◆ mempool_ini()

MemPool mempool_ini ( const size_t sz)

Definition at line 35 of file mpool.c.

35 {
36 MemPool p = (MemPool)xmalloc(sizeof(struct MemPool_));
37 p->master_pool = new_pool(sizeof(struct pool));
38 p->sizes = xmalloc((log2(sz) - 1) * sizeof(size_t));
39 p->sz = 0;
40 for (size_t j = SZ_INT, k = 0; sz >= k; k = j, j <<= 1) p->sizes[p->sz++] = j;
41 p->pools = (struct pool **)xcalloc(p->sz, sizeof(struct pool *));
42 return p;
43}
#define SZ_INT
Definition gwcommon.h:18
struct pool * new_pool(const uint32_t obj_sz)
Definition mpool.c:132
struct MemPool_ * MemPool
static ANEW void * xcalloc(const m_uint n, const m_uint sz)
Definition xalloc.h:18
static ANEW void * xmalloc(const m_uint sz)
Definition xalloc.h:13

◆ mp_end()

ANN void mp_end ( struct pool * p)

Definition at line 74 of file mpool.c.

74 {
76 for (uint32_t i = 0; i < p->nblk && p->data[i]; ++i) xfree(p->data[i]);
77 xfree(p->data);
78}
uint8_t ** data
Definition mpool.c:16
uint32_t nblk
Definition mpool.c:22
static ANN void gwt_lock_end(gwtlock_t *lock)
Definition threadpool.h:95

◆ mp_ini()

ANN struct pool * mp_ini ( MemPool p,
const uint32_t elementSize )

Definition at line 66 of file mpool.c.

66 {
67 for (size_t i = 0; i < mp->sz; ++i) {
68 if (obj_sz <= mp->sizes[i])
69 return mp->pools[i] ?: mp_create(mp, mp->sizes[i], i);
70 }
71 return NULL;
72}
static struct pool * mp_create(MemPool mp, const uint32_t obj_sz, const uint32_t idx)
Definition mpool.c:58

◆ mp_realloc()

void * mp_realloc ( MemPool mp,
void * ptr,
const m_uint curr,
const m_uint next )

Definition at line 148 of file mpool.c.

148 {
149 void *ret = _mp_malloc(mp, next);
150 if (ret != ptr) memcpy(ret, ptr, curr);
151 mp_free2(mp, curr, ptr);
152 return ret;
153}
#define mp_free2(p, sz, a)
Definition mpool.h:28
ANEW ANN void * _mp_malloc(MemPool, const m_uint) __attribute__((hot))

◆ new_pool()

ANN struct pool * new_pool ( const uint32_t elementSize)

Definition at line 132 of file mpool.c.

132 {
133 struct pool *p = (struct pool *)xmalloc(sizeof(struct pool));
134 mp_set(p, obj_sz);
135 return p;
136}
static ANN void mp_set(struct pool *p, const uint32_t obj_sz)
Definition mpool.c:25