2009-08-08 19:45:49 +00:00
|
|
|
|
|
|
|
#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;
|
2009-08-08 19:45:49 +00:00
|
|
|
|
|
|
|
void allocthings(void) {
|
|
|
|
int i;
|
|
|
|
const int num = MAXTHINGS;
|
|
|
|
|
|
|
|
if (!things) {
|
2009-08-11 15:19:43 +00:00
|
|
|
things = calloc(sizeof(genericlistitem), num);
|
2009-08-08 19:45:49 +00:00
|
|
|
} else {
|
2009-08-11 15:19:43 +00:00
|
|
|
memset(things, 0, sizeof(genericlistitem)*num);
|
2009-08-08 19:45:49 +00:00
|
|
|
}
|
|
|
|
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-08 19:45:49 +00:00
|
|
|
}
|
2009-08-11 15:19:43 +00:00
|
|
|
things[i].h.next = 0;
|
|
|
|
free_things = (listhead*) things;
|
2009-08-08 19:45:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void *allocentry(void) {
|
2009-08-11 15:19:43 +00:00
|
|
|
listhead *entry = free_things;
|
2009-08-08 19:45:49 +00:00
|
|
|
|
|
|
|
if (free_things) {
|
|
|
|
free_things = free_things->next;
|
2009-08-11 15:19:43 +00:00
|
|
|
memset(entry, 0, sizeof(genericlistitem));
|
2009-08-08 19:45:49 +00:00
|
|
|
}
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
2009-08-11 15:19:43 +00:00
|
|
|
static void freeentry(void *_entry) {
|
|
|
|
listhead *entry = _entry;
|
2009-08-08 19:45:49 +00:00
|
|
|
|
|
|
|
entry->next = free_things;
|
2009-08-11 15:19:43 +00:00
|
|
|
entry->prev = NULL;
|
2009-08-08 19:45:49 +00:00
|
|
|
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-08 19:45:49 +00:00
|
|
|
}
|
|
|
|
|
2009-08-11 15:19:43 +00:00
|
|
|
listhead* delinkhead(listhead *entry) {
|
|
|
|
listhead *result = entry->prev;
|
|
|
|
|
|
|
|
entry->next->prev = entry->prev;
|
|
|
|
entry->prev->next = entry->next;
|
2009-08-08 19:45:49 +00:00
|
|
|
|
|
|
|
freeentry(entry);
|
2009-08-11 15:19:43 +00:00
|
|
|
return result;
|
2009-08-08 19:45:49 +00:00
|
|
|
}
|
|
|
|
|
2009-08-11 15:19:43 +00:00
|
|
|
void initheader(listhead *head) {
|
|
|
|
head->next = head;
|
|
|
|
head->prev = head;
|
2009-08-08 19:45:49 +00:00
|
|
|
}
|
|
|
|
|