some coverity fixes.
[minix.git] / lib / libc / minix / minix-malloc-debug.c
blobc9edc7367df680a2b8754f0337e8bdec26b912e1
1 /* pointless without assertions */
2 #ifdef NDEBUG
3 #undef NDEBUG
4 #endif
6 #include <assert.h>
7 #include <machine/vm.h>
8 #include <machine/vmparam.h>
9 #include <minix/minlib.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <sys/mman.h>
13 #include <unistd.h>
15 #define mmap minix_mmap
16 #define munmap minix_munmap
18 #include "malloc-debug.h"
20 #if 0
21 #include <stdio.h>
22 static int reenter;
23 #define LOG(args) if (!reenter) { reenter++; printf args; reenter--; }
24 #else
25 #define LOG(args)
26 #endif
28 struct block {
29 size_t size;
30 unsigned magic;
33 static u8_t *ptr_min, *ptr_max;
35 #define MIN(x, y) ((x) < (y) ? (x) : (y))
37 static unsigned long page_round_down(unsigned long x)
39 return x - x % PAGE_SIZE;
42 static unsigned long page_round_up(unsigned long x)
44 unsigned long rem;
46 rem = x % PAGE_SIZE;
47 if (rem)
48 x += PAGE_SIZE - rem;
50 return x;
53 #define page_round_down_ptr(x) ((u8_t *) page_round_down((unsigned long) (x)))
54 #define page_round_up_ptr(x) ((u8_t *) page_round_up((unsigned long) (x)))
56 static unsigned long block_compute_magic(struct block *block)
58 return (unsigned long) block + block->size + 0xDEADBEEFUL;
61 static size_t block_get_totalsize(size_t size)
63 return page_round_up(sizeof(struct block) + size);
66 static u8_t *block_get_endptr(struct block *block)
68 return (u8_t *) block + block_get_totalsize(block->size);
71 static u8_t *block_get_dataptr(struct block *block)
73 return block_get_endptr(block) - block->size;
76 static void block_check(struct block *block)
78 u8_t *dataptr, *p;
80 /* check location */
81 assert(block);
82 assert(!((unsigned long) block % PAGE_SIZE));
83 assert((u8_t *) block >= ptr_min);
84 assert((u8_t *) block <= ptr_max);
86 /* check size */
87 assert(block->size > 0);
89 /* check fillers */
90 assert(block->magic == block_compute_magic(block));
91 dataptr = block_get_dataptr(block);
92 for (p = (u8_t *) (block + 1); p < dataptr; p++)
93 assert(*p == ((unsigned long) p & 0xff));
96 static struct block *block_alloc(size_t size)
98 struct block *block;
99 u8_t *dataptr, *p, *ptr;
100 unsigned page_index, page_index_max;
101 size_t sizerem, totalsize;
102 u64_t tsc;
104 LOG(("block_alloc; size=0x%x\n", size));
105 assert(size > 0);
107 /* round size up to machine word size */
108 sizerem = size % sizeof(long);
109 if (sizerem)
110 size += sizeof(long) - sizerem;
112 /* initialize address range */
113 if (!ptr_min && !ptr_max) {
114 /* keep a safe distance from areas that are in use:
115 * - 4MB from the break (should not change if traditional
116 * malloc is not used so a small margin is sufficient
117 * - 256MB from the stack (big margin because memory beyond
118 * this may be allocated by mmap when the address space
119 * starts to fill up)
121 ptr_min = page_round_up_ptr((u8_t *) sbrk(0) + 0x400000);
122 ptr_max = page_round_down_ptr((u8_t *) &size - 0x10000000);
124 assert(ptr_min);
125 assert(ptr_max);
126 assert(ptr_min < ptr_max);
128 /* select address at random */
129 #ifdef __NBSD_LIBC
130 tsc = 0;
131 #else
132 read_tsc_64(&tsc);
133 #endif
134 totalsize = block_get_totalsize(size);
135 page_index_max = (ptr_max - ptr_min - totalsize) / PAGE_SIZE;
136 page_index = (page_index_max > 0) ? (ex64lo(tsc) % page_index_max) : 0;
137 ptr = ptr_min + page_index * PAGE_SIZE;
139 /* allocate block */
140 block = (struct block *) mmap(
141 ptr, /* addr */
142 totalsize, /* len */
143 PROT_READ|PROT_WRITE, /* prot */
144 MAP_PREALLOC, /* flags */
145 -1, /* fd */
146 0); /* offset */
147 if (block == MAP_FAILED) {
148 /* mmap call failed */
149 abort();
152 /* block may not be at the requested location if that is in use */
153 if (ptr_min > (u8_t *) block)
154 ptr_min = (u8_t *) block;
156 if (ptr_max < (u8_t *) block)
157 ptr_max = (u8_t *) block;
159 /* initialize block, including fillers */
160 block->size = size;
161 block->magic = block_compute_magic(block);
162 dataptr = block_get_dataptr(block);
163 for (p = (u8_t *) (block + 1); p < dataptr; p++)
164 *p = ((unsigned long) p & 0xff);
166 LOG(("block_alloc; block=0x%x\n", block));
167 return block;
170 static struct block *block_find(const void *ptr)
172 struct block *block;
174 LOG(("block_find; ptr=0x%x\n", ptr));
175 assert(ptr);
177 /* locate block based on pointer, then check whether it is valid */
178 block = (struct block *) page_round_down(
179 (unsigned long) ((struct block *) __UNCONST(ptr) - 1));
180 block_check(block);
181 LOG(("block_find; block=0x%x\n", block));
182 return block;
185 static void block_free(struct block *block)
187 LOG(("block_free; block=0x%x\n", block));
188 assert(block);
190 /* simply unmap the block */
191 if (munmap(block, block_get_totalsize(block->size)) < 0) {
192 /* munmap call failed */
193 abort();
197 void *_dbg_malloc(size_t size)
199 struct block *newblock;
200 u8_t *ptr;
202 LOG(("_dbg_malloc; size=0x%x\n", size));
203 assert(size > 0); /* enforced by regular malloc */
205 newblock = block_alloc(size);
206 if (!newblock)
207 return NULL;
209 ptr = block_get_dataptr(newblock);
210 LOG(("_dbg_malloc; ptr=0x%x\n", ptr));
211 return ptr;
214 void *_dbg_realloc(void *oldp, size_t size)
216 u8_t *newp;
217 struct block *oldblock, *newblock;
219 LOG(("_dbg_realloc; oldp=0x%x; size=0x%x\n", oldp, size));
220 assert(oldp); /* enforced by regular realloc */
221 assert(size > 0); /* enforced by regular realloc */
223 /* always allocate new block */
224 newblock = block_alloc(size);
225 if (!newblock)
226 return NULL;
228 /* copy the data */
229 oldblock = block_find(oldp);
230 memcpy(block_get_dataptr(newblock),
231 block_get_dataptr(oldblock),
232 MIN(newblock->size, oldblock->size));
234 /* deallocate old block */
235 block_free(oldblock);
237 newp = block_get_dataptr(newblock);
238 LOG(("_dbg_realloc; newp=0x%x\n", newp));
239 return newp;
242 void _dbg_free(void *ptr)
244 LOG(("_dbg_free; ptr=0x%x\n", ptr));
245 assert(ptr); /* enforced by regular free */
247 /* find the block and free it */
248 block_free(block_find(ptr));
250 LOG(("_dbg_free done\n"));