| lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /**
 | 
 | 2 |  * 
 | 
 | 3 |  * @file      ringbuf_ex.h
 | 
 | 4 |  * @brief     
 | 
 | 5 |  *            This file is part of tools.
 | 
 | 6 |  *            ¹¤¾ß»·Ðλº³å¹ÜÀíÄ£¿é
 | 
 | 7 |  *            
 | 
 | 8 |  * @details   
 | 
 | 9 |  * @author    Tools Team.
 | 
 | 10 |  * @email     
 | 
 | 11 |  * @copyright Copyright (C) 2013 Sanechips Technology Co., Ltd.
 | 
 | 12 |  * @warning   
 | 
 | 13 |  * @date      2019/02/02
 | 
 | 14 |  * @version   1.1
 | 
 | 15 |  * @pre       
 | 
 | 16 |  * @post      
 | 
 | 17 |  *            
 | 
 | 18 |  * @par       
 | 
 | 19 |  * Change History :
 | 
 | 20 |  * ---------------------------------------------------------------------------
 | 
 | 21 |  * date        version  author         description
 | 
 | 22 |  * ---------------------------------------------------------------------------
 | 
 | 23 |  * 2013/01/21  1.0      lu.xieji       Create file
 | 
 | 24 |  * 2019/02/02  1.1      jiang.fenglin  ÐÞ¸Ä×¢ÊÍ·½Ê½Îªdoxygen
 | 
 | 25 |  * 2019/04/08  1.2      jiang.fenglin  È¥µô´íÎó¼ÆÊýÆ÷¸ÄÓöÏÑÔ£¬Ôö¼Óringbuf empty״̬
 | 
 | 26 |  * ---------------------------------------------------------------------------
 | 
 | 27 |  * 
 | 
 | 28 |  * 
 | 
 | 29 |  */
 | 
 | 30 | 
 | 
 | 31 | #ifndef RINGBUF_EX_H
 | 
 | 32 | #define RINGBUF_EX_H
 | 
 | 33 | 
 | 
 | 34 | 
 | 
 | 35 | #define RINGBUF_PAD_CHAR 0xaa
 | 
 | 36 | #define kLastBlock       -1
 | 
 | 37 | #define kMinChunkSize    16
 | 
 | 38 | 
 | 
 | 39 | enum
 | 
 | 40 | {
 | 
 | 41 |     kBlockNotUsed,      // ²»¿ÉÓ㬲»¿ÉÒÔ·ÖÅ䣬һ°ãÊÇĩβ²»×ãÒÔ·ÖÅäµÄ¿é
 | 
 | 42 |     kBlockFree,         // ¿ÕÏУ¬¿ÉÒÔ½øÐзÖÅä
 | 
 | 43 |     kBlockAllocated,    // ¿ÉÓ㬿ÉÒÔ´æÈëÊý¾ÝÖÐ
 | 
 | 44 |     kBlockReady,        // Êý¾ÝÒÑ×¼±¸ºÃ£¬¿ÉÒÔÈ¡³öÊý¾Ý
 | 
 | 45 |     kBlockEmpty,        // Êý¾ÝÒÑÈ¡³ö£¬¿ÉÒÔÊÍ·Å
 | 
 | 46 | };
 | 
 | 47 | 
 | 
 | 48 | 
 | 
 | 49 | typedef void (*mem_lock_fnc_type)(void * ptr);
 | 
 | 50 | typedef void (*mem_free_fnc_type)(void * ptr);
 | 
 | 51 | 
 | 
 | 52 | typedef struct
 | 
 | 53 | {
 | 
 | 54 |     unsigned long forw_offset;  /**< Forward offset. The value of the offset
 | 
 | 55 |                                                   includes the size of the header and the allocated block. */
 | 
 | 56 |     char          free_flag;    /**< Flag to indicate if this memory block  is free. */
 | 
 | 57 |     char          last_flag;    /**< Flag to indicate if this is the last block  in the allocated section. */
 | 
 | 58 |     unsigned char extra;        /**< Extra bytes at the end of a block. */
 | 
 | 59 |     unsigned char pad1;         /**< Padding at the end of a block. */
 | 
 | 60 | } ringbuf_ex_header_type;
 | 
 | 61 | 
 | 
 | 62 | typedef struct
 | 
 | 63 | {
 | 
 | 64 |     ringbuf_ex_header_type         *first_block;
 | 
 | 65 |     /**< First block in the ringbuf. */
 | 
 | 66 |     ringbuf_ex_header_type         *next_free_block;
 | 
 | 67 |     /**< Next free block in the ringbuf. */
 | 
 | 68 |     ringbuf_ex_header_type         *next_used_block;
 | 
 | 69 |     /**< Next used block in the ringbuf. */
 | 
 | 70 |     unsigned long                  total_bytes;
 | 
 | 71 |     void                           *base;
 | 
 | 72 |     /**< Total bytes available in the ringbuf. */
 | 
 | 73 |     // »¥³âÁ¿º¯Êý
 | 
 | 74 |     mem_lock_fnc_type              lock_fnc_ptr;
 | 
 | 75 |     mem_free_fnc_type              free_fnc_ptr;
 | 
 | 76 | } ringbuf_ex_type;
 | 
 | 77 | 
 | 
 | 78 | #ifdef __cplusplus
 | 
 | 79 | extern "C"
 | 
 | 80 | {
 | 
 | 81 | #endif
 | 
 | 82 | 
 | 
 | 83 | /**
 | 
 | 84 |  * @brief ringbuf³õʼ»¯
 | 
 | 85 |  * @param[in] ringbuf_ex_mem_ptr µÚÒ»¸öblockµÄÖ¸Õë
 | 
 | 86 |  * @param[in] ringbuf_ex_mem_size µÚÒ»¸öblockµÄ´óС
 | 
 | 87 |  * @return ringbuf_ex_ptr »·Ðλº³åÇøÖ¸Õë
 | 
 | 88 |  * @note
 | 
 | 89 |  * @see 
 | 
 | 90 |  */
 | 
 | 91 | ringbuf_ex_type *ringbuf_ex_init(void *ringbuf_ex_mem_ptr, UINT32 ringbuf_ex_mem_size);
 | 
 | 92 | 
 | 
 | 93 | /**
 | 
 | 94 |  * @brief ringbufÉêÇëÄÚ´æ
 | 
 | 95 |  * @param[out] ringbuf_ex_ptr »·Ðλº³åÇøÖ¸Õë
 | 
 | 96 |  * @param[in] size ÄÚ´æ¿é´óС
 | 
 | 97 |  * @return ÉêÇëºóµÄÄÚ´æÖ¸Õë
 | 
 | 98 |  * @note
 | 
 | 99 |  * @see 
 | 
 | 100 |  */
 | 
 | 101 | void *ringbuf_ex_malloc(ringbuf_ex_type *ringbuf_ex_ptr, int size);
 | 
 | 102 | 
 | 
 | 103 | /**
 | 
 | 104 |  * @brief ringbufÊÍ·ÅÄÚ´æ
 | 
 | 105 |  * @param[in] ringbuf_ex_ptr »·Ðλº³åÇøÖ¸Õë
 | 
 | 106 |  * @param[in] ptr ÄÚ´æ¿éÖ¸Õë
 | 
 | 107 |  * @return void
 | 
 | 108 |  * @note
 | 
 | 109 |  * @see 
 | 
 | 110 |  */
 | 
 | 111 | void ringbuf_ex_free(ringbuf_ex_type *ringbuf_ex_ptr, void *ptr);
 | 
 | 112 | 
 | 
 | 113 | /**
 | 
 | 114 |  * @brief ringbuf½«Ò»¸öÄÚ´æ¿éÖÃΪnotused̬
 | 
 | 115 |  * @param[in] ringbuf_ex_ptr »·Ðλº³åÇøÖ¸Õë
 | 
 | 116 |  * @param[in] ptr ÄÚ´æ¿éÖ¸Õë
 | 
 | 117 |  * @return 1 sucess, 0 fail
 | 
 | 118 |  * @note
 | 
 | 119 |  * @see 
 | 
 | 120 |  */
 | 
 | 121 | int ringbuf_ex_set_notused(ringbuf_ex_type *ringbuf_ex_ptr, void *ptr);
 | 
 | 122 | 
 | 
 | 123 | /**
 | 
 | 124 |  * @brief ringbuf½«Ò»¸öÄÚ´æ¿éÖÃΪready̬
 | 
 | 125 |  * @param[in] ringbuf_ex_ptr »·Ðλº³åÇøÖ¸Õë
 | 
 | 126 |  * @param[in] ptr ÄÚ´æ¿éÖ¸Õë
 | 
 | 127 |  * @return 1 sucess, 0 fail
 | 
 | 128 |  * @note
 | 
 | 129 |  * @see 
 | 
 | 130 |  */
 | 
 | 131 | int ringbuf_ex_set_ready(ringbuf_ex_type *ringbuf_ex_ptr, void *ptr);
 | 
 | 132 | 
 | 
 | 133 | /**
 | 
 | 134 |  * @brief ringbuf½«Ò»¸öÄÚ´æ¿éÖÃΪempty̬
 | 
 | 135 |  * @param[in] ringbuf_ex_ptr »·Ðλº³åÇøÖ¸Õë
 | 
 | 136 |  * @param[in] ptr ÄÚ´æ¿éÖ¸Õë
 | 
 | 137 |  * @return 1 sucess, 0 fail
 | 
 | 138 |  * @note
 | 
 | 139 |  * @see 
 | 
 | 140 |  */
 | 
 | 141 | int ringbuf_ex_set_empty(ringbuf_ex_type *ringbuf_ex_ptr, void *ptr);
 | 
 | 142 | 
 | 
 | 143 | /**
 | 
 | 144 |  * @brief ringbuf»ñÈ¡µÚÒ»¸öready buf
 | 
 | 145 |  * @param[in] ringbuf_ex_ptr »·Ðλº³åÇøÖ¸Õë
 | 
 | 146 |  * @param[in] ptr ÄÚ´æ¿éÖ¸Õë
 | 
 | 147 |  * @param[in] size ÄÚ´æ¿é´óС
 | 
 | 148 |  * @return 1 sucess, 0 fail
 | 
 | 149 |  * @note
 | 
 | 150 |  * @see 
 | 
 | 151 |  */
 | 
 | 152 | int ringbuf_ex_get_first_ready(ringbuf_ex_type *ringbuf_ex_ptr, void **ptr, UINT32 *size);
 | 
 | 153 | 
 | 
 | 154 | /**
 | 
 | 155 |  * @brief ÅжÏringbufµÄµÚÒ»¸öblockÊý¾ÝÊÇ·ñÒѾ׼±¸ºÃ
 | 
 | 156 |  * @param[in] ringbuf_ex_ptr »·Ðλº³åÇøÖ¸Õë
 | 
 | 157 |  * @return 1 sucess, 0 fail
 | 
 | 158 |  * @note
 | 
 | 159 |  * @see 
 | 
 | 160 |  */
 | 
 | 161 | int ringbuf_ex_first_block_is_ready(ringbuf_ex_type *ringbuf_ex_ptr);
 | 
 | 162 | 
 | 
 | 163 | /**
 | 
 | 164 |  * @brief ´´½¨ex2Ð͵Ļ·Ðλº³å
 | 
 | 165 |  * @param[in] size Óû§Ö¸¶¨µÄ´óС£¬×¢ÒâÓû§Êµ¼Ê¿ÉÓÿռä±ÈsizeС8
 | 
 | 166 |  * @return »·Ðλº³åÇøÖ¸Õë
 | 
 | 167 |  * @note
 | 
 | 168 |  * @see 
 | 
 | 169 |  */
 | 
 | 170 | void *ringbuf_ex2_create(unsigned int size);
 | 
 | 171 | 
 | 
 | 172 | /**
 | 
 | 173 |  * @brief Ïòex2Ð͵Ļ·Ðλº³åÖÐдÈëÊý¾Ý
 | 
 | 174 |  * @param[in] ringbuf_ex »·Ðλº³åÇøÖ¸Õë
 | 
 | 175 |  * @param[in] buf Êý¾ÝµØÖ·
 | 
 | 176 |  * @param[in] len Êý¾Ý³¤¶È
 | 
 | 177 |  * @return void
 | 
 | 178 |  * @note
 | 
 | 179 |  * @see 
 | 
 | 180 |  */
 | 
 | 181 | void ringbuf_ex2_write(void *ringbuf_ex, void *buf, unsigned int len);
 | 
 | 182 | 
 | 
 | 183 | #ifdef __cplusplus
 | 
 | 184 | }
 | 
 | 185 | #endif
 | 
 | 186 | 
 | 
 | 187 | 
 | 
 | 188 | #endif /* MEMHEAP_H */
 | 
 | 189 | 
 |