lisp-take-1/gc/linked_list/pop.c

66 lines
952 B
C

#include <assert.h>
#include <debug.h>
#include <value/struct.h>
#include "struct.h"
#include "pop.h"
struct value* linked_list_pop(
struct linked_list* list,
size_t offset)
{
ENTER;
assert(list->head);
struct value* value = list->head;
struct link* link = (void*) value + offset;
assert(link->in_set == true);
if (link->next)
{
struct value* new_head = link->next;
struct link* new_head_link = (void*) new_head + offset;
new_head_link->prev = NULL;
list->head = new_head;
}
else
{
// this is the last link, so we'll clear the list:
list->head = list->tail = NULL;
}
link->in_set = false;
#ifndef RELEASE_BUILD
// not strictly necessary, but makes the dotout easier to read.
link->next = link->prev = NULL;
#endif
EXIT;
return value;
}