sdlbomber/list.c

68 lines
1.3 KiB
C
Raw Normal View History

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