csutil/memheap.h Source File (Crystal Space 2.0 Public API Reference)

| ## Public API Reference

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

csutil/memheap.h
Go to the documentation of this file.

    00001 /*
    00002     Copyright (C) 2006 by Frank Richter
    00003
    00004     This library is free software; you can redistribute it and/or
    00005     modify it under the terms of the GNU Library General Public
    00006     License as published by the Free Software Foundation; either
    00007     version 2 of the License, or (at your option) any later version.
    00008
    00009     This library is distributed in the hope that it will be useful,
    00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
    00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00012     Library General Public License for more details.
    00013
    00014     You should have received a copy of the GNU Library General Public
    00015     License along with this library; if not, write to the Free
    00016     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    00017 */
    00018
    00019 #ifndef __CS_CSUTIL_MEMHEAP_H__
    00020 #define __CS_CSUTIL_MEMHEAP_H__
    00021
    00026 #if defined(CS_MEMORY_TRACKER)
    00027 #include "csutil/csstring.h"
    00028 #include "csutil/memdebug.h"
    00029 #include <typeinfo>
    00030 #endif
    00031
    00032 #include "csutil/spinlock.h"
    00033
    00037 namespace CS
    00038 {
    00039   namespace Memory
    00040   {
    00046     class CS_CRYSTALSPACE_EXPORT Heap
    00047     {
    00049       void* mspace;
    00050       SpinLock lock;
    00051
    00052       Heap (Heap const&);               // Illegal; unimplemented.
    00053       void operator= (Heap const&);     // Illegal; unimplemented.
    00054     public:
    00055       Heap();
    00056       ~Heap();
    00057
    00059       void* Alloc (const size_t n);
    00061       void Free (void* p);
    00063       void* Realloc (void* p, size_t newSize);
    00064
    00070       void Trim (size_t pad = 0);
    00071
    00075       size_t Footprint ();
    00076     };
    00077
    00083     template<class HeapAccess>
    00084     class AllocatorHeapBase : protected HeapAccess
    00085     {
    00086     #if defined(CS_MEMORY_TRACKER)
    00087       const char* mti;
    00088     #endif
    00089     public:
    00090     #if defined(CS_MEMORY_TRACKER)
    00091       AllocatorHeapBase () : mti (0) { }
    00092       AllocatorHeapBase (const HeapAccess& heap) : HeapAccess (heap), mti (0) {}
    00093     #else
    00094       AllocatorHeapBase () { }
    00095       AllocatorHeapBase (const HeapAccess& heap) : HeapAccess (heap) {}
    00096     #endif
    00097
    00098       void* Alloc (const size_t n)
    00099       {
    00100       #if defined(CS_MEMORY_TRACKER)
    00101         void* p = HeapAccess::Alloc (n);
    00102         if (mti == 0)
    00103         {
    00104           /*csString mtiInfo;
    00105           mtiInfo.Format ("%s with %p", typeid(*this).name(), HeapAccess::GetHeap());*/
    00106           mti = /*mtiInfo*/typeid(*this).name();
    00107         }
    00108         CS::Debug::MemTracker::RegisterAlloc (p, n, mti);
    00109         return p;
    00110       #else
    00111         return HeapAccess::Alloc (n);
    00112       #endif
    00113       }
    00115       void Free (void* p)
    00116       {
    00117       #if defined(CS_MEMORY_TRACKER)
    00118         CS::Debug::MemTracker::RegisterFree (p);
    00119       #endif
    00120         HeapAccess::Free (p);
    00121       }
    00123       void* Realloc (void* p, size_t newSize)
    00124       {
    00125       #ifdef CS_MEMORY_TRACKER
    00126         if (p == 0) return Alloc (newSize);
    00127         void* np = HeapAccess::Realloc (p, newSize);
    00128         CS::Debug::MemTracker::UpdateSize (p, np, newSize);
    00129         return np;
    00130       #else
    00131         return HeapAccess::Realloc (p, newSize);
    00132       #endif
    00133       }
    00135       void SetMemTrackerInfo (const char* info)
    00136       {
    00137       #ifdef CS_MEMORY_TRACKER
    00138         if (mti != 0) return;
    00139         /*csString mtiInfo;
    00140         mtiInfo.Format ("%s with %p for %s", typeid(*this).name(),
    00141           HeapAccess::GetHeap(), info);*/
    00142         mti = info;
    00143       #else
    00144         (void)info;
    00145       #endif
    00146       }
    00147     };
    00148
    00154     template<class HeapContainer = Heap*>
    00155     struct HeapAccessPointer
    00156     {
    00157       HeapContainer heap;
    00158
    00159       CS_DEPRECATED_METHOD_MSG ("HeapAccessPointer instance uninitialized")
    00160       HeapAccessPointer () {}
    00161       HeapAccessPointer (HeapContainer heap) : heap (heap) {}
    00162
    00163       void* Alloc (const size_t n)
    00164       {
    00165         return heap->Alloc (n);
    00166       }
    00167       void Free (void* p)
    00168       {
    00169         heap->Free (p);
    00170       }
    00171       void* Realloc (void* p, size_t newSize)
    00172       {
    00173         return heap->Realloc (p, newSize);
    00174       }
    00175       const HeapContainer& GetHeap ()
    00176       {
    00177         return heap;
    00178       }
    00179     };
    00180
    00186     template<class HeapPtr = Heap*>
    00187     class AllocatorHeap : public AllocatorHeapBase<HeapAccessPointer<HeapPtr> >
    00188     {
    00189     public:
    00190       CS_DEPRECATED_METHOD_MSG ("AllocatorHeap instance uninitialized")
    00191       AllocatorHeap () {}
    00192
    00193       AllocatorHeap (HeapPtr heap) :
    00194         AllocatorHeapBase<HeapAccessPointer<HeapPtr> > (
    00195           HeapAccessPointer<HeapPtr> (heap)) {}
    00196     };
    00197   } // namespace Memory
    00198 } // namespace CS
    00199
    00202 #endif // __CS_CSUTIL_MEMHEAP_H__