3 // Copyright (C) 2009, 2010 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the terms
7 // of the GNU General Public License as published by the Free Software
8 // Foundation; either version 2, or (at your option) any later
11 // This library is distributed in the hope that it will be useful, but
12 // WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this library; see the file COPYING. If not, write to
18 // the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
19 // MA 02111-1307, USA.
21 // As a special exception, you may use this file as part of a free
22 // software library without restriction. Specifically, if other files
23 // instantiate templates or use macros or inline functions from this
24 // file, or you compile this file and link it with other files to
25 // produce an executable, this file does not by itself cause the
26 // resulting executable to be covered by the GNU General Public
27 // License. This exception does not however invalidate any other
28 // reasons why the executable file might be covered by the GNU General
31 /** @file profile/impl/profiler_list_to_vector.h
32 * @brief diagnostics for list to vector.
35 // Written by Changhee Jung.
37 #ifndef _GLIBCXX_PROFILE_PROFILER_LIST_TO_VECTOR_H
38 #define _GLIBCXX_PROFILE_PROFILER_LIST_TO_VECTOR_H 1
42 #include "profile/impl/profiler.h"
43 #include "profile/impl/profiler_node.h"
44 #include "profile/impl/profiler_trace.h"
46 namespace __gnu_profile
48 /** @brief A list-to-vector instrumentation line in the object table. */
49 class __list2vector_info
50 : public __object_info_base
54 : _M_shift_count(0), _M_iterate(0), _M_resize(0), _M_list_cost(0),
55 _M_vector_cost(0), _M_valid(true), _M_max_size(0) { }
57 __list2vector_info(__stack_t __stack)
58 : __object_info_base(__stack), _M_shift_count(0), _M_iterate(0),
59 _M_resize(0), _M_list_cost(0), _M_vector_cost(0), _M_valid(true),
62 virtual ~__list2vector_info() { }
64 __list2vector_info(const __list2vector_info& __o)
65 : __object_info_base(__o), _M_shift_count(__o._M_shift_count),
66 _M_iterate(__o._M_iterate), _M_resize(__o._M_resize),
67 _M_list_cost(__o._M_list_cost), _M_vector_cost(__o._M_vector_cost),
68 _M_valid(__o._M_valid), _M_max_size(__o._M_max_size) { }
71 __merge(const __list2vector_info& __o)
73 _M_shift_count += __o._M_shift_count;
74 _M_iterate += __o._M_iterate;
75 _M_vector_cost += __o._M_vector_cost;
76 _M_list_cost += __o._M_list_cost;
77 _M_valid &= __o._M_valid;
78 _M_resize += __o._M_resize;
79 _M_max_size = std::max( _M_max_size, __o._M_max_size);
83 __write(FILE* __f) const
85 std::fprintf(__f, "%Zu %Zu %Zu %.0f %.0f\n", _M_shift_count,
86 _M_resize, _M_iterate, _M_vector_cost, _M_list_cost);
91 { return _M_list_cost - _M_vector_cost; }
96 std::stringstream __sstream;
98 << "change std::list to std::vector and its initial size from 0 to "
100 return __sstream.str();
105 { return _M_shift_count; }
109 { return _M_iterate; }
113 { return _M_list_cost; }
117 { return _M_resize; }
120 __set_list_cost(float __lc)
121 { _M_list_cost = __lc; }
124 __set_vector_cost(float __vc)
125 { _M_vector_cost = __vc; }
133 { _M_valid = false; }
136 __opr_insert(std::size_t __shift, std::size_t __size)
138 _M_shift_count += __shift;
139 _M_max_size = std::max(_M_max_size, __size);
143 __opr_iterate(std::size_t __num)
144 { _M_iterate += __num;}
147 __resize(std::size_t __from, std::size_t)
148 { _M_resize += __from; }
151 std::size_t _M_shift_count;
152 std::size_t _M_iterate;
153 std::size_t _M_resize;
155 float _M_vector_cost;
157 std::size_t _M_max_size;
160 class __list2vector_stack_info
161 : public __list2vector_info
164 __list2vector_stack_info(const __list2vector_info& __o)
165 : __list2vector_info(__o) {}
168 class __trace_list_to_vector
169 : public __trace_base<__list2vector_info, __list2vector_stack_info>
172 __trace_list_to_vector()
173 : __trace_base<__list2vector_info, __list2vector_stack_info>()
174 { __id = "list-to-vector"; }
176 ~__trace_list_to_vector() { }
178 // Insert a new node at construct with object, callstack and initial size.
180 __insert(__object_t __obj, __stack_t __stack)
181 { __add_object(__obj, __list2vector_info(__stack)); }
183 // Call at destruction/clean to set container final size.
185 __destruct(const void* __obj)
190 __list2vector_info* __res = __get_object_info(__obj);
194 float __vc = __vector_cost(__res->__shift_count(), __res->__iterate());
195 float __lc = __list_cost(__res->__shift_count(), __res->__iterate());
196 __res->__set_vector_cost(__vc);
197 __res->__set_list_cost(__lc);
198 __retire_object(__obj);
201 // Find the node in the live map.
202 __list2vector_info* __find(const void* __obj);
204 // Collect cost of operations.
206 __opr_insert(const void* __obj, std::size_t __shift, std::size_t __size)
208 __list2vector_info* __res = __get_object_info(__obj);
210 __res->__opr_insert(__shift, __size);
214 __opr_iterate(const void* __obj, std::size_t __num)
216 __list2vector_info* __res = __get_object_info(__obj);
218 __res->__opr_iterate(__num);
222 __invalid_operator(const void* __obj)
224 __list2vector_info* __res = __get_object_info(__obj);
226 __res->__set_invalid();
230 __resize(const void* __obj, std::size_t __from, std::size_t __to)
232 __list2vector_info* __res = __get_object_info(__obj);
234 __res->__resize(__from, __to);
238 __vector_cost(std::size_t __shift, std::size_t __iterate)
240 // The resulting vector will use a 'reserve' method.
242 * _GLIBCXX_PROFILE_DATA(__vector_shift_cost_factor).__value
244 * _GLIBCXX_PROFILE_DATA(__vector_iterate_cost_factor).__value);
248 __list_cost(std::size_t __shift, std::size_t __iterate)
251 * _GLIBCXX_PROFILE_DATA(__list_shift_cost_factor).__value
253 * _GLIBCXX_PROFILE_DATA(__list_iterate_cost_factor).__value);
259 __trace_list_to_vector_init()
260 { _GLIBCXX_PROFILE_DATA(_S_list_to_vector) = new __trace_list_to_vector(); }
263 __trace_list_to_vector_report(FILE* __f, __warning_vector_t& __warnings)
265 if (_GLIBCXX_PROFILE_DATA(_S_list_to_vector))
267 _GLIBCXX_PROFILE_DATA(_S_list_to_vector)->
268 __collect_warnings(__warnings);
269 _GLIBCXX_PROFILE_DATA(_S_list_to_vector)->__write(__f);
274 __trace_list_to_vector_construct(const void* __obj)
276 if (!__profcxx_init())
279 _GLIBCXX_PROFILE_DATA(_S_list_to_vector)->__insert(__obj, __get_stack());
283 __trace_list_to_vector_destruct(const void* __obj)
285 if (!__profcxx_init())
288 _GLIBCXX_PROFILE_DATA(_S_list_to_vector)->__destruct(__obj);
292 __trace_list_to_vector_insert(const void* __obj,
293 std::size_t __shift, std::size_t __size)
295 if (!__profcxx_init())
298 _GLIBCXX_PROFILE_DATA(_S_list_to_vector)->__opr_insert(__obj, __shift,
303 __trace_list_to_vector_iterate(const void* __obj, std::size_t __num = 1)
305 if (!__profcxx_init())
308 _GLIBCXX_PROFILE_DATA(_S_list_to_vector)->__opr_iterate(__obj, __num);
312 __trace_list_to_vector_invalid_operator(const void* __obj)
314 if (!__profcxx_init())
317 _GLIBCXX_PROFILE_DATA(_S_list_to_vector)->__invalid_operator(__obj);
321 __trace_list_to_vector_resize(const void* __obj,
322 std::size_t __from, std::size_t __to)
324 if (!__profcxx_init())
327 _GLIBCXX_PROFILE_DATA(_S_list_to_vector)->__resize(__obj, __from, __to);
330 } // namespace __gnu_profile
331 #endif /* _GLIBCXX_PROFILE_PROFILER_LIST_TO_VECTOR_H__ */