"Fossies" - the Fresh Open Source Software Archive

Member "ponyc-0.33.0/src/libponyrt/mem/alloc.c" (1 Nov 2019, 1508 Bytes) of package /linux/misc/ponyc-0.33.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "alloc.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 0.30.0_vs_0.31.0.

    1 #ifdef __linux__
    2 #define _GNU_SOURCE
    3 #endif
    4 #include <platform.h>
    5 #include <string.h>
    6 #include <stdio.h>
    7 
    8 #ifdef PLATFORM_IS_POSIX_BASED
    9 #include <sys/mman.h>
   10 #endif
   11 
   12 #if defined(PLATFORM_IS_MACOSX)
   13 #include <mach/vm_statistics.h>
   14 #endif
   15 
   16 void* ponyint_virt_alloc(size_t bytes)
   17 {
   18   void* p;
   19   bool ok = true;
   20 
   21 #if defined(PLATFORM_IS_WINDOWS)
   22   p = VirtualAlloc(NULL, bytes, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
   23   if(p == NULL)
   24     ok = false;
   25 #elif defined(PLATFORM_IS_POSIX_BASED)
   26 #if defined(PLATFORM_IS_LINUX)
   27   p = mmap(0, bytes, PROT_READ | PROT_WRITE,
   28     MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0);
   29 #elif defined(PLATFORM_IS_MACOSX)
   30   p = mmap(0, bytes, PROT_READ | PROT_WRITE,
   31     MAP_PRIVATE | MAP_ANON | VM_FLAGS_SUPERPAGE_SIZE_ANY, -1, 0);
   32 #elif defined(PLATFORM_IS_DRAGONFLY)
   33   p = mmap(0, bytes, PROT_READ | PROT_WRITE,
   34     MAP_PRIVATE | MAP_ANON, -1, 0);
   35 #elif defined(PLATFORM_IS_OPENBSD)
   36   p = mmap(0, bytes, PROT_READ | PROT_WRITE,
   37     MAP_PRIVATE | MAP_ANON, -1, 0);
   38 #elif defined(PLATFORM_IS_BSD)
   39 #ifndef MAP_ALIGNED_SUPER
   40 #define MAP_ALIGNED_SUPER 0
   41 #endif
   42   p = mmap(0, bytes, PROT_READ | PROT_WRITE,
   43     MAP_PRIVATE | MAP_ANON | MAP_ALIGNED_SUPER, -1, 0);
   44 #endif
   45   if(p == MAP_FAILED)
   46     ok = false;
   47 #endif
   48 
   49   if(!ok)
   50   {
   51     perror("out of memory: ");
   52     abort();
   53   }
   54 
   55   return p;
   56 }
   57 
   58 void ponyint_virt_free(void* p, size_t bytes)
   59 {
   60 #if defined(PLATFORM_IS_WINDOWS)
   61   VirtualFree(p, 0, MEM_RELEASE);
   62 #elif defined(PLATFORM_IS_POSIX_BASED)
   63   munmap(p, bytes);
   64 #endif
   65 }