forked from vurtun/nuklear
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnuklear_pool.c
63 lines (61 loc) · 1.94 KB
/
nuklear_pool.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include "nuklear.h"
#include "nuklear_internal.h"
/* ===============================================================
*
* POOL
*
* ===============================================================*/
NK_LIB void
nk_pool_init(struct nk_pool *pool, struct nk_allocator *alloc,
unsigned int capacity)
{
nk_zero(pool, sizeof(*pool));
pool->alloc = *alloc;
pool->capacity = capacity;
pool->type = NK_BUFFER_DYNAMIC;
pool->pages = 0;
}
NK_LIB void
nk_pool_free(struct nk_pool *pool)
{
struct nk_page *iter = pool->pages;
if (!pool) return;
if (pool->type == NK_BUFFER_FIXED) return;
while (iter) {
struct nk_page *next = iter->next;
pool->alloc.free(pool->alloc.userdata, iter);
iter = next;
}
}
NK_LIB void
nk_pool_init_fixed(struct nk_pool *pool, void *memory, nk_size size)
{
nk_zero(pool, sizeof(*pool));
NK_ASSERT(size >= sizeof(struct nk_page));
if (size < sizeof(struct nk_page)) return;
pool->capacity = (unsigned)(size - sizeof(struct nk_page)) / sizeof(struct nk_page_element);
pool->pages = (struct nk_page*)memory;
pool->type = NK_BUFFER_FIXED;
pool->size = size;
}
NK_LIB struct nk_page_element*
nk_pool_alloc(struct nk_pool *pool)
{
if (!pool->pages || pool->pages->size >= pool->capacity) {
/* allocate new page */
struct nk_page *page;
if (pool->type == NK_BUFFER_FIXED) {
NK_ASSERT(pool->pages);
if (!pool->pages) return 0;
NK_ASSERT(pool->pages->size < pool->capacity);
return 0;
} else {
nk_size size = sizeof(struct nk_page);
size += NK_POOL_DEFAULT_CAPACITY * sizeof(union nk_page_data);
page = (struct nk_page*)pool->alloc.alloc(pool->alloc.userdata,0, size);
page->next = pool->pages;
pool->pages = page;
page->size = 0;
}
} return &pool->pages->win[pool->pages->size++];
}