summaryrefslogtreecommitdiff
path: root/src/kpi/dlt-kpi-process-list.c
blob: 66d149e9155a07958148ee12526e873d60664d36 (plain)
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
/*
 * @licence app begin@
 * SPDX license identifier: MPL-2.0
 *
 * Copyright (C) 2011-2015, BMW AG
 *
 * This file is part of GENIVI Project DLT - Diagnostic Log and Trace.
 *
 * This Source Code Form is subject to the terms of the
 * Mozilla Public License (MPL), v. 2.0.
 * If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * For further information see http://www.genivi.org/.
 * @licence end@
 */

/*!
 * \author Sven Hassler <sven_hassler@mentor.com>
 *
 * \copyright Copyright © 2011-2015 BMW AG. \n
 * License MPL-2.0: Mozilla Public License version 2.0 http://mozilla.org/MPL/2.0/.
 *
 * \file dlt-kpi-process-list.c
 */

#include "dlt-kpi-process-list.h"

DltKpiProcessList *dlt_kpi_create_process_list()
{
    DltKpiProcessList *new_list = malloc(sizeof(DltKpiProcessList));

    if (new_list == NULL) {
        fprintf(stderr, "%s: Cannot create process list, out of memory\n", __func__);
        return NULL;
    }

    memset(new_list, 0, sizeof(DltKpiProcessList));
    new_list->start = new_list->cursor = NULL;

    return new_list;
}

DltReturnValue dlt_kpi_free_process_list_soft(DltKpiProcessList *list)
{
    if (list == NULL) {
        fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__);
        return DLT_RETURN_WRONG_PARAMETER;
    }

    free(list);

    return DLT_RETURN_OK;
}

DltReturnValue dlt_kpi_free_process_list(DltKpiProcessList *list)
{
    if (list == NULL) {
        fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__);
        return DLT_RETURN_WRONG_PARAMETER;
    }

    DltKpiProcess *tmp;

    list->cursor = list->start;

    while (list->cursor != NULL) {
        tmp = list->cursor->next;
        dlt_kpi_free_process(list->cursor);
        list->cursor = tmp;
    }

    return dlt_kpi_free_process_list_soft(list);
}

DltKpiProcess *dlt_kpi_get_process_at_cursor(DltKpiProcessList *list)
{
    if (list == NULL) {
        fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__);
        return NULL;
    }

    return list->cursor;
}

DltReturnValue dlt_kpi_reset_cursor(DltKpiProcessList *list)
{
    if (list == NULL) {
        fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__);
        return DLT_RETURN_WRONG_PARAMETER;
    }

    list->cursor = list->start;
    return DLT_RETURN_OK;
}

DltReturnValue dlt_kpi_set_cursor_at_end(DltKpiProcessList *list)
{
    if (list == NULL) {
        fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__);
        return DLT_RETURN_WRONG_PARAMETER;
    }

    list->cursor = list->start;

    if (list->cursor == NULL)
        return DLT_RETURN_OK;

    while (list->cursor->next != NULL)
        dlt_kpi_increment_cursor(list);

    return DLT_RETURN_OK;
}

DltReturnValue dlt_kpi_increment_cursor(DltKpiProcessList *list)
{
    if (list == NULL) {
        fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__);
        return DLT_RETURN_WRONG_PARAMETER;
    }

    if (list->cursor == NULL)
        return DLT_RETURN_ERROR;

    list->cursor = list->cursor->next;

    return DLT_RETURN_OK;
}

DltReturnValue dlt_kpi_decrement_cursor(DltKpiProcessList *list)
{
    if (list == NULL) {
        fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__);
        return DLT_RETURN_WRONG_PARAMETER;
    }

    if (list->cursor == NULL)
        return DLT_RETURN_ERROR;

    list->cursor = list->cursor->prev;

    return DLT_RETURN_OK;
}

DltReturnValue dlt_kpi_add_process_at_start(DltKpiProcessList *list, DltKpiProcess *process)
{
    if ((list == NULL) || (process == NULL)) {
        fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__);
        return DLT_RETURN_WRONG_PARAMETER;
    }

    if (list->start != NULL)
        list->start->prev = process;

    process->next = list->start;
    list->start = process;

    return DLT_RETURN_OK;
}

DltReturnValue dlt_kpi_add_process_before_cursor(DltKpiProcessList *list, DltKpiProcess *process)
{
    if ((list == NULL) || (process == NULL)) {
        fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__);
        return DLT_RETURN_WRONG_PARAMETER;
    }

    if (list->start == NULL) { /* Empty list? */
        DltReturnValue ret = dlt_kpi_add_process_at_start(list, process);
        list->cursor = NULL;
        return ret;
    }
    else if (list->cursor == NULL)
    {
        dlt_kpi_set_cursor_at_end(list);
        DltReturnValue ret = dlt_kpi_add_process_after_cursor(list, process);
        list->cursor = NULL;
        return ret;
    }

    if (list->cursor->prev != NULL)
        list->cursor->prev->next = process;
    else
        list->start = process;

    process->next = list->cursor;
    process->prev = list->cursor->prev;
    list->cursor->prev = process;

    return DLT_RETURN_OK;
}

DltReturnValue dlt_kpi_add_process_after_cursor(DltKpiProcessList *list, DltKpiProcess *process)
{
    if ((list == NULL) || (process == NULL)) {
        fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__);
        return DLT_RETURN_ERROR;
    }

    if (list->cursor == NULL)
        return dlt_kpi_add_process_at_start(list, process);

    if (list->cursor->next != NULL)
        list->cursor->next->prev = process;

    process->next = list->cursor->next;
    process->prev = list->cursor;
    list->cursor->next = process;

    return DLT_RETURN_OK;
}

DltReturnValue dlt_kpi_remove_process_at_cursor_soft(DltKpiProcessList *list)
{
    if (list == NULL) {
        fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__);
        return DLT_RETURN_WRONG_PARAMETER;
    }

    if (list->cursor == NULL) {
        fprintf(stderr, "%s: Cursor is Invalid (NULL)\n", __func__);
        return DLT_RETURN_ERROR;
    }

    DltKpiProcess *tmp = list->cursor;

    if (tmp->prev != NULL) {
        if (tmp->next != NULL) {
            tmp->prev->next = tmp->next;
            tmp->next->prev = tmp->prev;
        }
        else {
            tmp->prev->next = NULL;
        }
    }
    else {
        if (tmp->next != NULL) {
            tmp->next->prev = NULL;
            list->start = tmp->next;
        }
        else {
            list->start = NULL;
        }
    }

    list->cursor = tmp->next; /* becomes NULL if list is at end */

    return DLT_RETURN_OK;
}

DltReturnValue dlt_kpi_remove_process_at_cursor(DltKpiProcessList *list)
{
    if (list == NULL) {
        fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__);
        return DLT_RETURN_WRONG_PARAMETER;
    }

    if (list->cursor == NULL) {
        fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__);
        return DLT_RETURN_ERROR;
    }

    DltKpiProcess *tmp = list->cursor;
    DltReturnValue ret = dlt_kpi_remove_process_at_cursor_soft(list);

    if (ret < DLT_RETURN_OK)
        return ret;

    dlt_kpi_free_process(tmp);

    return DLT_RETURN_OK;
}