summaryrefslogtreecommitdiff
path: root/src/util/stack.h
blob: 01a48e599786adaedf76dc5fae967f8f9bb41ad3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#pragma once

#include <stddef.h>
#include <stdbool.h>

struct Stack {
    size_t size;
    size_t capacity;
    void* data;
};

void stack_init(struct Stack* stack, size_t size);
void stack_push(struct Stack* stack, void* data, size_t len);
void* stack_pop(struct Stack* stack, size_t len);
void stack_free(struct Stack* stack);

inline void stack_push_int(struct Stack* stack, int value) { 
    stack_push(stack, &value, sizeof(int)); 
}

inline bool stack_pop_int(struct Stack* stack, int* value) { 
    void* d = stack_pop(stack, sizeof(int));
    if (d == NULL) return false;
    *value = *(int*)(d);
    return true;
}