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