Stxxl  1.3.1
malloc.h
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 <dementiev@mpi-sb.mpg.de>
7  * Copyright (C) 2009 Andreas Beckmann <beckmann@cs.uni-frankfurt.de>
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_MALLOC_H
15 #define STXXL_MALLOC_H
16 
17 #include <ostream>
18 #ifdef __FreeBSD__
19  #include <stdlib.h>
20 #else
21  #include <malloc.h>
22 #endif
23 #include <cstdlib>
24 
25 #include <stxxl/bits/namespace.h>
26 
27 
28 __STXXL_BEGIN_NAMESPACE
29 
30 
32 
35 {
36 #if !defined(__APPLE__) && !defined(__FreeBSD__)
37 
38 public:
39  typedef int return_type;
40 
42  return_type from_system_nmmap() const
43  {
44  struct mallinfo info = mallinfo();
45  return info.arena;
46  }
47 
49  return_type free_chunks() const
50  {
51  struct mallinfo info = mallinfo();
52  return info.ordblks;
53  }
54 
56  return_type used() const
57  {
58  struct mallinfo info = mallinfo();
59  return info.uordblks;
60  }
61 
63  return_type not_used() const
64  {
65  struct mallinfo info = mallinfo();
66  return info.fordblks;
67  }
68 
70  return_type releasable() const
71  {
72  struct mallinfo info = mallinfo();
73  return info.keepcost;
74  }
75 
77  return_type max_allocated() const
78  {
79  struct mallinfo info = mallinfo();
80  return info.usmblks;
81  }
82 
84  return_type fastbin_blocks() const
85  {
86  struct mallinfo info = mallinfo();
87  return info.smblks;
88  }
89 
91  return_type fastbin_free() const
92  {
93  struct mallinfo info = mallinfo();
94  return info.fsmblks;
95  }
96 
98  return_type from_system_mmap() const
99  {
100  struct mallinfo info = mallinfo();
101  return info.hblkhd;
102  }
103 
105  return_type mmap_chunks() const
106  {
107  struct mallinfo info = mallinfo();
108  return info.hblks;
109  }
110 
112  return_type from_system_total() const
113  {
114  return from_system_nmmap() + from_system_mmap();
115  }
116 #endif
117 };
118 
120 inline std::ostream & operator << (std::ostream & s, const malloc_stats & st)
121 {
122 #if !defined(__APPLE__) && !defined(__FreeBSD__)
123  s << "MALLOC statistics" << std::endl;
124  s << "=================================================================" << std::endl;
125  s << "Space allocated from system not using mmap: " << st.from_system_nmmap() << " bytes" << std::endl;
126  s << " number of free chunks : " << st.free_chunks() << std::endl;
127  s << " space allocated and in use : " << st.used() << " bytes" << std::endl;
128  s << " space allocated but not in use : " << st.not_used() << " bytes" << std::endl;
129  s << " top-most, releasable (via malloc_trim) space: " << st.releasable() << " bytes" << std::endl;
130  s << " maximum total allocated space (?) : " << st.max_allocated() << " bytes" << std::endl;
131  s << " FASTBIN blocks " << std::endl;
132  s << " number of fastbin blocks: " << st.fastbin_blocks() << std::endl;
133  s << " space available in freed fastbin blocks: " << st.fastbin_free() << " bytes" << std::endl;
134  s << "Space allocated from system using mmap: " << st.from_system_mmap() << " bytes" << std::endl;
135  s << " number of chunks allocated via mmap(): " << st.mmap_chunks() << std::endl;
136  s << "Total space allocated from system (mmap and not mmap): " <<
137  st.from_system_total() << " bytes" << std::endl;
138  s << "=================================================================" << std::endl;
139 #else
140  s << "MALLOC statistics are not supported on this platform";
141  STXXL_UNUSED(st);
142 #endif
143  return s;
144 }
145 
146 class malloc_setup
147 { };
148 
149 __STXXL_END_NAMESPACE
150 
151 #endif // !STXXL_MALLOC_H