added any2unistr predefined function (artf724008)
[deliverable/titan.core.git] / compiler2 / ttcn3 / profiler.c
1 /******************************************************************************
2 * Copyright (c) 2000-2016 Ericsson Telecom AB
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * Balasko, Jeno
10 * Baranyi, Botond
11 *
12 ******************************************************************************/
13
14 #include "profiler.h"
15 #include "../../common/memory.h"
16 #include <string.h>
17 #include <stdio.h>
18
19 /** The file list */
20 static tcov_file_list *the_files = 0;
21
22 /** The database */
23 static profiler_data_t *the_data = 0;
24
25 /** The current database index for the next get_profiler_code_line() call */
26 static int current_index = 0;
27
28 /** The file parameter for the last get_profiler_code_line() call */
29 static char* last_file_name = 0;
30
31 void init_profiler_data(tcov_file_list* p_file_list)
32 {
33 the_files = p_file_list;
34 the_data = (profiler_data_t*)Malloc(sizeof(profiler_data_t));
35 the_data->nof_lines = 0;
36 the_data->size = 4;
37 the_data->lines = (profiler_code_line_t*)Malloc(
38 the_data->size * sizeof(profiler_code_line_t));
39 }
40
41 boolean is_file_profiled(const char* p_file_name)
42 {
43 tcov_file_list* file_walker = the_files;
44 while (0 != file_walker) {
45 if (0 == strcmp(p_file_name, file_walker->file_name)) {
46 return TRUE;
47 }
48 /* check for .ttcnpp instead of .ttcn */
49 size_t file_name_len = strlen(file_walker->file_name);
50 if ('p' == file_walker->file_name[file_name_len - 1] &&
51 'p' == file_walker->file_name[file_name_len - 2] &&
52 0 == strncmp(p_file_name, file_walker->file_name, file_name_len - 2)) {
53 return TRUE;
54 }
55 file_walker = file_walker->next;
56 }
57 return FALSE;
58 }
59
60 void insert_profiler_code_line(const char* p_file_name,
61 const char* p_function_name, int p_line_no)
62 {
63 /* check if the entry already exists */
64 int i;
65 for (i = 0; i < the_data->nof_lines; ++i) {
66 if (0 == strcmp(p_file_name,the_data->lines[i].file_name) &&
67 p_line_no == the_data->lines[i].line_no) {
68 if (0 == the_data->lines[i].function_name && 0 != p_function_name) {
69 the_data->lines[i].function_name = mcopystr(p_function_name);
70 }
71 return;
72 }
73 }
74 if (the_data->nof_lines == the_data->size) {
75 /* database size reached, increase the buffer size (exponentially) */
76 the_data->size *= 2;
77 the_data->lines = (profiler_code_line_t*)Realloc(the_data->lines,
78 the_data->size * sizeof(profiler_code_line_t));
79 }
80 the_data->lines[the_data->nof_lines].file_name = mcopystr(p_file_name);
81 if (0 != p_function_name) {
82 the_data->lines[the_data->nof_lines].function_name = mcopystr(p_function_name);
83 }
84 else {
85 the_data->lines[the_data->nof_lines].function_name = 0;
86 }
87 the_data->lines[the_data->nof_lines].line_no = p_line_no;
88 ++the_data->nof_lines;
89 }
90
91 boolean get_profiler_code_line(const char *p_file_name,
92 char **p_function_name, int *p_line_no)
93 {
94 if (0 == last_file_name || 0 != strcmp(last_file_name, p_file_name)) {
95 /* file parameter changed, reset the current index and store the new file name */
96 current_index = 0;
97 Free(last_file_name);
98 last_file_name = mcopystr(p_file_name);
99 }
100 while (current_index < the_data->nof_lines) {
101 /* find the first entry in the specified file and return it */
102 if (0 == strcmp(p_file_name, the_data->lines[current_index].file_name)) {
103 *p_function_name = the_data->lines[current_index].function_name;
104 *p_line_no = the_data->lines[current_index].line_no;
105 ++current_index;
106 return TRUE;
107 }
108 ++current_index;
109 }
110 /* no entry found with the specified file name */
111 return FALSE;
112 }
113
114 void free_profiler_data()
115 {
116 int i;
117 for (i = 0; i < the_data->nof_lines; ++i) {
118 Free(the_data->lines[i].file_name);
119 Free(the_data->lines[i].function_name);
120 }
121 Free(the_data->lines);
122 Free(the_data);
123 Free(last_file_name);
124
125 while (0 != the_files) {
126 tcov_file_list *next_file = the_files->next;
127 Free(the_files->file_name);
128 Free(the_files);
129 the_files = next_file;
130 }
131 }
This page took 0.033347 seconds and 5 git commands to generate.