sdlbomber/list.c

73 lines
1.4 KiB
C

#include "bomber.h"
#include "list.h"
#include "utils.h"
typedef union genericlistitem genericlistitem;
union genericlistitem {
genericlistitem *next;
bomb b;
flame f;
brickdecay bc;
player p;
bonustile bt;
generic g;
};
/* doesn't use prev link */
static genericlistitem *things=0;
static genericlistitem *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].next = &things[i+1];
}
things[i].next = 0;
free_things = things;
}
void *_allocentry(size_t size) {
genericlistitem *entry = free_things;
if (size > sizeof(genericlistitem)) return 0;
if (free_things) {
free_things = free_things->next;
memset(entry, 0, sizeof(genericlistitem));
}
return entry;
}
void freeentry(void *ptr) {
genericlistitem *entry = ptr;
entry->next = free_things;
free_things = entry;
}
void list_add_tail(listhead *head, listhead *entry) {
entry->next = head;
entry->prev = head->prev;
head->prev->next = entry;
head->prev = entry;
}
void list_del(listhead *entry) {
entry->next->prev = entry->prev;
entry->prev->next = entry->next;
entry->next = entry->prev = entry;
}
void list_init_head(listhead *head) {
head->next = head;
head->prev = head;
}