Back to index

lightning-sunbird  0.9+nobinonly
orkinHeap.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-  */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1999
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #ifndef _MDB_
00039 #include "mdb.h"
00040 #endif
00041 
00042 #ifndef _MORK_
00043 #include "mork.h"
00044 #endif
00045 
00046 #ifndef _ORKINHEAP_
00047 #include "orkinHeap.h"
00048 #endif
00049 
00050 #ifndef _MORKENV_
00051 #include "morkEnv.h"
00052 #endif
00053 
00054 //3456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789
00055 
00056 
00057 orkinHeap::orkinHeap() // does nothing
00058 #ifdef MORK_DEBUG_HEAP_STATS
00059   : sHeap_AllocCount( 0 )
00060   , sHeap_FreeCount( 0 )
00061   , sHeap_BlockCount( 0 )
00062   
00063   , sHeap_BlockVolume( 0 )
00064   , sHeap_HighWaterVolume( 0 )
00065   , sHeap_HighWaterTenKilo( 0 )
00066   , sHeap_HighWaterHundredKilo( 0 )
00067 #endif /*MORK_DEBUG_HEAP_STATS*/
00068 {
00069 }
00070 
00071 /*virtual*/
00072 orkinHeap::~orkinHeap() // does nothing
00073 {
00074 }
00075 
00076 // { ===== begin nsIMdbHeap methods =====
00077 /*virtual*/ mdb_err
00078 orkinHeap::Alloc(nsIMdbEnv* mev, // allocate a piece of memory
00079   mdb_size inSize,   // requested size of new memory block 
00080   void** outBlock)  // memory block of inSize bytes, or nil
00081 {
00082 #ifdef MORK_DEBUG_HEAP_STATS
00083   mdb_size realSize = inSize;
00084   inSize += 12; // sizeof(mork_u4) * 3
00085   ++sHeap_AllocCount;
00086 #endif /*MORK_DEBUG_HEAP_STATS*/
00087 
00088   MORK_USED_1(mev);
00089   mdb_err outErr = 0;
00090   void* block = ::operator new(inSize);
00091   if ( !block )
00092     outErr = morkEnv_kOutOfMemoryError;
00093 #ifdef MORK_DEBUG_HEAP_STATS
00094   else
00095   {
00096     printf("%lx allocating %d\n", this, realSize);
00097     mork_u4* array = (mork_u4*) block;
00098     *array++ = (mork_u4) this;
00099     *array++ = realSize;
00100     *array++ = orkinHeap_kTag;
00101     block = array;
00102     ++sHeap_BlockCount;
00103     mork_num blockVol = sHeap_BlockVolume + realSize;
00104     sHeap_BlockVolume = blockVol;
00105     if ( blockVol > sHeap_HighWaterVolume )
00106     {
00107       sHeap_HighWaterVolume = blockVol;
00108       
00109       mork_num tenKiloVol = blockVol / (10 * 1024);
00110       if ( tenKiloVol > sHeap_HighWaterTenKilo )
00111       {
00112         sHeap_HighWaterTenKilo = tenKiloVol;
00113       
00114         mork_num hundredKiloVol = blockVol / (100 * 1024);
00115         if ( hundredKiloVol > sHeap_HighWaterHundredKilo )
00116           sHeap_HighWaterHundredKilo = hundredKiloVol;
00117       }
00118     }
00119   }
00120 #endif /*MORK_DEBUG_HEAP_STATS*/
00121     
00122   MORK_ASSERT(outBlock);
00123   if ( outBlock )
00124     *outBlock = block;
00125   return outErr;
00126 }
00127   
00128 /*virtual*/ mdb_err
00129 orkinHeap::Free(nsIMdbEnv* mev, // free block allocated earlier by Alloc()
00130   void* inBlock)
00131 {
00132 #ifdef MORK_DEBUG_HEAP_STATS
00133   ++sHeap_FreeCount;
00134 #endif /*MORK_DEBUG_HEAP_STATS*/
00135 
00136   MORK_USED_1(mev);
00137   MORK_ASSERT(inBlock);
00138   if ( inBlock )
00139   {
00140 #ifdef MORK_DEBUG_HEAP_STATS
00141     morkEnv* ev = 0; //morkEnv::FromMdbEnv(mev);
00142     mork_u4* array = (mork_u4*) inBlock;
00143     if ( *--array != orkinHeap_kTag )
00144     {
00145       if ( ev )
00146         ev->NewWarning("heap block tag not hEaP");
00147     }
00148     mork_u4 realSize = *--array;
00149     inBlock = --array; // skip over heap ptr too.
00150     
00151     printf("%lx freeing %d\n", this, realSize);
00152     if ( sHeap_BlockCount )
00153       --sHeap_BlockCount;
00154     else if ( ev ) 
00155       ev->NewWarning("sHeap_BlockCount underflow");
00156     
00157     if ( sHeap_BlockVolume >= realSize )
00158       sHeap_BlockVolume -= realSize;
00159     else if ( ev )
00160     {
00161       sHeap_BlockVolume = 0;
00162       ev->NewWarning("sHeap_BlockVolume underflow");
00163     }
00164 #endif /*MORK_DEBUG_HEAP_STATS*/
00165     
00166     ::operator delete(inBlock);
00167   }
00168   return 0;
00169 }
00170 
00171 /*virtual*/ mdb_err
00172 orkinHeap::HeapAddStrongRef(nsIMdbEnv* ev) // does nothing
00173 {
00174   MORK_USED_1(ev);
00175   return 0;
00176 }
00177 
00178 /*virtual*/ mdb_err
00179 orkinHeap::HeapCutStrongRef(nsIMdbEnv* ev) // does nothing
00180 {
00181   MORK_USED_1(ev);
00182   return 0;
00183 }
00184 
00185 // } ===== end nsIMdbHeap methods =====
00186 
00187 //3456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789