| 1 | /* Copyright (C) 2011 Wildfire Games.
|
|---|
| 2 | *
|
|---|
| 3 | * Permission is hereby granted, free of charge, to any person obtaining
|
|---|
| 4 | * a copy of this software and associated documentation files (the
|
|---|
| 5 | * "Software"), to deal in the Software without restriction, including
|
|---|
| 6 | * without limitation the rights to use, copy, modify, merge, publish,
|
|---|
| 7 | * distribute, sublicense, and/or sell copies of the Software, and to
|
|---|
| 8 | * permit persons to whom the Software is furnished to do so, subject to
|
|---|
| 9 | * the following conditions:
|
|---|
| 10 | *
|
|---|
| 11 | * The above copyright notice and this permission notice shall be included
|
|---|
| 12 | * in all copies or substantial portions of the Software.
|
|---|
| 13 | *
|
|---|
| 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|---|
| 15 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|---|
| 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|---|
| 17 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|---|
| 18 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|---|
| 19 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|---|
| 20 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|---|
| 21 | */
|
|---|
| 22 |
|
|---|
| 23 | #include "precompiled.h"
|
|---|
| 24 | #include "lib/sysdep/vm.h"
|
|---|
| 25 |
|
|---|
| 26 | #include "lib/alignment.h"
|
|---|
| 27 |
|
|---|
| 28 | // "anonymous" effectively means mapping /dev/zero, but is more efficient.
|
|---|
| 29 | // MAP_ANONYMOUS is not in SUSv3, but is a very common extension.
|
|---|
| 30 | // unfortunately, MacOS X only defines MAP_ANON, which Solaris says is
|
|---|
| 31 | // deprecated. workaround there: define MAP_ANONYMOUS in terms of MAP_ANON.
|
|---|
| 32 | #ifndef MAP_ANONYMOUS
|
|---|
| 33 | # define MAP_ANONYMOUS MAP_ANON
|
|---|
| 34 | #endif
|
|---|
| 35 |
|
|---|
| 36 | static const int mmap_flags = MAP_PRIVATE|MAP_ANONYMOUS;
|
|---|
| 37 |
|
|---|
| 38 | namespace vm {
|
|---|
| 39 |
|
|---|
| 40 | void* ReserveAddressSpace(size_t size, size_t UNUSED(commitSize), PageType UNUSED(pageType), int UNUSED(prot))
|
|---|
| 41 | {
|
|---|
| 42 | errno = 0;
|
|---|
| 43 | void* p = mmap(0, size, PROT_NONE, mmap_flags|MAP_NORESERVE, -1, 0);
|
|---|
| 44 | if(p == MAP_FAILED)
|
|---|
| 45 | return 0;
|
|---|
| 46 | return p;
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | void ReleaseAddressSpace(void* p, size_t size)
|
|---|
| 50 | {
|
|---|
| 51 | ENSURE(size != 0);
|
|---|
| 52 |
|
|---|
| 53 | errno = 0;
|
|---|
| 54 | if(munmap(p, size) != 0)
|
|---|
| 55 | DEBUG_WARN_ERR(StatusFromErrno());
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 | bool Commit(uintptr_t address, size_t size, PageType UNUSED(pageType), int prot)
|
|---|
| 60 | {
|
|---|
| 61 | if(prot == PROT_NONE) // would be understood as a request to decommit
|
|---|
| 62 | {
|
|---|
| 63 | DEBUG_WARN_ERR(ERR::INVALID_PARAM);
|
|---|
| 64 | return false;
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | errno = 0;
|
|---|
| 68 | if(mmap((void*)address, size, prot, mmap_flags|MAP_FIXED, -1, 0) == MAP_FAILED)
|
|---|
| 69 | return false;
|
|---|
| 70 |
|
|---|
| 71 | if(prot != (PROT_READ|PROT_WRITE))
|
|---|
| 72 | (void)Protect(address, size, prot);
|
|---|
| 73 |
|
|---|
| 74 | return true;
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | bool Decommit(uintptr_t address, size_t size)
|
|---|
| 78 | {
|
|---|
| 79 | errno = 0;
|
|---|
| 80 | if(mmap((void*)address, size, PROT_NONE, mmap_flags|MAP_NORESERVE|MAP_FIXED, -1, 0) == MAP_FAILED)
|
|---|
| 81 | return false;
|
|---|
| 82 | return true;
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 | bool Protect(uintptr_t address, size_t size, int prot)
|
|---|
| 87 | {
|
|---|
| 88 | errno = 0;
|
|---|
| 89 | if(mprotect((void*)address, size, prot) != 0)
|
|---|
| 90 | {
|
|---|
| 91 | DEBUG_WARN_ERR(ERR::FAIL);
|
|---|
| 92 | return false;
|
|---|
| 93 | }
|
|---|
| 94 | return true;
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 |
|
|---|
| 98 | void* Allocate(size_t size, PageType pageType, int prot)
|
|---|
| 99 | {
|
|---|
| 100 | void* p = ReserveAddressSpace(size);
|
|---|
| 101 | if(!p)
|
|---|
| 102 | return 0;
|
|---|
| 103 |
|
|---|
| 104 | if(!Commit(uintptr_t(p), size, pageType, prot))
|
|---|
| 105 | {
|
|---|
| 106 | ReleaseAddressSpace(p, size);
|
|---|
| 107 | return 0;
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | return p;
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | void Free(void* p, size_t size)
|
|---|
| 114 | {
|
|---|
| 115 | // (only the Windows implementation distinguishes between Free and ReleaseAddressSpace)
|
|---|
| 116 | vm::ReleaseAddressSpace(p, size);
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 |
|
|---|
| 120 | void BeginOnDemandCommits()
|
|---|
| 121 | {
|
|---|
| 122 | // not yet implemented, but possible with a signal handler
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | void EndOnDemandCommits()
|
|---|
| 126 | {
|
|---|
| 127 | // not yet implemented, but possible with a signal handler
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 | void DumpStatistics()
|
|---|
| 132 | {
|
|---|
| 133 | // we haven't collected any statistics
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | } // namespace vm
|
|---|