2016-09-26 09:37:39 -04:00
|
|
|
|
2019-06-30 04:39:00 -04:00
|
|
|
#include "bt_common.h"
|
2018-04-08 00:10:50 -04:00
|
|
|
#include "osi/allocator.h"
|
|
|
|
#include "osi/list.h"
|
|
|
|
#include "osi/osi.h"
|
2016-09-26 09:37:39 -04:00
|
|
|
|
|
|
|
struct list_node_t {
|
2016-11-24 13:10:15 -05:00
|
|
|
struct list_node_t *next;
|
|
|
|
void *data;
|
2016-09-26 09:37:39 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct list_t {
|
2016-11-24 13:10:15 -05:00
|
|
|
list_node_t *head;
|
|
|
|
list_node_t *tail;
|
|
|
|
size_t length;
|
|
|
|
list_free_cb free_cb;
|
2016-09-26 09:37:39 -04:00
|
|
|
} list_t;
|
|
|
|
|
|
|
|
//static list_node_t *list_free_node_(list_t *list, list_node_t *node);
|
|
|
|
|
|
|
|
// Hidden constructor, only to be used by the hash map for the allocation tracker.
|
|
|
|
// Behaves the same as |list_new|, except you get to specify the allocator.
|
2018-08-14 22:40:51 -04:00
|
|
|
list_t *list_new_internal(list_free_cb callback)
|
2016-11-24 13:10:15 -05:00
|
|
|
{
|
2018-08-14 22:40:51 -04:00
|
|
|
list_t *list = (list_t *) osi_calloc(sizeof(list_t));
|
2016-11-24 13:10:15 -05:00
|
|
|
if (!list) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
list->head = list->tail = NULL;
|
|
|
|
list->length = 0;
|
|
|
|
list->free_cb = callback;
|
|
|
|
return list;
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
list_t *list_new(list_free_cb callback)
|
|
|
|
{
|
2018-08-14 22:40:51 -04:00
|
|
|
return list_new_internal(callback);
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
void list_free(list_t *list)
|
|
|
|
{
|
|
|
|
if (!list) {
|
|
|
|
return;
|
|
|
|
}
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
list_clear(list);
|
2018-08-14 22:40:51 -04:00
|
|
|
osi_free(list);
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
bool list_is_empty(const list_t *list)
|
|
|
|
{
|
|
|
|
assert(list != NULL);
|
|
|
|
return (list->length == 0);
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
2017-08-17 09:13:45 -04:00
|
|
|
bool list_contains(const list_t *list, const void *data)
|
|
|
|
{
|
2016-09-26 09:37:39 -04:00
|
|
|
assert(list != NULL);
|
|
|
|
assert(data != NULL);
|
|
|
|
|
2017-08-17 09:13:45 -04:00
|
|
|
for (const list_node_t *node = list_begin(list); node != list_end(list); node = list_next(node)) {
|
2019-03-26 02:37:37 -04:00
|
|
|
if (list_node(node) == data) {
|
2016-09-26 09:37:39 -04:00
|
|
|
return true;
|
2019-03-26 02:37:37 -04:00
|
|
|
}
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
size_t list_length(const list_t *list)
|
|
|
|
{
|
|
|
|
assert(list != NULL);
|
|
|
|
return list->length;
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
void *list_front(const list_t *list)
|
|
|
|
{
|
|
|
|
assert(list != NULL);
|
|
|
|
assert(!list_is_empty(list));
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
return list->head->data;
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void *list_back(const list_t *list) {
|
|
|
|
assert(list != NULL);
|
|
|
|
assert(!list_is_empty(list));
|
|
|
|
|
|
|
|
return list->tail->data;
|
|
|
|
}
|
|
|
|
|
2017-08-17 09:13:45 -04:00
|
|
|
list_node_t *list_back_node(const list_t *list) {
|
|
|
|
assert(list != NULL);
|
|
|
|
assert(!list_is_empty(list));
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2017-08-17 09:13:45 -04:00
|
|
|
return list->tail;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool list_insert_after(list_t *list, list_node_t *prev_node, void *data) {
|
2018-09-18 02:49:08 -04:00
|
|
|
assert(list != NULL);
|
|
|
|
assert(prev_node != NULL);
|
|
|
|
assert(data != NULL);
|
|
|
|
list_node_t *node = (list_node_t *)osi_calloc(sizeof(list_node_t));
|
|
|
|
if (!node) {
|
2019-09-18 05:43:35 -04:00
|
|
|
OSI_TRACE_ERROR("%s osi_calloc failed.\n", __FUNCTION__ );
|
2018-09-18 02:49:08 -04:00
|
|
|
return false;
|
|
|
|
}
|
2016-11-24 13:10:15 -05:00
|
|
|
node->next = prev_node->next;
|
|
|
|
node->data = data;
|
|
|
|
prev_node->next = node;
|
|
|
|
if (list->tail == prev_node) {
|
|
|
|
list->tail = node;
|
|
|
|
}
|
|
|
|
++list->length;
|
|
|
|
return true;
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
bool list_prepend(list_t *list, void *data)
|
|
|
|
{
|
|
|
|
assert(list != NULL);
|
|
|
|
assert(data != NULL);
|
2018-08-14 22:40:51 -04:00
|
|
|
list_node_t *node = (list_node_t *)osi_calloc(sizeof(list_node_t));
|
2016-11-24 13:10:15 -05:00
|
|
|
if (!node) {
|
2019-09-18 05:43:35 -04:00
|
|
|
OSI_TRACE_ERROR("%s osi_calloc failed.\n", __FUNCTION__ );
|
2016-11-24 13:10:15 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
node->next = list->head;
|
|
|
|
node->data = data;
|
|
|
|
list->head = node;
|
|
|
|
if (list->tail == NULL) {
|
|
|
|
list->tail = list->head;
|
|
|
|
}
|
|
|
|
++list->length;
|
|
|
|
return true;
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
bool list_append(list_t *list, void *data)
|
|
|
|
{
|
|
|
|
assert(list != NULL);
|
|
|
|
assert(data != NULL);
|
2018-08-14 22:40:51 -04:00
|
|
|
list_node_t *node = (list_node_t *)osi_calloc(sizeof(list_node_t));
|
2016-11-24 13:10:15 -05:00
|
|
|
if (!node) {
|
2019-09-18 05:43:35 -04:00
|
|
|
OSI_TRACE_ERROR("%s osi_calloc failed.\n", __FUNCTION__ );
|
2016-11-24 13:10:15 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
node->next = NULL;
|
|
|
|
node->data = data;
|
|
|
|
if (list->tail == NULL) {
|
|
|
|
list->head = node;
|
|
|
|
list->tail = node;
|
|
|
|
} else {
|
|
|
|
list->tail->next = node;
|
|
|
|
list->tail = node;
|
|
|
|
}
|
|
|
|
++list->length;
|
|
|
|
return true;
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
bool list_remove(list_t *list, void *data)
|
|
|
|
{
|
|
|
|
assert(list != NULL);
|
|
|
|
assert(data != NULL);
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
if (list_is_empty(list)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
if (list->head->data == data) {
|
|
|
|
list_node_t *next = list_free_node(list, list->head);
|
|
|
|
if (list->tail == list->head) {
|
|
|
|
list->tail = next;
|
|
|
|
}
|
|
|
|
list->head = next;
|
|
|
|
return true;
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
for (list_node_t *prev = list->head, *node = list->head->next; node; prev = node, node = node->next)
|
|
|
|
if (node->data == data) {
|
|
|
|
prev->next = list_free_node(list, node);
|
|
|
|
if (list->tail == node) {
|
|
|
|
list->tail = prev;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
2020-08-20 01:19:22 -04:00
|
|
|
bool list_delete(list_t *list, void *data)
|
|
|
|
{
|
|
|
|
assert(list != NULL);
|
|
|
|
assert(data != NULL);
|
|
|
|
|
|
|
|
if (list_is_empty(list)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (list->head->data == data) {
|
|
|
|
list_node_t *next = list_delete_node(list, list->head);
|
|
|
|
if (list->tail == list->head) {
|
|
|
|
list->tail = next;
|
|
|
|
}
|
|
|
|
list->head = next;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (list_node_t *prev = list->head, *node = list->head->next; node; prev = node, node = node->next)
|
|
|
|
if (node->data == data) {
|
|
|
|
prev->next = list_delete_node(list, node);
|
|
|
|
if (list->tail == node) {
|
|
|
|
list->tail = prev;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
void list_clear(list_t *list)
|
|
|
|
{
|
|
|
|
assert(list != NULL);
|
|
|
|
for (list_node_t *node = list->head; node; ) {
|
|
|
|
node = list_free_node(list, node);
|
|
|
|
}
|
|
|
|
list->head = NULL;
|
|
|
|
list->tail = NULL;
|
|
|
|
list->length = 0;
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
2017-08-17 09:13:45 -04:00
|
|
|
list_node_t *list_foreach(const list_t *list, list_iter_cb callback, void *context)
|
2016-11-24 13:10:15 -05:00
|
|
|
{
|
2017-08-17 09:13:45 -04:00
|
|
|
assert(list != NULL);
|
|
|
|
assert(callback != NULL);
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2017-08-17 09:13:45 -04:00
|
|
|
for (list_node_t *node = list->head; node; ) {
|
|
|
|
list_node_t *next = node->next;
|
2019-03-26 02:37:37 -04:00
|
|
|
if (!callback(node->data, context)) {
|
2017-08-17 09:13:45 -04:00
|
|
|
return node;
|
2019-03-26 02:37:37 -04:00
|
|
|
}
|
2017-08-17 09:13:45 -04:00
|
|
|
node = next;
|
|
|
|
}
|
|
|
|
return NULL;
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
list_node_t *list_begin(const list_t *list)
|
|
|
|
{
|
|
|
|
assert(list != NULL);
|
|
|
|
return list->head;
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
list_node_t *list_end(UNUSED_ATTR const list_t *list)
|
|
|
|
{
|
|
|
|
assert(list != NULL);
|
|
|
|
return NULL;
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
list_node_t *list_next(const list_node_t *node)
|
|
|
|
{
|
|
|
|
assert(node != NULL);
|
|
|
|
return node->next;
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
void *list_node(const list_node_t *node)
|
|
|
|
{
|
|
|
|
assert(node != NULL);
|
|
|
|
return node->data;
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
list_node_t *list_free_node(list_t *list, list_node_t *node)
|
|
|
|
{
|
|
|
|
assert(list != NULL);
|
|
|
|
assert(node != NULL);
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
list_node_t *next = node->next;
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
if (list->free_cb) {
|
|
|
|
list->free_cb(node->data);
|
|
|
|
}
|
2018-08-14 22:40:51 -04:00
|
|
|
osi_free(node);
|
2016-11-24 13:10:15 -05:00
|
|
|
--list->length;
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
return next;
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
2020-08-20 01:19:22 -04:00
|
|
|
|
|
|
|
// remove the element from list but do not free the node data
|
|
|
|
list_node_t *list_delete_node(list_t *list, list_node_t *node)
|
|
|
|
{
|
|
|
|
assert(list != NULL);
|
|
|
|
assert(node != NULL);
|
|
|
|
|
|
|
|
list_node_t *next = node->next;
|
|
|
|
|
|
|
|
osi_free(node);
|
|
|
|
--list->length;
|
|
|
|
|
|
|
|
return next;
|
|
|
|
}
|