Gnash  0.8.10
gmemory.h
Go to the documentation of this file.
00001 // 
00002 //   Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
00003 //   Free Software Foundation, Inc
00004 // 
00005 // This program is free software; you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation; either version 3 of the License, or
00008 // (at your option) any later version.
00009 // 
00010 // This program is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 // 
00015 // You should have received a copy of the GNU General Public License
00016 // along with this program; if not, write to the Free Software
00017 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018 
00019 //
00020 // This class is a memory allocation tracker used to optimize
00021 // the memory usage and find memory leaks.
00022 //
00023 #ifndef __MEMORY_H__
00024 #define __MEMORY_H__
00025 
00026 #ifdef HAVE_CONFIG_H
00027 #include "gnashconfig.h"
00028 #endif
00029 
00030 // If we don't have support for mallinfo(), this code is useless
00031 #ifdef HAVE_MALLINFO
00032 
00033 #include <cstdlib>
00034 #include <malloc.h>
00035 #include <ctime>
00036 #include "dsodefs.h" // DSOEXPORT
00037 
00038 namespace gnash {
00039   
00040 class DSOEXPORT Memory {
00041 public:
00042 
00043     // Borrowed from malloc.h and trimmed down.
00044     struct small_mallinfo {
00045         int line;     // line number of this data sample
00046         struct timespec stamp;  // the time stamp of this sample
00047         int arena;    // non-mmapped space allocated from system
00048         int uordblks; // total allocated space
00049         int fordblks; // total free space
00050     };
00051     Memory();
00052     Memory(size_t size);
00053     ~Memory();
00054 
00055     // Start collecting statistics. This can effect performance
00056     void startStats();
00057     
00058     // Stop collecting statistics
00059     void endStats() { addStats();_collecting = false; };
00060 
00061     // Erase all collected data and reset collections.
00062     void reset();
00063 
00064     // checkpoint()
00065     void startCheckpoint() { _checkpoint[0] = mallinfo(); };
00066     bool endCheckpoint();
00067     
00068     // Add or retrieve mallinfo data
00069     int addStats();
00070     int addStats(int line);
00071     int addStats(struct small_mallinfo *x);
00072     int addStats(struct small_mallinfo *x, int line);
00073     struct small_mallinfo *getStats() { return _info; };
00074     struct small_mallinfo *operator[](int x) { return _info + x; };
00075     int totalStats() { return _index; };
00076 
00077     // Analyze memory usage
00078     bool analyze();
00079 
00080     // Dump the differences of bytes allocated between two samples
00081     int diffStats();
00082     int diffStats(int x, int y);
00083 
00084     // Dump the differences in the timestamp between two samples
00085     int diffStamp();
00086     int diffStamp(int x, int y);
00087     
00088     // Dump the vector of stored classes
00089     void dump(struct mallinfo *x);
00090     void dump(struct small_mallinfo *x);
00091     void dump();
00092     void dumpCSV();
00093 private:
00094     bool                _collecting;
00095     // For data logging, we want to store as little as possible
00096     // so we don't impact the system too hard. Data logging memory
00097     // allocations can generate a huge amount of data, so we have
00098     // to be careful. We also can't use STL, as that does more
00099     // memory allocations, which will confuse our statistics
00100     // gathering.
00101     struct small_mallinfo *_info;
00102     // Since we aren't using STL, we have to store how much
00103     // data storage we have ourselves.
00104     size_t              _size;
00105     int                 _index;
00106     // For checkpoints, we want the all the data
00107     struct mallinfo     _checkpoint[2];
00108 };
00109 
00110 } // end of gnash namespace
00111 
00112 #endif // end of HAVE_MALLINFO
00113 
00114 // end of __MEMORY_H__
00115 #endif
00116 
00117 // Local Variables:
00118 // mode: C++
00119 // indent-tabs-mode: t
00120 // End: