MOS Source Code
Loading...
Searching...
No Matches
stack.c
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-3.0-or-later
2
4#include <mos/moslib_global.h>
5#include <mos_stdio.h>
6#include <mos_string.h>
7
8void stack_init(downwards_stack_t *stack, void *mem_region_bottom, size_t size)
9{
10 // the stack grows downwards
11 ptr_t stack_top = (ptr_t) mem_region_bottom + size;
12 stack->top = stack_top;
13 stack->head = stack_top;
14 stack->capacity = size;
15}
16
18{
19 memset(stack, 0, sizeof(downwards_stack_t));
20}
21
22void *stack_grow(downwards_stack_t *stack, size_t size)
23{
24 // high memory | top -----> head -----> top - capacity | low memory
25 if (unlikely(stack->head - (stack->top - stack->capacity) < size))
26 mos_panic("stack overflow on stack %p, attempted to push %zu bytes", (void *) stack, size);
27
28 stack->head = stack->head - size;
29 return (void *) stack->head;
30}
31
32void *stack_push(downwards_stack_t *stack, const void *data, size_t size)
33{
34 // high memory | top -----> head -----> top - capacity | low memory
35 if (unlikely(stack->head - (stack->top - stack->capacity) < size))
36 mos_panic("stack overflow on stack %p, attempted to push %zu bytes", (void *) stack, size);
37
38 stack->head = stack->head - size;
39 memcpy((void *) stack->head, data, size);
40 return (void *) stack->head;
41}
42
43void stack_pop(downwards_stack_t *stack, size_t size, void *data)
44{
45 // high memory | top -----> head -----> top - capacity | low memory
46 if (unlikely(stack->head - stack->top < size))
47 mos_panic("stack underflow on stack %p, attempted to pop %zu bytes", (void *) stack, size);
48
49 if (data != NULL)
50 memcpy(data, (void *) stack->head, size);
51 stack->head = stack->head + size;
52}
void stack_deinit(downwards_stack_t *stack)
Definition stack.c:17
void stack_init(downwards_stack_t *stack, void *mem_region_bottom, size_t size)
Definition stack.c:8
void * stack_grow(downwards_stack_t *stack, size_t size)
Definition stack.c:22
void stack_pop(downwards_stack_t *stack, size_t size, void *data)
Definition stack.c:43
void * stack_push(downwards_stack_t *stack, const void *data, size_t size)
Definition stack.c:32
#define unlikely(x)
Definition mos_global.h:40
#define mos_panic(fmt,...)
Definition panic.h:55
static void * memcpy(void *s1, const void *s2, size_t n)
Definition pb_syshdr.h:90
#define NULL
Definition pb_syshdr.h:46
static void * memset(void *s, int c, size_t n)
Definition pb_syshdr.h:101
size_t size
Definition slab.c:30
size_t capacity
Definition stack.h:18
unsigned long ptr_t
Definition types.h:25