1 /* 2 * Copyright (c) 2021 Bestechnic (Shanghai) Co., Ltd. All rights reserved. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #pragma once 16 17 #ifndef __LIST_H__ 18 #define __LIST_H__ 19 20 #include <stdbool.h> 21 #include <stdlib.h> 22 #include "heap_api.h" 23 24 #ifdef __cplusplus 25 extern "C" { 26 #endif 27 28 extern heap_handle_t g_nv_mempool; 29 30 #define pool_malloc(size) heap_malloc(g_nv_mempool,size) 31 #define pool_free(ptr) heap_free(g_nv_mempool,ptr) 32 #define pool_free_space() heap_free_size(g_nv_mempool) 33 34 typedef void (*list_free_cb)(void *data); 35 typedef bool (*list_iter_cb)(void *data); 36 37 typedef struct list_node_t { 38 struct list_node_t *next; 39 void *data; 40 } list_node_t; 41 42 typedef struct list_t { 43 list_node_t *head; 44 list_node_t *tail; 45 size_t length; 46 list_free_cb free_cb; 47 } list_t; 48 49 //struct list_t; 50 typedef struct list_t list_t; 51 void *zmalloc_ext (size_t size); 52 // Lifecycle. 53 list_t *list_new_ext(list_free_cb callback); 54 void list_free_ext(list_t *list); 55 56 // Accessors. 57 bool list_is_empty_ext(const list_t *list); 58 size_t list_length_ext(const list_t *list); 59 void *list_front_ext(const list_t *list); 60 void *list_back_ext(const list_t *list); 61 62 // Mutators. 63 bool list_insert_after_ext(list_t *list, list_node_t *prev_node, void *data); 64 bool list_prepend_ext(list_t *list, void *data); 65 bool list_append_ext(list_t *list, void *data); 66 bool list_remove_ext(list_t *list, void *data); 67 void list_clear_ext(list_t *list); 68 69 // Iteration. 70 void list_foreach_ext(const list_t *list, list_iter_cb callback); 71 72 list_node_t *list_begin_ext(const list_t *list); 73 list_node_t *list_end_ext(const list_t *list); 74 list_node_t *list_next_ext(const list_node_t *node); 75 void *list_node_ext(const list_node_t *node); 76 77 #ifdef __cplusplus 78 } 79 #endif 80 81 #endif//__FMDEC_H__ 82 83