sdlbomber/list.c

63 lines
1.1 KiB
C

#include "bomber.h"
#include "list.h"
#include "utils.h"
static listitem *things=0, *free_things;
void allocthings(void) {
int i;
const int num = MAXTHINGS;
if (!things) {
things = calloc(sizeof(listitem), num);
} else {
memset(things, 0, sizeof(listitem)*num);
}
if(!things) nomem("Trying to allocate thing memory");
for(i=0;i<num-1;++i) {
things[i].next = &things[i+1];
}
things[i].next = 0;
free_things = things;
}
void *allocentry(void) {
listitem *entry = free_things;
if (free_things) {
free_things = free_things->next;
memset(entry, 0, sizeof(*entry));
}
return entry;
}
void freeentry(void *_entry) {
listitem *entry = _entry;
entry->next = free_things;
free_things = entry;
}
void addtail(void *_header,void *_entry) {
listitem *header = _header, *entry = _entry;
while (header->next) header = header->next;
header->next = entry;
entry->next = 0;
}
void delink(void *_header,void *_entry) {
listitem *header = _header, *entry = _entry;
while (header->next != entry) header = header->next;
header->next = entry->next;
entry->next = 0;
freeentry(entry);
}
void initheader(void *p) {
memset(p,0,sizeof(list));
}