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