2015-05-16 22:04:00 +01:00
|
|
|
#include "hoedown/stack.h"
|
|
|
|
|
|
|
|
#include "hoedown/buffer.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
void
|
|
|
|
hoedown_stack_init(hoedown_stack *st, size_t initial_size)
|
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
assert(st);
|
2015-05-16 22:04:00 +01:00
|
|
|
|
2018-07-15 13:51:05 +01:00
|
|
|
st->item = NULL;
|
|
|
|
st->size = st->asize = 0;
|
2015-05-16 22:04:00 +01:00
|
|
|
|
2018-07-15 13:51:05 +01:00
|
|
|
if (!initial_size)
|
|
|
|
initial_size = 8;
|
2015-05-16 22:04:00 +01:00
|
|
|
|
2018-07-15 13:51:05 +01:00
|
|
|
hoedown_stack_grow(st, initial_size);
|
2015-05-16 22:04:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
hoedown_stack_uninit(hoedown_stack *st)
|
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
assert(st);
|
2015-05-16 22:04:00 +01:00
|
|
|
|
2018-07-15 13:51:05 +01:00
|
|
|
free(st->item);
|
2015-05-16 22:04:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
hoedown_stack_grow(hoedown_stack *st, size_t neosz)
|
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
assert(st);
|
2015-05-16 22:04:00 +01:00
|
|
|
|
2018-07-15 13:51:05 +01:00
|
|
|
if (st->asize >= neosz)
|
|
|
|
return;
|
2015-05-16 22:04:00 +01:00
|
|
|
|
2018-07-15 13:51:05 +01:00
|
|
|
st->item = hoedown_realloc(st->item, neosz * sizeof(void *));
|
|
|
|
memset(st->item + st->asize, 0x0, (neosz - st->asize) * sizeof(void *));
|
2015-05-16 22:04:00 +01:00
|
|
|
|
2018-07-15 13:51:05 +01:00
|
|
|
st->asize = neosz;
|
2015-05-16 22:04:00 +01:00
|
|
|
|
2018-07-15 13:51:05 +01:00
|
|
|
if (st->size > neosz)
|
|
|
|
st->size = neosz;
|
2015-05-16 22:04:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
hoedown_stack_push(hoedown_stack *st, void *item)
|
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
assert(st);
|
2015-05-16 22:04:00 +01:00
|
|
|
|
2018-07-15 13:51:05 +01:00
|
|
|
if (st->size >= st->asize)
|
|
|
|
hoedown_stack_grow(st, st->size * 2);
|
2015-05-16 22:04:00 +01:00
|
|
|
|
2018-07-15 13:51:05 +01:00
|
|
|
st->item[st->size++] = item;
|
2015-05-16 22:04:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
hoedown_stack_pop(hoedown_stack *st)
|
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
assert(st);
|
2015-05-16 22:04:00 +01:00
|
|
|
|
2018-07-15 13:51:05 +01:00
|
|
|
if (!st->size)
|
|
|
|
return NULL;
|
2015-05-16 22:04:00 +01:00
|
|
|
|
2018-07-15 13:51:05 +01:00
|
|
|
return st->item[--st->size];
|
2015-05-16 22:04:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
hoedown_stack_top(const hoedown_stack *st)
|
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
assert(st);
|
2015-05-16 22:04:00 +01:00
|
|
|
|
2018-07-15 13:51:05 +01:00
|
|
|
if (!st->size)
|
|
|
|
return NULL;
|
2015-05-16 22:04:00 +01:00
|
|
|
|
2018-07-15 13:51:05 +01:00
|
|
|
return st->item[st->size - 1];
|
2015-05-16 22:04:00 +01:00
|
|
|
}
|