blob: 15d5a8e080d25213be19ad7e0ac4e4fc7cd95781 (
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
27
28
29
30
31
32
|
#include "stack.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void stack_init(struct Stack* stack, size_t size) {
stack->size = 0;
stack->capacity = size;
stack->data = malloc(size);
}
void stack_push(struct Stack* stack, void* data, size_t len) {
size_t new_size = stack->size + len;
if (new_size >= stack->capacity) {
stack->capacity = new_size * 2;
stack->data = realloc(stack->data, stack->capacity);
}
memcpy((uint8_t*)stack->data + stack->size, data, len);
stack->size += len;
}
void* stack_pop(struct Stack* stack, size_t len) {
if (stack->size < len) return NULL;
stack->size -= len;
return (uint8_t*)stack->data + stack->size;
}
void stack_free(struct Stack *stack) {
free(stack->data);
}
|