Gnash  0.8.11dev
gmemory.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
3 // Free Software Foundation, Inc
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 
19 //
20 // This class is a memory allocation tracker used to optimize
21 // the memory usage and find memory leaks.
22 //
23 #ifndef __MEMORY_H__
24 #define __MEMORY_H__
25 
26 #ifdef HAVE_CONFIG_H
27 #include "gnashconfig.h"
28 #endif
29 
30 // If we don't have support for mallinfo(), this code is useless
31 #ifdef HAVE_MALLINFO
32 
33 #include <cstdlib>
34 #include <malloc.h>
35 #include <ctime>
36 #include "dsodefs.h" // DSOEXPORT
37 
38 namespace gnash {
39 
40 class DSOEXPORT Memory {
41 public:
42 
43  // Borrowed from malloc.h and trimmed down.
44  struct small_mallinfo {
45  int line; // line number of this data sample
46  struct timespec stamp; // the time stamp of this sample
47  int arena; // non-mmapped space allocated from system
48  int uordblks; // total allocated space
49  int fordblks; // total free space
50  };
51  Memory();
52  Memory(size_t size);
53  ~Memory();
54 
55  // Start collecting statistics. This can effect performance
56  void startStats();
57 
58  // Stop collecting statistics
59  void endStats() { addStats();_collecting = false; };
60 
61  // Erase all collected data and reset collections.
62  void reset();
63 
64  // checkpoint()
65  void startCheckpoint() { _checkpoint[0] = mallinfo(); };
66  bool endCheckpoint();
67 
68  // Add or retrieve mallinfo data
69  int addStats();
70  int addStats(int line);
71  int addStats(struct small_mallinfo *x);
72  int addStats(struct small_mallinfo *x, int line);
73  struct small_mallinfo *getStats() { return _info; };
74  struct small_mallinfo *operator[](int x) { return _info + x; };
75  int totalStats() { return _index; };
76 
77  // Analyze memory usage
78  bool analyze();
79 
80  // Dump the differences of bytes allocated between two samples
81  int diffStats();
82  int diffStats(int x, int y);
83 
84  // Dump the differences in the timestamp between two samples
85  int diffStamp();
86  int diffStamp(int x, int y);
87 
88  // Dump the vector of stored classes
89  void dump(struct mallinfo *x);
90  void dump(struct small_mallinfo *x);
91  void dump();
92  void dumpCSV();
93 private:
94  bool _collecting;
95  // For data logging, we want to store as little as possible
96  // so we don't impact the system too hard. Data logging memory
97  // allocations can generate a huge amount of data, so we have
98  // to be careful. We also can't use STL, as that does more
99  // memory allocations, which will confuse our statistics
100  // gathering.
101  struct small_mallinfo *_info;
102  // Since we aren't using STL, we have to store how much
103  // data storage we have ourselves.
104  size_t _size;
105  int _index;
106  // For checkpoints, we want the all the data
107  struct mallinfo _checkpoint[2];
108 };
109 
110 } // end of gnash namespace
111 
112 #endif // end of HAVE_MALLINFO
113 
114 // end of __MEMORY_H__
115 #endif
116 
117 // Local Variables:
118 // mode: C++
119 // indent-tabs-mode: t
120 // End:
Anonymous namespace for callbacks, local functions, event handlers etc.
Definition: dbus_ext.cpp:40
std::int32_t x
Definition: BitmapData_as.cpp:434
#define DSOEXPORT
Definition: dsodefs.h:55
std::int32_t y
Definition: BitmapData_as.cpp:435