xref: /illumos-gate/usr/src/tools/smatch/src/compat/mmap-blob.c (revision c3232c9513a02e659ae9307db0fc93d9730704f6)
1 #include <sys/mman.h>
2 #include <sys/types.h>
3 
4 /*
5  * Allow old BSD naming too, it would be a pity to have to make a
6  * separate file just for this.
7  */
8 #ifndef MAP_ANONYMOUS
9 #define MAP_ANONYMOUS MAP_ANON
10 #endif
11 
12 /*
13  * Our blob allocator enforces the strict CHUNK size
14  * requirement, as a portability check.
15  */
16 void *blob_alloc(unsigned long size)
17 {
18 	void *ptr;
19 
20 	if (size & ~CHUNK)
21 		die("internal error: bad allocation size (%lu bytes)", size);
22 	ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
23 	if (ptr == MAP_FAILED)
24 		ptr = NULL;
25 	return ptr;
26 }
27 
28 void blob_free(void *addr, unsigned long size)
29 {
30 	if (!size || (size & ~CHUNK) || ((unsigned long) addr & 512))
31 		die("internal error: bad blob free (%lu bytes at %p)", size, addr);
32 #ifndef DEBUG
33 	munmap(addr, size);
34 #else
35 	mprotect(addr, size, PROT_NONE);
36 #endif
37 }
38