2015-03-18 00:31:48 +01:00
|
|
|
/** A generic linked list
|
|
|
|
*
|
|
|
|
* Linked lists a used for several data structures in the code.
|
|
|
|
*
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
2017-03-03 20:20:13 -04:00
|
|
|
* @copyright 2017, Institute for Automation of Complex Power Systems, EONERC
|
2017-04-27 12:56:43 +02:00
|
|
|
* @license GNU General Public License (version 3)
|
|
|
|
*
|
|
|
|
* VILLASnode
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* any later version.
|
2017-05-05 19:24:16 +00:00
|
|
|
*
|
2017-04-27 12:56:43 +02:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2017-05-05 19:24:16 +00:00
|
|
|
*
|
2017-04-27 12:56:43 +02:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2015-06-02 21:53:04 +02:00
|
|
|
*********************************************************************************/
|
2015-03-18 00:31:48 +01:00
|
|
|
|
2015-10-09 12:50:35 +02:00
|
|
|
#include <stdlib.h>
|
2015-09-19 15:26:30 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
2017-12-09 02:19:28 +08:00
|
|
|
#include <villas/list.h>
|
|
|
|
#include <villas/utils.h>
|
2015-03-18 00:31:48 +01:00
|
|
|
|
2016-01-14 22:52:08 +01:00
|
|
|
/* Compare functions */
|
|
|
|
static int cmp_lookup(const void *a, const void *b) {
|
|
|
|
const struct {
|
|
|
|
char *name;
|
|
|
|
} *obj = a;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2016-01-14 22:52:08 +01:00
|
|
|
return strcmp(obj->name, b);
|
|
|
|
}
|
|
|
|
|
2016-03-29 08:59:26 +02:00
|
|
|
static int cmp_contains(const void *a, const void *b) {
|
2016-01-14 22:52:08 +01:00
|
|
|
return a == b ? 0 : 1;
|
|
|
|
}
|
|
|
|
|
2016-03-29 08:59:26 +02:00
|
|
|
static int cmp_sort(const void *a, const void *b, void *thunk) {
|
2016-01-14 22:52:08 +01:00
|
|
|
cmp_cb_t cmp = (cmp_cb_t) thunk;
|
|
|
|
|
|
|
|
return cmp(*(void **) a, *(void **) b);
|
|
|
|
}
|
|
|
|
|
2017-08-28 14:34:47 +02:00
|
|
|
int list_init(struct list *l)
|
2015-03-18 00:31:48 +01:00
|
|
|
{
|
2017-03-12 17:04:43 -03:00
|
|
|
assert(l->state == STATE_DESTROYED);
|
|
|
|
|
2015-03-18 00:31:48 +01:00
|
|
|
pthread_mutex_init(&l->lock, NULL);
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-05-06 11:40:44 +02:00
|
|
|
l->length = 0;
|
2015-10-09 12:50:35 +02:00
|
|
|
l->capacity = 0;
|
2015-12-04 01:49:00 +01:00
|
|
|
l->array = NULL;
|
2017-03-12 17:04:43 -03:00
|
|
|
l->state = STATE_INITIALIZED;
|
2017-10-18 15:39:53 +02:00
|
|
|
|
2017-08-28 14:34:47 +02:00
|
|
|
return 0;
|
2015-03-18 00:31:48 +01:00
|
|
|
}
|
|
|
|
|
2017-02-18 10:43:01 -05:00
|
|
|
int list_destroy(struct list *l, dtor_cb_t destructor, bool release)
|
2015-03-18 00:31:48 +01:00
|
|
|
{
|
|
|
|
pthread_mutex_lock(&l->lock);
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2017-03-12 17:04:43 -03:00
|
|
|
assert(l->state != STATE_DESTROYED);
|
|
|
|
|
2017-03-25 21:23:31 +01:00
|
|
|
for (size_t i = 0; i < list_length(l); i++) {
|
2018-03-26 12:51:43 +02:00
|
|
|
void *e = list_at(l, i);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2016-04-16 19:54:26 +02:00
|
|
|
if (destructor)
|
2018-03-26 12:51:43 +02:00
|
|
|
destructor(e);
|
2016-04-16 19:54:26 +02:00
|
|
|
if (release)
|
2018-03-26 12:51:43 +02:00
|
|
|
free(e);
|
2015-03-18 00:31:48 +01:00
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2015-12-04 01:49:00 +01:00
|
|
|
free(l->array);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2016-09-30 19:55:39 -04:00
|
|
|
l->length = -1;
|
2015-10-09 12:50:35 +02:00
|
|
|
l->capacity = 0;
|
2018-03-26 12:51:43 +02:00
|
|
|
l->array = NULL;
|
|
|
|
l->state = STATE_DESTROYED;
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-03-21 15:20:22 +01:00
|
|
|
pthread_mutex_unlock(&l->lock);
|
2015-03-18 00:31:48 +01:00
|
|
|
pthread_mutex_destroy(&l->lock);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-02-18 10:43:01 -05:00
|
|
|
return 0;
|
2015-03-18 00:31:48 +01:00
|
|
|
}
|
|
|
|
|
2015-03-18 15:47:49 +01:00
|
|
|
void list_push(struct list *l, void *p)
|
2015-03-18 00:31:48 +01:00
|
|
|
{
|
|
|
|
pthread_mutex_lock(&l->lock);
|
2017-03-12 17:04:43 -03:00
|
|
|
|
|
|
|
assert(l->state == STATE_INITIALIZED);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2015-10-09 12:50:35 +02:00
|
|
|
/* Resize array if out of capacity */
|
2015-12-04 01:49:00 +01:00
|
|
|
if (l->length >= l->capacity) {
|
2015-10-09 12:50:35 +02:00
|
|
|
l->capacity += LIST_CHUNKSIZE;
|
2015-12-04 01:49:00 +01:00
|
|
|
l->array = realloc(l->array, l->capacity * sizeof(void *));
|
2015-10-09 12:50:35 +02:00
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2015-12-04 01:49:00 +01:00
|
|
|
l->array[l->length] = p;
|
2015-05-06 11:40:44 +02:00
|
|
|
l->length++;
|
2015-03-18 15:47:49 +01:00
|
|
|
|
2015-03-18 00:31:48 +01:00
|
|
|
pthread_mutex_unlock(&l->lock);
|
|
|
|
}
|
|
|
|
|
2015-12-04 01:49:00 +01:00
|
|
|
void list_remove(struct list *l, void *p)
|
2015-06-10 14:56:55 +02:00
|
|
|
{
|
2015-12-04 01:49:00 +01:00
|
|
|
int removed = 0;
|
|
|
|
|
|
|
|
pthread_mutex_lock(&l->lock);
|
|
|
|
|
2017-03-12 17:04:43 -03:00
|
|
|
assert(l->state == STATE_INITIALIZED);
|
|
|
|
|
2017-03-25 21:23:31 +01:00
|
|
|
for (size_t i = 0; i < list_length(l); i++) {
|
2018-03-26 12:51:43 +02:00
|
|
|
if (list_at(l, i) == p)
|
2015-12-04 01:49:00 +01:00
|
|
|
removed++;
|
|
|
|
else
|
2018-03-26 12:51:43 +02:00
|
|
|
l->array[i - removed] = list_at(l, i);
|
2015-12-04 01:49:00 +01:00
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2015-12-04 01:49:00 +01:00
|
|
|
l->length -= removed;
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&l->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
void * list_lookup(struct list *l, const char *name)
|
|
|
|
{
|
2016-01-14 22:52:08 +01:00
|
|
|
return list_search(l, cmp_lookup, (void *) name);
|
2015-12-04 01:54:33 +01:00
|
|
|
}
|
|
|
|
|
2018-05-24 09:05:00 +02:00
|
|
|
ssize_t list_lookup_index(struct list *l, const char *name)
|
|
|
|
{
|
|
|
|
void *ptr = list_lookup(l, name);
|
|
|
|
if (!ptr)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return list_index(l, ptr);
|
|
|
|
}
|
|
|
|
|
2015-12-04 01:54:33 +01:00
|
|
|
int list_contains(struct list *l, void *p)
|
2017-05-05 19:24:16 +00:00
|
|
|
{
|
2016-01-14 22:52:08 +01:00
|
|
|
return list_count(l, cmp_contains, p);
|
2015-06-10 14:56:55 +02:00
|
|
|
}
|
|
|
|
|
2015-11-23 16:32:24 +01:00
|
|
|
int list_count(struct list *l, cmp_cb_t cmp, void *ctx)
|
|
|
|
{
|
|
|
|
int c = 0;
|
2018-03-26 12:51:43 +02:00
|
|
|
void *e;
|
2015-11-23 16:32:24 +01:00
|
|
|
|
|
|
|
pthread_mutex_lock(&l->lock);
|
2015-12-04 01:54:33 +01:00
|
|
|
|
2017-03-12 17:04:43 -03:00
|
|
|
assert(l->state == STATE_INITIALIZED);
|
|
|
|
|
2017-03-25 21:23:31 +01:00
|
|
|
for (size_t i = 0; i < list_length(l); i++) {
|
2018-03-26 12:51:43 +02:00
|
|
|
e = list_at(l, i);
|
2015-12-11 18:19:17 +01:00
|
|
|
if (cmp(e, ctx) == 0)
|
2015-11-23 16:32:24 +01:00
|
|
|
c++;
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&l->lock);
|
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2015-10-09 12:50:35 +02:00
|
|
|
void * list_search(struct list *l, cmp_cb_t cmp, void *ctx)
|
2015-09-19 15:26:30 +02:00
|
|
|
{
|
2016-01-14 22:52:08 +01:00
|
|
|
void *e;
|
|
|
|
|
2015-10-09 12:50:35 +02:00
|
|
|
pthread_mutex_lock(&l->lock);
|
2017-03-12 17:04:43 -03:00
|
|
|
|
|
|
|
assert(l->state == STATE_INITIALIZED);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-25 21:23:31 +01:00
|
|
|
for (size_t i = 0; i < list_length(l); i++) {
|
|
|
|
e = list_at(l, i);
|
|
|
|
if (cmp(e, ctx) == 0)
|
2015-10-09 12:50:35 +02:00
|
|
|
goto out;
|
2015-09-19 15:26:30 +02:00
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2015-10-09 12:50:35 +02:00
|
|
|
e = NULL; /* not found */
|
|
|
|
|
|
|
|
out: pthread_mutex_unlock(&l->lock);
|
2015-09-19 15:26:30 +02:00
|
|
|
|
2015-10-09 12:50:35 +02:00
|
|
|
return e;
|
2015-09-19 15:26:30 +02:00
|
|
|
}
|
|
|
|
|
2015-10-09 12:50:35 +02:00
|
|
|
void list_sort(struct list *l, cmp_cb_t cmp)
|
2015-08-07 01:11:43 +02:00
|
|
|
{
|
2015-10-09 12:50:35 +02:00
|
|
|
pthread_mutex_lock(&l->lock);
|
2015-12-04 01:49:00 +01:00
|
|
|
|
2017-03-12 17:04:43 -03:00
|
|
|
assert(l->state == STATE_INITIALIZED);
|
|
|
|
|
2016-01-14 22:52:08 +01:00
|
|
|
qsort_r(l->array, l->length, sizeof(void *), cmp_sort, (void *) cmp);
|
2015-12-04 01:49:00 +01:00
|
|
|
|
2015-10-09 12:50:35 +02:00
|
|
|
pthread_mutex_unlock(&l->lock);
|
2017-05-23 09:56:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int list_set(struct list *l, int index, void *value)
|
|
|
|
{
|
|
|
|
if (index >= l->length)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
l->array[index] = value;
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2017-05-23 09:56:47 +02:00
|
|
|
return 0;
|
2017-07-24 19:33:35 +02:00
|
|
|
}
|
2018-03-26 12:52:04 +02:00
|
|
|
|
|
|
|
ssize_t list_index(struct list *l, void *p)
|
|
|
|
{
|
|
|
|
void *e;
|
|
|
|
ssize_t f;
|
|
|
|
|
|
|
|
pthread_mutex_lock(&l->lock);
|
|
|
|
|
|
|
|
assert(l->state == STATE_INITIALIZED);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < list_length(l); i++) {
|
|
|
|
e = list_at(l, i);
|
|
|
|
if (e == p) {
|
|
|
|
f = i;
|
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
f = -1;
|
|
|
|
|
|
|
|
found: pthread_mutex_unlock(&l->lock);
|
|
|
|
|
|
|
|
return f;
|
|
|
|
}
|