Back to index

plt-scheme  4.2.1
vm_mmap.c
Go to the documentation of this file.
00001 /* Provides: */
00002 /*
00003 static void os_vm_free_pages(void *p, size_t len);
00004 static void *os_vm_alloc_pages(size_t len);
00005 static void vm_protect_pages(void *p, size_t len, int writeable);
00006 */
00007 /* Requires: */
00008 /* Optional:
00009       DONT_NEED_MAX_HEAP_SIZE --- to disable a provide
00010 */
00011 
00012 #include <unistd.h>
00013 #include <fcntl.h>
00014 #include <sys/types.h>
00015 #include <sys/mman.h>
00016 #include <errno.h>
00017 
00018 static long page_size;
00019 
00020 static void os_vm_free_pages(void *p, size_t len)
00021 {
00022   if (munmap(p, len)) {
00023     GCPRINT(GCOUTF, "Unmap warning: %lx, %ld, %d\n", (long)p, (long)len, errno);
00024   }
00025 }
00026 
00027 static void *os_vm_alloc_pages(size_t len)
00028 {
00029   void *r;
00030 
00031 #ifndef MAP_ANON
00032   static int fd;
00033   static int fd_created;
00034 
00035   if (!fd_created) {
00036     fd_created = 1;
00037     fd = open("/dev/zero", O_RDWR);
00038   }
00039 #endif
00040 
00041   /* Round up to nearest page: */
00042   if (len & (page_size - 1))
00043     len += page_size - (len & (page_size - 1));
00044 
00045 #ifdef MAP_ANON
00046   r = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
00047 #else
00048   r = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
00049 #endif
00050 
00051   if (r  == (void *)-1)
00052     return NULL;
00053 
00054   return r;
00055 }
00056 
00057 
00058 static void vm_protect_pages(void *p, size_t len, int writeable)
00059 {
00060   if (len & (page_size - 1)) {
00061     len += page_size - (len & (page_size - 1));
00062   }
00063 
00064   mprotect(p, len, (writeable ? (PROT_READ | PROT_WRITE) : PROT_READ));
00065 }
00066 
00067 #include "alloc_cache.c"
00068 #include "rlimit_heapsize.c"