Eneboo - Documentación para desarrolladores
|
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 _SIZEOWNERHEAP_H_ 00028 #define _SIZEOWNERHEAP_H_ 00029 00030 #include <assert.h> 00031 00032 #include "addheap.h" 00033 00039 namespace HL { 00040 00041 template <class Heap> 00042 class SizeOwner { 00043 public: 00044 union { 00045 struct { 00046 size_t size; 00047 Heap * owner; 00048 } s; 00049 double dummy; 00050 }; 00051 }; 00052 00053 template <class Super> 00054 class SizeOwnerHeap : public AddHeap<SizeOwner<Super>, Super> { 00055 private: 00056 00057 typedef AddHeap<SizeOwner<Super>, Super> SuperHeap; 00058 00059 public: 00060 00061 inline void * malloc (size_t sz) { 00062 void * ptr = SuperHeap::malloc (sz); 00063 // Store the requested size. 00064 SizeOwner<Super> * so = (SizeOwner<Super> *) ptr; 00065 so->s.size = sz; 00066 so->s.owner = this; 00067 // Store the owner. 00068 return (void *) (so + 1); 00069 } 00070 00071 inline void free (void * ptr) { 00072 void * origPtr = (void *) ((SizeOwner<Super> *) ptr - 1); 00073 SuperHeap::free (origPtr); 00074 } 00075 00076 static inline Super * owner (void * ptr) { 00077 SizeOwner<Super> * so = (SizeOwner<Super> *) ptr - 1; 00078 return so->s.owner; 00079 } 00080 00081 static inline size_t size (void * ptr) { 00082 SizeOwner<Super> * so = (SizeOwner<Super> *) ptr - 1; 00083 return so->s.size; 00084 } 00085 }; 00086 00087 }; 00088 00089 #endif