1 | /*
|
---|
2 | * Allocation functions (mmap)
|
---|
3 | *
|
---|
4 | * Copyright (c) 2014, 2015 Gregor Richards
|
---|
5 | *
|
---|
6 | * Permission to use, copy, modify, and/or distribute this software for any
|
---|
7 | * purpose with or without fee is hereby granted, provided that the above
|
---|
8 | * copyright notice and this permission notice appear in all copies.
|
---|
9 | *
|
---|
10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
---|
11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
---|
12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
---|
13 | * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
---|
14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
---|
15 | * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
---|
16 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
---|
17 | */
|
---|
18 |
|
---|
19 | static void *allocPool(int mustSucceed)
|
---|
20 | {
|
---|
21 | unsigned char *space, *aspace;
|
---|
22 | struct GGGGC_Pool *ret;
|
---|
23 |
|
---|
24 | /* allocate enough space that we can align it later */
|
---|
25 | space = mmap(NULL, GGGGC_POOL_BYTES*2, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0);
|
---|
26 | if (space == NULL) {
|
---|
27 | if (mustSucceed) {
|
---|
28 | perror("mmap");
|
---|
29 | abort();
|
---|
30 | }
|
---|
31 | return NULL;
|
---|
32 | }
|
---|
33 |
|
---|
34 | /* align it */
|
---|
35 | ret = GGGGC_POOL_OF(space + GGGGC_POOL_BYTES - 1);
|
---|
36 | aspace = (unsigned char *) ret;
|
---|
37 |
|
---|
38 | /* free unused space */
|
---|
39 | if (aspace > space)
|
---|
40 | munmap(space, aspace - space);
|
---|
41 | munmap(aspace + GGGGC_POOL_BYTES, space + GGGGC_POOL_BYTES - aspace);
|
---|
42 |
|
---|
43 | return ret;
|
---|
44 | }
|
---|