Eneboo - Documentación para desarrolladores
src/hoard/src/heaplayers/sizeheap.h
Ir a la documentación de este archivo.
00001 /* -*- C++ -*- */
00002 
00003 /*
00004 
00005   Heap Layers: An Extensible Memory Allocation Infrastructure
00006   
00007   Copyright (C) 2000-2003 by Emery Berger
00008   http://www.cs.umass.edu/~emery
00009   emery@cs.umass.edu
00010   
00011   This program is free software; you can redistribute it and/or modify
00012   it under the terms of the GNU General Public License as published by
00013   the Free Software Foundation; either version 2 of the License, or
00014   (at your option) any later version.
00015   
00016   This program is distributed in the hope that it will be useful,
00017   but WITHOUT ANY WARRANTY; without even the implied warranty of
00018   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019   GNU General Public License for more details.
00020   
00021   You should have received a copy of the GNU General Public License
00022   along with this program; if not, write to the Free Software
00023   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00024 
00025 */
00026 
00027 #ifndef _SIZEHEAP_H_
00028 #define _SIZEHEAP_H_
00029 
00035 #include <assert.h>
00036 
00037 #include "addheap.h"
00038 
00039 #if 1
00040 
00047 namespace HL {
00048 
00049 template <class Super>
00050 class UseSizeHeap : public Super {
00051 public:
00052   
00053   inline UseSizeHeap (void) {}
00054   
00055   inline static size_t getSize (const void * ptr) {
00056     return ((freeObject *) ptr - 1)->sz;
00057   }
00058 
00059 protected:
00060   union freeObject {
00061     size_t sz;
00062     double _dummy; // for alignment.
00063   };
00064 };
00065 
00071 template <class SuperHeap>
00072 class SizeHeap : public UseSizeHeap<SuperHeap> {
00073   typedef typename UseSizeHeap<SuperHeap>::freeObject freeObject;
00074 public:
00075   inline SizeHeap (void) {}
00076   inline void * malloc (const size_t sz) {
00077     // Add room for a size field.
00078     freeObject * ptr = (freeObject *)
00079       SuperHeap::malloc (sz + sizeof(freeObject));
00080     // Store the requested size.
00081     ptr->sz = sz;
00082     return (void *) (ptr + 1);
00083   }
00084   inline void free (void * ptr) {
00085     SuperHeap::free ((freeObject *) ptr - 1);
00086   }
00087 };
00088 
00089 };
00090 
00091 #else
00092 
00093 template <class Super>
00094 class SizeHeap : public Super {
00095 public:
00096   
00097   inline void * malloc (size_t sz) {
00098     // Add room for a size field.
00099           assert (sizeof(size_t) <= sizeof(double));
00100     void * ptr = Super::malloc (sz + sizeof(double));
00101     // Store the requested size.
00102     *((size_t *) ptr) = sz;
00103     return (void *) ((double *) ptr + 1);
00104   }
00105   
00106   inline void free (void * ptr) {
00107     void * origPtr = (void *) ((double *) ptr - 1);
00108         Super::free (origPtr);
00109   }
00110 
00111   inline static size_t getSize (void * ptr) {
00112     return *((size_t *) ((double *) ptr - 1));
00113   }
00114 };
00115 
00116 
00117 
00118 template <class Super>
00119 class UseSizeHeap : public Super {
00120 public:
00121   
00122   inline static size_t getSize (void * ptr) {
00123     return *((size_t *) ((double *) ptr - 1));
00124   }
00125 };
00126 
00127 #endif
00128 
00129 #endif
 Todo Clases Namespaces Archivos Funciones Variables 'typedefs' Enumeraciones Valores de enumeraciones Propiedades Amigas 'defines'