You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

list.c 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include "bomber.h"
  2. #include "list.h"
  3. #include "utils.h"
  4. typedef union genericlistitem genericlistitem;
  5. union genericlistitem {
  6. genericlistitem *next;
  7. bomb b;
  8. flame f;
  9. brickdecay bc;
  10. player p;
  11. bonustile bt;
  12. generic g;
  13. };
  14. /* doesn't use prev link */
  15. static genericlistitem *things=0;
  16. static genericlistitem *free_things;
  17. void allocthings(void) {
  18. int i;
  19. const int num = MAXTHINGS;
  20. if (!things) {
  21. things = calloc(sizeof(genericlistitem), num);
  22. } else {
  23. memset(things, 0, sizeof(genericlistitem)*num);
  24. }
  25. if(!things) nomem("Trying to allocate thing memory");
  26. for(i=0;i<num-1;++i) {
  27. things[i].next = &things[i+1];
  28. }
  29. things[i].next = 0;
  30. free_things = things;
  31. }
  32. void *_allocentry(size_t size) {
  33. genericlistitem *entry = free_things;
  34. if (size > sizeof(genericlistitem)) return 0;
  35. if (free_things) {
  36. free_things = free_things->next;
  37. memset(entry, 0, sizeof(genericlistitem));
  38. }
  39. return entry;
  40. }
  41. void freeentry(void *ptr) {
  42. genericlistitem *entry = ptr;
  43. entry->next = free_things;
  44. free_things = entry;
  45. }
  46. void list_add_tail(listhead *head, listhead *entry) {
  47. entry->next = head;
  48. entry->prev = head->prev;
  49. head->prev->next = entry;
  50. head->prev = entry;
  51. }
  52. void list_del(listhead *entry) {
  53. entry->next->prev = entry->prev;
  54. entry->prev->next = entry->next;
  55. entry->next = entry->prev = entry;
  56. }
  57. void list_init_head(listhead *head) {
  58. head->next = head;
  59. head->prev = head;
  60. }