Advertisement
funcelot

List<T> C implementation

Jan 18th, 2023
921
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.63 KB | Science | 0 0
  1. #include "playground/list/list.h"
  2. #include "std/common.h"
  3.  
  4. #define MAX_MEMORY 0xffff // 64K bytes
  5.  
  6. /*private */
  7.  
  8. // global allocated memory
  9. static void** ptr = 0;
  10.  
  11. static void list_init() {
  12.     ptr = calloc(1, MAX_MEMORY);
  13. }
  14.  
  15. static void list_destroy() {
  16.     free(ptr);
  17.     ptr = 0;
  18. }
  19.  
  20. static void list_push(void* data) {
  21.     *ptr++ = data;
  22. }
  23.  
  24. static void* list_pop() {
  25.     return *--ptr;
  26. }
  27.  
  28. static void* list_peek() {
  29.     return *(ptr - 1);
  30. }
  31.  
  32. /* public */
  33.  
  34. const struct list list_v1 = {
  35.     .init = list_init,
  36.     .destroy = list_destroy,
  37.     .push = list_push,
  38.     .pop = list_pop,
  39.     .peek = list_peek
  40. };
Tags: C89
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement