• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <stdlib.h>
2 #include <stdint.h>
3 #include <limits.h>
4 #include <string.h>
5 #include <sys/mman.h>
6 #include <errno.h>
7 
8 #include "meta.h"
9 
donate(unsigned char * base,size_t len)10 static void donate(unsigned char *base, size_t len)
11 {
12 	uintptr_t a = (uintptr_t)base;
13 	uintptr_t b = a + len;
14 	a += -a & (UNIT-1);
15 	b -= b & (UNIT-1);
16 	memset(base, 0, len);
17 	for (int sc=47; sc>0 && b>a; sc-=4) {
18 		if (b-a < (size_classes[sc]+1)*UNIT) continue;
19 		struct meta *m = alloc_meta();
20 		m->avail_mask = 0;
21 		m->freed_mask = 1;
22 		m->mem = (void *)a;
23 		m->mem->meta = encode_ptr(m, ctx.secret);
24 		m->last_idx = 0;
25 		m->freeable = 0;
26 		m->sizeclass = sc;
27 		m->maplen = 0;
28 		*((unsigned char *)m->mem+UNIT-4) = 0;
29 		*((unsigned char *)m->mem+UNIT-3) = 255;
30 		m->mem->storage[size_classes[sc]*UNIT-4] = 0;
31 		queue(&ctx.active[sc], m);
32 		a += (size_classes[sc]+1)*UNIT;
33 	}
34 }
35 
__malloc_donate(char * start,char * end)36 void __malloc_donate(char *start, char *end)
37 {
38 	donate((void *)start, end-start);
39 }
40