Skip to content

Instantly share code, notes, and snippets.

@default-writer
Created January 18, 2023 19:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save default-writer/b7ac8addfbf5cc2eacae545deb34cb14 to your computer and use it in GitHub Desktop.
Save default-writer/b7ac8addfbf5cc2eacae545deb34cb14 to your computer and use it in GitHub Desktop.
List<T> C implementation
#include "playground/list/list.h"
#include "std/common.h"
#define MAX_MEMORY 0xffff // 64K bytes
/*private */
// global allocated memory
static void** ptr = 0;
static void list_init() {
ptr = calloc(1, MAX_MEMORY);
}
static void list_destroy() {
free(ptr);
ptr = 0;
}
static void list_push(void* data) {
*ptr++ = data;
}
static void* list_pop() {
return *--ptr;
}
static void* list_peek() {
return *(ptr - 1);
}
/* public */
const struct list list_v1 = {
.init = list_init,
.destroy = list_destroy,
.push = list_push,
.pop = list_pop,
.peek = list_peek
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment