"Fossies" - the Fresh Open Source Software Archive

Member "heaplayers-351/heaplayers/mmapwrapper.h" (8 Sep 2005, 3011 Bytes) of package /linux/misc/old/heaplayers_3_5_1.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 "mmapwrapper.h" see the Fossies "Dox" file reference documentation.

    1 // -*- C++ -*-
    2 
    3 /*
    4 
    5   Heap Layers: An Extensible Memory Allocation Infrastructure
    6   
    7   Copyright (C) 2000-2005 by Emery Berger
    8   http://www.cs.umass.edu/~emery
    9   emery@cs.umass.edu
   10   
   11   This program is free software; you can redistribute it and/or modify
   12   it under the terms of the GNU General Public License as published by
   13   the Free Software Foundation; either version 2 of the License, or
   14   (at your option) any later version.
   15   
   16   This program is distributed in the hope that it will be useful,
   17   but WITHOUT ANY WARRANTY; without even the implied warranty of
   18   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   19   GNU General Public License for more details.
   20   
   21   You should have received a copy of the GNU General Public License
   22   along with this program; if not, write to the Free Software
   23   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   24 
   25 */
   26 
   27 #ifndef _MMAPWRAPPER_H_
   28 #define _MMAPWRAPPER_H_
   29 
   30 #if defined(_WIN32)
   31 #include <windows.h>
   32 #else
   33 // UNIX
   34 #include <sys/types.h>
   35 #include <sys/stat.h>
   36 #include <fcntl.h>
   37 #include <unistd.h>
   38 #include <sys/mman.h>
   39 #include <map>
   40 #endif
   41 
   42 #if HL_EXECUTABLE_HEAP
   43 #define HL_MMAP_PROTECTION_MASK (PROT_READ | PROT_WRITE | PROT_EXEC)
   44 #else
   45 #define HL_MMAP_PROTECTION_MASK (PROT_READ | PROT_WRITE)
   46 #endif
   47 
   48 #if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
   49 #define MAP_ANONYMOUS MAP_ANON
   50 #endif
   51 
   52 namespace HL {
   53 
   54 class MmapWrapper {
   55 public:
   56 
   57 #if defined(_WIN32) 
   58   
   59   // Microsoft Windows has 4K pages aligned to a 64K boundary.
   60   enum { Size = 4 * 1024 };
   61   enum { Alignment = 64 * 1024 };
   62 
   63   static void * map (size_t sz) {
   64     void * ptr;
   65 #if HL_EXECUTABLE_HEAP
   66     const int permflags = PAGE_EXECUTE_READWRITE;
   67 #else
   68     const int permflags = PAGE_READWRITE;
   69 #endif
   70     ptr = VirtualAlloc (NULL, sz, MEM_RESERVE | MEM_COMMIT | MEM_TOP_DOWN, permflags);
   71     return  ptr;
   72   }
   73   
   74   static void unmap (void * ptr, size_t) {
   75     VirtualFree (ptr, 0, MEM_RELEASE);
   76   }
   77 
   78 #else
   79 
   80 #if defined(__SVR4)
   81   // Solaris aligns 8K pages to a 64K boundary.
   82   enum { Size = 8 * 1024 };
   83   enum { Alignment = 64 * 1024 };
   84 #else
   85   // Linux and most other operating systems align memory to a 4K boundary.
   86   enum { Size = 4 * 1024 };
   87   enum { Alignment = 4 * 1024 };
   88 #endif
   89 
   90   static void * map (size_t sz) {
   91 
   92     if (sz == 0) {
   93       return NULL;
   94     }
   95 
   96     void * ptr;
   97 
   98 #if defined(MAP_ALIGN) && defined(MAP_ANON)
   99     // Request memory aligned to the Alignment value above.
  100     ptr = mmap ((char *) Alignment, sz, HL_MMAP_PROTECTION_MASK, MAP_PRIVATE | MAP_ALIGN | MAP_ANON, -1, 0);
  101 #elif !defined(MAP_ANONYMOUS)
  102     static int fd = ::open ("/dev/zero", O_RDWR);
  103     ptr = mmap (NULL, sz, HL_MMAP_PROTECTION_MASK, MAP_PRIVATE, fd, 0);
  104 #else
  105     ptr = mmap (NULL, sz, HL_MMAP_PROTECTION_MASK, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  106 #endif
  107 
  108     if (ptr == MAP_FAILED) {
  109       return NULL;
  110     } else {
  111       return ptr;
  112     }
  113   }
  114 
  115   static void unmap (void * ptr, size_t sz) {
  116     munmap (reinterpret_cast<char *>(ptr), sz);
  117   }
  118    
  119 #endif
  120 
  121 };
  122 
  123 }
  124 
  125 #endif