sdlbomber/list.c

67 lines
1.2 KiB
C

#include "bomber.h"
#include "list.h"
#include "utils.h"
/* doesn't use prev link */
static genericlistitem *things=0;
static listhead *free_things;
void allocthings(void) {
int i;
const int num = MAXTHINGS;
if (!things) {
things = calloc(sizeof(genericlistitem), num);
} else {
memset(things, 0, sizeof(genericlistitem)*num);
}
if(!things) nomem("Trying to allocate thing memory");
for(i=0;i<num-1;++i) {
things[i].h.next = (listhead*) &things[i+1];
}
things[i].h.next = 0;
free_things = (listhead*) things;
}
void *allocentry(void) {
listhead *entry = free_things;
if (free_things) {
free_things = free_things->next;
memset(entry, 0, sizeof(genericlistitem));
}
return entry;
}
static void freeentry(void *_entry) {
listhead *entry = _entry;
entry->next = free_things;
entry->prev = NULL;
free_things = entry;
}
void _addtail(listhead *head, listhead *entry) {
entry->next = head;
entry->prev = head->prev;
head->prev->next = entry;
head->prev = entry;
}
listhead* delinkhead(listhead *entry) {
listhead *result = entry->prev;
entry->next->prev = entry->prev;
entry->prev->next = entry->next;
freeentry(entry);
return result;
}
void initheader(listhead *head) {
head->next = head;
head->prev = head;
}