brzeszczot

brzeszczot.git
git clone git://git.lenczewski.org/brzeszczot.git
Log | Files | Refs

inibin.c (2360B)


      1 #include "libriot/inibin.h"
      2 
      3 b32
      4 riot_inibin_ctx_init(struct riot_inibin_ctx *self) {
      5 	assert(self);
      6 
      7 	if (!MEM_POOL_INIT(&self->str_pool, char, RIOT_INIBIN_CTX_STR_POOL_SZ))
      8 		goto str_pool_alloc_failure;
      9 
     10 	if (!MEM_POOL_INIT(&self->field_pool, struct riot_inibin_field, RIOT_INIBIN_CTX_FIELD_POOL_SZ))
     11 		goto field_pool_alloc_failure;
     12 
     13 	if (!MEM_POOL_INIT(&self->pair_pool, struct riot_inibin_pair, RIOT_INIBIN_CTX_PAIR_POOL_SZ))
     14 		goto pair_pool_alloc_failure;
     15 
     16 	if (!MEM_POOL_INIT(&self->node_pool, struct riot_inibin_node, RIOT_INIBIN_CTX_NODE_POOL_SZ))
     17 		goto node_pool_alloc_failure;
     18 
     19 	return true;
     20 
     21 node_pool_alloc_failure:
     22 	mem_pool_free(&self->pair_pool);
     23 pair_pool_alloc_failure:
     24 	mem_pool_free(&self->field_pool);
     25 field_pool_alloc_failure:
     26 	mem_pool_free(&self->str_pool);
     27 str_pool_alloc_failure:
     28 	return false;
     29 }
     30 
     31 void
     32 riot_inibin_ctx_free(struct riot_inibin_ctx *self) {
     33 	assert(self);
     34 
     35 	mem_pool_free(&self->str_pool);
     36 	mem_pool_free(&self->field_pool);
     37 	mem_pool_free(&self->pair_pool);
     38 	mem_pool_free(&self->node_pool);
     39 }
     40 
     41 b32
     42 riot_inibin_ctx_push_str(struct riot_inibin_ctx *self, u16 len, riot_offptr_t *out) {
     43 	assert(self);
     44 	assert(out);
     45 
     46 	void *absptr = MEM_POOL_ALLOC(&self->str_pool, char, len);
     47 	if (!absptr) return false;
     48 
     49 	*out = (char *)absptr - (char *)self->str_pool.ptr;
     50 
     51 	return true;
     52 }
     53 
     54 b32
     55 riot_inibin_ctx_pushn_field(struct riot_inibin_ctx *self, u16 count, riot_offptr_t *out) {
     56 	assert(self);
     57 	assert(out);
     58 
     59 	void *absptr = MEM_POOL_ALLOC(&self->field_pool, struct riot_inibin_field, count);
     60 	if (!absptr) return false;
     61 
     62 	*out = (struct riot_inibin_field *)absptr - (struct riot_inibin_field *)self->field_pool.ptr;
     63 
     64 	return true;
     65 }
     66 
     67 b32
     68 riot_inibin_ctx_pushn_pair(struct riot_inibin_ctx *self, u32 count, riot_offptr_t *out) {
     69 	assert(self);
     70 	assert(out);
     71 
     72 	void *absptr = MEM_POOL_ALLOC(&self->pair_pool, struct riot_inibin_pair, count);
     73 	if (!absptr) return false;
     74 
     75 	*out = (struct riot_inibin_pair *)absptr - (struct riot_inibin_pair *)self->pair_pool.ptr;
     76 
     77 	return true;
     78 }
     79 
     80 b32
     81 riot_inibin_ctx_pushn_node(struct riot_inibin_ctx *self, u32 count, riot_offptr_t *out) {
     82 	assert(self);
     83 	assert(out);
     84 
     85 	void *absptr = MEM_POOL_ALLOC(&self->node_pool, struct riot_inibin_node, count);
     86 	if (!absptr) return false;
     87 
     88 	*out = (struct riot_inibin_node *)absptr - (struct riot_inibin_node *)self->node_pool.ptr;
     89 
     90 	return true;
     91 }