Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

RegionAllocator.hpp

Go to the documentation of this file.
00001 /*==========================================================================
00002  * Copyright (c) 2005 University of Massachusetts.  All Rights Reserved.
00003  *
00004  * Use of the Lemur Toolkit for Language Modeling and Information Retrieval
00005  * is subject to the terms of the software license set forth in the LICENSE
00006  * file included with this software, and also available at
00007  * http://www.lemurproject.org/license.html
00008  *
00009  *==========================================================================
00010  */
00011 
00012 //
00013 // RegionAllocator
00014 //
00015 // 9 March 2005 -- tds
00016 //
00017 
00018 #ifndef INDRI_REGIONALLOCATOR_HPP
00019 #define INDRI_REGIONALLOCATOR_HPP
00020 
00021 #include "indri/delete_range.hpp"
00022 #include "indri/Buffer.hpp"
00023 #include <memory>
00024 namespace indri
00025 {
00026   namespace utility
00027   {
00028     
00029     class RegionAllocator {
00030     private:
00031       std::vector<Buffer*> _buffers;
00032       std::vector<void*> _malloced;
00033       size_t _mallocBytes;
00034 
00035     public:
00036       RegionAllocator() :
00037         _mallocBytes(0)
00038       {
00039       }
00040 
00041       ~RegionAllocator() {
00042         delete_vector_contents( _buffers );
00043 
00044         for( size_t i=0; i<_malloced.size(); i++ )
00045           free( _malloced[i] );
00046       }
00047 
00048       void* allocate( size_t bytes ) {
00049 
00050         if( bytes > 1024*32 ) {
00051           _mallocBytes += bytes;
00052           _malloced.push_back( malloc( bytes ) );
00053           return _malloced.back();
00054         }
00055     
00056         bytes = (bytes+7) & ~7; // round up
00057     
00058         if( _buffers.size() && _buffers.back()->remaining() >= bytes ) {
00059           return _buffers.back()->write( bytes );
00060         }
00061 
00062         _buffers.push_back( new Buffer( 1024*1024 ) );
00063         return allocate( bytes );
00064       }
00065 
00066       size_t allocatedBytes() {
00067         return _buffers.size() * 1024*1024 + _mallocBytes;
00068       }
00069     };
00070   }
00071 }
00072 
00073 #endif // INDRI_REGIONALLOCATOR_HPP
00074 

Generated on Tue Jun 15 11:02:55 2010 for Lemur by doxygen 1.3.4