#include #include "stack-man.h" typedef struct { int begin; int end; } alloc_t; bool_t stack_manager_init (stack_man_t *st) { st->total_size = 0; return array_create (&st->allocations, sizeof (alloc_t)); } bool_t stack_manager_alloc (stack_man_t *st, int size, int *offset) { array_t *new_allocs; alloc_t *all; size_t n_allocs; size_t i; int location; alloc_t *tail; if (!array_create (&new_allocs, sizeof (alloc_t))) return FALSE; all = array_get_data (&st->allocations, &n_allocs); location = 0; for (i = 0; i < n_allocs; ++i) { if (!array_append (&new_allocs, 1, &tail)) goto out; memcpy (tail, all, sizeof (alloc_t)); if (all->begin - location >= size) break; location = all->end; all++; } if (!array_append (&new_allocs, 1, &tail)) goto out; if (location + size > st->total_size) st->total_size = location + size; tail->begin = location; tail->end = location + size; for (; i < n_allocs; ++i) { if (!array_append (&new_allocs, 1, &tail)) goto out; memcpy (tail, all, sizeof (alloc_t)); all++; } *offset = location; array_free (&st->allocations); st->allocations = new_allocs; return TRUE; out: array_free (&new_allocs); return FALSE; } void stack_manager_free (stack_man_t *st, int offset) { alloc_t *alloc; size_t n_allocs; int i, d; alloc = array_get_data (&st->allocations, &n_allocs); d = 0; for (i = 0; i < n_allocs; ++i) { if (alloc[i].begin == offset) d++; else alloc[i - d] = alloc[i]; } array_delete_tail (&st->allocations, d); }