libnexus-rv
return-stack.h
1 #ifndef LIBNEXUS_RV_RETURN_STACK_H
2 #define LIBNEXUS_RV_RETURN_STACK_H
3 
4 #include <assert.h>
5 #include <stdint.h>
6 #include <stdlib.h>
7 
8 typedef struct nexusrv_return_stack {
9  uint64_t *entries;
10  unsigned size;
11  unsigned used;
12  unsigned end;
14 
15 static inline int nexusrv_retstack_init(nexusrv_return_stack *stack,
16  unsigned size) {
17  stack->entries = (uint64_t*)malloc(sizeof(stack->entries[0]) * size);
18  if (!stack->entries)
19  return -nexus_no_mem;
20  stack->size = size;
21  stack->used = stack->end = 0;
22  return 0;
23 }
24 
25 static inline void nexusrv_retstack_fini(nexusrv_return_stack *stack) {
26  free(stack->entries);
27  stack->entries = NULL;
28 }
29 
30 static inline unsigned nexusrv_retstack_used(nexusrv_return_stack *stack) {
31  return stack->used;
32 }
33 
34 static inline void nexusrv_retstack_clear(nexusrv_return_stack *stack) {
35  stack->used = 0;
36 }
37 
38 static inline void nexusrv_retstack_push(nexusrv_return_stack *stack,
39  uint64_t addr) {
40  stack->entries[stack->end++] = addr;
41  stack->end %= stack->size;
42  if (stack->used < stack->size)
43  ++stack->used;
44 }
45 
46 static inline uint64_t nexusrv_retstack_pop(nexusrv_return_stack *stack) {
47  assert(stack->used);
48  if (!stack->end)
49  stack->end = stack->size;
50  uint64_t ret = stack->entries[--stack->end];
51  --stack->used;
52  return ret;
53 }
54 
55 #endif
Definition: return-stack.h:8