libstdc++
profiler_list_to_slist.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Copyright (C) 2009-2015 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 3, or (at your option)
00009 // any later version.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License along
00021 // with this library; see the file COPYING3.  If not see
00022 // <http://www.gnu.org/licenses/>.
00023 
00024 /** @file profile/impl/profiler_list_to_slist.h
00025  *  @brief Diagnostics for list to slist.
00026  */
00027 
00028 // Written by Changhee Jung.
00029 
00030 #ifndef _GLIBCXX_PROFILE_PROFILER_LIST_TO_SLIST_H
00031 #define _GLIBCXX_PROFILE_PROFILER_LIST_TO_SLIST_H 1
00032 
00033 #include "profile/impl/profiler.h"
00034 #include "profile/impl/profiler_node.h"
00035 #include "profile/impl/profiler_trace.h"
00036 
00037 namespace __gnu_profile
00038 {
00039   class __list2slist_info
00040   : public __object_info_base
00041   {
00042   public:
00043     __list2slist_info(__stack_t __stack)
00044     : __object_info_base(__stack), _M_rewind(false), _M_operations(0) { }
00045 
00046     // XXX: the magnitude should be multiplied with a constant factor F,
00047     // where F is 1 when the malloc size class of list nodes is different
00048     // from the malloc size class of slist nodes.  When they fall into the same
00049     // class, the only slist benefit is from having to set fewer links, so
00050     // the factor F should be much smaller, closer to 0 than to 1.
00051     // This could be implemented by passing the size classes in the config 
00052     // file.  For now, we always assume F to be 1.
00053 
00054     float
00055     __magnitude() const
00056     {
00057       if (!_M_rewind)
00058         return _M_operations;
00059       else
00060         return 0;
00061     }
00062     
00063     void
00064     __write(FILE* __f) const
00065     { std::fprintf(__f, "%s\n", _M_rewind ? "invalid" : "valid"); }
00066 
00067     std::string
00068     __advice() const
00069     { return "change std::list to std::forward_list"; }
00070 
00071     void
00072     __opr_rewind()
00073     {
00074       _M_rewind = true;
00075       __set_invalid();
00076     }
00077 
00078     void
00079     __record_operation()
00080     { ++_M_operations; }
00081 
00082     bool
00083     __has_rewind()
00084     { return _M_rewind; }
00085 
00086   private:
00087     bool _M_rewind;
00088     std::size_t _M_operations;
00089   };
00090 
00091   class __list2slist_stack_info
00092   : public __list2slist_info
00093   {
00094   public:
00095     __list2slist_stack_info(const __list2slist_info& __o) 
00096     : __list2slist_info(__o) { }
00097   };
00098 
00099   class __trace_list_to_slist
00100   : public __trace_base<__list2slist_info, __list2slist_stack_info> 
00101   {
00102   public:
00103     ~__trace_list_to_slist() { }
00104 
00105     __trace_list_to_slist()
00106     : __trace_base<__list2slist_info, __list2slist_stack_info>()
00107     { __id = "list-to-slist"; }
00108 
00109     void
00110     __destruct(__list2slist_info* __obj_info)
00111     { __retire_object(__obj_info); }
00112   };
00113 
00114 
00115   inline void
00116   __trace_list_to_slist_init()
00117   { _GLIBCXX_PROFILE_DATA(_S_list_to_slist) = new __trace_list_to_slist(); }
00118 
00119   inline void
00120   __trace_list_to_slist_free()
00121   { delete _GLIBCXX_PROFILE_DATA(_S_list_to_slist); }
00122 
00123   inline void
00124   __trace_list_to_slist_report(FILE* __f, __warning_vector_t& __warnings)
00125   { __trace_report(_GLIBCXX_PROFILE_DATA(_S_list_to_slist), __f, __warnings); }
00126 
00127   inline __list2slist_info*
00128   __trace_list_to_slist_construct()
00129   {
00130     if (!__profcxx_init())
00131       return 0;
00132 
00133     if (!__reentrance_guard::__get_in())
00134       return 0;
00135 
00136     __reentrance_guard __get_out;
00137     return _GLIBCXX_PROFILE_DATA(_S_list_to_slist)->__add_object(__get_stack());
00138   }
00139 
00140   inline void
00141   __trace_list_to_slist_rewind(__list2slist_info* __obj_info) 
00142   {
00143     if (!__obj_info)
00144       return;
00145 
00146     __obj_info->__opr_rewind();
00147   }
00148 
00149   inline void
00150   __trace_list_to_slist_operation(__list2slist_info* __obj_info) 
00151   {
00152     if (!__obj_info)
00153       return;
00154 
00155     __obj_info->__record_operation();
00156   }
00157 
00158   inline void
00159   __trace_list_to_slist_destruct(__list2slist_info* __obj_info)
00160   {
00161     if (!__obj_info)
00162       return;
00163 
00164     _GLIBCXX_PROFILE_DATA(_S_list_to_slist)->__destruct(__obj_info);
00165   }
00166 
00167 } // namespace __gnu_profile
00168 #endif /* _GLIBCXX_PROFILE_PROFILER_LIST_TO_SLIST_H */