Eneboo - Documentación para desarrolladores
src/hoard/src/heaplayers/chunkheap.h
Ir a la documentación de este archivo.
00001 /* -*- C++ -*- */
00002 
00003 #ifndef _CHUNKHEAP_H_
00004 #define _CHUNKHEAP_H_
00005 
00006 /*
00007 
00008   Heap Layers: An Extensible Memory Allocation Infrastructure
00009   
00010   Copyright (C) 2000-2003 by Emery Berger
00011   http://www.cs.umass.edu/~emery
00012   emery@cs.umass.edu
00013   
00014   This program is free software; you can redistribute it and/or modify
00015   it under the terms of the GNU General Public License as published by
00016   the Free Software Foundation; either version 2 of the License, or
00017   (at your option) any later version.
00018   
00019   This program is distributed in the hope that it will be useful,
00020   but WITHOUT ANY WARRANTY; without even the implied warranty of
00021   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00022   GNU General Public License for more details.
00023   
00024   You should have received a copy of the GNU General Public License
00025   along with this program; if not, write to the Free Software
00026   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00027 
00028 */
00029 
00030 #include <assert.h>
00031 
00038 namespace HL {
00039 
00040   template <int ChunkSize, class SuperHeap>
00041   class ChunkHeap : public SuperHeap {
00042   public:
00043 
00044     inline ChunkHeap (void)
00045       : buffer (NULL),
00046         eob (NULL)
00047     {}
00048 
00049     inline void * malloc (const size_t sz) {
00050       void * ptr = buffer;
00051       buffer += sz;
00052       if (buffer <= eob) {
00053         assert (eob != NULL);
00054         assert ((size_t) (eob - (char *) ptr + 1) >= sz);
00055         return ptr;
00056       }
00057           buffer -= sz;         // we didn't succeed, back up
00058       return getMoreMemory(sz);
00059     }
00060 
00061     inline void clear (void) {
00062       buffer = NULL;
00063       eob = NULL;
00064       SuperHeap::clear ();
00065     }
00066 
00067   private:
00068 
00069     // Disabled.
00070     inline int remove (void *);
00071 
00072     void * getMoreMemory (size_t sz) {
00073       assert (sz > 0);
00074       // Round sz to the next chunk size.
00075       size_t reqSize = (((sz-1) / ChunkSize) + 1) * ChunkSize;
00076       char * buf = (char *) SuperHeap::malloc (reqSize);
00077       if (buf == NULL) {
00078         return NULL;
00079       }
00080       // If the current end of buffer is not the same as the new buffer,
00081       // reset the buffer pointer.
00082       if (eob != buf) {
00083         buffer = buf;
00084       }
00085           else {
00086                   // we still have a bit leftover at the end of previous buffer
00087                   reqSize += eob - buffer;
00088           }
00089       eob = buffer + reqSize;
00090     
00091       void * ptr = buffer;
00092       buffer += sz;
00093       return ptr;
00094     }
00095 
00097     char * buffer;
00098 
00100     char * eob;
00101   };
00102 
00103 }
00104 
00105 #endif
 Todo Clases Namespaces Archivos Funciones Variables 'typedefs' Enumeraciones Valores de enumeraciones Propiedades Amigas 'defines'