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