static int test_cpool(void) { test_start("cpool"); cpool_t p; cpool_init(&p, 1, 10240); for (int i = 1; i < 10240; i+=2) { int v = cpool_alloc(&p); test_assert(v >= 1 && v <= 10240); test_assert(cpool_check(&p, v) == false); } cpool_free(&p, 1); int v = cpool_alloc(&p); test_assert(v == 1); cpool_release(&p); return test_success(); }
int coat_alloc(coat_t *t, uint32_t arg[COAT_ARGS], cvector_item_t *out_obj) { assert(t); // Allocate new ID. int id = cpool_alloc(&t->pool); if (!id || id == COAT_INVALID_ID) { goto error; } // Potentially expand ID table vector. while (cvector_count(&t->table) <= id) { if (t->oat_expand) { t->oat_expand(&t->table); continue; } // Defaults to adding NULL pointers to fill ID table. cvector_add(&t->table, (cvector_item_t) NULL); } cvector_item_t obj = cvector_get(&t->table, id); if (!obj && t->oat_create) { // Create object structure and store it. obj = t->oat_create(t, id, arg); if (!obj) { goto error; } cvector_set(&t->table, id, obj); } if (out_obj) { (*out_obj) = obj; } return id; error: if (id) cpool_free(&t->pool, id); return COAT_INVALID_ID; }