* @param[in] head the list head
*/
static void
-ne_list_head_init (ne_list_node *head)
+list_head_init (list_node *head)
{
head->prev = head->next = head;
}
* @param[in] list the list instance
*/
void
-ne_list_init (ne_list *list)
+list_init (list *list)
{
- ne_list_head_init (&list->head);
+ list_head_init (&list->head);
list->num = 0;
}
* @return true if the first node
*/
bool
-ne_list_is_first (ne_list *list, ne_list_node *node)
+list_is_first (list *list, list_node *node)
{
return list->head.next == node;
}
* @return true if the last node
*/
bool
-ne_list_is_last (ne_list *list, ne_list_node *node)
+list_is_last (list *list, list_node *node)
{
return list->head.prev == node;
}
* @param[in] next next list node
*/
static void
-_ne_list_add (ne_list_node *node, ne_list_node *prev, ne_list_node *next)
+_list_add (list_node *node, list_node *prev, list_node *next)
{
next->prev = node;
node->next = next;
* @param[in] node the node to be added
*/
void
-ne_list_add (ne_list *list, ne_list_node *node)
+list_add (list *list, list_node *node)
{
- ne_list_node *head = &list->head;
+ list_node *head = &list->head;
- _ne_list_add (node, head, head->next);
+ _list_add (node, head, head->next);
list->num++;
}
* @param[in] node the node to be added
*/
void
-ne_list_add_tail (ne_list *list, ne_list_node *node)
+list_add_tail (list *list, list_node *node)
{
- ne_list_node *head = &list->head;
+ list_node *head = &list->head;
- _ne_list_add (node, head->prev, head);
+ _list_add (node, head->prev, head);
list->num++;
}
* @param[in] next the node indicating a position.
*/
void
-ne_list_add_next (ne_list *list, ne_list_node *node, ne_list_node *next)
+list_add_next (list *list, list_node *node, list_node *next)
{
- _ne_list_add (node, next->prev, next);
+ _list_add (node, next->prev, next);
list->num++;
}
* @param[in] next next list node
*/
static void
-_ne_list_del (ne_list_node *prev, ne_list_node *next)
+_list_del (list_node *prev, list_node *next)
{
next->prev = prev;
prev->next = next;
* @param[in] node the node to be deleted
*/
void
-ne_list_del (ne_list *list, ne_list_node *node)
+list_del (list *list, list_node *node)
{
assert (list->num > 0);
- _ne_list_del (node->prev, node->next);
+ _list_del (node->prev, node->next);
list->num--;
}
* @param[in] size the size of bucket (should be a power of 2)
*/
void
-ne_hash_table_init (ne_hash_table *ht, uint32_t size)
+hash_table_init (hash_table *ht, uint32_t size)
{
int i;
ht->num = 0;
ht->size = size;
- ht->list = (ne_list *) malloc (sizeof (ne_list) * size);
+ ht->list = (list *) malloc (sizeof (list) * size);
for (i = 0; i < size; i++)
- ne_list_init (&ht->list[i]);
+ list_init (&ht->list[i]);
}
/**
* @param[in] key key
*/
void
-ne_hash_table_add (ne_hash_table *ht, ne_hash_node *node, uint32_t key)
+hash_table_add (hash_table *ht, hash_node *node, uint32_t key)
{
uint32_t index = HASH_INDEX (ht, key);
- ne_list_add (&ht->list[index], &node->list);
+ list_add (&ht->list[index], &node->list);
node->key = key;
* @param[in] node the node to be deleted
*/
void
-ne_hash_table_del (ne_hash_table *ht, ne_hash_node *node)
+hash_table_del (hash_table *ht, hash_node *node)
{
uint32_t index = HASH_INDEX (ht, node->key);
- ne_list_del (&ht->list[index], &node->list);
+ list_del (&ht->list[index], &node->list);
ht->num--;
}
/**
* @brief macros for list iteration
*/
-#define LIST_FIRST_ENTRY(ptr, type, member) \
+#define list_first_entry(ptr, type, member) \
CONTAINER_OF((ptr)->next, type, member)
-#define LIST_LAST_ENTRY(ptr, type, member) \
+#define list_last_entry(ptr, type, member) \
CONTAINER_OF((ptr)->prev, type, member)
-#define LIST_PREV_ENTRY(pos, member) \
+#define list_prev_entry(pos, member) \
CONTAINER_OF((pos)->member.prev, __typeof__(*(pos)), member)
-#define LIST_NEXT_ENTRY(pos, member) \
+#define list_next_entry(pos, member) \
CONTAINER_OF((pos)->member.next, __typeof__(*(pos)), member)
-#define ne_list_for_each_entry(pos, list, member) \
- for (pos = LIST_FIRST_ENTRY (&(list).head, __typeof__(*pos), member); \
- &pos->member != &(list).head; \
- pos = LIST_NEXT_ENTRY (pos, member))
+#define list_for_each_entry(pos, list, member) \
+ for (pos = list_first_entry (&(list).head, __typeof__(*pos), member); \
+ &pos->member != &(list).head || (pos = NULL) != NULL; \
+ pos = list_next_entry (pos, member))
-#define ne_list_for_each_entry_reverse(pos, list, member) \
- for (pos = LIST_LAST_ENTRY (&(list).head, __typeof__(*pos), member); \
- &pos->member != &(list).head; \
- pos = LIST_PREV_ENTRY (pos, member))
+#define list_for_each_entry_reverse(pos, list, member) \
+ for (pos = list_last_entry (&(list).head, __typeof__(*pos), member); \
+ &pos->member != &(list).head || (pos = NULL) != NULL; \
+ pos = list_prev_entry (pos, member))
-#define ne_list_for_each_entry_safe(pos, tmp, list, member) \
- for (pos = LIST_FIRST_ENTRY (&(list).head, __typeof__(*pos), member), \
- tmp = LIST_NEXT_ENTRY (pos, member); \
- &pos->member != &(list).head; \
- pos = tmp, tmp = LIST_NEXT_ENTRY (pos, member))
+#define list_for_each_entry_safe(pos, tmp, list, member) \
+ for (pos = list_first_entry (&(list).head, __typeof__(*pos), member), \
+ tmp = list_next_entry (pos, member); \
+ &pos->member != &(list).head || (pos = NULL) != NULL; \
+ pos = tmp, tmp = list_next_entry (pos, member))
/**
* @brief structure for list node
*/
-typedef struct _ne_list_node {
- struct _ne_list_node *next, *prev;
-} ne_list_node;
+typedef struct _list_node {
+ struct _list_node *next, *prev;
+} list_node;
/**
* @brief structure for list
*/
-typedef struct _ne_list {
+typedef struct _list {
uint32_t num;
- ne_list_node head;
-} ne_list;
+ list_node head;
+} list;
/**
* @brief initialize list
* @param[in] list the list instance
*/
-void ne_list_init (ne_list *list);
+void list_init (list *list);
/**
* @brief check whether the node is the first element or not
* @param[in] node the node to be checked
* @return true if the first node
*/
-bool ne_list_is_first (ne_list *list, ne_list_node *node);
+bool list_is_first (list *list, list_node *node);
/**
* @brief check whether the node is the last element or not
* @param[in] node the node to be checked
* @return true if the last node
*/
-bool ne_list_is_last (ne_list *list, ne_list_node *node);
+bool list_is_last (list *list, list_node *node);
/**
* @brief add the node into the first position in the list
* @param[in] list the list instance
* @param[in] node the node to be added
*/
-void ne_list_add (ne_list *list, ne_list_node *node);
+void list_add (list *list, list_node *node);
/**
* @brief add the node into the last position in the list
* @param[in] list the list instance
* @param[in] node the node to be added
*/
-void ne_list_add_tail (ne_list *list, ne_list_node *node);
+void list_add_tail (list *list, list_node *node);
/**
* @brief add the node into the specific position in the list
* @param[in] node the node to be added
* @param[in] next the node indicating a position.
*/
-void ne_list_add_next (ne_list *list, ne_list_node *node, ne_list_node *next);
+void list_add_next (list *list, list_node *node, list_node *next);
/**
* @brief delete the node from the list
* @param[in] list the list instance
* @param[in] node the node to be deleted
*/
-void ne_list_del (ne_list *list, ne_list_node *node);
+void list_del (list *list, list_node *node);
/* hash table */
#define GOLDEN_RATIO_32 0x61C88647 /* 32-bits hash */
/**
* @brief structure for hash node
*/
-typedef struct _ne_hash_node {
+typedef struct _hash_node {
uint32_t key;
- ne_list_node list; /* use a double-linked list */
-} ne_hash_node;
+ list_node list; /* use a double-linked list */
+} hash_node;
/**
* @brief structure for hash table
*/
-typedef struct _ne_hash_table {
+typedef struct _hash_table {
uint32_t num;
uint32_t size; /* should be a power of 2 */
- ne_list *list; /* in each bucket, there is a list */
-} ne_hash_table;
+ list *list; /* in each bucket, there is a list */
+} hash_table;
/**
* @brief initialize hash table
* @param[in] ht the instance of hash table
* @param[in] size the size of bucket (should be a power of 2)
*/
-void ne_hash_table_init (ne_hash_table *ht, uint32_t size);
+void hash_table_init (hash_table *ht, uint32_t size);
/**
* @brief add the node with key into the hash table
* @param[in] node the node to be added
* @param[in] key key
*/
-void ne_hash_table_add (ne_hash_table *ht, ne_hash_node *node, uint32_t key);
+void hash_table_add (hash_table *ht, hash_node *node, uint32_t key);
/**
* @brief delete the node from the hash table
* @param[in] ht the instance of hash table
* @param[in] node the node to be deleted
*/
-void ne_hash_table_del (ne_hash_table *ht, ne_hash_node *node);
+void hash_table_del (hash_table *ht, hash_node *node);
/**
* @brief macros for hash table iteration
*/
-#define ne_hash_for_each(obj, ht, bkt, member) \
+#define hash_for_each(obj, ht, bkt, member) \
for ((bkt) = 0; (bkt) < ht.size; (bkt)++) \
- ne_list_for_each_entry(obj, ht.list[bkt], member.list)
+ list_for_each_entry(obj, ht.list[bkt], member.list)
-#define ne_hash_for_each_safe(obj, tmp, ht, bkt, member) \
+#define hash_for_each_safe(obj, tmp, ht, bkt, member) \
for ((bkt) = 0; (bkt) < ht.size; (bkt)++) \
- ne_list_for_each_entry_safe(obj, tmp, ht.list[bkt], member.list)
+ list_for_each_entry_safe(obj, tmp, ht.list[bkt], member.list)
-#define ne_hash_for_each_possible(obj, ht, member, key) \
- ne_list_for_each_entry(obj, ht.list[HASH_INDEX (&ht, key)], member.list)
+#define hash_for_each_possible(obj, ht, member, key) \
+ list_for_each_entry(obj, ht.list[HASH_INDEX (&ht, key)], member.list)
-#define ne_hash_for_each_possible_safe(obj, tmp, ht, member, key) \
- ne_list_for_each_entry_safe(obj, tmp, ht.list[HASH_INDEX (&ht, key)], member.list)
+#define hash_for_each_possible_safe(obj, tmp, ht, member, key) \
+ list_for_each_entry_safe(obj, tmp, ht.list[HASH_INDEX (&ht, key)], member.list)
#endif /* __NPU_ENGINE_UTILS_H__ */
*/
typedef struct _test_elem {
uint32_t val;
- ne_list_node list;
- ne_hash_node hash;
+ list_node list;
+ hash_node hash;
} test_elem;
/**
TEST (test_ne_utils, linked_list)
{
test_elem *elem, *tmp;
- ne_list test_list;
+ list test_list;
uint32_t i, num = 100;
- ne_list_init (&test_list);
+ list_init (&test_list);
for (i = 0; i < num; i++) {
elem = (test_elem*) malloc(sizeof(test_elem));
elem->val = i;
- ne_list_add (&test_list, &elem->list);
+ list_add (&test_list, &elem->list);
}
i = num;
- ne_list_for_each_entry (elem, test_list, list) {
+ list_for_each_entry (elem, test_list, list) {
assert (elem->val == --i);
}
i = 0;
- ne_list_for_each_entry_reverse (elem, test_list, list) {
+ list_for_each_entry_reverse (elem, test_list, list) {
assert (elem->val == i++);
}
i = num;
- ne_list_for_each_entry_safe (elem, tmp, test_list, list) {
+ list_for_each_entry_safe (elem, tmp, test_list, list) {
assert (elem->val == --i);
- ne_list_del (&test_list, &elem->list);
+ list_del (&test_list, &elem->list);
free (elem);
}
i = 0;
- ne_list_for_each_entry (elem, test_list, list) {
+ list_for_each_entry (elem, test_list, list) {
/* dummy */
i++;
}
TEST (test_ne_utils, hash_table)
{
test_elem *elem, *tmp;
- ne_hash_table test_ht;
+ hash_table test_ht;
uint32_t i, bkt, num = 100;
- ne_hash_table_init (&test_ht, 1 << 10);
+ hash_table_init (&test_ht, 1 << 10);
for (i = 0; i < num; i++) {
test_elem *elem = (test_elem*) malloc (sizeof(test_elem));
elem->val = i;
- ne_hash_table_add (&test_ht, &elem->hash, i);
+ hash_table_add (&test_ht, &elem->hash, i);
}
assert (test_ht.num == num);
- ne_hash_for_each_possible (elem, test_ht, hash, num/2) {
+ hash_for_each_possible (elem, test_ht, hash, num/2) {
if (elem->val == num/2) {
break;
}
assert (elem->val == num/2);
- ne_hash_table_del (&test_ht, &elem->hash);
+ hash_table_del (&test_ht, &elem->hash);
free (elem);
i = 0;
- ne_hash_for_each (elem, test_ht, bkt, hash) {
+ hash_for_each (elem, test_ht, bkt, hash) {
i++;
}
assert (i == num - 1);
i = 0;
- ne_hash_for_each_safe (elem, tmp, test_ht, bkt, hash) {
- ne_hash_table_del (&test_ht, &elem->hash);
+ hash_for_each_safe (elem, tmp, test_ht, bkt, hash) {
+ hash_table_del (&test_ht, &elem->hash);
free (elem);
i++;
}