ordinary/src/maint.c

106 lines
2.0 KiB
C

#include <ordinary/inspect.h>
#include <ordinary/maint.h>
#include <stdlib.h>
static void ordinary_list_insert(struct ordinary_list *list, struct ordinary_node *node) {
if(!list->head) {
list->head = node;
list->tail = node;
} else if(list->head == list->tail) {
list->tail = node;
list->head->next = list->tail;
list->tail->prev = list->head;
} else {
list->tail->next = node;
node->prev = list->tail;
list->tail = node;
}
if(list->limit) {
list->count += 1;
}
}
struct ordinary_node *ordinary_list_add(struct ordinary_list *list, void *val) {
if(ordinary_list_full(list)) {
return NULL;
}
struct ordinary_node *node = malloc(sizeof(struct ordinary_node));
if(!node) {
return NULL;
}
node->prev = NULL;
node->next = NULL;
node->val = val;
ordinary_list_insert(list, node);
return node;
}
uint8_t ordinary_list_mov(struct ordinary_list *dst, struct ordinary_list *src, struct ordinary_node *node) {
if(ordinary_list_full(dst) || ordinary_list_empty(src)) {
return 0;
}
struct ordinary_node *prev = node->prev;
struct ordinary_node *next = node->next;
if(prev) {
prev->next = next;
node->prev = NULL;
}
if(next) {
next->prev = prev;
node->next = NULL;
}
ordinary_list_insert(dst, node);
return 1;
}
struct ordinary_node *ordinary_list_pop(struct ordinary_list *list) {
if(ordinary_list_empty(list)) {
return NULL;
}
struct ordinary_node *node = list->tail;
if(list->head == list->tail) {
list->head = NULL;
list->tail = NULL;
} else {
list->tail = list->tail->prev;
list->tail->next = NULL;
}
if(list->limit) {
list->count -= 1;
}
return node;
}
void ordinary_list_rem(struct ordinary_list *list, struct ordinary_node *node) {
struct ordinary_node *prev = node->prev;
struct ordinary_node *next = node->next;
if(prev) {
prev->next = next;
node->prev = NULL;
}
if(next) {
next->prev = prev;
node->next = NULL;
}
free(node);
if(list->limit) {
list->count -= 1;
}
}