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 /* 00028 00029 StaticHeap: manage a fixed range of memory. 00030 00031 */ 00032 00033 #ifndef _STATICHEAP_H_ 00034 #define _STATICHEAP_H_ 00035 00036 namespace HL { 00037 00038 template <int MemorySize> 00039 class StaticHeap { 00040 public: 00041 00042 StaticHeap (void) 00043 : ptr (&buf[0]), 00044 remaining (MemorySize) 00045 {} 00046 00047 enum { Alignment = 1 }; 00048 00049 inline void * malloc (size_t sz) { 00050 if (remaining < sz) { 00051 return NULL; 00052 } 00053 void * p = ptr; 00054 ptr += sz; 00055 remaining -= sz; 00056 return p; 00057 } 00058 00059 void free (void *) {} 00060 int remove (void *) { return 0; } 00061 00062 int isValid (void * ptr) { 00063 return (((size_t) ptr >= (size_t) buf) && 00064 ((size_t) ptr < (size_t) buf)); 00065 } 00066 00067 private: 00068 00069 // Disable copying and assignment. 00070 StaticHeap (const StaticHeap& treap); 00071 StaticHeap& operator= (const StaticHeap& treap); 00072 00073 char buf[MemorySize]; 00074 char * ptr; 00075 size_t remaining; 00076 }; 00077 00078 }; 00079 00080 #endif