kubo/kubo_context.c

54 lines
1.4 KiB
C

#include "kubo_context.h"
struct kubo_context *kubo_context_init() {
struct kubo_context *context = malloc(sizeof(struct kubo_context));
context->num_walls_alloc = KUBO_CONTEXT_NUM_WALLS_ALLOC;
context->walls =
malloc(sizeof(struct kubo_wall) * context->num_walls_alloc);
return context;
}
void kubo_context_cleanup(struct kubo_context *context) {
free(context->walls);
free(context);
}
bool kubo_context_add_wall(struct kubo_context *context,
struct kubo_wall *wall) {
struct kubo_wall **new_walls;
if (context->num_walls == context->num_walls_alloc - 1) {
context->num_walls_alloc *= 2;
new_walls = realloc(context->walls, sizeof(struct kubo_wall *) *
context->num_walls_alloc);
if (!new_walls)
return false;
context->walls = new_walls;
}
context->walls[context->num_walls++] = wall;
return true;
}
struct kubo_wall *kubo_context_get_pending_wall(struct kubo_context *context) {
if (context->num_walls > 0 &&
(context->walls[context->num_walls - 1]->wall_state == KUBO_WALL_INIT ||
context->walls[context->num_walls - 1]->wall_state ==
KUBO_WALL_STARTED)) {
return context->walls[context->num_walls - 1];
}
struct kubo_wall *wall = kubo_wall_init();
kubo_context_add_wall(context, wall);
return wall;
}