00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #ifndef STXXL_MALLOC_H
00015 #define STXXL_MALLOC_H
00016
00017 #include <ostream>
00018 #ifdef __FreeBSD__
00019 #include <stdlib.h>
00020 #else
00021 #include <malloc.h>
00022 #endif
00023 #include <cstdlib>
00024
00025 #include <stxxl/bits/namespace.h>
00026
00027
00028 __STXXL_BEGIN_NAMESPACE
00029
00030
00032
00034 class malloc_stats
00035 {
00036 #if !defined(__APPLE__) && !defined(__FreeBSD__)
00037
00038 public:
00039 typedef int return_type;
00040
00042 return_type from_system_nmmap() const
00043 {
00044 struct mallinfo info = mallinfo();
00045 return info.arena;
00046 }
00047
00049 return_type free_chunks() const
00050 {
00051 struct mallinfo info = mallinfo();
00052 return info.ordblks;
00053 }
00054
00056 return_type used() const
00057 {
00058 struct mallinfo info = mallinfo();
00059 return info.uordblks;
00060 }
00061
00063 return_type not_used() const
00064 {
00065 struct mallinfo info = mallinfo();
00066 return info.fordblks;
00067 }
00068
00070 return_type releasable() const
00071 {
00072 struct mallinfo info = mallinfo();
00073 return info.keepcost;
00074 }
00075
00077 return_type max_allocated() const
00078 {
00079 struct mallinfo info = mallinfo();
00080 return info.usmblks;
00081 }
00082
00084 return_type fastbin_blocks() const
00085 {
00086 struct mallinfo info = mallinfo();
00087 return info.smblks;
00088 }
00089
00091 return_type fastbin_free() const
00092 {
00093 struct mallinfo info = mallinfo();
00094 return info.fsmblks;
00095 }
00096
00098 return_type from_system_mmap() const
00099 {
00100 struct mallinfo info = mallinfo();
00101 return info.hblkhd;
00102 }
00103
00105 return_type mmap_chunks() const
00106 {
00107 struct mallinfo info = mallinfo();
00108 return info.hblks;
00109 }
00110
00112 return_type from_system_total() const
00113 {
00114 return from_system_nmmap() + from_system_mmap();
00115 }
00116 #endif
00117 };
00118
00120 inline std::ostream & operator << (std::ostream & s, const malloc_stats & st)
00121 {
00122 #if !defined(__APPLE__) && !defined(__FreeBSD__)
00123 s << "MALLOC statistics" << std::endl;
00124 s << "=================================================================" << std::endl;
00125 s << "Space allocated from system not using mmap: " << st.from_system_nmmap() << " bytes" << std::endl;
00126 s << " number of free chunks : " << st.free_chunks() << std::endl;
00127 s << " space allocated and in use : " << st.used() << " bytes" << std::endl;
00128 s << " space allocated but not in use : " << st.not_used() << " bytes" << std::endl;
00129 s << " top-most, releasable (via malloc_trim) space: " << st.releasable() << " bytes" << std::endl;
00130 s << " maximum total allocated space (?) : " << st.max_allocated() << " bytes" << std::endl;
00131 s << " FASTBIN blocks " << std::endl;
00132 s << " number of fastbin blocks: " << st.fastbin_blocks() << std::endl;
00133 s << " space available in freed fastbin blocks: " << st.fastbin_free() << " bytes" << std::endl;
00134 s << "Space allocated from system using mmap: " << st.from_system_mmap() << " bytes" << std::endl;
00135 s << " number of chunks allocated via mmap(): " << st.mmap_chunks() << std::endl;
00136 s << "Total space allocated from system (mmap and not mmap): " <<
00137 st.from_system_total() << " bytes" << std::endl;
00138 s << "=================================================================" << std::endl;
00139 #else
00140 s << "MALLOC statistics are not supported on this platform";
00141 STXXL_UNUSED(st);
00142 #endif
00143 return s;
00144 }
00145
00146 class malloc_setup
00147 { };
00148
00149 __STXXL_END_NAMESPACE
00150
00151 #endif // !STXXL_MALLOC_H