Update bug status.
[valgrind.git] / callgrind / fn.c
blobe9d8dd214377a67c61eadb3b7aeccaec1d1aad22
1 /*--------------------------------------------------------------------*/
2 /*--- Callgrind ---*/
3 /*--- ct_fn.c ---*/
4 /*--------------------------------------------------------------------*/
6 /*
7 This file is part of Callgrind, a Valgrind tool for call tracing.
9 Copyright (C) 2002-2017, Josef Weidendorfer (Josef.Weidendorfer@gmx.de)
11 This program is free software; you can redistribute it and/or
12 modify it under the terms of the GNU General Public License as
13 published by the Free Software Foundation; either version 2 of the
14 License, or (at your option) any later version.
16 This program is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, see <http://www.gnu.org/licenses/>.
24 The GNU General Public License is contained in the file COPYING.
27 #include "global.h"
29 #define N_INITIAL_FN_ARRAY_SIZE 10071
31 static fn_array current_fn_active;
33 static Addr runtime_resolve_addr = 0;
34 static int runtime_resolve_length = 0;
36 // a code pattern is a list of tuples (start offset, length)
37 struct chunk_t { int start, len; };
38 struct pattern
40 const HChar* name;
41 int len;
42 struct chunk_t chunk[];
45 /* Scan for a pattern in the code of an ELF object.
46 * If found, return true and set runtime_resolve_{addr,length}
48 __attribute__((unused)) // Possibly; depends on the platform.
49 static Bool check_code(obj_node* obj,
50 UChar code[], struct pattern* pat)
52 Bool found;
53 Addr addr, end;
54 int chunk, start, len;
56 /* first chunk of pattern should always start at offset 0 and
57 * have at least 3 bytes */
58 CLG_ASSERT((pat->chunk[0].start == 0) && (pat->chunk[0].len >2));
60 CLG_DEBUG(1, "check_code: %s, pattern %s, check %d bytes of [%x %x %x...]\n",
61 obj->name, pat->name, pat->chunk[0].len, code[0], code[1], code[2]);
63 end = obj->start + obj->size - pat->len;
64 addr = obj->start;
65 while(addr < end) {
66 found = (VG_(memcmp)( (void*)addr, code, pat->chunk[0].len) == 0);
68 if (found) {
69 chunk = 1;
70 while(1) {
71 start = pat->chunk[chunk].start;
72 len = pat->chunk[chunk].len;
73 if (len == 0) break;
75 CLG_ASSERT(len >2);
76 CLG_DEBUG(1, " found chunk %d at %#lx, checking %d bytes "
77 "of [%x %x %x...]\n",
78 chunk-1, addr - obj->start, len,
79 code[start], code[start+1], code[start+2]);
81 if (VG_(memcmp)( (void*)(addr+start), code+start, len) != 0) {
82 found = False;
83 break;
85 chunk++;
88 if (found) {
89 CLG_DEBUG(1, "found at offset %#lx.\n", addr - obj->start);
90 if (VG_(clo_verbosity) > 1)
91 VG_(message)(Vg_DebugMsg, "Found runtime_resolve (%s): "
92 "%s +%#lx=%#lx, length %d\n",
93 pat->name, obj->name + obj->last_slash_pos,
94 addr - obj->start, addr, pat->len);
96 runtime_resolve_addr = addr;
97 runtime_resolve_length = pat->len;
98 return True;
101 addr++;
103 CLG_DEBUG(1, " found nothing.\n");
104 return False;
108 /* _ld_runtime_resolve, located in ld.so, needs special handling:
109 * The jump at end into the resolved function should not be
110 * represented as a call (as usually done in callgrind with jumps),
111 * but as a return + call. Otherwise, the repeated existence of
112 * _ld_runtime_resolve in call chains will lead to huge cycles,
113 * making the profile almost worthless.
115 * If ld.so is stripped, the symbol will not appear. But as this
116 * function is handcrafted assembler, we search for it.
118 * We stop if the ELF object name does not seem to be the runtime linker
120 static Bool search_runtime_resolve(obj_node* obj)
122 #if defined(VGP_x86_linux)
123 static UChar code[] = {
124 /* 0*/ 0x50, 0x51, 0x52, 0x8b, 0x54, 0x24, 0x10, 0x8b,
125 /* 8*/ 0x44, 0x24, 0x0c, 0xe8, 0x70, 0x01, 0x00, 0x00,
126 /*16*/ 0x5a, 0x59, 0x87, 0x04, 0x24, 0xc2, 0x08, 0x00 };
127 /* Check ranges [0-11] and [16-23] ([12-15] is an absolute address) */
128 static struct pattern pat = {
129 "x86-def", 24, {{ 0,12 }, { 16,8 }, { 24,0}} };
131 /* Pattern for glibc-2.8 on OpenSuse11.0 */
132 static UChar code_28[] = {
133 /* 0*/ 0x50, 0x51, 0x52, 0x8b, 0x54, 0x24, 0x10, 0x8b,
134 /* 8*/ 0x44, 0x24, 0x0c, 0xe8, 0x70, 0x01, 0x00, 0x00,
135 /*16*/ 0x5a, 0x8b, 0x0c, 0x24, 0x89, 0x04, 0x24, 0x8b,
136 /*24*/ 0x44, 0x24, 0x04, 0xc2, 0x0c, 0x00 };
137 static struct pattern pat_28 = {
138 "x86-glibc2.8", 30, {{ 0,12 }, { 16,14 }, { 30,0}} };
140 if (VG_(strncmp)(obj->name, "/lib/ld", 7) != 0) return False;
141 if (check_code(obj, code, &pat)) return True;
142 if (check_code(obj, code_28, &pat_28)) return True;
143 return False;
144 #endif
146 #if defined(VGP_ppc32_linux)
147 static UChar code[] = {
148 /* 0*/ 0x94, 0x21, 0xff, 0xc0, 0x90, 0x01, 0x00, 0x0c,
149 /* 8*/ 0x90, 0x61, 0x00, 0x10, 0x90, 0x81, 0x00, 0x14,
150 /*16*/ 0x7d, 0x83, 0x63, 0x78, 0x90, 0xa1, 0x00, 0x18,
151 /*24*/ 0x7d, 0x64, 0x5b, 0x78, 0x90, 0xc1, 0x00, 0x1c,
152 /*32*/ 0x7c, 0x08, 0x02, 0xa6, 0x90, 0xe1, 0x00, 0x20,
153 /*40*/ 0x90, 0x01, 0x00, 0x30, 0x91, 0x01, 0x00, 0x24,
154 /*48*/ 0x7c, 0x00, 0x00, 0x26, 0x91, 0x21, 0x00, 0x28,
155 /*56*/ 0x91, 0x41, 0x00, 0x2c, 0x90, 0x01, 0x00, 0x08,
156 /*64*/ 0x48, 0x00, 0x02, 0x91, 0x7c, 0x69, 0x03, 0xa6, /* at 64: bl aff0 <fixup> */
157 /*72*/ 0x80, 0x01, 0x00, 0x30, 0x81, 0x41, 0x00, 0x2c,
158 /*80*/ 0x81, 0x21, 0x00, 0x28, 0x7c, 0x08, 0x03, 0xa6,
159 /*88*/ 0x81, 0x01, 0x00, 0x24, 0x80, 0x01, 0x00, 0x08,
160 /*96*/ 0x80, 0xe1, 0x00, 0x20, 0x80, 0xc1, 0x00, 0x1c,
161 /*104*/0x7c, 0x0f, 0xf1, 0x20, 0x80, 0xa1, 0x00, 0x18,
162 /*112*/0x80, 0x81, 0x00, 0x14, 0x80, 0x61, 0x00, 0x10,
163 /*120*/0x80, 0x01, 0x00, 0x0c, 0x38, 0x21, 0x00, 0x40,
164 /*128*/0x4e, 0x80, 0x04, 0x20 };
165 static struct pattern pat = {
166 "ppc32-def", 132, {{ 0,65 }, { 68,64 }, { 132,0 }} };
168 if (VG_(strncmp)(obj->name, "/lib/ld", 7) != 0) return False;
169 return check_code(obj, code, &pat);
170 #endif
172 #if defined(VGP_amd64_linux)
173 static UChar code[] = {
174 /* 0*/ 0x48, 0x83, 0xec, 0x38, 0x48, 0x89, 0x04, 0x24,
175 /* 8*/ 0x48, 0x89, 0x4c, 0x24, 0x08, 0x48, 0x89, 0x54, 0x24, 0x10,
176 /*18*/ 0x48, 0x89, 0x74, 0x24, 0x18, 0x48, 0x89, 0x7c, 0x24, 0x20,
177 /*28*/ 0x4c, 0x89, 0x44, 0x24, 0x28, 0x4c, 0x89, 0x4c, 0x24, 0x30,
178 /*38*/ 0x48, 0x8b, 0x74, 0x24, 0x40, 0x49, 0x89, 0xf3,
179 /*46*/ 0x4c, 0x01, 0xde, 0x4c, 0x01, 0xde, 0x48, 0xc1, 0xe6, 0x03,
180 /*56*/ 0x48, 0x8b, 0x7c, 0x24, 0x38, 0xe8, 0xee, 0x01, 0x00, 0x00,
181 /*66*/ 0x49, 0x89, 0xc3, 0x4c, 0x8b, 0x4c, 0x24, 0x30,
182 /*74*/ 0x4c, 0x8b, 0x44, 0x24, 0x28, 0x48, 0x8b, 0x7c, 0x24, 0x20,
183 /*84*/ 0x48, 0x8b, 0x74, 0x24, 0x18, 0x48, 0x8b, 0x54, 0x24, 0x10,
184 /*94*/ 0x48, 0x8b, 0x4c, 0x24, 0x08, 0x48, 0x8b, 0x04, 0x24,
185 /*103*/0x48, 0x83, 0xc4, 0x48, 0x41, 0xff, 0xe3 };
186 static struct pattern pat = {
187 "amd64-def", 110, {{ 0,62 }, { 66,44 }, { 110,0 }} };
189 if ((VG_(strncmp)(obj->name, "/lib/ld", 7) != 0) &&
190 (VG_(strncmp)(obj->name, "/lib64/ld", 9) != 0)) return False;
191 return check_code(obj, code, &pat);
192 #endif
194 /* For other platforms, no patterns known */
195 return False;
199 /*------------------------------------------------------------*/
200 /*--- Object/File/Function hash entry operations ---*/
201 /*------------------------------------------------------------*/
203 /* Object hash table, fixed */
204 static obj_node* obj_table[N_OBJ_ENTRIES];
206 void CLG_(init_obj_table)()
208 Int i;
209 for (i = 0; i < N_OBJ_ENTRIES; i++)
210 obj_table[i] = 0;
213 #define HASH_CONSTANT 256
215 static UInt str_hash(const HChar *s, UInt table_size)
217 int hash_value = 0;
218 for ( ; *s; s++)
219 hash_value = (HASH_CONSTANT * hash_value + *s) % table_size;
220 return hash_value;
224 static const HChar* anonymous_obj = "???";
226 static __inline__
227 obj_node* new_obj_node(DebugInfo* di, obj_node* next)
229 Int i;
230 obj_node* obj;
232 obj = (obj_node*) CLG_MALLOC("cl.fn.non.1", sizeof(obj_node));
233 obj->name = di ? VG_(strdup)( "cl.fn.non.2",
234 VG_(DebugInfo_get_filename)(di) )
235 : anonymous_obj;
236 for (i = 0; i < N_FILE_ENTRIES; i++) {
237 obj->files[i] = NULL;
239 CLG_(stat).distinct_objs ++;
240 obj->number = CLG_(stat).distinct_objs;
241 /* JRS 2008 Feb 19: maybe rename .start/.size/.offset to
242 .text_avma/.text_size/.test_bias to make it clearer what these
243 fields really mean */
244 obj->start = di ? VG_(DebugInfo_get_text_avma)(di) : 0;
245 obj->size = di ? VG_(DebugInfo_get_text_size)(di) : 0;
246 obj->offset = di ? VG_(DebugInfo_get_text_bias)(di) : 0;
247 obj->next = next;
249 // not only used for debug output (see static.c)
250 obj->last_slash_pos = 0;
251 i = 0;
252 while(obj->name[i]) {
253 if (obj->name[i]=='/') obj->last_slash_pos = i+1;
254 i++;
257 if (runtime_resolve_addr == 0) search_runtime_resolve(obj);
259 return obj;
262 obj_node* CLG_(get_obj_node)(DebugInfo* di)
264 obj_node* curr_obj_node;
265 UInt objname_hash;
266 const HChar* obj_name;
268 obj_name = di ? VG_(DebugInfo_get_filename)(di) : anonymous_obj;
270 /* lookup in obj hash */
271 objname_hash = str_hash(obj_name, N_OBJ_ENTRIES);
272 curr_obj_node = obj_table[objname_hash];
273 while (NULL != curr_obj_node &&
274 VG_(strcmp)(obj_name, curr_obj_node->name) != 0) {
275 curr_obj_node = curr_obj_node->next;
277 if (NULL == curr_obj_node) {
278 obj_table[objname_hash] = curr_obj_node =
279 new_obj_node(di, obj_table[objname_hash]);
282 return curr_obj_node;
286 static __inline__
287 file_node* new_file_node(const HChar *filename,
288 obj_node* obj, file_node* next)
290 Int i;
291 file_node* file = (file_node*) CLG_MALLOC("cl.fn.nfn.1",
292 sizeof(file_node));
293 file->name = VG_(strdup)("cl.fn.nfn.2", filename);
294 for (i = 0; i < N_FN_ENTRIES; i++) {
295 file->fns[i] = NULL;
297 CLG_(stat).distinct_files++;
298 file->number = CLG_(stat).distinct_files;
299 file->obj = obj;
300 file->next = next;
301 return file;
305 file_node* CLG_(get_file_node)(obj_node* curr_obj_node,
306 const HChar *dir, const HChar *file)
308 file_node* curr_file_node;
309 UInt filename_hash;
311 /* Build up an absolute pathname, if there is a directory available */
312 HChar filename[VG_(strlen)(dir) + 1 + VG_(strlen)(file) + 1];
313 VG_(strcpy)(filename, dir);
314 if (filename[0] != '\0') {
315 VG_(strcat)(filename, "/");
317 VG_(strcat)(filename, file);
319 /* lookup in file hash */
320 filename_hash = str_hash(filename, N_FILE_ENTRIES);
321 curr_file_node = curr_obj_node->files[filename_hash];
322 while (NULL != curr_file_node &&
323 VG_(strcmp)(filename, curr_file_node->name) != 0) {
324 curr_file_node = curr_file_node->next;
326 if (NULL == curr_file_node) {
327 curr_obj_node->files[filename_hash] = curr_file_node =
328 new_file_node(filename, curr_obj_node,
329 curr_obj_node->files[filename_hash]);
332 return curr_file_node;
335 /* forward decl. */
336 static void resize_fn_array(void);
338 static __inline__
339 fn_node* new_fn_node(const HChar *fnname,
340 file_node* file, fn_node* next)
342 fn_node* fn = (fn_node*) CLG_MALLOC("cl.fn.nfnnd.1",
343 sizeof(fn_node));
344 fn->name = VG_(strdup)("cl.fn.nfnnd.2", fnname);
346 CLG_(stat).distinct_fns++;
347 fn->number = CLG_(stat).distinct_fns;
348 fn->last_cxt = 0;
349 fn->pure_cxt = 0;
350 fn->file = file;
351 fn->next = next;
353 fn->dump_before = False;
354 fn->dump_after = False;
355 fn->zero_before = False;
356 fn->toggle_collect = False;
357 fn->skip = False;
358 fn->pop_on_jump = CLG_(clo).pop_on_jump;
359 fn->is_malloc = False;
360 fn->is_realloc = False;
361 fn->is_free = False;
363 fn->group = 0;
364 fn->separate_callers = CLG_(clo).separate_callers;
365 fn->separate_recursions = CLG_(clo).separate_recursions;
367 #if CLG_ENABLE_DEBUG
368 fn->verbosity = -1;
369 #endif
371 if (CLG_(stat).distinct_fns >= current_fn_active.size)
372 resize_fn_array();
374 return fn;
378 /* Get a function node in hash2 with known file node.
379 * hash nodes are created if needed
381 static
382 fn_node* get_fn_node_infile(file_node* curr_file_node,
383 const HChar *fnname)
385 fn_node* curr_fn_node;
386 UInt fnname_hash;
388 CLG_ASSERT(curr_file_node != 0);
390 /* lookup in function hash */
391 fnname_hash = str_hash(fnname, N_FN_ENTRIES);
392 curr_fn_node = curr_file_node->fns[fnname_hash];
393 while (NULL != curr_fn_node &&
394 VG_(strcmp)(fnname, curr_fn_node->name) != 0) {
395 curr_fn_node = curr_fn_node->next;
397 if (NULL == curr_fn_node) {
398 curr_file_node->fns[fnname_hash] = curr_fn_node =
399 new_fn_node(fnname, curr_file_node,
400 curr_file_node->fns[fnname_hash]);
403 return curr_fn_node;
407 /* Get a function node in a Segment.
408 * Hash nodes are created if needed.
410 static __inline__
411 fn_node* get_fn_node_inseg(DebugInfo* di,
412 const HChar *dirname,
413 const HChar *filename,
414 const HChar *fnname)
416 obj_node *obj = CLG_(get_obj_node)(di);
417 file_node *file = CLG_(get_file_node)(obj, dirname, filename);
418 fn_node *fn = get_fn_node_infile(file, fnname);
420 return fn;
424 Bool CLG_(get_debug_info)(Addr instr_addr,
425 const HChar **dir,
426 const HChar **file,
427 const HChar **fn_name, UInt* line_num,
428 DebugInfo** pDebugInfo)
430 Bool found_file_line, found_fn, result = True;
431 UInt line;
433 CLG_DEBUG(6, " + get_debug_info(%#lx)\n", instr_addr);
435 DiEpoch ep = VG_(current_DiEpoch)();
436 if (pDebugInfo) {
437 *pDebugInfo = VG_(find_DebugInfo)(ep, instr_addr);
439 // for generated code in anonymous space, pSegInfo is 0
442 found_file_line = VG_(get_filename_linenum)(ep, instr_addr,
443 file,
444 dir,
445 &line);
446 found_fn = VG_(get_fnname)(ep, instr_addr, fn_name);
448 if (!found_file_line && !found_fn) {
449 CLG_(stat).no_debug_BBs++;
450 *file = "???";
451 *fn_name = "???";
452 if (line_num) *line_num=0;
453 result = False;
455 } else if ( found_file_line && found_fn) {
456 CLG_(stat).full_debug_BBs++;
457 if (line_num) *line_num=line;
459 } else if ( found_file_line && !found_fn) {
460 CLG_(stat).file_line_debug_BBs++;
461 *fn_name = "???";
462 if (line_num) *line_num=line;
464 } else /*(!found_file_line && found_fn)*/ {
465 CLG_(stat).fn_name_debug_BBs++;
466 *file = "???";
467 if (line_num) *line_num=0;
470 CLG_DEBUG(6, " - get_debug_info(%#lx): seg '%s', fn %s\n",
471 instr_addr,
472 !pDebugInfo ? "-" :
473 (*pDebugInfo) ? VG_(DebugInfo_get_filename)(*pDebugInfo) :
474 "(None)",
475 *fn_name);
477 return result;
480 /* for _libc_freeres_wrapper => _exit renaming */
481 static BB* exit_bb = 0;
485 * Attach function struct to a BB from debug info.
487 fn_node* CLG_(get_fn_node)(BB* bb)
489 const HChar *fnname, *filename, *dirname;
490 DebugInfo* di;
491 UInt line_num;
492 fn_node* fn;
494 /* fn from debug info is idempotent for a BB */
495 if (bb->fn) return bb->fn;
497 CLG_DEBUG(3,"+ get_fn_node(BB %#lx)\n", bb_addr(bb));
499 /* get function/file name, line number and object of
500 * the BB according to debug information
502 CLG_(get_debug_info)(bb_addr(bb),
503 &dirname, &filename, &fnname, &line_num, &di);
505 DiEpoch ep = VG_(current_DiEpoch)();
506 if (0 == VG_(strcmp)(fnname, "???")) {
507 int p;
508 static HChar buf[32]; // for sure large enough
509 /* Use address as found in library */
510 if (sizeof(Addr) == 4)
511 p = VG_(sprintf)(buf, "%#08lx", (UWord)bb->offset);
512 else
513 // 64bit address
514 p = VG_(sprintf)(buf, "%#016lx", (UWord)bb->offset);
516 VG_(sprintf)(buf + p, "%s",
517 (bb->sect_kind == Vg_SectData) ? " [Data]" :
518 (bb->sect_kind == Vg_SectBSS) ? " [BSS]" :
519 (bb->sect_kind == Vg_SectGOT) ? " [GOT]" :
520 (bb->sect_kind == Vg_SectPLT) ? " [PLT]" : "");
521 fnname = buf;
523 else {
524 if (VG_(get_fnname_if_entry)(ep, bb_addr(bb), &fnname))
525 bb->is_entry = 1;
528 /* HACK for correct _exit:
529 * _exit is redirected to VG_(__libc_freeres_wrapper) by valgrind,
530 * so we rename it back again :-)
532 if (0 == VG_(strcmp)(fnname, "vgPlain___libc_freeres_wrapper")
533 && exit_bb) {
534 CLG_(get_debug_info)(bb_addr(exit_bb),
535 &dirname, &filename, &fnname, &line_num, &di);
537 CLG_DEBUG(1, "__libc_freeres_wrapper renamed to _exit\n");
539 if (0 == VG_(strcmp)(fnname, "_exit") && !exit_bb)
540 exit_bb = bb;
542 if (runtime_resolve_addr &&
543 (bb_addr(bb) >= runtime_resolve_addr) &&
544 (bb_addr(bb) < runtime_resolve_addr + runtime_resolve_length)) {
545 /* BB in runtime_resolve found by code check; use this name */
546 fnname = "_dl_runtime_resolve";
549 /* get fn_node struct for this function */
550 fn = get_fn_node_inseg( di, dirname, filename, fnname);
552 /* if this is the 1st time the function is seen,
553 * some attributes are set */
554 if (fn->pure_cxt == 0) {
556 /* Every function gets a "pure" context, i.e. a context with stack
557 * depth 1 only with this function. This is for compression of mangled
558 * names
560 fn_node* pure[2];
561 pure[0] = 0;
562 pure[1] = fn;
563 fn->pure_cxt = CLG_(get_cxt)(pure+1);
565 if (bb->sect_kind == Vg_SectPLT)
566 fn->skip = CLG_(clo).skip_plt;
568 if (VG_(strcmp)(fn->name, "_dl_runtime_resolve")==0) {
569 fn->pop_on_jump = True;
571 if (VG_(clo_verbosity) > 1)
572 VG_(message)(Vg_DebugMsg, "Symbol match: found runtime_resolve:"
573 " %s +%#lx=%#lx\n",
574 bb->obj->name + bb->obj->last_slash_pos,
575 (UWord)bb->offset, bb_addr(bb));
578 fn->is_malloc = (VG_(strcmp)(fn->name, "malloc")==0);
579 fn->is_realloc = (VG_(strcmp)(fn->name, "realloc")==0);
580 fn->is_free = (VG_(strcmp)(fn->name, "free")==0);
582 /* apply config options from function name patterns
583 * given on command line */
584 CLG_(update_fn_config)(fn);
588 bb->fn = fn;
589 bb->line = line_num;
591 if (dirname[0]) {
592 CLG_DEBUG(3,"- get_fn_node(BB %#lx): %s (in %s:%u)\n",
593 bb_addr(bb), fnname, filename, line_num);
594 } else
595 CLG_DEBUG(3,"- get_fn_node(BB %#lx): %s (in %s/%s:%u)\n",
596 bb_addr(bb), fnname, dirname, filename, line_num);
598 return fn;
602 /*------------------------------------------------------------*/
603 /*--- Active function array operations ---*/
604 /*------------------------------------------------------------*/
606 /* The active function array is a thread-specific array
607 * of UInts, mapping function numbers to the active count of
608 * functions.
609 * The active count is the number of times a function appears
610 * in the current call stack, and is used when costs for recursion
611 * levels should be separated.
614 UInt* CLG_(get_fn_entry)(Int n)
616 CLG_ASSERT(n < current_fn_active.size);
617 return current_fn_active.array + n;
620 void CLG_(init_fn_array)(fn_array* a)
622 Int i;
624 CLG_ASSERT(a != 0);
626 a->size = N_INITIAL_FN_ARRAY_SIZE;
627 if (a->size <= CLG_(stat).distinct_fns)
628 a->size = CLG_(stat).distinct_fns+1;
630 a->array = (UInt*) CLG_MALLOC("cl.fn.gfe.1",
631 a->size * sizeof(UInt));
632 for(i=0;i<a->size;i++)
633 a->array[i] = 0;
636 void CLG_(copy_current_fn_array)(fn_array* dst)
638 CLG_ASSERT(dst != 0);
640 dst->size = current_fn_active.size;
641 dst->array = current_fn_active.array;
644 fn_array* CLG_(get_current_fn_array)()
646 return &current_fn_active;
649 void CLG_(set_current_fn_array)(fn_array* a)
651 CLG_ASSERT(a != 0);
653 current_fn_active.size = a->size;
654 current_fn_active.array = a->array;
655 if (current_fn_active.size <= CLG_(stat).distinct_fns)
656 resize_fn_array();
659 /* ensure that active_array is big enough:
660 * <distinct_fns> is the highest index, so <fn_active_array_size>
661 * has to be bigger than that.
663 static void resize_fn_array(void)
665 UInt* new_array;
666 Int i;
668 UInt newsize = current_fn_active.size;
669 while (newsize <= CLG_(stat).distinct_fns) newsize *=2;
671 CLG_DEBUG(0, "Resize fn_active_array: %u => %u\n",
672 current_fn_active.size, newsize);
674 new_array = (UInt*) CLG_MALLOC("cl.fn.rfa.1", newsize * sizeof(UInt));
675 for(i=0;i<current_fn_active.size;i++)
676 new_array[i] = current_fn_active.array[i];
677 while(i<newsize)
678 new_array[i++] = 0;
680 VG_(free)(current_fn_active.array);
681 current_fn_active.size = newsize;
682 current_fn_active.array = new_array;
683 CLG_(stat).fn_array_resizes++;