blob: fc3e65e90e12b7306202aa66a2ca87821d3c66c8 [file] [log] [blame]
/**
*
* @file ringbuf_ex.h
* @brief
* This file is part of tools.
* ¹¤¾ß»·Ðλº³å¹ÜÀíÄ£¿é
*
* @details
* @author Tools Team.
* @email
* @copyright Copyright (C) 2013 Sanechips Technology Co., Ltd.
* @warning
* @date 2019/02/02
* @version 1.1
* @pre
* @post
*
* @par
* Change History :
* ---------------------------------------------------------------------------
* date version author description
* ---------------------------------------------------------------------------
* 2013/01/21 1.0 lu.xieji Create file
* 2019/02/02 1.1 jiang.fenglin ÐÞ¸Ä×¢ÊÍ·½Ê½Îªdoxygen
* 2019/04/08 1.2 jiang.fenglin È¥µô´íÎó¼ÆÊýÆ÷¸ÄÓöÏÑÔ£¬Ôö¼Óringbuf empty״̬
* ---------------------------------------------------------------------------
*
*
*/
#ifndef RINGBUF_EX_H
#define RINGBUF_EX_H
#define RINGBUF_PAD_CHAR 0xaa
#define kLastBlock -1
#define kMinChunkSize 16
enum
{
kBlockNotUsed, // ²»¿ÉÓ㬲»¿ÉÒÔ·ÖÅ䣬һ°ãÊÇĩβ²»×ãÒÔ·ÖÅäµÄ¿é
kBlockFree, // ¿ÕÏУ¬¿ÉÒÔ½øÐзÖÅä
kBlockAllocated, // ¿ÉÓ㬿ÉÒÔ´æÈëÊý¾ÝÖÐ
kBlockReady, // Êý¾ÝÒÑ×¼±¸ºÃ£¬¿ÉÒÔÈ¡³öÊý¾Ý
kBlockEmpty, // Êý¾ÝÒÑÈ¡³ö£¬¿ÉÒÔÊÍ·Å
};
typedef void (*mem_lock_fnc_type)(void * ptr);
typedef void (*mem_free_fnc_type)(void * ptr);
typedef struct
{
unsigned long forw_offset; /**< Forward offset. The value of the offset
includes the size of the header and the allocated block. */
char free_flag; /**< Flag to indicate if this memory block is free. */
char last_flag; /**< Flag to indicate if this is the last block in the allocated section. */
unsigned char extra; /**< Extra bytes at the end of a block. */
unsigned char pad1; /**< Padding at the end of a block. */
} ringbuf_ex_header_type;
typedef struct
{
ringbuf_ex_header_type *first_block;
/**< First block in the ringbuf. */
ringbuf_ex_header_type *next_free_block;
/**< Next free block in the ringbuf. */
ringbuf_ex_header_type *next_used_block;
/**< Next used block in the ringbuf. */
unsigned long total_bytes;
void *base;
/**< Total bytes available in the ringbuf. */
// »¥³âÁ¿º¯Êý
mem_lock_fnc_type lock_fnc_ptr;
mem_free_fnc_type free_fnc_ptr;
} ringbuf_ex_type;
#ifdef __cplusplus
extern "C"
{
#endif
/**
* @brief ringbuf³õʼ»¯
* @param[in] ringbuf_ex_mem_ptr µÚÒ»¸öblockµÄÖ¸Õë
* @param[in] ringbuf_ex_mem_size µÚÒ»¸öblockµÄ´óС
* @return ringbuf_ex_ptr »·Ðλº³åÇøÖ¸Õë
* @note
* @see
*/
ringbuf_ex_type *ringbuf_ex_init(void *ringbuf_ex_mem_ptr, UINT32 ringbuf_ex_mem_size);
/**
* @brief ringbufÉêÇëÄÚ´æ
* @param[out] ringbuf_ex_ptr »·Ðλº³åÇøÖ¸Õë
* @param[in] size ÄÚ´æ¿é´óС
* @return ÉêÇëºóµÄÄÚ´æÖ¸Õë
* @note
* @see
*/
void *ringbuf_ex_malloc(ringbuf_ex_type *ringbuf_ex_ptr, int size);
/**
* @brief ringbufÊÍ·ÅÄÚ´æ
* @param[in] ringbuf_ex_ptr »·Ðλº³åÇøÖ¸Õë
* @param[in] ptr ÄÚ´æ¿éÖ¸Õë
* @return void
* @note
* @see
*/
void ringbuf_ex_free(ringbuf_ex_type *ringbuf_ex_ptr, void *ptr);
/**
* @brief ringbuf½«Ò»¸öÄÚ´æ¿éÖÃΪnotused̬
* @param[in] ringbuf_ex_ptr »·Ðλº³åÇøÖ¸Õë
* @param[in] ptr ÄÚ´æ¿éÖ¸Õë
* @return 1 sucess, 0 fail
* @note
* @see
*/
int ringbuf_ex_set_notused(ringbuf_ex_type *ringbuf_ex_ptr, void *ptr);
/**
* @brief ringbuf½«Ò»¸öÄÚ´æ¿éÖÃΪready̬
* @param[in] ringbuf_ex_ptr »·Ðλº³åÇøÖ¸Õë
* @param[in] ptr ÄÚ´æ¿éÖ¸Õë
* @return 1 sucess, 0 fail
* @note
* @see
*/
int ringbuf_ex_set_ready(ringbuf_ex_type *ringbuf_ex_ptr, void *ptr);
/**
* @brief ringbuf½«Ò»¸öÄÚ´æ¿éÖÃΪempty̬
* @param[in] ringbuf_ex_ptr »·Ðλº³åÇøÖ¸Õë
* @param[in] ptr ÄÚ´æ¿éÖ¸Õë
* @return 1 sucess, 0 fail
* @note
* @see
*/
int ringbuf_ex_set_empty(ringbuf_ex_type *ringbuf_ex_ptr, void *ptr);
/**
* @brief ringbuf»ñÈ¡µÚÒ»¸öready buf
* @param[in] ringbuf_ex_ptr »·Ðλº³åÇøÖ¸Õë
* @param[in] ptr ÄÚ´æ¿éÖ¸Õë
* @param[in] size ÄÚ´æ¿é´óС
* @return 1 sucess, 0 fail
* @note
* @see
*/
int ringbuf_ex_get_first_ready(ringbuf_ex_type *ringbuf_ex_ptr, void **ptr, UINT32 *size);
/**
* @brief ÅжÏringbufµÄµÚÒ»¸öblockÊý¾ÝÊÇ·ñÒѾ­×¼±¸ºÃ
* @param[in] ringbuf_ex_ptr »·Ðλº³åÇøÖ¸Õë
* @return 1 sucess, 0 fail
* @note
* @see
*/
int ringbuf_ex_first_block_is_ready(ringbuf_ex_type *ringbuf_ex_ptr);
/**
* @brief ´´½¨ex2Ð͵Ļ·Ðλº³å
* @param[in] size Óû§Ö¸¶¨µÄ´óС£¬×¢ÒâÓû§Êµ¼Ê¿ÉÓÿռä±ÈsizeС8
* @return »·Ðλº³åÇøÖ¸Õë
* @note
* @see
*/
void *ringbuf_ex2_create(unsigned int size);
/**
* @brief Ïòex2Ð͵Ļ·Ðλº³åÖÐдÈëÊý¾Ý
* @param[in] ringbuf_ex »·Ðλº³åÇøÖ¸Õë
* @param[in] buf Êý¾ÝµØÖ·
* @param[in] len Êý¾Ý³¤¶È
* @return void
* @note
* @see
*/
void ringbuf_ex2_write(void *ringbuf_ex, void *buf, unsigned int len);
#ifdef __cplusplus
}
#endif
#endif /* MEMHEAP_H */