Back to index

plt-scheme  4.2.1
vm_memalign.c
Go to the documentation of this file.
00001 /* 
00002    Provides:
00003       posix_memalign-based allocator
00004       determine_max_heap_size() (uses rlimit_heapsize.c)
00005    Requires:
00006    Optional:
00007       DONT_NEED_MAX_HEAP_SIZE --- to disable a provide
00008 */
00009 
00010 #include <unistd.h>
00011 #include <sys/types.h>
00012 #include <sys/mman.h>
00013 #include <errno.h>
00014 
00015 static int page_size; /* OS page size */
00016 
00017 static void *vm_malloc_pages(VM *vm, size_t len, size_t alignment, int dirty_ok)
00018 {
00019   void *r;
00020 
00021   if (!page_size)
00022     page_size = getpagesize();
00023 
00024   /* Round up to nearest page: */
00025   if (len & (page_size - 1))
00026     len += page_size - (len & (page_size - 1));
00027 
00028   if (posix_memalign(&r, alignment, len)) {
00029     if (errno == EINVAL)
00030       printf("Invalid request\n");
00031     return NULL;
00032   }
00033 
00034   vm_memory_allocated_inc(vm, len);
00035 
00036   if(!dirty_ok)
00037     memset(p, 0, len);
00038   return r;
00039 }
00040 
00041 static void vm_free_pages(VM *vm, void *p, size_t len)
00042 {
00043   vm_memory_allocated_dec(vm, len);
00044   free(p);
00045 }
00046 
00047 static void vm_flush_freed_pages(VM *vm)
00048 {
00049 }
00050 
00051 static void vm_protect_pages(void *p, size_t len, int writeable)
00052 {
00053   if (len & (page_size - 1)) {
00054     len += page_size - (len & (page_size - 1));
00055   }
00056 
00057   mprotect(p, len, (writeable ? (PROT_READ | PROT_WRITE) : PROT_READ));
00058 }
00059 
00060 /*************************************************************/
00061 
00062 # include "rlimit_heapsize.c"