• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

contrib/maloc/src/efence/page.c

00001 #include "efence.h"
00002 #include <stdlib.h>
00003 #include <unistd.h>
00004 #include <fcntl.h>
00005 #include <sys/mman.h>
00006 #include <stdio.h>
00007 #include <errno.h>
00008 #include <string.h>
00009 
00010 /*
00011  * Lots of systems are missing the definition of PROT_NONE.
00012  */
00013 #ifndef    PROT_NONE
00014 #define    PROT_NONE  0
00015 #endif
00016 
00017 /*
00018  * 386 BSD has MAP_ANON instead of MAP_ANONYMOUS.
00019  */
00020 #if ( !defined(MAP_ANONYMOUS) && defined(MAP_ANON) )
00021 #define    MAP_ANONYMOUS         MAP_ANON
00022 #endif
00023 
00024 /*
00025  * For some reason, I can't find mprotect() in any of the headers on
00026  * IRIX or SunOS 4.1.2
00027  */
00028 /* extern C_LINKAGE int mprotect(void * addr, size_t len, int prot); */
00029 
00030 static caddr_t        startAddr = (caddr_t) 0;
00031 
00032 #if ( !defined(sgi) && !defined(_AIX) && !defined(__linux__) )
00033 extern int sys_nerr;
00034 extern char *         sys_errlist[];
00035 #endif
00036 
00037 static const char *
00038 stringErrorReport(void)
00039 {
00040 #if ( defined(sgi) )
00041            return strerror(oserror());
00042 #elif ( defined(_AIX) )
00043            return strerror(errno);
00044 #else
00045            if ( errno > 0 && errno < sys_nerr )
00046                       return sys_errlist[errno];
00047            else
00048                       return "Unknown error.\n";
00049 #endif
00050 }
00051 
00052 /*
00053  * Create memory.
00054  */
00055 #if defined(MAP_ANONYMOUS)
00056 void *
00057 Page_Create(size_t size)
00058 {
00059            caddr_t               allocation;
00060 
00061            /*
00062             * In this version, "startAddr" is a _hint_, not a demand.
00063             * When the memory I map here is contiguous with other
00064             * mappings, the allocator can coalesce the memory from two
00065             * or more mappings into one large contiguous chunk, and thus
00066             * might be able to find a fit that would not otherwise have
00067             * been possible. I could _force_ it to be contiguous by using
00068             * the MMAP_FIXED flag, but I don't want to stomp on memory mappings
00069             * generated by other software, etc.
00070             */
00071            allocation = (caddr_t) mmap(
00072             startAddr
00073            ,(int)size
00074            ,PROT_READ|PROT_WRITE
00075            ,MAP_PRIVATE|MAP_ANONYMOUS
00076            ,-1
00077            ,0);
00078 
00079 #ifndef    __hpux
00080            /*
00081             * Set the "address hint" for the next mmap() so that it will abut
00082             * the mapping we just created.
00083             *
00084             * HP/UX 9.01 has a kernel bug that makes mmap() fail sometimes
00085             * when given a non-zero address hint, so we'll leave the hint set
00086             * to zero on that system. HP recently told me this is now fixed.
00087             * Someone please tell me when it is probable to assume that most
00088             * of those systems that were running 9.01 have been upgraded.
00089             */
00090            startAddr = allocation + size;
00091 #endif
00092 
00093            if ( allocation == (caddr_t)-1 )
00094                       EF_Exit("mmap() failed: %s", stringErrorReport());
00095 
00096            return (void *)allocation;
00097 }
00098 #else
00099 void *
00100 Page_Create(size_t size)
00101 {
00102            static int devZeroFd = -1;
00103            caddr_t               allocation;
00104 
00105            if ( devZeroFd == -1 ) {
00106                       devZeroFd = open("/dev/zero", O_RDWR);
00107                       if ( devZeroFd < 0 )
00108                                  EF_Exit(
00109                                   "open() on /dev/zero failed: %s"
00110                                  ,stringErrorReport());
00111            }
00112 
00113            /*
00114             * In this version, "startAddr" is a _hint_, not a demand.
00115             * When the memory I map here is contiguous with other
00116             * mappings, the allocator can coalesce the memory from two
00117             * or more mappings into one large contiguous chunk, and thus
00118             * might be able to find a fit that would not otherwise have
00119             * been possible. I could _force_ it to be contiguous by using
00120             * the MMAP_FIXED flag, but I don't want to stomp on memory mappings
00121             * generated by other software, etc.
00122             */
00123            allocation = (caddr_t) mmap(
00124             startAddr
00125            ,(int)size
00126            ,PROT_READ|PROT_WRITE
00127            ,MAP_PRIVATE
00128            ,devZeroFd
00129            ,0);
00130 
00131            startAddr = allocation + size;
00132 
00133            if ( allocation == (caddr_t)-1 )
00134                       EF_Exit("mmap() failed: %s", stringErrorReport());
00135 
00136            return (void *)allocation;
00137 }
00138 #endif
00139 
00140 static void
00141 mprotectFailed(void)
00142 {
00143            EF_Exit("mprotect() failed: %s", stringErrorReport());
00144 }
00145 
00146 void
00147 Page_AllowAccess(void * address, size_t size)
00148 {
00149            if ( mprotect((caddr_t)address, size, PROT_READ|PROT_WRITE) < 0 )
00150                       mprotectFailed();
00151 }
00152 
00153 void
00154 Page_DenyAccess(void * address, size_t size)
00155 {
00156            if ( mprotect((caddr_t)address, size, PROT_NONE) < 0 )
00157                       mprotectFailed();
00158 }
00159 
00160 void
00161 Page_Delete(void * address, size_t size)
00162 {
00163            if ( munmap((caddr_t)address, size) < 0 )
00164                       Page_DenyAccess(address, size);
00165 }
00166 
00167 #if defined(_SC_PAGESIZE)
00168 size_t
00169 Page_Size(void)
00170 {
00171            return (size_t)sysconf(_SC_PAGESIZE);
00172 }
00173 #elif defined(_SC_PAGE_SIZE)
00174 size_t
00175 Page_Size(void)
00176 {
00177            return (size_t)sysconf(_SC_PAGE_SIZE);
00178 }
00179 #else
00180 /* extern int         getpagesize(); */
00181 size_t
00182 Page_Size(void)
00183 {
00184            return getpagesize();
00185 }
00186 #endif

Generated on Wed Oct 20 2010 11:12:14 for APBS by  doxygen 1.7.2