STXXL  1.4-dev
 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 
29 
30 //! Access to some useful malloc statistics.
31 
32 //! malloc is default C++ allocator
34 {
35 #if STXXL_HAVE_MALLINFO_PROTO
36 
37 public:
38  typedef int return_type;
39 
40  //! Returns number of bytes allocated from system not including mmapped regions.
41  return_type from_system_nmmap() const
42  {
43  struct mallinfo info = mallinfo();
44  return info.arena;
45  }
46 
47  //! Returns number of free chunks.
48  return_type free_chunks() const
49  {
50  struct mallinfo info = mallinfo();
51  return info.ordblks;
52  }
53 
54  //! Number of bytes allocated and in use.
55  return_type used() const
56  {
57  struct mallinfo info = mallinfo();
58  return info.uordblks;
59  }
60 
61  //! Number of bytes allocated but not in use.
62  return_type not_used() const
63  {
64  struct mallinfo info = mallinfo();
65  return info.fordblks;
66  }
67 
68  //! Top-most, releasable (via malloc_trim) space (bytes).
69  return_type releasable() const
70  {
71  struct mallinfo info = mallinfo();
72  return info.keepcost;
73  }
74 
75  //! Maximum total allocated space (bytes) (always 0 ?).
76  return_type max_allocated() const
77  {
78  struct mallinfo info = mallinfo();
79  return info.usmblks;
80  }
81 
82  //! Number of fastbin blocks.
83  return_type fastbin_blocks() const
84  {
85  struct mallinfo info = mallinfo();
86  return info.smblks;
87  }
88 
89  //! Space available in freed fastbin blocks (bytes).
90  return_type fastbin_free() const
91  {
92  struct mallinfo info = mallinfo();
93  return info.fsmblks;
94  }
95 
96  //! Returns number of bytes allocated from system using mmap.
97  return_type from_system_mmap() const
98  {
99  struct mallinfo info = mallinfo();
100  return info.hblkhd;
101  }
102 
103  //! Number of chunks allocated via mmap().
104  return_type mmap_chunks() const
105  {
106  struct mallinfo info = mallinfo();
107  return info.hblks;
108  }
109 
110  //! Returns \b total number of bytes allocated from system including mmapped regions.
111  return_type from_system_total() const
112  {
113  return from_system_nmmap() + from_system_mmap();
114  }
115 #endif
116 };
117 
118 //! Prints current malloc statistics in a convenient way.
119 inline std::ostream& operator << (std::ostream& s, const malloc_stats& st)
120 {
121 #if STXXL_HAVE_MALLINFO_PROTO
122  s << "MALLOC statistics" << std::endl;
123  s << "=================================================================" << std::endl;
124  s << "Space allocated from system not using mmap: " << st.from_system_nmmap() << " bytes" << std::endl;
125  s << " number of free chunks : " << st.free_chunks() << std::endl;
126  s << " space allocated and in use : " << st.used() << " bytes" << std::endl;
127  s << " space allocated but not in use : " << st.not_used() << " bytes" << std::endl;
128  s << " top-most, releasable (via malloc_trim) space: " << st.releasable() << " bytes" << std::endl;
129  s << " maximum total allocated space (?) : " << st.max_allocated() << " bytes" << std::endl;
130  s << " FASTBIN blocks " << std::endl;
131  s << " number of fastbin blocks: " << st.fastbin_blocks() << std::endl;
132  s << " space available in freed fastbin blocks: " << st.fastbin_free() << " bytes" << std::endl;
133  s << "Space allocated from system using mmap: " << st.from_system_mmap() << " bytes" << std::endl;
134  s << " number of chunks allocated via mmap(): " << st.mmap_chunks() << std::endl;
135  s << "Total space allocated from system (mmap and not mmap): " <<
136  st.from_system_total() << " bytes" << std::endl;
137  s << "=================================================================" << std::endl;
138 #else
139  s << "MALLOC statistics are not supported on this platform";
140  STXXL_UNUSED(st);
141 #endif
142  return s;
143 }
144 
146 { };
147 
149 
150 #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:228
Access to some useful malloc statistics.
Definition: malloc.h:33
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
void STXXL_UNUSED(const U &)
Definition: unused.h:22
#define STXXL_END_NAMESPACE
Definition: namespace.h:17