1 /***************************************************************************
2 *
3 * Copyright (c) 2015 Advanced Driver Information Technology.
4 * This code is developed by Advanced Driver Information Technology.
5 * Copyright of Advanced Driver Information Technology, Bosch, and DENSO.
6 * All rights reserved.
7 *
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 *
21 ****************************************************************************/
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include "util.h"
26
27 void*
allocate(uint32_t alloc_size,int clear)28 allocate(uint32_t alloc_size, int clear)
29 {
30 if (0 == alloc_size)
31 return NULL;
32
33 return (0 == clear) ? malloc(alloc_size)
34 : calloc(1, alloc_size);
35 }
36
37 void
log_array_init(struct event_log_array * p_array,int n_alloc)38 log_array_init(struct event_log_array *p_array, int n_alloc)
39 {
40 if (NULL != p_array)
41 {
42 memset(p_array, 0x00, sizeof *p_array);
43 if (0 < n_alloc)
44 {
45 p_array->p_logs =
46 (struct event_log*)malloc(sizeof(struct event_log) * n_alloc);
47 if (NULL == p_array->p_logs)
48 {
49 LOG_ERROR("Memory allocation for logs failed\n");
50 exit(-1);
51 }
52 p_array->n_alloc = n_alloc;
53 }
54 }
55 }
56
57 void
log_array_release(struct event_log_array * p_array)58 log_array_release(struct event_log_array *p_array)
59 {
60 if (NULL != p_array)
61 {
62 if ((0 < p_array->n_alloc) && (NULL != p_array->p_logs))
63 {
64 free(p_array->p_logs);
65 }
66 memset(p_array, 0x00, sizeof *p_array);
67 }
68 }
69
70 void
log_array_add(struct event_log_array * p_array,struct event_log * p_log)71 log_array_add(struct event_log_array *p_array, struct event_log *p_log)
72 {
73 if ((NULL != p_array) && (NULL != p_log))
74 {
75 if ((p_array->n_log + 1) > p_array->n_alloc)
76 {
77 p_array->n_alloc += 500;
78 p_array->p_logs = (struct event_log*)
79 realloc(p_array->p_logs,
80 sizeof(struct event_log) * p_array->n_alloc);
81 if (NULL == p_array->p_logs)
82 {
83 LOG_ERROR("Memory allocation for logs failed\n");
84 exit(-1);
85 }
86 }
87
88 p_array->p_logs[p_array->n_log] = *p_log;
89 ++(p_array->n_log);
90 }
91 }
92