"Fossies" - the Fresh Open Source Software Archive

Member "heaplayers-351/heaplayers/slopheap.h" (28 Dec 2003, 2545 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 "slopheap.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-2003 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 
   28 #ifndef _SLOPHEAP_H_
   29 #define _SLOPHEAP_H_
   30 
   31 /**
   32  * @class SlopHeap
   33  * 
   34  * SlopHeap is designed to guarantee that you always have an extra N bytes
   35  * available after the most recent malloc. This is necessary for the current
   36  * coalescing support, which can look past the last allocated object.
   37  *
   38  * @param SuperHeap The parent heap.
   39  * @param SLOP The amount of extra memory required, in bytes.
   40  */
   41   
   42 namespace HL {
   43   
   44   template <class SuperHeap, int SLOP = 16>
   45   class SlopHeap : public SuperHeap {
   46   public:
   47     SlopHeap (void)
   48       : remaining (0),
   49     ptr (NULL)
   50     {}
   51   
   52     inline void * malloc (const size_t nbytes) {
   53 
   54       // Put the usual case up front.
   55       if (nbytes <= remaining) {
   56     remaining -= nbytes;
   57     char * p = ptr;
   58     ptr += nbytes;
   59     return (void *) p;
   60       }
   61     
   62       //
   63       // We don't have enough space to satisfy the current
   64       // request, so get more memory.
   65       //
   66 
   67       return getMoreMemory(nbytes);
   68     }
   69   
   70     inline void clear (void) {
   71       ptr = NULL;
   72       remaining = 0;
   73       SuperHeap::clear ();
   74     }
   75 
   76     inline void free (void *) {}
   77 
   78   private:
   79   
   80     inline int remove (void *) {}
   81 
   82     void * getMoreMemory (size_t nbytes) {
   83       char * newptr = (char *) SuperHeap::malloc (nbytes + SLOP);
   84 
   85       if (newptr == NULL) {
   86     return NULL;
   87       }
   88 
   89       //
   90       // If this new memory is contiguous with the previous one,
   91       // reclaim the "slop".
   92       //
   93     
   94       if ((ptr != NULL) && (ptr + remaining + SLOP == newptr)) {
   95     remaining += SLOP;
   96       } else {
   97     ptr = newptr;
   98     remaining = 0;
   99       }
  100       char * p = ptr;
  101       ptr += nbytes;
  102 
  103       return (void *) p;
  104     }
  105 
  106     char * ptr;
  107     size_t remaining;
  108 
  109   };
  110   
  111 }
  112 
  113 #endif