"Fossies" - the Fresh Open Source Software Archive

Member "Hoard-3.13/src/source/libhoard.cpp" (2 Jan 2019, 4509 Bytes) of package /linux/misc/Hoard-3.13.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 "libhoard.cpp" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 3.12_vs_3.13.

    1 /* -*- C++ -*- */
    2 
    3 /*
    4   The Hoard Multiprocessor Memory Allocator
    5   www.hoard.org
    6 
    7   Author: Emery Berger, http://www.emeryberger.org
    8  
    9   Copyright (c) 1998-2019 Emery Berger
   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 /*
   28  * @file   libhoard.cpp
   29  * @brief  This file replaces malloc etc. in your application.
   30  * @author Emery Berger <http://www.cs.umass.edu/~emery>
   31  */
   32 
   33 #include <cstddef>
   34 #include <new>
   35 
   36 #include "VERSION.h"
   37 
   38 #define versionMessage "Using the Hoard memory allocator (http://www.hoard.org), version " HOARD_VERSION_STRING "\n"
   39 
   40 #include "heaplayers.h"
   41 
   42 // The undef below ensures that any pthread_* calls get strong
   43 // linkage.  Otherwise, our versions here won't replace them.  It is
   44 // IMPERATIVE that this line appear before any files get included.
   45 
   46 #undef __GXX_WEAK__ 
   47 
   48 #if defined(_WIN32)
   49 #define WIN32_LEAN_AND_MEAN
   50 
   51 // Maximize the degree of inlining.
   52 #pragma inline_depth(255)
   53 
   54 // Turn inlining hints into requirements.
   55 #define inline __forceinline
   56 #pragma warning(disable:4273)
   57 #pragma warning(disable: 4098)  // Library conflict.
   58 #pragma warning(disable: 4355)  // 'this' used in base member initializer list.
   59 #pragma warning(disable: 4074)  // initializers put in compiler reserved area.
   60 #pragma warning(disable: 6326)  // comparison between constants.
   61 
   62 #endif
   63 
   64 #if HOARD_NO_LOCK_OPT
   65 // Disable lock optimization.
   66 volatile bool anyThreadCreated = true;
   67 #else
   68 // The normal case. See heaplayers/spinlock.h.
   69 volatile bool anyThreadCreated = false;
   70 #endif
   71 
   72 namespace Hoard {
   73   
   74   // HOARD_MMAP_PROTECTION_MASK defines the protection flags used for
   75   // freshly-allocated memory. The default case is that heap memory is
   76   // NOT executable, thus preventing the class of attacks that inject
   77   // executable code on the heap.
   78   // 
   79   // While this is not recommended, you can define HL_EXECUTABLE_HEAP as
   80   // 1 in heaplayers/heaplayers.h if you really need to (i.e., you're
   81   // doing dynamic code generation into malloc'd space).
   82   
   83 #if HL_EXECUTABLE_HEAP
   84 #define HOARD_MMAP_PROTECTION_MASK (PROT_READ | PROT_WRITE | PROT_EXEC)
   85 #else
   86 #define HOARD_MMAP_PROTECTION_MASK (PROT_READ | PROT_WRITE)
   87 #endif
   88 
   89 } // namespace Hoard
   90 
   91 #include "hoardtlab.h"
   92 
   93 //
   94 // The base Hoard heap.
   95 //
   96 
   97 
   98 /// Maintain a single instance of the main Hoard heap.
   99 
  100 Hoard::HoardHeapType * getMainHoardHeap() {
  101   // This function is C++ magic that ensures that the heap is
  102   // initialized before its first use. First, allocate a static buffer
  103   // to hold the heap.
  104 
  105   static double thBuf[sizeof(Hoard::HoardHeapType) / sizeof(double) + 1];
  106 
  107   // Now initialize the heap into that buffer.
  108   static auto * th = new (thBuf) Hoard::HoardHeapType;
  109   return th;
  110 }
  111 
  112 TheCustomHeapType * getCustomHeap();
  113 
  114 enum { MAX_LOCAL_BUFFER_SIZE = 256 * 131072 };
  115 static char initBuffer[MAX_LOCAL_BUFFER_SIZE];
  116 static char * initBufferPtr = initBuffer;
  117 
  118 extern bool isCustomHeapInitialized();
  119 
  120 extern "C" {
  121 
  122   void * xxmalloc (size_t sz) {
  123     if (isCustomHeapInitialized()) {
  124       void * ptr = getCustomHeap()->malloc (sz);
  125       if (ptr == nullptr) {
  126     fprintf(stderr, "INTERNAL FAILURE.\n");
  127     abort();
  128       }
  129       return ptr;
  130     }
  131     // We still haven't initialized the heap. Satisfy this memory
  132     // request from the local buffer.
  133     void * ptr = initBufferPtr;
  134     initBufferPtr += sz;
  135     if (initBufferPtr > initBuffer + MAX_LOCAL_BUFFER_SIZE) {
  136       abort();
  137     }
  138     {
  139       static bool initialized = false;
  140       if (!initialized) {
  141     initialized = true;
  142 #if !defined(_WIN32)
  143     fprintf(stderr, versionMessage);
  144 #endif
  145       }
  146     }
  147     return ptr;
  148   }
  149 
  150   void xxfree (void * ptr) {
  151     getCustomHeap()->free (ptr);
  152   }
  153 
  154   size_t xxmalloc_usable_size (void * ptr) {
  155     return getCustomHeap()->getSize (ptr);
  156   }
  157 
  158   void xxmalloc_lock() {
  159     // Undefined for Hoard.
  160   }
  161 
  162   void xxmalloc_unlock() {
  163     // Undefined for Hoard.
  164   }
  165 
  166 } // namespace Hoard