#ifndef UCX_STACK_H
#define UCX_STACK_H
#include "ucx.h"
#include "allocator.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct {
UcxAllocator allocator;
size_t size;
char *space;
char *top;
} UcxStack;
struct ucx_stack_metadata {
char *prev;
size_t size;
};
void ucx_stack_init(UcxStack *stack,
char* space,
size_t size);
void *ucx_stack_malloc(UcxStack *stack,
size_t n);
#define ucx_stack_push(stack, n) ucx_stack_malloc(stack, n)
void *ucx_stack_calloc(UcxStack *stack,
size_t nelem,
size_t elsize);
#define ucx_stack_pusharr(stack,n,elsize) ucx_stack_calloc(stack,n,elssize)
void *ucx_stack_realloc(UcxStack *stack,
void *ptr,
size_t n);
void ucx_stack_free(UcxStack *stack,
void *ptr);
#define ucx_stack_topsize(stack) ((stack)->top ? ((
struct ucx_stack_metadata*)\
(stack)->top -
1)->size :
0)
#define ucx_stack_pop(stack, dest) ucx_stack_popn(stack, dest, (
size_t)-
1)
void ucx_stack_popn(UcxStack *stack,
void *dest,
size_t n);
size_t ucx_stack_avail(UcxStack *stack);
#define ucx_stack_empty(stack) (!(stack)->top)
#define ucx_stack_dim(size, elems) (size+
sizeof(
struct ucx_stack_metadata) * \
(elems +
1))
#ifdef __cplusplus
}
#endif
#endif