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.

91 lines
1.7KB

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