math: fix an out of memory issue
[smatch.git] / compat / mmap-blob.c
blob1cab4dead96df37719479db3bb281aac1e21332a
1 #include <sys/mman.h>
2 #include <sys/types.h>
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
13 * Our blob allocator enforces the strict CHUNK size
14 * requirement, as a portability check.
16 void *blob_alloc(unsigned long size)
18 void *ptr;
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;
28 void blob_free(void *addr, unsigned long size)
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