Skip to content

Commit 910eb92

Browse files
committed
Add struct am_onesize_cfg with crit_enter(exit) callbacks
1 parent 451780a commit 910eb92

File tree

4 files changed

+79
-33
lines changed

4 files changed

+79
-33
lines changed

libs/event/event.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ void am_event_add_pool(void *pool, int size, int block_size, int alignment) {
6363
memset(&blk, 0, sizeof(blk));
6464
blk.ptr = pool;
6565
blk.size = size;
66-
am_onesize_init(&me->pool[me->npool], &blk, block_size, alignment);
66+
struct am_onesize_cfg cfg = {
67+
.pool = &blk, .block_size = block_size, .alignment = alignment
68+
};
69+
am_onesize_ctor(&me->pool[me->npool], &cfg);
6770

6871
++me->npool;
6972
}

libs/onesize/onesize.c

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,17 @@ void *am_onesize_allocate(struct am_onesize *hnd, int size) {
6060
return NULL;
6161
}
6262

63+
hnd->crit_enter();
6364
if (am_slist_is_empty(&hnd->fl)) {
65+
hnd->crit_exit();
6466
return NULL;
6567
}
6668

6769
struct am_slist_item *elem = am_slist_pop_front(&hnd->fl);
6870
AM_ASSERT(elem);
6971

7072
am_onesize_run_stats(hnd, 1, 0);
73+
hnd->crit_exit();
7174

7275
return elem;
7376
}
@@ -82,15 +85,17 @@ void am_onesize_free(struct am_onesize *hnd, const void *ptr) {
8285

8386
struct am_slist_item *p = AM_CAST(struct am_slist_item *, ptr);
8487

88+
hnd->crit_enter();
8589
am_slist_push_front(&hnd->fl, p);
8690
am_onesize_run_stats(hnd, 0, 1);
91+
hnd->crit_exit();
8792
}
8893

8994
/**
9095
* Internal initialization routine.
9196
* @param hnd the allocator
9297
*/
93-
static void am_onesize_init_internal(struct am_onesize *hnd) {
98+
static void am_onesize_ctor_internal(struct am_onesize *hnd) {
9499
am_slist_init(&hnd->fl);
95100

96101
char *ptr = (char *)hnd->pool.ptr;
@@ -108,9 +113,11 @@ void am_onesize_free_all(struct am_onesize *hnd) {
108113

109114
struct am_onesize *s = (struct am_onesize *)hnd;
110115

116+
hnd->crit_enter();
111117
int minfree = hnd->minfree;
112-
am_onesize_init_internal(s);
118+
am_onesize_ctor_internal(s);
113119
hnd->minfree = minfree; /* cppcheck-suppress redundantAssignment */
120+
hnd->crit_exit();
114121
}
115122

116123
void am_onesize_iterate_over_allocated(
@@ -128,17 +135,21 @@ void am_onesize_iterate_over_allocated(
128135
}
129136
int iterated = 0;
130137
num = AM_MIN(num, total);
138+
hnd->crit_enter();
131139
for (int i = 0; (i < total) && (iterated < num); i++) {
132140
AM_ASSERT(AM_ALIGNOF_PTR(ptr) >= AM_ALIGNOF(struct am_slist_item));
133141
struct am_slist_item *item = AM_CAST(struct am_slist_item *, ptr);
134142
if (am_slist_owns(&impl->fl, item)) {
135143
continue; /* the item is free */
136144
}
137145
/* the item is allocated */
146+
hnd->crit_exit();
138147
cb(ctx, iterated, (char *)item, impl->block_size);
148+
hnd->crit_enter();
139149
ptr += impl->block_size;
140150
iterated++;
141151
}
152+
hnd->crit_exit();
142153
}
143154

144155
int am_onesize_get_nfree(const struct am_onesize *hnd) {
@@ -161,31 +172,42 @@ int am_onesize_get_nblocks(const struct am_onesize *hnd) {
161172
return hnd->ntotal;
162173
}
163174

164-
void am_onesize_init(
165-
struct am_onesize *hnd, struct am_blk *pool, int block_size, int alignment
166-
) {
175+
static void am_onesize_crit_enter(void) {}
176+
static void am_onesize_crit_exit(void) {}
177+
178+
void am_onesize_ctor(struct am_onesize *hnd, struct am_onesize_cfg *cfg) {
167179
AM_ASSERT(hnd);
168-
AM_ASSERT(pool);
169-
AM_ASSERT(pool->ptr);
170-
AM_ASSERT(pool->size > 0);
171-
AM_ASSERT(pool->size >= block_size);
172-
AM_ASSERT(alignment >= AM_ALIGNOF(struct am_slist_item));
180+
AM_ASSERT(cfg);
181+
AM_ASSERT(cfg->pool);
182+
AM_ASSERT(cfg->pool->ptr);
183+
AM_ASSERT(cfg->pool->size > 0);
184+
AM_ASSERT(cfg->pool->size >= cfg->block_size);
185+
AM_ASSERT(cfg->alignment >= AM_ALIGNOF(struct am_slist_item));
173186

174187
memset(hnd, 0, sizeof(*hnd));
175188

176-
void *alignedptr = AM_ALIGN_PTR_UP(pool->ptr, alignment);
177-
int affix = (int)((uintptr_t)alignedptr - (uintptr_t)pool->ptr);
178-
AM_ASSERT(affix < pool->size);
179-
pool->size -= affix;
180-
pool->ptr = alignedptr;
189+
void *alignedptr = AM_ALIGN_PTR_UP(cfg->pool->ptr, cfg->alignment);
190+
int affix = (int)((uintptr_t)alignedptr - (uintptr_t)cfg->pool->ptr);
191+
AM_ASSERT(affix < cfg->pool->size);
192+
cfg->pool->size -= affix;
193+
cfg->pool->ptr = alignedptr;
181194

182-
block_size = AM_MAX(block_size, (int)sizeof(struct am_slist_item));
183-
block_size = AM_MAX(block_size, alignment);
195+
cfg->block_size =
196+
AM_MAX(cfg->block_size, (int)sizeof(struct am_slist_item));
197+
cfg->block_size = AM_MAX(cfg->block_size, cfg->alignment);
184198

185-
AM_ASSERT(pool->size >= block_size);
199+
AM_ASSERT(cfg->pool->size >= cfg->block_size);
186200

187-
hnd->pool = *pool;
188-
hnd->block_size = block_size;
201+
hnd->pool = *cfg->pool;
202+
hnd->block_size = cfg->block_size;
203+
204+
if (cfg->crit_enter && cfg->crit_exit) {
205+
hnd->crit_enter = cfg->crit_enter;
206+
hnd->crit_exit = cfg->crit_exit;
207+
} else {
208+
hnd->crit_enter = am_onesize_crit_enter;
209+
hnd->crit_exit = am_onesize_crit_exit;
210+
}
189211

190-
am_onesize_init_internal(hnd);
212+
am_onesize_ctor_internal(hnd);
191213
}

libs/onesize/onesize.h

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,25 +45,41 @@ struct am_onesize {
4545
int nfree; /**< current number of blocks in free list */
4646
int ntotal; /**< total number of blocks */
4747
int minfree; /**< minimum number of blocks in free list */
48+
/** Enter critical section */
49+
void (*crit_enter)(void);
50+
/** Exit critical section */
51+
void (*crit_exit)(void);
52+
};
53+
54+
/** Onesize configuration. */
55+
struct am_onesize_cfg {
56+
/** the memory pool */
57+
struct am_blk *pool;
58+
/**
59+
* The maximum size of the memory block the allocator
60+
* can allocate [bytes]. The allocation of memory blocks
61+
* bigger that this size will fail.
62+
*/
63+
int block_size;
64+
/** The alignment of allocated memory blocks [bytes] */
65+
int alignment;
66+
/** Enter critical section */
67+
void (*crit_enter)(void);
68+
/** Exit critical section */
69+
void (*crit_exit)(void);
4870
};
4971

5072
/**
51-
* Initializes a new onesize allocator.
73+
* Construct a new onesize allocator.
5274
* Allocation requests up to block_size bytes are
5375
* rounded up to block_size bytes and served from a singly-linked
5476
* list of buffers. Due to the simplicity of onesize allocator
5577
* management, allocations from it are fast.
5678
*
57-
* @param hnd the allocator
58-
* @param pool the memory pool
59-
* @param block_size the maximum size of the memory block the allocator
60-
* can allocate [bytes]. The allocation of memory blocks
61-
* bigger that this size will fail.
62-
* @param alignment the alignment of allocated memory blocks [bytes]
79+
* @param hnd the allocator
80+
* @param cfg configuration
6381
*/
64-
void am_onesize_init(
65-
struct am_onesize *hnd, struct am_blk *pool, int block_size, int alignment
66-
);
82+
void am_onesize_ctor(struct am_onesize *hnd, struct am_onesize_cfg *cfg);
6783

6884
/**
6985
* Allocate memory if \p size is <= block_size. The block at the front

libs/onesize/test.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,12 @@ int main(void) {
4444
} test_arr[2];
4545
struct am_blk blk = {.ptr = &test_arr[0], .size = sizeof(test_arr)};
4646

47-
am_onesize_init(&ma, &blk, sizeof(struct test), AM_ALIGNOF(struct test));
47+
struct am_onesize_cfg cfg = {
48+
.pool = &blk,
49+
.block_size = sizeof(struct test),
50+
.alignment = AM_ALIGNOF(struct test)
51+
};
52+
am_onesize_ctor(&ma, &cfg);
4853

4954
AM_ASSERT(am_onesize_get_nfree(&ma) == 2);
5055

0 commit comments

Comments
 (0)