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 = (unsigned char *) |
---|
26 | VirtualAlloc(NULL, GGGGC_POOL_BYTES*2, MEM_COMMIT|MEM_RESERVE, PAGE_READWRITE); |
---|
27 | if (space == NULL) { |
---|
28 | if (mustSucceed) { |
---|
29 | perror("mmap"); |
---|
30 | abort(); |
---|
31 | } |
---|
32 | return NULL; |
---|
33 | } |
---|
34 | |
---|
35 | /* align it */ |
---|
36 | ret = GGGGC_POOL_OF(space + GGGGC_POOL_BYTES - 1); |
---|
37 | aspace = (unsigned char *) ret; |
---|
38 | |
---|
39 | /* free unused space */ |
---|
40 | if (aspace > space) |
---|
41 | VirtualFree(space, aspace - space, MEM_RELEASE); |
---|
42 | VirtualFree(aspace + GGGGC_POOL_BYTES, space + GGGGC_POOL_BYTES - aspace, MEM_RELEASE); |
---|
43 | |
---|
44 | return ret; |
---|
45 | } |
---|