Skip to content

Instantly share code, notes, and snippets.

@carld
Last active August 29, 2015 14:24
Show Gist options
  • Save carld/99be0893e5272db3e3f7 to your computer and use it in GitHub Desktop.
Save carld/99be0893e5272db3e3f7 to your computer and use it in GitHub Desktop.
pointers
void _pstack_init(void *stack) {
*(void **)stack = (char *)stack + sizeof (char *);
}
void _pstack_push(void *stack, void *data) {
*(void **)stack = (char *)*(void **)stack + sizeof (char *);
**(void ***)stack = data;
}
void _pstack_pop (void *stack) {
**(void ***)stack = 0;
*(void **)stack = (char *)*(void **)stack - sizeof (char *);
}
void * _pstack_peek(void *stack) {
return **(void ***)stack;
}
const int list_size = 1 << 8; /* 256 */
void _pq_init(void *list) {
((long **)list)[0] = 0; /* tail */
((long **)list)[1] = 0; /* head */
}
long _pq_length(void *list) {
long tail = ((long *)list)[0];
long head = ((long *)list)[1];
return (list_size - tail) - (list_size - head);
}
void _pq_shift(void *list, void *data) {
long head = ((long *)list)[1];
char **ptr;
ptr = (char **) list + (2 * sizeof(long *)) + (head * sizeof(void *));
*ptr = data;
head = head + 1;
head = head & (list_size - 1);
((long *)list)[1] = head;
}
void * _pq_pop(void *list) {
long tail = ((long *)list)[0];
char **ptr;
ptr = (char **) list + (2 * sizeof(long *)) + (tail * sizeof(void *));
tail = tail + 1;
tail = tail & (list_size - 1);
((long *)list)[0] = tail;
return *ptr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment