-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcprof_profile.c
More file actions
319 lines (241 loc) · 8.7 KB
/
cprof_profile.c
File metadata and controls
319 lines (241 loc) · 8.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* CProfiles
* =========
* Copyright (C) 2024 The CProfiles Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cprofiles/cprofiles.h>
struct cprof_profile *cprof_profile_create()
{
struct cprof_profile *profile;
profile = calloc(1, sizeof(struct cprof_profile));
if (profile == NULL) {
return NULL;
}
cfl_list_init(&profile->sample_type);
cfl_list_init(&profile->samples);
cfl_list_init(&profile->mappings);
cfl_list_init(&profile->locations);
cfl_list_init(&profile->functions);
cfl_list_init(&profile->attribute_units);
cfl_list_init(&profile->link_table);
profile->attributes = cfl_kvlist_create();
if (profile->attributes == NULL) {
cprof_profile_destroy(profile);
return NULL;
}
profile->attribute_table = cfl_kvlist_create();
if (profile->attribute_table == NULL) {
cprof_profile_destroy(profile);
return NULL;
}
return profile;
}
int cprof_profile_add_location_index(struct cprof_profile *profile, int64_t index)
{
size_t new_size;
size_t alloc_slots = 32;
int64_t *reallocated_array;
if (profile->location_indices == NULL) {
profile->location_indices = calloc(alloc_slots, sizeof(int64_t));
if (profile->location_indices == NULL) {
return -1;
}
profile->location_indices_count = 0;
profile->location_indices_size = alloc_slots;
}
if (profile->location_indices_count >= profile->location_indices_size) {
new_size = profile->location_indices_size + alloc_slots;
reallocated_array = realloc(profile->location_indices, new_size * sizeof(int64_t));
if (reallocated_array == NULL) {
return -1;
}
profile->location_indices = reallocated_array;
profile->location_indices_size = new_size;
}
profile->location_indices[profile->location_indices_count] = index;
profile->location_indices_count++;
return 0;
}
void cprof_profile_destroy(struct cprof_profile *instance)
{
struct cfl_list *iterator_backup;
struct cprof_attribute_unit *attribute_unit;
struct cprof_value_type *value_type;
struct cprof_location *location;
struct cprof_function *function;
struct cfl_list *iterator;
struct cprof_mapping *mapping;
struct cprof_sample *sample;
size_t index;
struct cprof_link *link;
if (instance->attributes != NULL) {
cfl_kvlist_destroy(instance->attributes);
}
if (instance->original_payload_format != NULL) {
cfl_sds_destroy(instance->original_payload_format);
}
if (instance->original_payload != NULL) {
cfl_sds_destroy(instance->original_payload);
}
cfl_list_foreach_safe(iterator,
iterator_backup,
&instance->sample_type) {
value_type = cfl_list_entry(iterator,
struct cprof_value_type,
_head);
cfl_list_del(&value_type->_head);
cprof_sample_type_destroy(value_type);
}
cfl_list_foreach_safe(iterator,
iterator_backup,
&instance->samples) {
sample = cfl_list_entry(iterator,
struct cprof_sample,
_head);
cfl_list_del(&sample->_head);
cprof_sample_destroy(sample);
}
cfl_list_foreach_safe(iterator,
iterator_backup,
&instance->mappings) {
mapping = cfl_list_entry(iterator,
struct cprof_mapping,
_head);
cfl_list_del(&mapping->_head);
cprof_mapping_destroy(mapping);
}
cfl_list_foreach_safe(iterator,
iterator_backup,
&instance->locations) {
location = cfl_list_entry(iterator,
struct cprof_location,
_head);
cfl_list_del(&location->_head);
cprof_location_destroy(location);
}
if (instance->location_indices != NULL) {
free(instance->location_indices);
}
cfl_list_foreach_safe(iterator,
iterator_backup,
&instance->functions) {
function = cfl_list_entry(iterator,
struct cprof_function,
_head);
cfl_list_del(&function->_head);
cprof_function_destroy(function);
}
if (instance->attribute_table != NULL) {
cfl_kvlist_destroy(instance->attribute_table);
}
cfl_list_foreach_safe(iterator,
iterator_backup,
&instance->attribute_units) {
attribute_unit = cfl_list_entry(iterator,
struct cprof_attribute_unit,
_head);
cfl_list_del(&attribute_unit->_head);
cprof_attribute_unit_destroy(attribute_unit);
}
cfl_list_foreach_safe(iterator,
iterator_backup,
&instance->link_table) {
link = cfl_list_entry(iterator,
struct cprof_link,
_head);
cfl_list_del(&link->_head);
cprof_link_destroy(link);
}
if (instance->string_table != NULL) {
for (index = 0 ; index < instance->string_table_count ; index++) {
cfl_sds_destroy(instance->string_table[index]);
}
free(instance->string_table);
}
if (instance->comments != NULL) {
free(instance->comments);
}
free(instance);
}
size_t cprof_profile_string_add(struct cprof_profile *profile, char *str, int str_len)
{
const int alloc_size = 64;
size_t id;
size_t new_size;
if (!str) {
return -1;
}
if (str_len <= 0) {
str_len = strlen(str);
}
if (profile->string_table == NULL) {
profile->string_table = calloc(alloc_size, sizeof(cfl_sds_t));
if (profile->string_table == NULL) {
return -1;
}
profile->string_table_size = alloc_size;
profile->string_table_count = 0;
}
if (profile->string_table_count == 0 && str_len > 0) {
/* string_table[0] must always be "" */
profile->string_table[0] = cfl_sds_create_len("", 0);
if (!profile->string_table[0]) {
return -1;
}
profile->string_table_count = 1;
}
/* check there is enough room for a new entry */
if (profile->string_table_count >= profile->string_table_size) {
new_size = profile->string_table_size + alloc_size;
profile->string_table = realloc(profile->string_table, new_size * sizeof(cfl_sds_t));
if (!profile->string_table) {
return -1;
}
profile->string_table_size = alloc_size;
}
id = profile->string_table_count;
profile->string_table[id] = cfl_sds_create_len(str, str_len);
if (!profile->string_table[id]) {
return -1;
}
profile->string_table_count++;
return id;
}
int cprof_profile_add_comment(struct cprof_profile *profile, int64_t comment)
{
size_t new_size;
size_t alloc_slots = 32;
int64_t *reallocated_array;
if (profile->comments == NULL) {
profile->comments = calloc(alloc_slots, sizeof(int64_t));
if (profile->comments == NULL) {
return -1;
}
profile->comments_count = 0;
profile->comments_size = alloc_slots;
}
if (profile->comments_count >= profile->comments_size) {
new_size = profile->comments_size + alloc_slots;
reallocated_array = realloc(profile->comments, new_size * sizeof(int64_t));
if (reallocated_array == NULL) {
return -1;
}
profile->comments = reallocated_array;
profile->comments_size = new_size;
}
profile->comments[profile->comments_count] = comment;
profile->comments_count++;
return 0;
}