1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00

refactor: list_* -> vlist_*

This commit is contained in:
Steffen Vogel 2019-01-07 10:27:59 +01:00
parent b39daa5c59
commit d66ae1a893
6 changed files with 104 additions and 104 deletions

View file

@ -59,7 +59,7 @@ struct pci_region {
};
struct pci {
struct list devices; /**< List of available PCI devices in the system (struct pci_device) */
struct vlist devices; /**< List of available PCI devices in the system (struct pci_device) */
};
/** Initialize Linux PCI handle.

View file

@ -44,24 +44,24 @@ extern "C" {
#define LIST_INIT_STATIC(l) \
__attribute__((constructor(105))) static void UNIQUE(__ctor)() {\
if ((l)->state == STATE_DESTROYED) \
list_init(l); \
vlist_init(l); \
} \
__attribute__((destructor(105))) static void UNIQUE(__dtor)() { \
list_destroy(l, NULL, false); \
vlist_destroy(l, NULL, false); \
}
#define list_length(list) ((list)->length)
#define list_at_safe(list, index) ((list)->length > index ? (list)->array[index] : NULL)
#define list_at(list, index) ((list)->array[index])
#define vlist_length(list) ((list)->length)
#define vlist_at_safe(list, index) ((list)->length > index ? (list)->array[index] : NULL)
#define vlist_at(list, index) ((list)->array[index])
#define list_first(list) list_at(list, 0)
#define list_last(list) list_at(list, (list)->length-1)
#define vlist_first(list) vlist_at(list, 0)
#define vlist_last(list) vlist_at(list, (list)->length-1)
/** Callback to search or sort a list. */
typedef int (*cmp_cb_t)(const void *, const void *);
/* The list data structure. */
struct list {
struct vlist {
void **array; /**< Array of pointers to list elements */
size_t capacity; /**< Size of list::array in elements */
size_t length; /**< Number of elements of list::array which are in use */
@ -73,21 +73,21 @@ struct list {
*
* @param l A pointer to the list data structure.
*/
int list_init(struct list *l);
int vlist_init(struct vlist *l);
/** Destroy a list and call destructors for all list elements
*
* @param free free() all list members during when calling list_destroy()
* @param free free() all list members during when calling vlist_destroy()
* @param dtor A function pointer to a desctructor which will be called for every list item when the list is destroyed.
* @param l A pointer to the list data structure.
*/
int list_destroy(struct list *l, dtor_cb_t dtor, bool free);
int vlist_destroy(struct vlist *l, dtor_cb_t dtor, bool free);
/** Append an element to the end of the list */
void list_push(struct list *l, void *p);
void vlist_push(struct vlist *l, void *p);
/** Remove all occurences of a list item */
void list_remove(struct list *l, void *p);
void vlist_remove(struct vlist *l, void *p);
/** Return the first list element which is identified by a string in its first member variable.
*
@ -100,34 +100,34 @@ void list_remove(struct list *l, void *p);
*
* @see Only possible because of §1424 of http://c0x.coding-guidelines.com/6.7.2.1.html
*/
void * list_lookup(struct list *l, const char *name);
void * vlist_lookup(struct vlist *l, const char *name);
ssize_t list_lookup_index(struct list *l, const char *name);
ssize_t vlist_lookup_index(struct vlist *l, const char *name);
/** Return the first element of the list for which cmp returns zero */
void * list_search(struct list *l, cmp_cb_t cmp, void *ctx);
void * vlist_search(struct vlist *l, cmp_cb_t cmp, void *ctx);
/** Returns the number of occurences for which cmp returns zero when called on all list elements. */
int list_count(struct list *l, cmp_cb_t cmp, void *ctx);
int vlist_count(struct vlist *l, cmp_cb_t cmp, void *ctx);
/** Return 0 if list contains pointer p */
int list_contains(struct list *l, void *p);
int vlist_contains(struct vlist *l, void *p);
/** Sort the list using the quicksort algorithm of libc */
void list_sort(struct list *l, cmp_cb_t cmp);
void vlist_sort(struct vlist *l, cmp_cb_t cmp);
/** Set single element in list */
int list_set(struct list *l, int index, void *value);
int vlist_set(struct vlist *l, int index, void *value);
/** Return index in list for value.
*
* @retval <0 No list entry matching \p value was found.
* @retval >=0 Entry \p value was found at returned index.
*/
ssize_t list_index(struct list *l, void *value);
ssize_t vlist_index(struct vlist *l, void *value);
/** Extend the list to the given length by filling new slots with given value. */
void list_extend(struct list *l, size_t len, void *val);
void vlist_extend(struct vlist *l, size_t len, void *val);
#ifdef __cplusplus
}

View file

@ -39,7 +39,7 @@ int pci_init(struct pci *p)
char path[PATH_MAX];
int ret;
list_init(&p->devices);
vlist_init(&p->devices);
snprintf(path, sizeof(path), "%s/bus/pci/devices", SYSFS_PATH);
@ -83,7 +83,7 @@ int pci_init(struct pci *p)
if (ret != 4)
error("Failed to parse PCI slot number: %s", e->d_name);
list_push(&p->devices, d);
vlist_push(&p->devices, d);
}
closedir(dp);
@ -93,7 +93,7 @@ int pci_init(struct pci *p)
int pci_destroy(struct pci *p)
{
list_destroy(&p->devices, NULL, true);
vlist_destroy(&p->devices, NULL, true);
return 0;
}
@ -250,7 +250,7 @@ int pci_device_compare(const struct pci_device *d, const struct pci_device *f)
struct pci_device * pci_lookup_device(struct pci *p, struct pci_device *f)
{
return list_search(&p->devices, (cmp_cb_t) pci_device_compare, (void *) f);
return vlist_search(&p->devices, (cmp_cb_t) pci_device_compare, (void *) f);
}
size_t pci_get_regions(const struct pci_device *d, struct pci_region** regions)

View file

@ -71,7 +71,7 @@ int vfio_destroy(struct vfio_container *v)
int ret;
/* Release memory and close fds */
list_destroy(&v->groups, (dtor_cb_t) vfio_group_destroy, true);
vlist_destroy(&v->groups, (dtor_cb_t) vfio_group_destroy, true);
/* Close container */
ret = close(v->fd);
@ -87,7 +87,7 @@ int vfio_group_destroy(struct vfio_group *g)
{
int ret;
list_destroy(&g->devices, (dtor_cb_t) vfio_device_destroy, false);
vlist_destroy(&g->devices, (dtor_cb_t) vfio_device_destroy, false);
ret = ioctl(g->fd, VFIO_GROUP_UNSET_CONTAINER);
if (ret)
@ -131,7 +131,7 @@ int vfio_init(struct vfio_container *v)
/* Initialize datastructures */
memset(v, 0, sizeof(*v));
list_init(&v->groups);
vlist_init(&v->groups);
/* Load VFIO kernel module */
if (kernel_module_load("vfio"))
@ -168,7 +168,7 @@ int vfio_group_attach(struct vfio_group *g, struct vfio_container *c, int index)
g->index = index;
g->container = c;
list_init(&g->devices);
vlist_init(&g->devices);
/* Open group fd */
snprintf(buf, sizeof(buf), VFIO_DEV("%u"), g->index);
@ -196,7 +196,7 @@ int vfio_group_attach(struct vfio_group *g, struct vfio_container *c, int index)
if (!(g->status.flags & VFIO_GROUP_FLAGS_VIABLE))
error("VFIO group is not available: bind all devices to the VFIO driver!");
list_push(&c->groups, g);
vlist_push(&c->groups, g);
return 0;
}
@ -244,8 +244,8 @@ int vfio_device_attach(struct vfio_device *d, struct vfio_container *c, const ch
struct vfio_group *g = NULL;
/* Check if group already exists */
for (size_t i = 0; i < list_length(&c->groups); i++) {
struct vfio_group *h = (struct vfio_group *) list_at(&c->groups, i);
for (size_t i = 0; i < vlist_length(&c->groups); i++) {
struct vfio_group *h = (struct vfio_group *) vlist_at(&c->groups, i);
if (h->index == index)
g = h;
@ -305,7 +305,7 @@ int vfio_device_attach(struct vfio_device *d, struct vfio_container *c, const ch
serror("Failed to get IRQs of VFIO device: %s", d->name);
}
list_push(&d->group->devices, d);
vlist_push(&d->group->devices, d);
return 0;
}
@ -496,8 +496,8 @@ void vfio_dump(struct vfio_container *v)
info("VFIO Version: %u", v->version);
info("VFIO Extensions: %#x", v->extensions);
for (size_t i = 0; i < list_length(&v->groups); i++) {
struct vfio_group *g = (struct vfio_group *) list_at(&v->groups, i);
for (size_t i = 0; i < vlist_length(&v->groups); i++) {
struct vfio_group *g = (struct vfio_group *) vlist_at(&v->groups, i);
info("VFIO Group %u, viable=%u, container=%d", g->index,
(g->status.flags & VFIO_GROUP_FLAGS_VIABLE) > 0,
@ -505,8 +505,8 @@ void vfio_dump(struct vfio_container *v)
);
for (size_t i = 0; i < list_length(&g->devices); i++) {
struct vfio_device *d = (struct vfio_device *) list_at(&g->devices, i);
for (size_t i = 0; i < vlist_length(&g->devices); i++) {
struct vfio_device *d = (struct vfio_device *) vlist_at(&g->devices, i);
info("Device %s: regions=%u, irqs=%u, flags=%#x", d->name,
d->info.num_regions,

View file

@ -51,7 +51,7 @@ static int cmp_sort(const void *a, const void *b, void *thunk) {
return cmp(*(const void **) a, *(const void **) b);
}
int list_init(struct list *l)
int vlist_init(struct vlist *l)
{
assert(l->state == STATE_DESTROYED);
@ -65,14 +65,14 @@ int list_init(struct list *l)
return 0;
}
int list_destroy(struct list *l, dtor_cb_t destructor, bool release)
int vlist_destroy(struct vlist *l, dtor_cb_t destructor, bool release)
{
pthread_mutex_lock(&l->lock);
assert(l->state != STATE_DESTROYED);
for (size_t i = 0; i < list_length(l); i++) {
void *e = list_at(l, i);
for (size_t i = 0; i < vlist_length(l); i++) {
void *e = vlist_at(l, i);
if (destructor)
destructor(e);
@ -93,7 +93,7 @@ int list_destroy(struct list *l, dtor_cb_t destructor, bool release)
return 0;
}
void list_push(struct list *l, void *p)
void vlist_push(struct vlist *l, void *p)
{
pthread_mutex_lock(&l->lock);
@ -111,7 +111,7 @@ void list_push(struct list *l, void *p)
pthread_mutex_unlock(&l->lock);
}
void list_remove(struct list *l, void *p)
void vlist_remove(struct vlist *l, void *p)
{
int removed = 0;
@ -119,11 +119,11 @@ void list_remove(struct list *l, void *p)
assert(l->state == STATE_INITIALIZED);
for (size_t i = 0; i < list_length(l); i++) {
if (list_at(l, i) == p)
for (size_t i = 0; i < vlist_length(l); i++) {
if (vlist_at(l, i) == p)
removed++;
else
l->array[i - removed] = list_at(l, i);
l->array[i - removed] = vlist_at(l, i);
}
l->length -= removed;
@ -131,26 +131,26 @@ void list_remove(struct list *l, void *p)
pthread_mutex_unlock(&l->lock);
}
void * list_lookup(struct list *l, const char *name)
void * vlist_lookup(struct vlist *l, const char *name)
{
return list_search(l, cmp_lookup, (void *) name);
return vlist_search(l, cmp_lookup, (void *) name);
}
ssize_t list_lookup_index(struct list *l, const char *name)
ssize_t vlist_lookup_index(struct vlist *l, const char *name)
{
void *ptr = list_lookup(l, name);
void *ptr = vlist_lookup(l, name);
if (!ptr)
return -1;
return list_index(l, ptr);
return vlist_index(l, ptr);
}
int list_contains(struct list *l, void *p)
int vlist_contains(struct vlist *l, void *p)
{
return list_count(l, cmp_contains, p);
return vlist_count(l, cmp_contains, p);
}
int list_count(struct list *l, cmp_cb_t cmp, void *ctx)
int vlist_count(struct vlist *l, cmp_cb_t cmp, void *ctx)
{
int c = 0;
void *e;
@ -159,8 +159,8 @@ int list_count(struct list *l, cmp_cb_t cmp, void *ctx)
assert(l->state == STATE_INITIALIZED);
for (size_t i = 0; i < list_length(l); i++) {
e = list_at(l, i);
for (size_t i = 0; i < vlist_length(l); i++) {
e = vlist_at(l, i);
if (cmp(e, ctx) == 0)
c++;
}
@ -170,7 +170,7 @@ int list_count(struct list *l, cmp_cb_t cmp, void *ctx)
return c;
}
void * list_search(struct list *l, cmp_cb_t cmp, void *ctx)
void * vlist_search(struct vlist *l, cmp_cb_t cmp, void *ctx)
{
void *e;
@ -178,8 +178,8 @@ void * list_search(struct list *l, cmp_cb_t cmp, void *ctx)
assert(l->state == STATE_INITIALIZED);
for (size_t i = 0; i < list_length(l); i++) {
e = list_at(l, i);
for (size_t i = 0; i < vlist_length(l); i++) {
e = vlist_at(l, i);
if (cmp(e, ctx) == 0)
goto out;
}
@ -191,7 +191,7 @@ out: pthread_mutex_unlock(&l->lock);
return e;
}
void list_sort(struct list *l, cmp_cb_t cmp)
void vlist_sort(struct vlist *l, cmp_cb_t cmp)
{
pthread_mutex_lock(&l->lock);
@ -206,7 +206,7 @@ void list_sort(struct list *l, cmp_cb_t cmp)
pthread_mutex_unlock(&l->lock);
}
int list_set(struct list *l, int index, void *value)
int vlist_set(struct vlist *l, int index, void *value)
{
if (index >= l->length)
return -1;
@ -216,7 +216,7 @@ int list_set(struct list *l, int index, void *value)
return 0;
}
ssize_t list_index(struct list *l, void *p)
ssize_t vlist_index(struct vlist *l, void *p)
{
void *e;
ssize_t f;
@ -225,8 +225,8 @@ ssize_t list_index(struct list *l, void *p)
assert(l->state == STATE_INITIALIZED);
for (size_t i = 0; i < list_length(l); i++) {
e = list_at(l, i);
for (size_t i = 0; i < vlist_length(l); i++) {
e = vlist_at(l, i);
if (e == p) {
f = i;
goto found;
@ -240,8 +240,8 @@ found: pthread_mutex_unlock(&l->lock);
return f;
}
void list_extend(struct list *l, size_t len, void *val)
void vlist_extend(struct vlist *l, size_t len, void *val)
{
while (list_length(l) < len)
list_push(l, val);
while (vlist_length(l) < len)
vlist_push(l, val);
}

View file

@ -36,12 +36,12 @@ struct data {
TestSuite(list, .description = "List datastructure");
Test(list, list_lookup)
Test(list, vlist_lookup)
{
struct list l;
struct vlist l;
l.state = STATE_DESTROYED;
list_init(&l);
vlist_init(&l);
for (unsigned i = 0; i < ARRAY_LEN(nouns); i++) {
struct data *d = new struct data;
@ -49,42 +49,42 @@ Test(list, list_lookup)
d->tag = nouns[i];
d->data = i;
list_push(&l, d);
vlist_push(&l, d);
}
struct data *found = (struct data *) list_lookup(&l, "woman");
struct data *found = (struct data *) vlist_lookup(&l, "woman");
cr_assert_eq(found->data, 13);
list_destroy(&l, nullptr, true);
vlist_destroy(&l, nullptr, true);
}
Test(list, list_search)
Test(list, vlist_search)
{
struct list l;
struct vlist l;
l.state = STATE_DESTROYED;
list_init(&l);
vlist_init(&l);
/* Fill list */
for (unsigned i = 0; i < ARRAY_LEN(nouns); i++)
list_push(&l, (void *) nouns[i]);
vlist_push(&l, (void *) nouns[i]);
cr_assert_eq(list_length(&l), ARRAY_LEN(nouns));
cr_assert_eq(vlist_length(&l), ARRAY_LEN(nouns));
/* Declare on stack! */
char positive[] = "woman";
char negative[] = "dinosaurrier";
char *found = (char *) list_search(&l, (cmp_cb_t) strcmp, positive);
char *found = (char *) vlist_search(&l, (cmp_cb_t) strcmp, positive);
cr_assert_not_null(found);
cr_assert_eq(found, nouns[13], "found = %p, nouns[13] = %p", found, nouns[13]);
cr_assert_str_eq(found, positive);
char *not_found = (char *) list_search(&l, (cmp_cb_t) strcmp, negative);
char *not_found = (char *) vlist_search(&l, (cmp_cb_t) strcmp, negative);
cr_assert_null(not_found);
list_destroy(&l, nullptr, false);
vlist_destroy(&l, nullptr, false);
}
struct content {
@ -102,18 +102,18 @@ static int dtor(void *ptr)
Test(list, destructor)
{
struct list l;
struct vlist l;
l.state = STATE_DESTROYED;
struct content elm;
elm.destroyed = 0;
list_init(&l);
list_push(&l, &elm);
vlist_init(&l);
vlist_push(&l, &elm);
cr_assert_eq(list_length(&l), 1);
cr_assert_eq(vlist_length(&l), 1);
list_destroy(&l, dtor, false);
vlist_destroy(&l, dtor, false);
cr_assert_eq(elm.destroyed, 1);
}
@ -122,47 +122,47 @@ Test(list, basics)
{
uintptr_t i;
int ret;
struct list l;
struct vlist l;
l.state = STATE_DESTROYED;
list_init(&l);
vlist_init(&l);
for (i = 0; i < 100; i++) {
cr_assert_eq(list_length(&l), i);
cr_assert_eq(vlist_length(&l), i);
list_push(&l, (void *) i);
vlist_push(&l, (void *) i);
}
cr_assert_eq(list_at_safe(&l, 555), nullptr);
cr_assert_eq(list_last(&l), (void *) 99);
cr_assert_eq(list_first(&l), (void *) 0);
cr_assert_eq(vlist_at_safe(&l, 555), nullptr);
cr_assert_eq(vlist_last(&l), (void *) 99);
cr_assert_eq(vlist_first(&l), (void *) 0);
for (size_t j = 0, i = 0; j < list_length(&l); j++) {
void *k = list_at(&l, j);
for (size_t j = 0, i = 0; j < vlist_length(&l); j++) {
void *k = vlist_at(&l, j);
cr_assert_eq(k, (void *) i++);
}
list_sort(&l, (cmp_cb_t) [](const void *a, const void *b) -> int {
vlist_sort(&l, (cmp_cb_t) [](const void *a, const void *b) -> int {
return (intptr_t) b - (intptr_t) a;
});
for (size_t j = 0, i = 99; j <= 99; j++, i--) {
uintptr_t k = (uintptr_t) list_at(&l, j);
uintptr_t k = (uintptr_t) vlist_at(&l, j);
cr_assert_eq(k, i, "Is %zu, expected %zu", k, i);
}
ret = list_contains(&l, (void *) 55);
ret = vlist_contains(&l, (void *) 55);
cr_assert(ret);
list_remove(&l, (void *) 55);
vlist_remove(&l, (void *) 55);
ret = list_contains(&l, (void *) 55);
ret = vlist_contains(&l, (void *) 55);
cr_assert(!ret);
ret = list_destroy(&l, nullptr, false);
ret = vlist_destroy(&l, nullptr, false);
cr_assert(!ret);
ret = list_length(&l);
ret = vlist_length(&l);
cr_assert_eq(ret, -1, "List not properly destroyed: l.length = %zd", l.length);
}