Debian Codegraph @

prboom2/src/z_zone.c

raw
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 * Zone Memory Allocation. Neat.
31 *
32 * Neat enough to be rewritten by Lee Killough...
33 *
34 * Must not have been real neat :)
35 *
36 * Made faster and more general, and added wrappers for all of Doom's
37 * memory allocation functions, including malloc() and similar functions.
38 * Added line and file numbers, in case of error. Added performance
39 * statistics and tunables.
40 *-----------------------------------------------------------------------------
41 */
42
43
44 // use config.h if autoconf made one -- josh
45 #ifdef HAVE_CONFIG_H
46 #include "config.h"
47 #endif
48
49 #include <stdlib.h>
50 #include <stdio.h>
51
52 #include "z_zone.h"
53 #include "doomstat.h"
54 #include "v_video.h"
55 #include "g_game.h"
56 #include "lprintf.h"
57
58 #ifdef DJGPP
59 #include <dpmi.h>
60 #endif
61
62 #define ZONE_SIGNATURE 0x931d4a11
63
64 enum {
65 ZONE_STATIC,
66 ZONE_LEVEL,
67 ZONE_MAX
68 };
69
70 typedef struct memblock {
71 unsigned signature;
72 struct memblock *next,*prev;
73 size_t size;
74 unsigned char tag;
75 } memblock_t;
76
77 static const size_t HEADER_SIZE = sizeof(memblock_t);
78
79 static memblock_t *blockbytag[ZONE_MAX];
80
81 /* Z_Malloc
82 * cph - the algorithm here was a very simple first-fit round-robin
83 * one - just keep looping around, freeing everything we can until
84 * we get a large enough space
85 *
86 * This has been changed now; we still do the round-robin first-fit,
87 * but we only free the blocks we actually end up using; we don't
88 * free all the stuff we just pass on the way.
89 */
90
91 static void *Z_MallocTag(size_t size, int tag)
92 {
93 memblock_t *block = NULL;
94
95 if (!size)
96 return NULL; // malloc(0) returns NULL
97
98 if (!(block = malloc(size + HEADER_SIZE)))
99 {
100 I_Error ("Z_Malloc: Failure trying to allocate %lu bytes", (unsigned long) size);
101 }
102
103 if (!blockbytag[tag])
104 {
105 blockbytag[tag] = block;
106 block->next = block->prev = block;
107 }
108 else
109 {
110 blockbytag[tag]->prev->next = block;
111 block->prev = blockbytag[tag]->prev;
112 block->next = blockbytag[tag];
113 blockbytag[tag]->prev = block;
114 }
115
116 block->size = size;
117 block->signature = ZONE_SIGNATURE;
118 block->tag = tag; // tag
119 block = (memblock_t *)((char *) block + HEADER_SIZE);
120
121 return block;
122 }
123
124 void Z_Free(void *p)
125 {
126 memblock_t *block = (memblock_t *)((char *) p - HEADER_SIZE);
127
128 if (!p)
129 return;
130
131 if (block->signature != ZONE_SIGNATURE)
132 I_Error("Z_Free: freed a non-zone pointer");
133 block->signature = 0; // Nullify signature so another free fails
134
135 if (block == block->next)
136 blockbytag[block->tag] = NULL;
137 else
138 if (blockbytag[block->tag] == block)
139 blockbytag[block->tag] = block->next;
140 block->prev->next = block->next;
141 block->next->prev = block->prev;
142
143 free(block);
144 }
145
146 static void Z_FreeTag(int tag)
147 {
148 memblock_t *block, *end_block;
149
150 if (tag < 0 || tag >= ZONE_MAX)
151 I_Error("Z_FreeTag: Tag %i does not exist", tag);
152
153 block = blockbytag[tag];
154 if (!block)
155 return;
156 end_block = block->prev;
157 while (1)
158 {
159 memblock_t *next = block->next;
160 Z_Free((char *) block + HEADER_SIZE);
161 if (block == end_block)
162 break;
163 block = next; // Advance to next block
164 }
165 }
166
167 static void *Z_ReallocTag(void *ptr, size_t n, int tag)
168 {
169 void *p = Z_MallocTag(n, tag);
170 if (ptr)
171 {
172 memblock_t *block = (memblock_t *)((char *) ptr - HEADER_SIZE);
173 memcpy(p, ptr, n <= block->size ? n : block->size);
174 Z_Free(ptr);
175 }
176 return p;
177 }
178
179 static void *Z_CallocTag(size_t n1, size_t n2, int tag)
180 {
181 return
182 (n1*=n2) ? memset(Z_MallocTag(n1, tag), 0, n1) : NULL;
183 }
184
185 static char *Z_StrdupTag(const char *s, int tag)
186 {
187 return strcpy(Z_MallocTag(strlen(s)+1, tag), s);
188 }
189
190 void *Z_Malloc(size_t size)
191 {
192 return Z_MallocTag(size, ZONE_STATIC);
193 }
194
195 void *Z_Calloc(size_t n, size_t n2)
196 {
197 return Z_CallocTag(n, n2, ZONE_STATIC);
198 }
199
200 void *Z_Realloc(void *p, size_t n)
201 {
202 return Z_ReallocTag(p, n, ZONE_STATIC);
203 }
204
205 char *Z_Strdup(const char *s)
206 {
207 return Z_StrdupTag(s, ZONE_STATIC);
208 }
209
210 void Z_FreeLevel(void)
211 {
212 return Z_FreeTag(ZONE_LEVEL);
213 }
214
215 void *Z_MallocLevel(size_t size)
216 {
217 return Z_MallocTag(size, ZONE_LEVEL);
218 }
219
220 void *Z_CallocLevel(size_t n, size_t n2)
221 {
222 return Z_CallocTag(n, n2, ZONE_LEVEL);
223 }
224
225 void *Z_ReallocLevel(void *p, size_t n)
226 {
227 return Z_ReallocTag(p, n, ZONE_LEVEL);
228 }
229
230 char *Z_StrdupLevel(const char *s)
231 {
232 return Z_StrdupTag(s, ZONE_LEVEL);
233 }