aboutsummaryrefslogtreecommitdiff
path: root/platform/linux-generic/odp_sorted_list.c
blob: 3cca8be0c71b07760ac7897d47887ea7c046490b (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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright (c) 2015 EZchip Semiconductor Ltd.
 * Copyright (c) 2015-2018 Linaro Limited
 */

#include <stdint.h>
#include <string.h>
#include <malloc.h>
#include <stdio.h>
#include <inttypes.h>
#include <odp_debug_internal.h>
#include <odp_sorted_list_internal.h>

typedef struct sorted_list_item_s sorted_list_item_t;

struct sorted_list_item_s {
	sorted_list_item_t *next_item;
	uint64_t            sort_key;
	uint64_t            user_data;
};

typedef struct {
	sorted_list_item_t *first_item;
	uint32_t            sorted_list_len;
	uint32_t            pad;
} sorted_list_desc_t;

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
typedef struct {
	sorted_list_desc_t descs[0];
} sorted_list_descs_t;
#pragma GCC diagnostic pop

typedef struct {
	uint64_t             total_inserts;
	uint64_t             total_deletes;
	uint64_t             total_removes;
	uint32_t             max_sorted_lists;
	uint32_t             next_list_idx;
	sorted_list_descs_t *list_descs;
} sorted_pool_t;

_odp_int_sorted_pool_t _odp_sorted_pool_create(uint32_t max_sorted_lists)
{
	sorted_list_descs_t *list_descs;
	sorted_pool_t       *pool;
	uint32_t             malloc_len;

	pool = malloc(sizeof(sorted_pool_t));
	memset(pool, 0, sizeof(sorted_pool_t));
	pool->max_sorted_lists = max_sorted_lists;
	pool->next_list_idx    = 1;

	malloc_len = max_sorted_lists * sizeof(sorted_list_desc_t);
	list_descs = malloc(malloc_len);
	memset(list_descs, 0, malloc_len);
	pool->list_descs = list_descs;
	return (_odp_int_sorted_pool_t)(uintptr_t)pool;
}

_odp_int_sorted_list_t
_odp_sorted_list_create(_odp_int_sorted_pool_t sorted_pool,
			uint32_t max_entries ODP_UNUSED)
{
	sorted_pool_t *pool;
	uint32_t       list_idx;

	pool     = (sorted_pool_t *)(uintptr_t)sorted_pool;
	list_idx = pool->next_list_idx++;
	return (_odp_int_sorted_list_t)list_idx;
}

int _odp_sorted_list_insert(_odp_int_sorted_pool_t sorted_pool,
			    _odp_int_sorted_list_t sorted_list,
			    uint64_t              sort_key,
			    uint64_t              user_data)
{
	sorted_list_desc_t *list_desc;
	sorted_list_item_t *new_list_item, *list_item, *prev_list_item;
	sorted_pool_t      *pool;
	uint32_t            list_idx;

	pool     = (sorted_pool_t *)(uintptr_t)sorted_pool;
	list_idx = (uint32_t)sorted_list;
	if ((pool->next_list_idx    <= list_idx) ||
	    (pool->max_sorted_lists <= list_idx))
		return -1;

	list_desc     = &pool->list_descs->descs[list_idx];
	new_list_item = malloc(sizeof(sorted_list_item_t));
	memset(new_list_item, 0, sizeof(sorted_list_item_t));
	new_list_item->next_item = NULL;
	new_list_item->sort_key  = sort_key;
	new_list_item->user_data = user_data;

       /* Now insert the new_list_item according to the sort_key (lowest
	* value first).
	*/
	list_item      = list_desc->first_item;
	prev_list_item = NULL;
	while ((list_item) && (list_item->sort_key <= sort_key)) {
		prev_list_item = list_item;
		list_item      = list_item->next_item;
	}

	new_list_item->next_item = list_item;
	if (!prev_list_item)
		list_desc->first_item = new_list_item;
	else
		prev_list_item->next_item = new_list_item;

	list_desc->sorted_list_len++;
	pool->total_inserts++;
	return 0;
}

int _odp_sorted_list_find(_odp_int_sorted_pool_t sorted_pool,
			  _odp_int_sorted_list_t sorted_list,
			  uint64_t              user_data,
			  uint64_t             *sort_key_ptr)
{
	sorted_list_desc_t *list_desc;
	sorted_list_item_t *list_item;
	sorted_pool_t      *pool;
	uint32_t            list_idx;

	pool     = (sorted_pool_t *)(uintptr_t)sorted_pool;
	list_idx = (uint32_t)sorted_list;
	if ((pool->next_list_idx    <= list_idx) ||
	    (pool->max_sorted_lists <= list_idx))
		return -1;

	list_desc = &pool->list_descs->descs[list_idx];

       /* Now search the sorted linked list - as described by list_desc -
	* until an entry is found whose user_data field matches the supplied
	* user_data or the end of the list is reached.
	*/
	list_item = list_desc->first_item;
	while (list_item) {
		if (list_item->user_data == user_data) {
			if (sort_key_ptr)
				*sort_key_ptr = list_item->sort_key;

			return 1;
		}

		list_item = list_item->next_item;
	}

	return 0;
}

int _odp_sorted_list_delete(_odp_int_sorted_pool_t sorted_pool,
			    _odp_int_sorted_list_t sorted_list,
			    uint64_t              user_data)
{
	sorted_list_desc_t *list_desc;
	sorted_list_item_t *next_list_item, *list_item, *prev_list_item;
	sorted_pool_t      *pool;
	uint32_t            list_idx;

	pool     = (sorted_pool_t *)(uintptr_t)sorted_pool;
	list_idx = (uint32_t)sorted_list;
	if ((pool->next_list_idx    <= list_idx) ||
	    (pool->max_sorted_lists <= list_idx))
		return -1;

	list_desc = &pool->list_descs->descs[list_idx];

       /* Now search the sorted linked list - as described by list_desc -
	* until an entry is found whose user_data field matches the supplied
	* user_data or the end of the list is reached.
	*/
	list_item      = list_desc->first_item;
	prev_list_item = NULL;
	while (list_item) {
		next_list_item = list_item->next_item;

		if (list_item->user_data == user_data) {
			if (!prev_list_item)
				list_desc->first_item = next_list_item;
			else
				prev_list_item->next_item = next_list_item;

			list_desc->sorted_list_len--;
			free(list_item);
			pool->total_deletes++;
			return 0;
		}

		prev_list_item = list_item;
		list_item      = next_list_item;
	}

	return -1;
}

int _odp_sorted_list_remove(_odp_int_sorted_pool_t sorted_pool,
			    _odp_int_sorted_list_t sorted_list,
			    uint64_t              *sort_key_ptr,
			    uint64_t              *user_data_ptr)
{
	sorted_list_desc_t *list_desc;
	sorted_list_item_t *list_item;
	sorted_pool_t      *pool;
	uint32_t            list_idx;

	pool     = (sorted_pool_t *)(uintptr_t)sorted_pool;
	list_idx = (uint32_t)sorted_list;
	if ((pool->next_list_idx    <= list_idx) ||
	    (pool->max_sorted_lists <= list_idx))
		return -1;

	list_desc = &pool->list_descs->descs[list_idx];
	if ((list_desc->sorted_list_len == 0) ||
	    (!list_desc->first_item))
		return -1;

	list_item             = list_desc->first_item;
	list_desc->first_item = list_item->next_item;
	list_desc->sorted_list_len--;

	if (sort_key_ptr)
		*sort_key_ptr = list_item->sort_key;

	if (user_data_ptr)
		*user_data_ptr = list_item->user_data;

	free(list_item);
	pool->total_removes++;
	return 1;
}

int _odp_sorted_list_destroy(_odp_int_sorted_pool_t sorted_pool,
			     _odp_int_sorted_list_t sorted_list)
{
	sorted_list_desc_t *list_desc;
	sorted_pool_t      *pool;
	uint32_t            list_idx;

	pool     = (sorted_pool_t *)(uintptr_t)sorted_pool;
	list_idx = (uint32_t)sorted_list;
	if ((pool->next_list_idx    <= list_idx) ||
	    (pool->max_sorted_lists <= list_idx))
		return -1;

	list_desc = &pool->list_descs->descs[list_idx];
	if (list_desc->sorted_list_len != 0)
		return -2;

	/* TBD Mark the list as free. */
	return 0;
}

void _odp_sorted_list_stats_print(_odp_int_sorted_pool_t sorted_pool)
{
	sorted_pool_t *pool;

	pool = (sorted_pool_t *)(uintptr_t)sorted_pool;
	_ODP_PRINT("  sorted_pool=0x%" PRIX64 "\n", sorted_pool);
	_ODP_PRINT("    max_sorted_lists=%u next_list_idx=%u\n",
		   pool->max_sorted_lists, pool->next_list_idx);
	_ODP_PRINT("    total_inserts=%" PRIu64 " total_deletes=%" PRIu64
		   " total_removes=%" PRIu64 "\n", pool->total_inserts,
		   pool->total_deletes, pool->total_removes);
}

void _odp_sorted_pool_destroy(_odp_int_sorted_pool_t sorted_pool)
{
	sorted_list_descs_t *list_descs;
	sorted_list_desc_t  *list_desc;
	sorted_list_item_t  *list_item, *next_list_item;
	sorted_pool_t       *pool;
	uint32_t             list_idx;

	pool       = (sorted_pool_t *)(uintptr_t)sorted_pool;
	list_descs = pool->list_descs;

	for (list_idx = 0; list_idx < pool->next_list_idx; list_idx++) {
		list_desc = &list_descs->descs[list_idx];
		list_item = list_desc->first_item;
		while (list_item) {
			next_list_item = list_item->next_item;
			free(list_item);
			list_item = next_list_item;
		}
	}

	free(list_descs);
	free(pool);
}