Nuklear
This is a minimal-state, immediate-mode graphical user interface toolkit written in ANSI C and licensed under public domain. It was designed as a simple embeddable user interface for application and does not have any dependencies, a default render backend or OS window/input handling but instead provides a highly modular, library-based approach, with simple input state for input and draw commands describing primitive shapes as output. So instead of providing a layered library that tries to abstract over a number of platform and render backends, it focuses only on the actual UI.
 
Loading...
Searching...
No Matches
nuklear_pool.c
1#include "nuklear.h"
2#include "nuklear_internal.h"
3
4/* ===============================================================
5 *
6 * POOL
7 *
8 * ===============================================================*/
9NK_LIB void
10nk_pool_init(struct nk_pool *pool, const struct nk_allocator *alloc,
11 unsigned int capacity)
12{
13 NK_ASSERT(capacity >= 1);
14 nk_zero(pool, sizeof(*pool));
15 pool->alloc = *alloc;
16 pool->capacity = capacity;
17 pool->type = NK_BUFFER_DYNAMIC;
18 pool->pages = 0;
19}
20NK_LIB void
21nk_pool_free(struct nk_pool *pool)
22{
23 struct nk_page *iter;
24 if (!pool) return;
25 iter = pool->pages;
26 if (pool->type == NK_BUFFER_FIXED) return;
27 while (iter) {
28 struct nk_page *next = iter->next;
29 pool->alloc.free(pool->alloc.userdata, iter);
30 iter = next;
31 }
32}
33NK_LIB void
34nk_pool_init_fixed(struct nk_pool *pool, void *memory, nk_size size)
35{
36 nk_zero(pool, sizeof(*pool));
37 NK_ASSERT(size >= sizeof(struct nk_page));
38 if (size < sizeof(struct nk_page)) return;
39 /* first nk_page_element is embedded in nk_page, additional elements follow in adjacent space */
40 pool->capacity = (unsigned)(1 + (size - sizeof(struct nk_page)) / sizeof(struct nk_page_element));
41 pool->pages = (struct nk_page*)memory;
42 pool->type = NK_BUFFER_FIXED;
43 pool->size = size;
44}
45NK_LIB struct nk_page_element*
46nk_pool_alloc(struct nk_pool *pool)
47{
48 if (!pool->pages || pool->pages->size >= pool->capacity) {
49 /* allocate new page */
50 struct nk_page *page;
51 if (pool->type == NK_BUFFER_FIXED) {
52 NK_ASSERT(pool->pages);
53 if (!pool->pages) return 0;
54 NK_ASSERT(pool->pages->size < pool->capacity);
55 return 0;
56 } else {
57 nk_size size = sizeof(struct nk_page);
58 size += (pool->capacity - 1) * sizeof(struct nk_page_element);
59 page = (struct nk_page*)pool->alloc.alloc(pool->alloc.userdata,0, size);
60 page->next = pool->pages;
61 pool->pages = page;
62 page->size = 0;
63 }
64 } return &pool->pages->win[pool->pages->size++];
65}
66
main API and documentation file