Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

allocator.cpp

00001 /*
00002  *                         Sun Public License Notice
00003  *
00004  * The contents of this file are subject to the Sun Public License
00005  * Version 1.0 (the "License"); you may not use this file except
00006  * in compliance with the License. A copy of the License is available
00007  * at http://www.sun.com/
00008  *
00009  * The Original Code is the Java Profiler module.  The Initial Developers
00010  * of the Original Code are Jan Stola, Pavel Vacha, Michal Pise, Petr Luner,
00011  * Lukas Petru and Marek Przeczek.
00012  *
00013  * Portions created by Jan Stola are Copyright (C) 2000-2001.
00014  * All Rights Reserved.
00015  *
00016  * Portions created by Pavel Vacha are Copyright (C) 2000-2001.
00017  * All Rights Reserved.
00018  *
00019  * Portions created by Michal Pise are Copyright (C) 2000-2001.
00020  * All Rights Reserved.
00021  *
00022  * Portions created by Petr Luner are Copyright (C) 2000-2001.
00023  * All Rights Reserved.
00024  *
00025  * Portions created by Lukas Petru are Copyright (C) 2000-2001.
00026  * All Rights Reserved.
00027  *
00028  * Portions created by Marek Przeczek are Copyright (C) 2000-2001.
00029  * All Rights Reserved.
00030  *
00031  * Contributors: Jan Stola, Pavel Vacha, Michal Pise, Petr Luner,
00032  *               Lukas Petru and Marek Przeczek.
00033  */
00034 
00035 #ifdef USE_ALLOCATOR
00036 
00037 #include "../allocator/allocator.h"
00038 #include "../prof/prof.h"
00039 
00040 void* Allocator::get( size_t unitSize) {
00041 
00042         if( !_unitSize) _unitSize = unitSize;
00043 
00044         if( !_freeUnits) allocNewBlock();
00045 
00046         void* unit = _freeUnits;
00047         _freeUnits = *(void**)_freeUnits;
00048                 
00049         return unit;
00050 }
00051 
00052 void Allocator::put( void* unit) {
00053 
00054         *(void**)unit = _freeUnits;
00055         _freeUnits = unit;
00056 }
00057 
00058 void Allocator::allocNewBlock() {
00059 
00060         size_t len = _unitSize*NUM_UNITS_IN_BLOCK+sizeof( void*);
00061 
00062         char* block = ::new char[len];
00063 
00064         *(void**)(block+len-sizeof( void*)) = _blocks;
00065         _blocks = block;
00066 
00067         char* unit = block;
00068 
00069         for( int i = 0; i < NUM_UNITS_IN_BLOCK; i++, unit += _unitSize) {
00070 
00071                 *(void**)unit = _freeUnits;
00072                 _freeUnits = unit;
00073         }
00074 }
00075 
00076 Allocator::~Allocator() {
00077 
00078         size_t len = _unitSize*NUM_UNITS_IN_BLOCK+sizeof( void*);
00079 
00080         void* del;
00081         while( del = _blocks) {
00082 
00083                 _blocks = *(void**)((char*)_blocks+len-sizeof( void*));
00084                 ::delete[] (char*)del;
00085         }
00086 }
00087 
00088 #endif
00089 

Generated on Mon Jan 28 14:53:25 2002 for Java Profiler Dynamic Library by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001