14
* Lots of systems are missing the definition of PROT_NONE.
21
* 386 BSD has MAP_ANON instead of MAP_ANONYMOUS.
23
#if ( !defined(MAP_ANONYMOUS) && defined(MAP_ANON) )
24
#define MAP_ANONYMOUS MAP_ANON
28
* For some reason, I can't find mprotect() in any of the headers on
31
/* extern C_LINKAGE int mprotect(void * addr, size_t len, int prot); */
33
static caddr_t startAddr = (caddr_t) 0;
36
stringErrorReport(void)
38
return strerror(errno);
44
#if defined(MAP_ANONYMOUS)
46
Page_Create(size_t size)
51
* In this version, "startAddr" is a _hint_, not a demand.
52
* When the memory I map here is contiguous with other
53
* mappings, the allocator can coalesce the memory from two
54
* or more mappings into one large contiguous chunk, and thus
55
* might be able to find a fit that would not otherwise have
56
* been possible. I could _force_ it to be contiguous by using
57
* the MMAP_FIXED flag, but I don't want to stomp on memory mappings
58
* generated by other software, etc.
60
allocation = (caddr_t) mmap(
64
,MAP_PRIVATE|MAP_ANONYMOUS
70
* Set the "address hint" for the next mmap() so that it will abut
71
* the mapping we just created.
73
* HP/UX 9.01 has a kernel bug that makes mmap() fail sometimes
74
* when given a non-zero address hint, so we'll leave the hint set
75
* to zero on that system. HP recently told me this is now fixed.
76
* Someone please tell me when it is probable to assume that most
77
* of those systems that were running 9.01 have been upgraded.
79
startAddr = allocation + size;
82
if ( allocation == (caddr_t)-1 )
83
EF_Exit("mmap() failed: %s", stringErrorReport());
85
return (void *)allocation;
89
Page_Create(size_t size)
91
static int devZeroFd = -1;
94
if ( devZeroFd == -1 ) {
95
devZeroFd = open("/dev/zero", O_RDWR);
98
"open() on /dev/zero failed: %s"
99
,stringErrorReport());
103
* In this version, "startAddr" is a _hint_, not a demand.
104
* When the memory I map here is contiguous with other
105
* mappings, the allocator can coalesce the memory from two
106
* or more mappings into one large contiguous chunk, and thus
107
* might be able to find a fit that would not otherwise have
108
* been possible. I could _force_ it to be contiguous by using
109
* the MMAP_FIXED flag, but I don't want to stomp on memory mappings
110
* generated by other software, etc.
112
allocation = (caddr_t) mmap(
115
,PROT_READ|PROT_WRITE
120
startAddr = allocation + size;
122
if ( allocation == (caddr_t)-1 )
123
EF_Exit("mmap() failed: %s", stringErrorReport());
125
return (void *)allocation;
132
EF_Exit("mprotect() failed: %s", stringErrorReport());
136
Page_AllowAccess(void * address, size_t size)
138
if ( mprotect((caddr_t)address, size, PROT_READ|PROT_WRITE) < 0 )
143
Page_DenyAccess(void * address, size_t size)
145
if ( mprotect((caddr_t)address, size, PROT_NONE) < 0 )
150
Page_Delete(void * address, size_t size)
152
if ( munmap((caddr_t)address, size) < 0 )
153
Page_DenyAccess(address, size);
156
#if defined(_SC_PAGESIZE)
160
return (size_t)sysconf(_SC_PAGESIZE);
162
#elif defined(_SC_PAGE_SIZE)
166
return (size_t)sysconf(_SC_PAGE_SIZE);
169
/* extern int getpagesize(); */
173
return getpagesize();