"Fossies" - the Fresh Open Source Software Archive

Member "Hoard-3.13/src/include/util/bumpalloc.h" (2 Jan 2019, 2468 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 "bumpalloc.h" 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 
    5   The Hoard Multiprocessor Memory Allocator
    6   www.hoard.org
    7 
    8   Author: Emery Berger, http://www.emeryberger.com
    9  
   10   Copyright (c) 1998-2018 Emery Berger
   11   
   12   This program is free software; you can redistribute it and/or modify
   13   it under the terms of the GNU General Public License as published by
   14   the Free Software Foundation; either version 2 of the License, or
   15   (at your option) any later version.
   16   
   17   This program is distributed in the hope that it will be useful,
   18   but WITHOUT ANY WARRANTY; without even the implied warranty of
   19   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   20   GNU General Public License for more details.
   21   
   22   You should have received a copy of the GNU General Public License
   23   along with this program; if not, write to the Free Software
   24   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   25 
   26 */
   27 
   28 #ifndef HOARD_BUMPALLOC_H
   29 #define HOARD_BUMPALLOC_H
   30 
   31 /**
   32  * @class BumpAlloc
   33  * @brief Obtains memory in chunks and bumps a pointer through the chunks.
   34  * @author Emery Berger <http://www.emeryberger.com>
   35  */
   36 
   37 #include "mallocinfo.h"
   38 
   39 
   40 namespace Hoard {
   41 
   42   template <int ChunkSize,
   43     class SuperHeap>
   44     class BumpAlloc : public SuperHeap {
   45   public:
   46 
   47     enum { Alignment = HL::MallocInfo::Alignment };
   48 
   49     BumpAlloc()
   50       : _bump (nullptr),
   51     _remaining (0)
   52     {}
   53 
   54     inline void * malloc (size_t sz) {
   55       if (sz < HL::MallocInfo::MinSize) {
   56     sz = HL::MallocInfo::MinSize;
   57       }
   58       sz = HL::align<HL::MallocInfo::Alignment>(sz);
   59       // If there's not enough space left to fulfill this request, get
   60       // another chunk.
   61       if (_remaining < sz) {
   62     refill(sz);
   63       }
   64       if (_bump) {
   65     char * old = _bump;
   66     _bump += sz;
   67     _remaining -= sz;
   68     assert ((size_t) old % Alignment == 0);
   69     return old;
   70       } else {
   71     // We were unable to get memory.
   72     return nullptr;
   73       }
   74     }
   75 
   76     /// Free is disabled (we only bump, never reclaim).
   77     inline void free (void *) {}
   78 
   79   private:
   80 
   81     /// The bump pointer.
   82     char * _bump;
   83 
   84     /// How much space remains in the current chunk.
   85     size_t _remaining;
   86 
   87     // Get another chunk.
   88     void refill (size_t sz) {
   89       // Always get at least a ChunkSize worth of memory.
   90       if (sz < ChunkSize) {
   91     sz = ChunkSize;
   92       }
   93       _bump = (char *) SuperHeap::malloc (sz);
   94       assert ((size_t) _bump % Alignment == 0);
   95       if (_bump) {
   96     _remaining = sz;
   97       } else {
   98     _remaining = 0;
   99       }
  100     }
  101 
  102   };
  103 
  104 }
  105 
  106 #endif