Stxxl  1.4.0
utils/malloc.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002  *  utils/malloc.cpp
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 #include <iostream>
00015 #if !defined(__APPLE__) && !defined(__FreeBSD__)
00016 #include <malloc.h>
00017 #endif
00018 #include <cstdlib>
00019 #include <unistd.h>
00020 #include <stxxl/bits/verbose.h>
00021 
00022 
00023 void print_malloc_stats()
00024 {
00025 #if !defined(__APPLE__) && !defined(__FreeBSD__)
00026     struct mallinfo info = mallinfo();
00027     STXXL_MSG("MALLOC statistics BEGIN");
00028     STXXL_MSG("===============================================================");
00029     STXXL_MSG("non-mmapped space allocated from system (bytes): " << info.arena);
00030     STXXL_MSG("number of free chunks                          : " << info.ordblks);
00031     STXXL_MSG("number of fastbin blocks                       : " << info.smblks);
00032     STXXL_MSG("number of chunks allocated via mmap()          : " << info.hblks);
00033     STXXL_MSG("total number of bytes allocated via mmap()     : " << info.hblkhd);
00034     STXXL_MSG("maximum total allocated space (bytes)          : " << info.usmblks);
00035     STXXL_MSG("space available in freed fastbin blocks (bytes): " << info.fsmblks);
00036     STXXL_MSG("number of bytes allocated and in use           : " << info.uordblks);
00037     STXXL_MSG("number of bytes allocated but not in use       : " << info.fordblks);
00038     STXXL_MSG("top-most, releasable (via malloc_trim) space   : " << info.keepcost);
00039     STXXL_MSG("================================================================");
00040 #else
00041     STXXL_MSG("MALLOC statistics are not supported on this platform");
00042 #endif
00043 }
00044 
00045 int main(int argc, char * argv[])
00046 {
00047     using std::cout;
00048     using std::cerr;
00049     using std::endl;
00050 
00051     if (argc < 2)
00052     {
00053         cerr << "Usage: " << argv[0] << " bytes_to_allocate" << endl;
00054         return -1;
00055     }
00056     sbrk(128 * 1024 * 1024);
00057     cout << "Nothing allocated" << endl;
00058     print_malloc_stats();
00059     const unsigned bytes = atoi(argv[1]);
00060     char * ptr = new char[bytes];
00061     cout << "Allocated " << bytes << " bytes" << endl;
00062     print_malloc_stats();
00063     delete[] ptr;
00064     cout << "Deallocated " << endl;
00065     print_malloc_stats();
00066 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines