OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (c) 2011 The Native Client Authors. All rights reserved. | 2 * Copyright (c) 2011 The Native Client Authors. All rights reserved. |
3 * Use of this source code is governed by a BSD-style license that can be | 3 * Use of this source code is governed by a BSD-style license that can be |
4 * found in the LICENSE file. | 4 * found in the LICENSE file. |
5 */ | 5 */ |
6 | 6 |
7 /* | 7 /* |
8 * NaCl Service Runtime memory allocation code | 8 * NaCl Service Runtime memory allocation code |
9 */ | 9 */ |
10 #include "native_client/src/include/portability.h" | 10 #include "native_client/src/include/portability.h" |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
54 SYSTEM_INFO sys_info; | 54 SYSTEM_INFO sys_info; |
55 | 55 |
56 int attempt_count; | 56 int attempt_count; |
57 | 57 |
58 void *hint = *p; | 58 void *hint = *p; |
59 void *addr; | 59 void *addr; |
60 void *end_addr; | 60 void *end_addr; |
61 void *chunk; | 61 void *chunk; |
62 void *unroll; | 62 void *unroll; |
63 | 63 |
64 MEMORY_BASIC_INFORMATION mem; | |
65 const SIZE_T kOneGb = 0x40000000; | |
66 char* start; | |
67 | |
64 /* | 68 /* |
65 * We have to allocate every 64KB -- the windows allocation | 69 * We have to allocate every 64KB -- the windows allocation |
66 * granularity -- because VirtualFree will only accept an address | 70 * granularity -- because VirtualFree will only accept an address |
67 * that was returned by a call to VirtualAlloc. NB: memory pages | 71 * that was returned by a call to VirtualAlloc. NB: memory pages |
68 * put into the address space via MapViewOfFile{,Ex} must be | 72 * put into the address space via MapViewOfFile{,Ex} must be |
69 * released by UnmapViewOfFile. Thus, in order for us to open up a | 73 * released by UnmapViewOfFile. Thus, in order for us to open up a |
70 * hole in the NaCl application's address space to map in a file, we | 74 * hole in the NaCl application's address space to map in a file, we |
71 * must allocate the entire address space in 64KB chunks, so we can | 75 * must allocate the entire address space in 64KB chunks, so we can |
72 * later pick an arbitrary range of addresses (in 64KB chunks) to | 76 * later pick an arbitrary range of addresses (in 64KB chunks) to |
73 * free up and map in a file later. | 77 * free up and map in a file later. |
(...skipping 20 matching lines...) Expand all Loading... | |
94 sys_info.dwAllocationGranularity, | 98 sys_info.dwAllocationGranularity, |
95 NACL_MAP_PAGESIZE); | 99 NACL_MAP_PAGESIZE); |
96 } | 100 } |
97 | 101 |
98 /* | 102 /* |
99 * Round allocation request up to next NACL_MAP_PAGESIZE. This is | 103 * Round allocation request up to next NACL_MAP_PAGESIZE. This is |
100 * assumed to have taken place in NaCl_page_free. | 104 * assumed to have taken place in NaCl_page_free. |
101 */ | 105 */ |
102 num_bytes = NaClRoundAllocPage(num_bytes); | 106 num_bytes = NaClRoundAllocPage(num_bytes); |
103 | 107 |
108 /* | |
109 * If the request is for 1Gb (the sandbox), look for a memory region that | |
110 * was reserved by our parent process. | |
111 */ | |
112 addr = 0; | |
113 if (num_bytes == kOneGb) { | |
Mark Seaborn
2011/08/12 21:57:54
Having a special case for 1GB in NaCl_page_alloc()
| |
114 start = sys_info.lpMinimumApplicationAddress; | |
115 while (VirtualQuery((LPCVOID)start, &mem, sizeof(mem))) | |
116 { | |
Mark Seaborn
2011/08/12 21:57:54
Style: '{' should be on previous line
| |
117 if (mem.State == MEM_RESERVE && mem.RegionSize == kOneGb) | |
118 { | |
Mark Seaborn
2011/08/12 21:57:54
Ditto
| |
119 addr = start; | |
120 break; | |
121 } | |
122 start += mem.RegionSize; | |
123 if ((LPVOID)start > sys_info.lpMaximumApplicationAddress) | |
124 break; | |
125 } | |
126 } | |
127 | |
104 for (attempt_count = 0; | 128 for (attempt_count = 0; |
105 attempt_count < NACL_MEMORY_ALLOC_RETRY_MAX; | 129 attempt_count < NACL_MEMORY_ALLOC_RETRY_MAX; |
106 ++attempt_count) { | 130 ++attempt_count) { |
107 | 131 |
108 addr = VirtualAlloc(hint, num_bytes, MEM_RESERVE, PAGE_NOACCESS); | 132 if (addr == NULL) |
133 addr = VirtualAlloc(hint, num_bytes, MEM_RESERVE, PAGE_NOACCESS); | |
109 if (addr == NULL) { | 134 if (addr == NULL) { |
110 NaClLog(0, | 135 NaClLog(0, |
111 "NaCl_page_alloc: VirtualAlloc(*,0x%"NACL_PRIxS") failed\n", | 136 "NaCl_page_alloc: VirtualAlloc(*,0x%"NACL_PRIxS") failed\n", |
112 num_bytes); | 137 num_bytes); |
113 return -ENOMEM; | 138 return -ENOMEM; |
114 } | 139 } |
115 NaClLog(3, | 140 NaClLog(3, |
116 ("NaCl_page_alloc:" | 141 ("NaCl_page_alloc:" |
117 " VirtualAlloc(*,0x%"NACL_PRIxS")" | 142 " VirtualAlloc(*,0x%"NACL_PRIxS")" |
118 " succeeded, 0x%016"NACL_PRIxPTR"," | 143 " succeeded, 0x%016"NACL_PRIxPTR"," |
(...skipping 27 matching lines...) Expand all Loading... | |
146 } | 171 } |
147 NaClLog(2, | 172 NaClLog(2, |
148 ("NaCl_page_alloc: *p = 0x%016"NACL_PRIxPTR"," | 173 ("NaCl_page_alloc: *p = 0x%016"NACL_PRIxPTR"," |
149 " returning success.\n"), | 174 " returning success.\n"), |
150 (uintptr_t) addr); | 175 (uintptr_t) addr); |
151 *p = addr; | 176 *p = addr; |
152 return 0; | 177 return 0; |
153 retry: | 178 retry: |
154 NaClLog(2, "NaCl_page_alloc_hint: retrying w/o hint\n"); | 179 NaClLog(2, "NaCl_page_alloc_hint: retrying w/o hint\n"); |
155 hint = NULL; | 180 hint = NULL; |
181 /* | |
182 * If the parent process had reserved memory, it's gone now. | |
183 */ | |
184 addr = NULL; | |
156 } | 185 } |
157 | 186 |
158 return -ENOMEM; | 187 return -ENOMEM; |
159 } | 188 } |
160 | 189 |
161 int NaCl_page_alloc(void **p, | 190 int NaCl_page_alloc(void **p, |
162 size_t num_bytes) { | 191 size_t num_bytes) { |
163 *p = NULL; | 192 *p = NULL; |
164 return NaCl_page_alloc_hint(p, num_bytes); | 193 return NaCl_page_alloc_hint(p, num_bytes); |
165 } | 194 } |
(...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
399 break; | 428 break; |
400 case MADV_NORMAL: | 429 case MADV_NORMAL: |
401 memset(start, 0, length); | 430 memset(start, 0, length); |
402 break; | 431 break; |
403 default: | 432 default: |
404 return -EINVAL; | 433 return -EINVAL; |
405 } | 434 } |
406 NaClLog(2, "NaCl_madvise: done\n"); | 435 NaClLog(2, "NaCl_madvise: done\n"); |
407 return 0; | 436 return 0; |
408 } | 437 } |
OLD | NEW |