• Home
  • Raw
  • Download

Lines Matching full:stack

18  * \brief Stack container implementation.
20 #include "epid/common/src/stack.h"
24 /// Internal representation of a Stack
25 struct Stack { struct
29 size_t top; ///< Stack top, the number of elements in the stack argument
32 bool CreateStack(size_t element_size, Stack** stack) { in CreateStack() argument
33 if (!stack || 0 == element_size) return false; in CreateStack()
34 *stack = SAFE_ALLOC(sizeof(Stack)); in CreateStack()
35 if (!*stack) return false; in CreateStack()
36 (*stack)->element_size = element_size; in CreateStack()
40 void* StackPushN(Stack* stack, size_t n, void* elements) { in StackPushN() argument
41 if (!stack) return 0; in StackPushN()
43 size_t max_size_required = stack->top + n; in StackPushN()
44 if (n > (SIZE_MAX / stack->element_size) - stack->top) in StackPushN()
46 if (max_size_required > stack->max_size) { in StackPushN()
48 SAFE_REALLOC(stack->buf, max_size_required * stack->element_size); in StackPushN()
50 stack->buf = reallocated; in StackPushN()
51 stack->max_size = max_size_required; in StackPushN()
54 // Memory copy is used to copy variable number of elements to stack in StackPushN()
55 if (0 != memcpy_S((uint8_t*)stack->buf + stack->top * stack->element_size, in StackPushN()
56 (stack->max_size - stack->top) * stack->element_size, in StackPushN()
57 elements, n * stack->element_size)) { in StackPushN()
61 stack->top += n; in StackPushN()
63 return (uint8_t*)stack->buf + (stack->top - n) * stack->element_size; in StackPushN()
66 bool StackPopN(Stack* stack, size_t n, void* elements) { in StackPopN() argument
67 if (!stack) return false; in StackPopN()
69 if (n > stack->top) return false; in StackPopN()
71 // Memory copy is used to copy variable number of elements from stack in StackPopN()
72 if (0 != memcpy_S(elements, n * stack->element_size, in StackPopN()
73 (uint8_t*)stack->buf + in StackPopN()
74 (stack->top - n) * stack->element_size, in StackPopN()
75 n * stack->element_size)) { in StackPopN()
78 stack->top -= n; in StackPopN()
84 size_t StackGetSize(Stack const* stack) { in StackGetSize() argument
85 return stack ? stack->top : (size_t)0; in StackGetSize()
88 void* StackGetBuf(Stack const* stack) { return stack ? stack->buf : NULL; } in StackGetBuf() argument
90 void DeleteStack(Stack** stack) { in DeleteStack() argument
91 if (stack && *stack) { in DeleteStack()
92 SAFE_FREE((*stack)->buf); in DeleteStack()
93 SAFE_FREE(*stack); in DeleteStack()