代码拉取完成,页面将自动刷新
#define __BYTE_QUEUE_CLASS_IMPLEMENT__
#include "./byte_queue.h"
#if USE_SERVICE_QUEUE == ENABLED
#undef this
#define this (*ptThis)
byte_queue_t * queue_init_byte(byte_queue_t *ptObj, void *pBuffer, uint16_t hwItemSize)
{
class_internal(ptObj, ptThis, byte_queue_t);
if(pBuffer == NULL || hwItemSize == 0 || ptObj == NULL) {
return NULL;
}
queue_protect() {
this.pchBuffer = pBuffer;
this.hwSize = hwItemSize;
this.hwHead = 0;
this.hwTail = 0;
this.hwLength = 0;
this.hwPeek = this.hwHead;
this.hwPeekLength = 0;
}
return ptObj;
}
bool enqueue_byte(byte_queue_t *ptObj, uint8_t chByte)
{
class_internal(ptObj, ptThis, byte_queue_t);
if( ptObj == NULL) {
return false;
}
if(this.hwHead == this.hwTail &&
0 != this.hwLength ) {
return false;
}
queue_protect() {
this.pchBuffer[this.hwTail++] = chByte;
if(this.hwTail >= this.hwSize) {
this.hwTail = 0;
}
this.hwLength++;
this.hwPeekLength++;
}
return true;
}
int16_t enqueue_bytes(byte_queue_t *ptObj, void *pByte, uint16_t hwLength)
{
class_internal(ptObj, ptThis, byte_queue_t);
if( ptObj == NULL) {
return -1;
}
if(this.hwHead == this.hwTail &&
0 != this.hwLength ) {
return 0;
}
queue_protect() {
if(hwLength > (this.hwSize - this.hwLength)) {
hwLength = this.hwSize - this.hwLength;
}
do {
if(hwLength < (this.hwSize - this.hwTail)) {
memcpy(&this.pchBuffer[this.hwTail], (const uint8_t *)pByte, hwLength);
this.hwTail += hwLength;
break;
}
memcpy(&this.pchBuffer[this.hwTail], (const uint8_t *)pByte, this.hwSize - this.hwTail);
memcpy(&this.pchBuffer[0], (const uint8_t *)&pByte + (this.hwSize - this.hwTail), hwLength - (this.hwSize - this.hwTail));
this.hwTail = hwLength - (this.hwSize - this.hwTail);
} while(0);
this.hwLength += hwLength;
this.hwPeekLength += hwLength;
}
return hwLength;
}
bool dequeue_byte(byte_queue_t *ptObj, uint8_t *pchByte)
{
class_internal(ptObj, ptThis, byte_queue_t);
if(pchByte == NULL || ptObj == NULL) {
return false;
}
if(this.hwHead == this.hwTail &&
0 == this.hwLength ) {
return false;
}
queue_protect() {
*pchByte = this.pchBuffer[this.hwHead++];
if(this.hwHead >= this.hwSize) {
this.hwHead = 0;
}
this.hwLength--;
this.hwPeek = this.hwHead;
this.hwPeekLength = this.hwLength;
}
return true;
}
int16_t dequeue_bytes(byte_queue_t *ptObj, void *pByte, uint16_t hwLength)
{
class_internal(ptObj, ptThis, byte_queue_t);
if(pByte == NULL || ptObj == NULL) {
return -1;
}
if(this.hwHead == this.hwTail &&
0 == this.hwLength ) {
return 0;
}
queue_protect() {
if(hwLength > this.hwLength) {
hwLength = this.hwLength;
}
do {
if(hwLength < (this.hwSize - this.hwHead)) {
memcpy(pByte, (const void *)&this.pchBuffer[this.hwHead], hwLength);
this.hwHead += hwLength;
break;
}
memcpy((uint8_t *)pByte, (const void *)&this.pchBuffer[this.hwHead], this.hwSize - this.hwHead);
memcpy((uint8_t *)pByte + (this.hwSize - this.hwHead), (const void *)&this.pchBuffer[0], hwLength - (this.hwSize - this.hwHead));
this.hwHead = hwLength - (this.hwSize - this.hwHead);
} while(0);
this.hwLength -= hwLength;
this.hwPeek = this.hwHead;
this.hwPeekLength = this.hwLength;
}
return hwLength;
}
bool is_byte_queue_empty(byte_queue_t *ptObj)
{
class_internal(ptObj, ptThis, byte_queue_t);
if(ptObj == NULL) {
return false;
}
if(this.hwHead == this.hwTail &&
0 == this.hwLength ) {
return true;
}
return false;
}
int16_t get_queue_count(byte_queue_t *ptObj)
{
class_internal(ptObj, ptThis, byte_queue_t);
if(ptObj == NULL) {
return -1;
}
return (this.hwLength);
}
int16_t get_queue_available_count(byte_queue_t *ptObj)
{
class_internal(ptObj, ptThis, byte_queue_t);
if(ptObj == NULL) {
return -1;
}
return (this.hwSize - this.hwLength);
}
bool is_peek_empty(byte_queue_t *ptObj)
{
class_internal(ptObj, ptThis, byte_queue_t);
if(ptObj == NULL) {
return false;
}
if(this.hwPeek == this.hwTail &&
0 == this.hwPeekLength ) {
return true;
}
return false;
}
bool peek_byte_queue(byte_queue_t *ptObj, uint8_t *pchByte)
{
class_internal(ptObj, ptThis, byte_queue_t);
if(ptObj == NULL || pchByte == NULL) {
return false;
}
if(this.hwPeek == this.hwTail &&
0 == this.hwPeekLength ) {
return false;
}
queue_protect() {
*pchByte = this.pchBuffer[this.hwPeek++];
if(this.hwPeek >= this.hwSize) {
this.hwPeek = 0;
}
this.hwPeekLength--;
}
return true;
}
int16_t peek_bytes_queue(byte_queue_t *ptObj, void *pByte, uint16_t hwLength)
{
class_internal(ptObj, ptThis, byte_queue_t);
if(ptObj == NULL || pByte == NULL) {
return -1;
}
if(this.hwPeek == this.hwTail &&
0 == this.hwPeekLength ) {
return 0;
}
queue_protect() {
if(hwLength > this.hwPeekLength) {
hwLength = this.hwPeekLength;
}
do {
if(hwLength < (this.hwSize - this.hwPeek)) {
memcpy(pByte, (const void *)&this.pchBuffer[this.hwPeek], hwLength);
this.hwPeek += hwLength;
break;
}
memcpy((uint8_t *)pByte, (const void *)&this.pchBuffer[this.hwPeek], this.hwSize - this.hwPeek);
memcpy((uint8_t *)pByte + (this.hwSize - this.hwPeek), (const void *)&this.pchBuffer[0], hwLength - (this.hwSize - this.hwPeek));
this.hwPeek = hwLength - (this.hwSize - this.hwPeek);
} while(0);
this.hwPeekLength -= hwLength;
}
return hwLength;
}
bool reset_peek_byte(byte_queue_t *ptObj)
{
class_internal(ptObj, ptThis, byte_queue_t);
if(ptObj == NULL) {
return false;
}
queue_protect() {
this.hwPeek = this.hwHead;
this.hwPeekLength = this.hwLength;
}
return true;
}
bool get_all_peeked_byte(byte_queue_t *ptObj)
{
class_internal(ptObj, ptThis, byte_queue_t);
if(ptObj == NULL) {
return false;
}
queue_protect() {
this.hwHead = this.hwPeek;
this.hwLength = this.hwPeekLength;
}
return true;
}
uint16_t get_peek_status(byte_queue_t *ptObj)
{
uint16_t hwCount;
class_internal(ptObj, ptThis, byte_queue_t);
if(ptObj == NULL) {
return false;
}
queue_protect() {
if(this.hwPeek >= this.hwHead) {
hwCount = this.hwPeek - this.hwHead;
} else {
hwCount = this.hwSize - this.hwHead + this.hwPeek;
}
}
return hwCount;
}
bool restore_peek_status(byte_queue_t *ptObj, uint16_t hwCount)
{
class_internal(ptObj, ptThis, byte_queue_t);
if(ptObj == NULL) {
return false;
}
queue_protect() {
if(this.hwHead + hwCount < this.hwSize) {
this.hwPeek = this.hwHead + hwCount;
} else {
this.hwPeek = hwCount - (this.hwSize - this.hwHead);
}
this.hwPeekLength = this.hwPeekLength - hwCount;
}
return true;
}
#endif
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。