| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2010 Apple Inc. All rights reserved. | 2 * Copyright (C) 2010 Apple Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
| 8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
| 10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 #include "PageAllocation.h" | 31 #include "PageAllocation.h" |
| 32 #include <errno.h> | 32 #include <errno.h> |
| 33 #include <sys/mman.h> | 33 #include <sys/mman.h> |
| 34 #include <wtf/Assertions.h> | 34 #include <wtf/Assertions.h> |
| 35 #include <wtf/UnusedParam.h> | 35 #include <wtf/UnusedParam.h> |
| 36 | 36 |
| 37 namespace WTF { | 37 namespace WTF { |
| 38 | 38 |
| 39 void* OSAllocator::reserveUncommitted(size_t bytes, Usage usage, bool writable,
bool executable, bool includesGuardPages) | 39 void* OSAllocator::reserveUncommitted(size_t bytes, Usage usage, bool writable,
bool executable, bool includesGuardPages) |
| 40 { | 40 { |
| 41 #if OS(QNX) | 41 #if OS(LINUX) |
| 42 // Reserve memory with PROT_NONE and MAP_LAZY so it isn't committed now. | |
| 43 void* result = mmap(0, bytes, PROT_NONE, MAP_LAZY | MAP_PRIVATE | MAP_ANON,
-1, 0); | |
| 44 if (result == MAP_FAILED) | |
| 45 CRASH(); | |
| 46 #elif OS(LINUX) | |
| 47 UNUSED_PARAM(usage); | 42 UNUSED_PARAM(usage); |
| 48 UNUSED_PARAM(writable); | 43 UNUSED_PARAM(writable); |
| 49 UNUSED_PARAM(executable); | 44 UNUSED_PARAM(executable); |
| 50 UNUSED_PARAM(includesGuardPages); | 45 UNUSED_PARAM(includesGuardPages); |
| 51 | 46 |
| 52 void* result = mmap(0, bytes, PROT_NONE, MAP_NORESERVE | MAP_PRIVATE | MAP_A
NON, -1, 0); | 47 void* result = mmap(0, bytes, PROT_NONE, MAP_NORESERVE | MAP_PRIVATE | MAP_A
NON, -1, 0); |
| 53 if (result == MAP_FAILED) | 48 if (result == MAP_FAILED) |
| 54 CRASH(); | 49 CRASH(); |
| 55 madvise(result, bytes, MADV_DONTNEED); | 50 madvise(result, bytes, MADV_DONTNEED); |
| 56 #else | 51 #else |
| 57 void* result = reserveAndCommit(bytes, usage, writable, executable, includes
GuardPages); | 52 void* result = reserveAndCommit(bytes, usage, writable, executable, includes
GuardPages); |
| 58 #if HAVE(MADV_FREE_REUSE) | 53 #if HAVE(MADV_FREE_REUSE) |
| 59 // To support the "reserve then commit" model, we have to initially decommit
. | 54 // To support the "reserve then commit" model, we have to initially decommit
. |
| 60 while (madvise(result, bytes, MADV_FREE_REUSABLE) == -1 && errno == EAGAIN)
{ } | 55 while (madvise(result, bytes, MADV_FREE_REUSABLE) == -1 && errno == EAGAIN)
{ } |
| 61 #endif | 56 #endif |
| 62 | 57 |
| 63 #endif // OS(QNX) | 58 #endif // OS(LINUX) |
| 64 | 59 |
| 65 return result; | 60 return result; |
| 66 } | 61 } |
| 67 | 62 |
| 68 void* OSAllocator::reserveAndCommit(size_t bytes, Usage usage, bool writable, bo
ol executable, bool includesGuardPages) | 63 void* OSAllocator::reserveAndCommit(size_t bytes, Usage usage, bool writable, bo
ol executable, bool includesGuardPages) |
| 69 { | 64 { |
| 70 // All POSIX reservations start out logically committed. | 65 // All POSIX reservations start out logically committed. |
| 71 int protection = PROT_READ; | 66 int protection = PROT_READ; |
| 72 if (writable) | 67 if (writable) |
| 73 protection |= PROT_WRITE; | 68 protection |= PROT_WRITE; |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 // breaks the madvise based mechanism we use to return physical memory | 114 // breaks the madvise based mechanism we use to return physical memory |
| 120 // to the OS. | 115 // to the OS. |
| 121 mmap(result, pageSize(), PROT_NONE, MAP_FIXED | MAP_PRIVATE | MAP_ANON,
fd, 0); | 116 mmap(result, pageSize(), PROT_NONE, MAP_FIXED | MAP_PRIVATE | MAP_ANON,
fd, 0); |
| 122 mmap(static_cast<char*>(result) + bytes - pageSize(), pageSize(), PROT_N
ONE, MAP_FIXED | MAP_PRIVATE | MAP_ANON, fd, 0); | 117 mmap(static_cast<char*>(result) + bytes - pageSize(), pageSize(), PROT_N
ONE, MAP_FIXED | MAP_PRIVATE | MAP_ANON, fd, 0); |
| 123 } | 118 } |
| 124 return result; | 119 return result; |
| 125 } | 120 } |
| 126 | 121 |
| 127 void OSAllocator::commit(void* address, size_t bytes, bool writable, bool execut
able) | 122 void OSAllocator::commit(void* address, size_t bytes, bool writable, bool execut
able) |
| 128 { | 123 { |
| 129 #if OS(QNX) | 124 #if OS(LINUX) |
| 130 int protection = PROT_READ; | 125 int protection = PROT_READ; |
| 131 if (writable) | 126 if (writable) |
| 132 protection |= PROT_WRITE; | 127 protection |= PROT_WRITE; |
| 133 if (executable) | |
| 134 protection |= PROT_EXEC; | |
| 135 if (MAP_FAILED == mmap(address, bytes, protection, MAP_FIXED | MAP_PRIVATE |
MAP_ANON, -1, 0)) | |
| 136 CRASH(); | |
| 137 #elif OS(LINUX) | |
| 138 int protection = PROT_READ; | |
| 139 if (writable) | |
| 140 protection |= PROT_WRITE; | |
| 141 if (executable) | 128 if (executable) |
| 142 protection |= PROT_EXEC; | 129 protection |= PROT_EXEC; |
| 143 if (mprotect(address, bytes, protection)) | 130 if (mprotect(address, bytes, protection)) |
| 144 CRASH(); | 131 CRASH(); |
| 145 madvise(address, bytes, MADV_WILLNEED); | 132 madvise(address, bytes, MADV_WILLNEED); |
| 146 #elif HAVE(MADV_FREE_REUSE) | 133 #elif HAVE(MADV_FREE_REUSE) |
| 147 UNUSED_PARAM(writable); | 134 UNUSED_PARAM(writable); |
| 148 UNUSED_PARAM(executable); | 135 UNUSED_PARAM(executable); |
| 149 while (madvise(address, bytes, MADV_FREE_REUSE) == -1 && errno == EAGAIN) {
} | 136 while (madvise(address, bytes, MADV_FREE_REUSE) == -1 && errno == EAGAIN) {
} |
| 150 #else | 137 #else |
| 151 // Non-MADV_FREE_REUSE reservations automatically commit on demand. | 138 // Non-MADV_FREE_REUSE reservations automatically commit on demand. |
| 152 UNUSED_PARAM(address); | 139 UNUSED_PARAM(address); |
| 153 UNUSED_PARAM(bytes); | 140 UNUSED_PARAM(bytes); |
| 154 UNUSED_PARAM(writable); | 141 UNUSED_PARAM(writable); |
| 155 UNUSED_PARAM(executable); | 142 UNUSED_PARAM(executable); |
| 156 #endif | 143 #endif |
| 157 } | 144 } |
| 158 | 145 |
| 159 void OSAllocator::decommit(void* address, size_t bytes) | 146 void OSAllocator::decommit(void* address, size_t bytes) |
| 160 { | 147 { |
| 161 #if OS(QNX) | 148 #if OS(LINUX) |
| 162 // Use PROT_NONE and MAP_LAZY to decommit the pages. | |
| 163 mmap(address, bytes, PROT_NONE, MAP_FIXED | MAP_LAZY | MAP_PRIVATE | MAP_ANO
N, -1, 0); | |
| 164 #elif OS(LINUX) | |
| 165 madvise(address, bytes, MADV_DONTNEED); | 149 madvise(address, bytes, MADV_DONTNEED); |
| 166 if (mprotect(address, bytes, PROT_NONE)) | 150 if (mprotect(address, bytes, PROT_NONE)) |
| 167 CRASH(); | 151 CRASH(); |
| 168 #elif HAVE(MADV_FREE_REUSE) | 152 #elif HAVE(MADV_FREE_REUSE) |
| 169 while (madvise(address, bytes, MADV_FREE_REUSABLE) == -1 && errno == EAGAIN)
{ } | 153 while (madvise(address, bytes, MADV_FREE_REUSABLE) == -1 && errno == EAGAIN)
{ } |
| 170 #elif HAVE(MADV_FREE) | 154 #elif HAVE(MADV_FREE) |
| 171 while (madvise(address, bytes, MADV_FREE) == -1 && errno == EAGAIN) { } | 155 while (madvise(address, bytes, MADV_FREE) == -1 && errno == EAGAIN) { } |
| 172 #elif HAVE(MADV_DONTNEED) | 156 #elif HAVE(MADV_DONTNEED) |
| 173 while (madvise(address, bytes, MADV_DONTNEED) == -1 && errno == EAGAIN) { } | 157 while (madvise(address, bytes, MADV_DONTNEED) == -1 && errno == EAGAIN) { } |
| 174 #else | 158 #else |
| 175 UNUSED_PARAM(address); | 159 UNUSED_PARAM(address); |
| 176 UNUSED_PARAM(bytes); | 160 UNUSED_PARAM(bytes); |
| 177 #endif | 161 #endif |
| 178 } | 162 } |
| 179 | 163 |
| 180 void OSAllocator::releaseDecommitted(void* address, size_t bytes) | 164 void OSAllocator::releaseDecommitted(void* address, size_t bytes) |
| 181 { | 165 { |
| 182 int result = munmap(address, bytes); | 166 int result = munmap(address, bytes); |
| 183 if (result == -1) | 167 if (result == -1) |
| 184 CRASH(); | 168 CRASH(); |
| 185 } | 169 } |
| 186 | 170 |
| 187 } // namespace WTF | 171 } // namespace WTF |
| 188 | 172 |
| 189 #endif // OS(UNIX) | 173 #endif // OS(UNIX) |
| OLD | NEW |