gwion-util
utilities for the Gwion project
Loading...
Searching...
No Matches
Data Structures | Macros | Functions
mpool.c File Reference
#include "gwion_util.h"

Go to the source code of this file.

Data Structures

struct  Recycle
 
struct  pool
 

Macros

#define SMALL_BLK   512
 
#define BIG_BLK   16
 
#define MPHUGE   (64 * SZ_INT)
 
#define BLK(obj_sz)   (obj_sz < MPHUGE ? SMALL_BLK : BIG_BLK)
 
#define MP_ALLOC(name, zero, default)
 

Functions

static ANN void mp_set (struct pool *p, const uint32_t obj_sz)
 
MemPool mempool_ini (const size_t sz)
 
void mempool_end (MemPool mp)
 
static struct poolmp_create (MemPool mp, const uint32_t obj_sz, const uint32_t idx)
 
ANN struct poolmp_ini (MemPool mp, const uint32_t obj_sz)
 
void mp_end (struct pool *p)
 
static void _realloc (struct pool *p)
 
static void * __mp_calloc2 (struct pool *p, const bool zero)
 
void * _mp_calloc2 (struct pool *p, const bool zero)
 
void _mp_free2 (struct pool *p, void *ptr)
 
void _mp_free (MemPool mp, const m_uint size, void *ptr)
 
struct poolnew_pool (const uint32_t obj_sz)
 
void * mp_realloc (MemPool mp, void *ptr, const m_uint curr, const m_uint next)
 

Macro Definition Documentation

◆ BIG_BLK

#define BIG_BLK   16

Definition at line 7 of file mpool.c.

◆ BLK

#define BLK ( obj_sz)    (obj_sz < MPHUGE ? SMALL_BLK : BIG_BLK)

Definition at line 9 of file mpool.c.

◆ MP_ALLOC

#define MP_ALLOC ( name,
zero,
default )
Value:
void *_mp_##name(MemPool mp, const m_uint size) { \
struct pool *p = mp_ini(mp, size); \
void * ret = p ? _mp_calloc2(p, zero) : (void *)default; \
return ret; \
}
uintptr_t m_uint
Definition gwcommon.h:11
ANN struct pool * mp_ini(MemPool mp, const uint32_t obj_sz)
Definition mpool.c:66
void * _mp_calloc2(struct pool *p, const bool zero)
Definition mpool.c:106
Definition mpool.c:15

Definition at line 138 of file mpool.c.

138#define MP_ALLOC(name, zero, default) \
139 void *_mp_##name(MemPool mp, const m_uint size) { \
140 struct pool *p = mp_ini(mp, size); \
141 void * ret = p ? _mp_calloc2(p, zero) : (void *)default; \
142 return ret; \
143 }

◆ MPHUGE

#define MPHUGE   (64 * SZ_INT)

Definition at line 8 of file mpool.c.

◆ SMALL_BLK

#define SMALL_BLK   512

Definition at line 6 of file mpool.c.

Function Documentation

◆ __mp_calloc2()

static void * __mp_calloc2 ( struct pool * p,
const bool zero )
static

Definition at line 92 of file mpool.c.

92 {
93 if (p->next) {
94 volatile struct Recycle *const recycle = p->next;
95 #ifdef USE_HELGRIND
96 VALGRIND_HG_CLEAN_MEMORY(recycle, p->obj_sz);
97 #endif
98 p->next = p->next->next;
99 if (zero) memset((void*)recycle, 0, p->obj_sz);
100 return (void*)recycle;
101 }
102 if (++p->obj_id == BLK(p->obj_sz)) _realloc(p);
103 return p->data[p->blk_id] + p->obj_id * p->obj_sz;
104}
#define BLK(obj_sz)
Definition mpool.c:9
static void _realloc(struct pool *p)
Definition mpool.c:80
volatile struct Recycle * next
Definition mpool.c:12
int32_t blk_id
Definition mpool.c:21
uint8_t ** data
Definition mpool.c:16
uint32_t obj_id
Definition mpool.c:20
uint32_t obj_sz
Definition mpool.c:19
volatile struct Recycle * next
Definition mpool.c:18

◆ _mp_calloc2()

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()

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}
void _mp_free2(struct pool *p, void *ptr)
Definition mpool.c:113
#define xfree(a)
Definition xalloc.h:28

◆ _mp_free2()

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}

◆ _realloc()

static void _realloc ( struct pool * p)
static

Definition at line 80 of file mpool.c.

80 {
81 p->obj_id = 0;
82 if (++p->blk_id == (int32_t)p->nblk) {
83 const uint32_t nblk = p->nblk + 1;
84 p->nblk <<= 1;
85 p->data = (uint8_t **)xrealloc(p->data, sizeof(uint8_t *) * p->nblk);
86 for (uint32_t i = nblk; i < p->nblk; ++i)
87 p->data[i] = NULL;
88 }
89 p->data[p->blk_id] = (uint8_t *)xcalloc(BLK(p->obj_sz), p->obj_sz);
90}
uint32_t nblk
Definition mpool.c:22
static ANEW void * xcalloc(const m_uint n, const m_uint sz)
Definition xalloc.h:18
static ANEW void * xrealloc(void *p, const m_uint sz)
Definition xalloc.h:23

◆ 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
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 * xmalloc(const m_uint sz)
Definition xalloc.h:13

◆ mp_create()

static struct pool * mp_create ( MemPool mp,
const uint32_t obj_sz,
const uint32_t idx )
inlinestatic

Definition at line 58 of file mpool.c.

59 {
60 struct pool *p = (struct pool *)_mp_calloc2(mp->master_pool, 0);
61 mp_set(p, obj_sz);
62 mp->pools[idx] = p;
63 return p;
64}
static ANN void mp_set(struct pool *p, const uint32_t obj_sz)
Definition mpool.c:25

◆ mp_end()

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}
static ANN void gwt_lock_end(gwtlock_t *lock)
Definition threadpool.h:95

◆ mp_ini()

ANN struct pool * mp_ini ( MemPool mp,
const uint32_t obj_sz )

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))

◆ mp_set()

static ANN void mp_set ( struct pool * p,
const uint32_t obj_sz )
static

Definition at line 25 of file mpool.c.

25 {
26 p->obj_sz = obj_sz;
27 p->obj_id = BLK(obj_sz) - 1;
28 p->blk_id = -1;
29 p->nblk = 1;
30 p->next = NULL;
31 p->data = (uint8_t **)xcalloc(1, sizeof(uint8_t *));
33}
static ANN int gwt_lock_ini(gwtlock_t *lock)
Definition threadpool.h:89

◆ new_pool()

struct pool * new_pool ( const uint32_t obj_sz)

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}