cmtkMemory.cxx

Go to the documentation of this file.
00001 /*
00002 //
00003 //  Copyright 1997-2009 Torsten Rohlfing
00004 //
00005 //  Copyright 2004-2010 SRI International
00006 //
00007 //  This file is part of the Computational Morphometry Toolkit.
00008 //
00009 //  http://www.nitrc.org/projects/cmtk/
00010 //
00011 //  The Computational Morphometry Toolkit is free software: you can
00012 //  redistribute it and/or modify it under the terms of the GNU General Public
00013 //  License as published by the Free Software Foundation, either version 3 of
00014 //  the License, or (at your option) any later version.
00015 //
00016 //  The Computational Morphometry Toolkit is distributed in the hope that it
00017 //  will be useful, but WITHOUT ANY WARRANTY; without even the implied
00018 //  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 //  GNU General Public License for more details.
00020 //
00021 //  You should have received a copy of the GNU General Public License along
00022 //  with the Computational Morphometry Toolkit.  If not, see
00023 //  <http://www.gnu.org/licenses/>.
00024 //
00025 //  $Revision: 2478 $
00026 //
00027 //  $LastChangedDate: 2010-10-20 15:07:14 -0700 (Wed, 20 Oct 2010) $
00028 //
00029 //  $LastChangedBy: torstenrohlfing $
00030 //
00031 */
00032 
00033 #include "cmtkMemory.h"
00034 
00035 #ifdef HAVE_MALLOC_H
00036 #  include <malloc.h>
00037 #endif
00038 
00039 #include <stdio.h> // thanks to Hans Johnson for pointing this out
00040 #include <limits.h>
00041 
00042 namespace
00043 cmtk
00044 {
00045 
00046 namespace
00047 Memory
00048 {
00049 
00050 size_t
00051 GetNextPowerOfTwo( size_t k )
00052 {
00053 
00054 // http://en.wikipedia.org/wiki/Power_of_two#Algorithm_to_find_the_next-highest_power_of_two 
00055 
00056   if (k == 0)
00057     return 1;
00058   
00059   k--;
00060   for (size_t i=1; i<sizeof(size_t)*CHAR_BIT; i<<=1)
00061     k = k | k >> i;
00062 
00063   return k+1;
00064 }
00065 
00066 size_t
00067 Used () 
00068 {
00069 #ifdef HAVE_MALLINFO
00070   struct mallinfo stats = mallinfo();
00071   return stats.uordblks + stats.usmblks;
00072 #else
00073   return 0;
00074 #endif
00075 }
00076 
00077 void
00078 Info ( const char *msg ) 
00079 {
00080   const int used = Used();
00081   if (msg )
00082     printf("%d bytes in use %s\n",used,msg);
00083   else
00084     printf("%d bytes in use.\n",used);
00085 }
00086 
00087 void
00088 Diff ( const size_t before, const char *msg ) 
00089 {
00090   const int diff = Used()-before;
00091   if (diff<0)
00092     printf("%s freed %d bytes.\n",msg,-diff);
00093   else
00094     printf("%s allocated %d bytes.\n",msg,diff);
00095 }
00096 
00097 } // namespace Memory
00098 
00099 } // namespace cmtk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines