| 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 |
|
| 34 |
#ifdef HAVE_CONFIG_H |
| 35 |
#include "config.h" |
| 36 |
#endif |
| 37 |
|
| 38 |
#include <string.h> |
| 39 |
|
| 40 |
#include "doomtype.h" |
| 41 |
#include "z_zone.h" |
| 42 |
#include "z_bmalloc.h" |
| 43 |
#include "lprintf.h" |
| 44 |
|
| 45 |
typedef struct bmalpool_s { |
| 46 |
struct bmalpool_s *nextpool; |
| 47 |
size_t blocks; |
| 48 |
byte used[0]; |
| 49 |
} bmalpool_t; |
| 50 |
|
| 51 |
inline static void* getelem(bmalpool_t *p, size_t size, size_t n) |
| 52 |
{ |
| 53 |
return (((byte*)p) + sizeof(bmalpool_t) + sizeof(byte)*(p->blocks) + size*n); |
| 54 |
} |
| 55 |
|
| 56 |
inline static PUREFUNC int iselem(const bmalpool_t *pool, size_t size, const void* p) |
| 57 |
{ |
| 58 |
// CPhipps - need portable # of bytes between pointers |
| 59 |
int dif = (const char*)p - (const char*)pool; |
| 60 |
|
| 61 |
dif -= sizeof(bmalpool_t); |
| 62 |
dif -= pool->blocks; |
| 63 |
if (dif<0) return -1; |
| 64 |
dif /= size; |
| 65 |
return (((size_t)dif >= pool->blocks) ? -1 : dif); |
| 66 |
} |
| 67 |
|
| 68 |
enum { unused_block = 0, used_block = 1}; |
| 69 |
|
| 70 |
void* Z_BMalloc(struct block_memory_alloc_s *pzone) |
| 71 |
{ |
| 72 |
register bmalpool_t **pool = (bmalpool_t **)&(pzone->firstpool); |
| 73 |
while (*pool != NULL) { |
| 74 |
byte *p = memchr((*pool)->used, unused_block, (*pool)->blocks); // Scan for unused marker |
| 75 |
if (p) { |
| 76 |
int n = p - (*pool)->used; |
| 77 |
#ifdef SIMPLECHECKS |
| 78 |
if ((n<0) || ((size_t)n>=(*pool)->blocks)) |
| 79 |
I_Error("Z_BMalloc: memchr returned pointer outside of array"); |
| 80 |
#endif |
| 81 |
(*pool)->used[n] = used_block; |
| 82 |
return getelem(*pool, pzone->size, n); |
| 83 |
} else |
| 84 |
pool = &((*pool)->nextpool); |
| 85 |
} |
| 86 |
{ |
| 87 |
// Nothing available, must allocate a new pool |
| 88 |
bmalpool_t *newpool; |
| 89 |
|
| 90 |
// CPhipps: Allocate new memory, initialised to 0 |
| 91 |
|
| 92 |
*pool = newpool = |
| 93 |
Z_CallocLevel(sizeof(*newpool) + (sizeof(byte) + pzone->size)*(pzone->perpool), 1); |
| 94 |
newpool->nextpool = NULL; // NULL = (void*)0 so this is redundant |
| 95 |
|
| 96 |
// Return element 0 from this pool to satisfy the request |
| 97 |
newpool->used[0] = used_block; |
| 98 |
newpool->blocks = pzone->perpool; |
| 99 |
return getelem(newpool, pzone->size, 0); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
void Z_BFree(struct block_memory_alloc_s *pzone, void* p) |
| 104 |
{ |
| 105 |
register bmalpool_t **pool = (bmalpool_t**)&(pzone->firstpool); |
| 106 |
|
| 107 |
while (*pool != NULL) { |
| 108 |
int n = iselem(*pool, pzone->size, p); |
| 109 |
if (n >= 0) { |
| 110 |
#ifdef SIMPLECHECKS |
| 111 |
if ((*pool)->used[n] == unused_block) |
| 112 |
I_Error("Z_BFree: Refree in zone %s", pzone->desc); |
| 113 |
#endif |
| 114 |
(*pool)->used[n] = unused_block; |
| 115 |
if (memchr(((*pool)->used), used_block, (*pool)->blocks) == NULL) { |
| 116 |
// Block is all unused, can be freed |
| 117 |
bmalpool_t *oldpool = *pool; |
| 118 |
*pool = (*pool)->nextpool; |
| 119 |
Z_Free(oldpool); |
| 120 |
} |
| 121 |
return; |
| 122 |
} else pool = &((*pool)->nextpool); |
| 123 |
} |
| 124 |
I_Error("Z_BFree: Free not in zone %s", pzone->desc); |
| 125 |
} |