z_bmalloc.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* Emacs style mode select -*- C++ -*-
  2. *-----------------------------------------------------------------------------
  3. *
  4. *
  5. * PrBoom: a Doom port merged with LxDoom and LSDLDoom
  6. * based on BOOM, a modified and improved DOOM engine
  7. * Copyright (C) 1999 by
  8. * id Software, Chi Hoang, Lee Killough, Jim Flynn, Rand Phares, Ty Halderman
  9. * Copyright (C) 1999-2000 by
  10. * Jess Haas, Nicolas Kalkhof, Colin Phipps, Florian Schulze
  11. * Copyright 2005, 2006 by
  12. * Florian Schulze, Colin Phipps, Neil Stevens, Andrey Budko
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License
  16. * as published by the Free Software Foundation; either version 2
  17. * of the License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  27. * 02111-1307, USA.
  28. *
  29. * DESCRIPTION:
  30. * This is designed to be a fast allocator for small, regularly used block sizes
  31. *-----------------------------------------------------------------------------
  32. */
  33. #ifdef HAVE_CONFIG_H
  34. #include "config.h"
  35. #endif
  36. #include "doomtype.h"
  37. #include "z_zone.h"
  38. #include "z_bmalloc.h"
  39. #include "lprintf.h"
  40. typedef struct bmalpool_s {
  41. struct bmalpool_s *nextpool;
  42. size_t blocks;
  43. byte used[0];
  44. } bmalpool_t;
  45. inline static void* getelem(bmalpool_t *p, size_t size, size_t n)
  46. {
  47. return (((byte*)p) + sizeof(bmalpool_t) + sizeof(byte)*(p->blocks) + size*n);
  48. }
  49. inline static PUREFUNC int iselem(const bmalpool_t *pool, size_t size, const void* p)
  50. {
  51. // CPhipps - need portable # of bytes between pointers
  52. int dif = (const char*)p - (const char*)pool;
  53. dif -= sizeof(bmalpool_t);
  54. dif -= pool->blocks;
  55. if (dif<0) return -1;
  56. dif /= size;
  57. return (((size_t)dif >= pool->blocks) ? -1 : dif);
  58. }
  59. enum { unused_block = 0, used_block = 1};
  60. void* Z_BMalloc(struct block_memory_alloc_s *pzone)
  61. {
  62. register bmalpool_t **pool = (bmalpool_t **)&(pzone->firstpool);
  63. while (*pool != NULL) {
  64. byte *p = memchr((*pool)->used, unused_block, (*pool)->blocks); // Scan for unused marker
  65. if (p) {
  66. int n = p - (*pool)->used;
  67. #ifdef SIMPLECHECKS
  68. if ((n<0) || ((size_t)n>=(*pool)->blocks))
  69. I_Error("Z_BMalloc: memchr returned pointer outside of array");
  70. #endif
  71. (*pool)->used[n] = used_block;
  72. return getelem(*pool, pzone->size, n);
  73. } else
  74. pool = &((*pool)->nextpool);
  75. }
  76. {
  77. // Nothing available, must allocate a new pool
  78. bmalpool_t *newpool;
  79. // CPhipps: Allocate new memory, initialised to 0
  80. *pool = newpool = Z_Calloc(sizeof(*newpool) + (sizeof(byte) + pzone->size)*(pzone->perpool),
  81. 1, pzone->tag, NULL);
  82. newpool->nextpool = NULL; // NULL = (void*)0 so this is redundant
  83. // Return element 0 from this pool to satisfy the request
  84. newpool->used[0] = used_block;
  85. newpool->blocks = pzone->perpool;
  86. return getelem(newpool, pzone->size, 0);
  87. }
  88. }
  89. void Z_BFree(struct block_memory_alloc_s *pzone, void* p)
  90. {
  91. register bmalpool_t **pool = (bmalpool_t**)&(pzone->firstpool);
  92. while (*pool != NULL) {
  93. int n = iselem(*pool, pzone->size, p);
  94. if (n >= 0) {
  95. #ifdef SIMPLECHECKS
  96. if ((*pool)->used[n] == unused_block)
  97. I_Error("Z_BFree: Refree in zone %s", pzone->desc);
  98. #endif
  99. (*pool)->used[n] = unused_block;
  100. if (memchr(((*pool)->used), used_block, (*pool)->blocks) == NULL) {
  101. // Block is all unused, can be freed
  102. bmalpool_t *oldpool = *pool;
  103. *pool = (*pool)->nextpool;
  104. Z_Free(oldpool);
  105. }
  106. return;
  107. } else pool = &((*pool)->nextpool);
  108. }
  109. I_Error("Z_BFree: Free not in zone %s", pzone->desc);
  110. }