Back to index

plt-scheme  4.2.1
vm.c
Go to the documentation of this file.
00001 
00002 /******************************************************************************/
00003 /*                     OS-specific low-level allocator                        */
00004 /******************************************************************************/
00005 
00006 #ifndef GCPRINT
00007 # define GCPRINT fprintf
00008 # define GCOUTF stderr
00009 #endif
00010 
00011 static inline size_t vm_round_up_to_page_size(size_t len, size_t page_size) {
00012   len += (page_size -1) - (len & (page_size - 1));
00013   return len;
00014 };
00015 
00016 #if !( defined(_WIN32) || defined(OSKIT) )
00017 typedef struct {
00018   void *start;
00019   long len;
00020   short age;
00021   short zeroed;
00022 } FreeBlock;
00023 #endif
00024 
00025 typedef struct VM {
00026 #if !( defined(_WIN32) || defined(OSKIT) )
00027   FreeBlock *freeblocks;
00028 #endif
00029   size_t memory_allocated;
00030 } VM;
00031 
00032 static VM *vm_create() {
00033   VM *vm = ofm_malloc(sizeof(VM));
00034   memset(vm, 0, sizeof(VM));
00035 #if !( defined(_WIN32) || defined(OSKIT) )
00036   #define BLOCKFREE_CACHE_SIZE 96
00037   vm->freeblocks = ofm_malloc(sizeof(FreeBlock) * BLOCKFREE_CACHE_SIZE);
00038   memset(vm->freeblocks, 0, sizeof(FreeBlock) * BLOCKFREE_CACHE_SIZE);
00039 #endif
00040   return vm;
00041 }
00042 
00043 static void vm_free(VM *vm) {
00044 #if !( defined(_WIN32) || defined(OSKIT) )
00045   free(vm->freeblocks);
00046 #endif
00047   free(vm);
00048 }
00049 
00050 static size_t vm_memory_allocated(VM *vm) {
00051   return vm->memory_allocated;
00052 }
00053 
00054 static size_t vm_memory_allocated_inc(VM *vm, size_t len) {
00055   vm->memory_allocated += len;
00056   return vm->memory_allocated;
00057 }
00058 
00059 static size_t vm_memory_allocated_dec(VM *vm, size_t len) {
00060   vm->memory_allocated -= len;
00061   return vm->memory_allocated;
00062 }
00063 
00064 
00065 #if _WIN32            /* Windows */
00066 # include "vm_win.c"
00067 #elif defined(OSKIT)  /* OSKit */
00068 # include "vm_osk.c"
00069 #elif defined(OS_X)   /* OS X */
00070 # include "vm_osx.c"
00071 #else                 /* Default: mmap */
00072 # include "vm_mmap.c"
00073 #endif