Stxxl
1.4.0
|
00001 /*************************************************************************** 00002 * include/stxxl/bits/utils/malloc.h 00003 * 00004 * Part of the STXXL. See http://stxxl.sourceforge.net 00005 * 00006 * Copyright (C) 2003 Roman Dementiev <dementiev@mpi-sb.mpg.de> 00007 * Copyright (C) 2009 Andreas Beckmann <beckmann@cs.uni-frankfurt.de> 00008 * 00009 * Distributed under the Boost Software License, Version 1.0. 00010 * (See accompanying file LICENSE_1_0.txt or copy at 00011 * http://www.boost.org/LICENSE_1_0.txt) 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 00031 //! \brief Access to some useful malloc statistics 00032 00033 //! malloc is default C++ allocator 00034 class malloc_stats 00035 { 00036 #if !defined(__APPLE__) && !defined(__FreeBSD__) 00037 00038 public: 00039 typedef int return_type; 00040 00041 //! \brief Returns number of bytes allocated from system not including mmapped regions 00042 return_type from_system_nmmap() const 00043 { 00044 struct mallinfo info = mallinfo(); 00045 return info.arena; 00046 } 00047 00048 //! \brief Returns number of free chunks 00049 return_type free_chunks() const 00050 { 00051 struct mallinfo info = mallinfo(); 00052 return info.ordblks; 00053 } 00054 00055 //! \brief Number of bytes allocated and in use 00056 return_type used() const 00057 { 00058 struct mallinfo info = mallinfo(); 00059 return info.uordblks; 00060 } 00061 00062 //! \brief Number of bytes allocated but not in use 00063 return_type not_used() const 00064 { 00065 struct mallinfo info = mallinfo(); 00066 return info.fordblks; 00067 } 00068 00069 //! \brief Top-most, releasable (via malloc_trim) space (bytes) 00070 return_type releasable() const 00071 { 00072 struct mallinfo info = mallinfo(); 00073 return info.keepcost; 00074 } 00075 00076 //! \brief Maximum total allocated space (bytes) (always 0 ?) 00077 return_type max_allocated() const 00078 { 00079 struct mallinfo info = mallinfo(); 00080 return info.usmblks; 00081 } 00082 00083 //! \brief Number of fastbin blocks 00084 return_type fastbin_blocks() const 00085 { 00086 struct mallinfo info = mallinfo(); 00087 return info.smblks; 00088 } 00089 00090 //! \brief Space available in freed fastbin blocks (bytes) 00091 return_type fastbin_free() const 00092 { 00093 struct mallinfo info = mallinfo(); 00094 return info.fsmblks; 00095 } 00096 00097 //! \brief Returns number of bytes allocated from system using mmap 00098 return_type from_system_mmap() const 00099 { 00100 struct mallinfo info = mallinfo(); 00101 return info.hblkhd; 00102 } 00103 00104 //! \brief Number of chunks allocated via mmap() 00105 return_type mmap_chunks() const 00106 { 00107 struct mallinfo info = mallinfo(); 00108 return info.hblks; 00109 } 00110 00111 //! \brief Returns \b total number of bytes allocated from system including mmapped regions 00112 return_type from_system_total() const 00113 { 00114 return from_system_nmmap() + from_system_mmap(); 00115 } 00116 #endif 00117 }; 00118 00119 //! \brief Prints current malloc statistics in a convenient way 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