Files
TencentOS-tiny/kernel/core/include/tos_binary_heap.h
daishengdong d0b8d0675e add ring queue/message queue/mail queue, binary heap/priority queue/priority message queue/priority mail queue
1. remove the old msg queue and queue:
i. msg queue is not a common and reusable/flexible component(need user to config the msg pool size and this componet can only be used by tos_queue)
ii. tos_queue can only deliver the pointer message(cannot do a memory buffer deliver)

2. add ring queue(tos_ring_q) componet
rinq queue can be reused by tos_chr_fifi/tos_msg_q/tos_mail_q as the foundational data container

3. add message queue(tos_msg_q)
a little like the old queue mechanism, supply the capability to deliver a pointer message

4. add mail queue(tos_mail_q)
supply the capability to deliver a memory buffer

5. add binary heap(tos_bin_heap)
the basement componet to implement priority queue

6. add priority queue(tos_prio_q)
can be reused by the priority message/mail queue  as the foundational data container.

7. add priority message queue(tos_prio_msg_q)
a message(pointer) deliver mechanism, supply the capability of delivering the message with priority(message with higher priority comes faster to the pender than with lower)

8. add priority mail queue(tos_prio_mail_q)
a mail(memory buffer) deliver mechanism, supply the capability of delivering the mail with priority(mail with higher priority comes faster to the pender than with lower)
2019-10-28 15:50:46 +08:00

150 lines
5.1 KiB
C

/*----------------------------------------------------------------------------
* Tencent is pleased to support the open source community by making TencentOS
* available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
* If you have downloaded a copy of the TencentOS binary from Tencent, please
* note that the TencentOS binary is licensed under the BSD 3-Clause License.
*
* If you have downloaded a copy of the TencentOS source code from Tencent,
* please note that TencentOS source code is licensed under the BSD 3-Clause
* License, except for the third-party components listed below which are
* subject to different license terms. Your integration of TencentOS into your
* own projects may require compliance with the BSD 3-Clause License, as well
* as the other licenses applicable to the third-party components included
* within TencentOS.
*---------------------------------------------------------------------------*/
#ifndef _TOS_BINARY_HEAP_H_
#define _TOS_BINARY_HEAP_H_
typedef int (*k_bin_heap_cmp)(void *first, void *second);
typedef struct k_binary_heap_st {
#if TOS_CFG_OBJECT_VERIFY_EN > 0u
knl_obj_t knl_obj;
#endif
size_t total;
k_bin_heap_cmp cmp;
size_t item_size;
size_t item_cnt;
uint8_t *pool;
} k_bin_heap_t;
#define BIN_HEAP_FIRST_ITEM(bin_heap) (void *)(&bin_heap->pool[0])
#define BIN_HEAP_LAST_ITEM(bin_heap) (void *)(&bin_heap->pool[bin_heap->total * bin_heap->item_size])
#define BIN_HEAP_THE_ITEM(bin_heap, index) (void *)(&bin_heap->pool[index * bin_heap->item_size])
#define BIN_HEAP_PARENT(index) ((index - 1) / 2)
#define BIN_HEAP_RCHILD(index) (2 * (index + 1))
#define BIN_HEAP_LSIBLING(index) (index - 1)
/**
* @brief Create a binary heap.
* create a binary heap.
*
* @attention None
*
* @param[in] bin_heap pointer to the handler of the binary heap.
* @param[in] pool pool buffer of the binary heap.
* @param[in] item_cnt item count of the binary heap.
* @param[in] item_size size of each item of the binary heap.
* @param[in] cmp compare function to determine two items which is bigger or smaller.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_bin_heap_create(k_bin_heap_t *bin_heap, void *pool, size_t item_cnt, size_t item_size, k_bin_heap_cmp cmp);
/**
* @brief Destroy a binary heap.
* destroy a binary heap.
*
* @attention None
*
* @param[in] bin_heap pointer to the handler of the binary heap.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_bin_heap_destroy(k_bin_heap_t *bin_heap);
/**
* @brief Push an item.
* push an item into the binary heap.
*
* @attention None
*
* @param[in] bin_heap pointer to the handler of the binary heap.
* @param[in] item the item to be pushed.
* @param[in] item_size size of the item(should be consistent with the item_size passed to tos_bin_heap_create).
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
* @retval #K_ERR_BIN_HEAP_ITEM_SIZE_NOT_MATCH the item_size is not consistent with the item_size passed to tos_bin_heap_create.
* @retval #K_ERR_BIN_HEAP_FULL the binary heap is full.
*/
__API__ k_err_t tos_bin_heap_push(k_bin_heap_t *bin_heap, void *item, size_t item_size);
/**
* @brief Pop an item.
* pop an item from the binary heap.
*
* @attention None
*
* @param[in] bin_heap pointer to the handler of the binary heap.
* @param[out] item buffer to hold the item poped.
* @param[out] item_size size of the item poped(should be consistent with the item_size passed to tos_bin_heap_create).
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
* @retval #K_ERR_BIN_HEAP_EMPTY the ring queue is empty.
*/
__API__ k_err_t tos_bin_heap_pop(k_bin_heap_t *bin_heap, void *item, size_t *item_size);
/**
* @brief Flush the binary heap.
* flush the binary heap.
*
* @attention None
*
* @param[in] bin_heap pointer to the handler of the binary heap.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_bin_heap_flush(k_bin_heap_t *bin_heap);
/**
* @brief Whether the binary heap is empty.
* Whether the binary heap is empty.
*
* @attention None
*
* @param[in] bin_heap pointer to the handler of the binary heap.
*
* @return whether the binary heap is emtpy.
* @retval #0 the binary heap is not empty.
* @retval #Not 0 the binary heap is empty.
*/
__API__ int tos_bin_heap_is_empty(k_bin_heap_t *bin_heap);
/**
* @brief Whether the binary heap is full.
* Whether the binary heap is full.
*
* @attention None
*
* @param[in] bin_heap pointer to the handler of the binary heap.
*
* @return whether the binary hea is full.
* @retval #0 the binary hea is not full.
* @retval #Not 0 the binary hea is full.
*/
__API__ int tos_bin_heap_is_full(k_bin_heap_t *bin_heap);
#endif /* _TOS_BINARY_HEAP_H_ */