STXXL  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
malloc.h
Go to the documentation of this file.
1 /***************************************************************************
2  * include/stxxl/bits/utils/malloc.h
3  *
4  * Part of the STXXL. See http://stxxl.sourceforge.net
5  *
6  * Copyright (C) 2003 Roman Dementiev <[email protected]>
7  * Copyright (C) 2009 Andreas Beckmann <[email protected]>
8  *
9  * Distributed under the Boost Software License, Version 1.0.
10  * (See accompanying file LICENSE_1_0.txt or copy at
11  * http://www.boost.org/LICENSE_1_0.txt)
12  **************************************************************************/
13 
14 #ifndef STXXL_UTILS_MALLOC_HEADER
15 #define STXXL_UTILS_MALLOC_HEADER
16 
17 #include <stxxl/bits/config.h>
18 
19 #include <ostream>
20 #if STXXL_HAVE_MALLINFO_PROTO
21  #include <malloc.h>
22 #endif
23 #include <cstdlib>
24 
25 #include <stxxl/bits/namespace.h>
26 #include <stxxl/bits/unused.h>
27 
28 
30 
31 
32 //! Access to some useful malloc statistics.
33 
34 //! malloc is default C++ allocator
36 {
37 #if STXXL_HAVE_MALLINFO_PROTO
38 
39 public:
40  typedef int return_type;
41 
42  //! Returns number of bytes allocated from system not including mmapped regions.
43  return_type from_system_nmmap() const
44  {
45  struct mallinfo info = mallinfo();
46  return info.arena;
47  }
48 
49  //! Returns number of free chunks.
50  return_type free_chunks() const
51  {
52  struct mallinfo info = mallinfo();
53  return info.ordblks;
54  }
55 
56  //! Number of bytes allocated and in use.
57  return_type used() const
58  {
59  struct mallinfo info = mallinfo();
60  return info.uordblks;
61  }
62 
63  //! Number of bytes allocated but not in use.
64  return_type not_used() const
65  {
66  struct mallinfo info = mallinfo();
67  return info.fordblks;
68  }
69 
70  //! Top-most, releasable (via malloc_trim) space (bytes).
71  return_type releasable() const
72  {
73  struct mallinfo info = mallinfo();
74  return info.keepcost;
75  }
76 
77  //! Maximum total allocated space (bytes) (always 0 ?).
78  return_type max_allocated() const
79  {
80  struct mallinfo info = mallinfo();
81  return info.usmblks;
82  }
83 
84  //! Number of fastbin blocks.
85  return_type fastbin_blocks() const
86  {
87  struct mallinfo info = mallinfo();
88  return info.smblks;
89  }
90 
91  //! Space available in freed fastbin blocks (bytes).
92  return_type fastbin_free() const
93  {
94  struct mallinfo info = mallinfo();
95  return info.fsmblks;
96  }
97 
98  //! Returns number of bytes allocated from system using mmap.
99  return_type from_system_mmap() const
100  {
101  struct mallinfo info = mallinfo();
102  return info.hblkhd;
103  }
104 
105  //! Number of chunks allocated via mmap().
106  return_type mmap_chunks() const
107  {
108  struct mallinfo info = mallinfo();
109  return info.hblks;
110  }
111 
112  //! Returns \b total number of bytes allocated from system including mmapped regions.
113  return_type from_system_total() const
114  {
115  return from_system_nmmap() + from_system_mmap();
116  }
117 #endif
118 };
119 
120 //! Prints current malloc statistics in a convenient way.
121 inline std::ostream& operator << (std::ostream& s, const malloc_stats& st)
122 {
123 #if STXXL_HAVE_MALLINFO_PROTO
124  s << "MALLOC statistics" << std::endl;
125  s << "=================================================================" << std::endl;
126  s << "Space allocated from system not using mmap: " << st.from_system_nmmap() << " bytes" << std::endl;
127  s << " number of free chunks : " << st.free_chunks() << std::endl;
128  s << " space allocated and in use : " << st.used() << " bytes" << std::endl;
129  s << " space allocated but not in use : " << st.not_used() << " bytes" << std::endl;
130  s << " top-most, releasable (via malloc_trim) space: " << st.releasable() << " bytes" << std::endl;
131  s << " maximum total allocated space (?) : " << st.max_allocated() << " bytes" << std::endl;
132  s << " FASTBIN blocks " << std::endl;
133  s << " number of fastbin blocks: " << st.fastbin_blocks() << std::endl;
134  s << " space available in freed fastbin blocks: " << st.fastbin_free() << " bytes" << std::endl;
135  s << "Space allocated from system using mmap: " << st.from_system_mmap() << " bytes" << std::endl;
136  s << " number of chunks allocated via mmap(): " << st.mmap_chunks() << std::endl;
137  s << "Total space allocated from system (mmap and not mmap): " <<
138  st.from_system_total() << " bytes" << std::endl;
139  s << "=================================================================" << std::endl;
140 #else
141  s << "MALLOC statistics are not supported on this platform";
142  STXXL_UNUSED(st);
143 #endif
144  return s;
145 }
146 
148 { };
149 
151 
152 #endif // !STXXL_UTILS_MALLOC_HEADER
friend std::ostream & operator<<(std::ostream &os, const uint_pair &a)
make a uint_pair outputtable via iostreams, using unsigned long long.
Definition: uint_types.h:231
Access to some useful malloc statistics.
Definition: malloc.h:35
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
void STXXL_UNUSED(const U &)
Definition: unused.h:23
#define STXXL_END_NAMESPACE
Definition: namespace.h:17