memory_manager_chunks.h

Go to the documentation of this file.
00001 /*****************************************************************************/
00002 /*!
00003  * \file memory_manager.h
00004  * 
00005  * Author: Sergey Berezin
00006  * 
00007  * Created: Tue Apr 19 14:30:36 2005
00008  *
00009  * <hr>
00010  * Copyright (C) 2003 by the Board of Trustees of Leland Stanford
00011  * Junior University and by New York University. 
00012  *
00013  * License to use, copy, modify, sell and/or distribute this software
00014  * and its documentation for any purpose is hereby granted without
00015  * royalty, subject to the terms and conditions defined in the \ref
00016  * LICENSE file provided with this distribution.  In particular:
00017  *
00018  * - The above copyright notice and this permission notice must appear
00019  * in all copies of the software and related documentation.
00020  *
00021  * - THE SOFTWARE IS PROVIDED "AS-IS", WITHOUT ANY WARRANTIES,
00022  * EXPRESSED OR IMPLIED.  USE IT AT YOUR OWN RISK.
00023  * 
00024  * <hr>
00025  * 
00026  * Class MemoryManager: allocates/deallocates memory for objects of a
00027  * fixed size (the size is a parameter to the constructor).  The
00028  * actual memory is allocated in big chunks, which (at the moment) are
00029  * never released back.  However, the deallocated blocks are later reused.
00030  * 
00031  * Typical use of this class is to create 
00032  * MemoryManager* mm = new MemoryManager(sizeof(YourClass)); 
00033  * where YourClass has operators new and delete redefined:
00034  * void* YourClass::operator new(size_t, MemoryManager* mm)
00035  * { return mm->newData(); }
00036  * void YourClass::delete(void*) { } // do not deallocate memory here
00037  * Then, create objects with obj = new(mm) YourClass(), and destroy them with
00038  * delete obj; mm->deleteData(obj);
00039  */
00040 /*****************************************************************************/
00041 
00042 #ifndef _CVC_lite__memory_manager_chunks_h
00043 #define _CVC_lite__memory_manager_chunks_h
00044 
00045 #include <vector>
00046 #include "memory_manager.h"
00047 
00048 namespace CVCL {
00049 
00050 class MemoryManagerChunks: public MemoryManager {
00051  private:
00052   unsigned d_dataSize; // #bytes in each data element
00053   unsigned d_chunkSize; // number of data elements
00054   unsigned d_chunkSizeBytes; // #bytes in each chunk
00055   std::vector<char*> d_freeList;
00056   std::vector<char*> d_chunkList; // Pointers to the beginning of each chunk
00057   // Pointer to the next free block of memory in the current chunk
00058   char* d_nextFree;
00059   // End of current chunk (1 byte off the end)
00060   char* d_endChunk;
00061 
00062   // Private methods
00063   void newChunk() { // Allocate new chunk
00064     d_nextFree = (char*)malloc(d_chunkSizeBytes);
00065     FatalAssert(d_nextFree != NULL, "Out of memory");
00066     d_endChunk = d_nextFree + d_chunkSizeBytes;
00067     d_chunkList.push_back(d_nextFree);
00068   }
00069 
00070  public:
00071   // Constructor
00072   MemoryManagerChunks(unsigned dataSize, unsigned chunkSize = 1024)
00073     : d_dataSize(dataSize), d_chunkSize(chunkSize),
00074       d_chunkSizeBytes(dataSize*chunkSize),
00075       d_nextFree(NULL), d_endChunk(NULL) { }
00076   // Destructor
00077   ~MemoryManagerChunks() {
00078     while(d_chunkList.size() > 0) {
00079       free(d_chunkList.back());
00080       d_chunkList.pop_back();
00081     }
00082   }
00083 
00084   void* newData(size_t size) {
00085     DebugAssert(size == d_dataSize,
00086                 "MemoryManager::newData: the data size doesn't match");
00087     void* res;
00088     // Check the free list first
00089     if(d_freeList.size() > 0) {
00090       res = (void*)d_freeList.back();
00091       d_freeList.pop_back();
00092       return res;
00093     }
00094     if(d_nextFree == NULL || d_nextFree == d_endChunk)
00095       newChunk();
00096     res = (void*)d_nextFree;
00097     d_nextFree += d_dataSize;
00098     return res;
00099   }
00100 
00101   void deleteData(void* d) {
00102     d_freeList.push_back((char*)d);
00103   }
00104 }; // end of class MemoryManager
00105 
00106 }
00107 
00108 #endif

Generated on Thu Apr 13 16:57:32 2006 for CVC Lite by  doxygen 1.4.4