Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
page_aligned.cpp
Go to the documentation of this file.
1 /* Copyright (c) 2010 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"
25 
26 #include "lib/alignment.h"
27 #include "lib/sysdep/cpu.h" // cpu_CAS
28 
29 
30 //-----------------------------------------------------------------------------
31 
32 static inline Status StatusFromMap(void* ret)
33 {
34  if(ret != MAP_FAILED)
35  return INFO::OK;
37 }
38 
39 // "anonymous" effectively means mapping /dev/zero, but is more efficient.
40 // MAP_ANONYMOUS is not in SUSv3, but is a very common extension.
41 // unfortunately, MacOS X only defines MAP_ANON, which Solaris says is
42 // deprecated. workaround there: define MAP_ANONYMOUS in terms of MAP_ANON.
43 #ifndef MAP_ANONYMOUS
44 # define MAP_ANONYMOUS MAP_ANON
45 #endif
46 
48 
49 Status mem_Reserve(size_t size, u8** pp)
50 {
51  errno = 0;
52  void* ret = mmap(0, size, PROT_NONE, mmap_flags|MAP_NORESERVE, -1, 0);
53  *pp = (u8*)ret;
54  return StatusFromMap(ret);
55 }
56 
57 Status mem_Release(u8* p, size_t size)
58 {
59  errno = 0;
60  if(munmap(p, size) != 0)
62  return 0;
63 }
64 
65 Status mem_Commit(u8* p, size_t size, int prot)
66 {
67  // avoid misinterpretation by mmap.
68  if(prot == PROT_NONE)
70 
71  errno = 0;
72  void* ret = mmap(p, size, prot, mmap_flags|MAP_FIXED, -1, 0);
73  return StatusFromMap(ret);
74 }
75 
76 Status mem_Decommit(u8* p, size_t size)
77 {
78  errno = 0;
79  void* ret = mmap(p, size, PROT_NONE, mmap_flags|MAP_NORESERVE|MAP_FIXED, -1, 0);
80  return StatusFromMap(ret);
81 }
82 
83 Status mem_Protect(u8* p, size_t size, int prot)
84 {
85  errno = 0;
86  if(mprotect(p, size, prot) != 0)
88  return 0;
89 
90 }
91 
92 
93 //-----------------------------------------------------------------------------
94 
95 void* page_aligned_alloc(size_t size)
96 {
97  const size_t alignedSize = Align<pageSize>(size);
98  u8* p = 0;
99  RETURN_0_IF_ERR(mem_Reserve(alignedSize, &p));
101  return p;
102 }
103 
104 
105 void page_aligned_free(void* p, size_t size)
106 {
107  if(!p)
108  return;
110  const size_t alignedSize = Align<pageSize>(size);
111  (void)mem_Release((u8*)p, alignedSize);
112 }
#define PROT_WRITE
Definition: wmman.h:33
#define u8
Definition: types.h:39
void page_aligned_free(void *p, size_t size)
free a previously allocated page-aligned region.
static const size_t pageSize
Definition: alignment.h:61
Status mem_Commit(u8 *p, size_t size, int prot)
const Status OK
Definition: status.h:386
int mprotect(void *addr, size_t len, int prot)
Definition: wmman.cpp:67
Status mem_Release(u8 *p, size_t size)
static const int mmap_flags
Status mem_Decommit(u8 *p, size_t size)
bool IsAligned(T t, uintptr_t multiple)
Definition: alignment.h:8
#define ENSURE(expr)
ensure the expression &lt;expr&gt; evaluates to non-zero.
Definition: debug.h:282
void * mmap(void *start, size_t len, int prot, int flags, int fd, off_t ofs)
Definition: wmman.cpp:202
#define MAP_FAILED
Definition: wmman.h:52
#define MAP_FIXED
Definition: wmman.h:39
void * page_aligned_alloc(size_t size)
allocate memory aligned to the system page size.
const Status INVALID_PARAM
Definition: status.h:423
i64 Status
Error handling system.
Definition: status.h:171
Status mem_Protect(u8 *p, size_t size, int prot)
#define PROT_READ
Definition: wmman.h:32
#define MAP_PRIVATE
Definition: wmman.h:38
Status StatusFromErrno()
Definition: status.cpp:105
Status mem_Reserve(size_t size, u8 **pp)
#define RETURN_0_IF_ERR(expression)
Definition: status.h:350
#define MAP_NORESERVE
Definition: wmman.h:42
#define WARN_RETURN(status)
Definition: status.h:255
static Status StatusFromMap(void *ret)
int munmap(void *start, size_t len)
Definition: wmman.cpp:222
#define MAP_ANONYMOUS
#define PROT_NONE
Definition: wmman.h:31