1. 优化环状数组
This commit is contained in:
parent
c2ed44d4ba
commit
fc00bd72e8
@ -264,6 +264,7 @@ HDRPCValue_t _HDRPCGetValue(HDRPCSession *session, uint8_t type, uint8_t index);
|
||||
* @return 参数
|
||||
*/
|
||||
void *_HDRPCGetData(HDRPCSession *session, uint8_t type, uint8_t index, uint8_t *len);
|
||||
#define HDRPCGetCharData(session, index, len) (char *)_HDRPCGetData(session, kHDRPCBuffer, index, len)
|
||||
#define HDRPCGetData(session, index, len) (uint8_t *)_HDRPCGetData(session, kHDRPCBuffer, index, len)
|
||||
#define HDRPCGetUserData(session, index, len) (uint8_t *)_HDRPCGetData(session, kHDRPCUserData, index, len)
|
||||
#define HDRPCGetString(session, index, len) (uint16_t *)_HDRPCGetData(session, kHDRPCString, index, len)
|
||||
|
||||
@ -14,7 +14,7 @@
|
||||
#endif
|
||||
|
||||
|
||||
static void InitRingBuffer(HRingBufferType *ringBuffer) {
|
||||
static inline void InitRingBuffer(HRingBufferType *ringBuffer) {
|
||||
if (ringBuffer == NULL) {
|
||||
LogD("RingBuffer is NULL");
|
||||
return ;
|
||||
@ -34,7 +34,7 @@ static void InitRingBuffer(HRingBufferType *ringBuffer) {
|
||||
base->end = _HRING_BUFFER_INIT_SIZE_INIT(len);
|
||||
}
|
||||
|
||||
static uint8_t GetRingBufferType(const HRingBufferType *ringBuffer) {
|
||||
static inline uint8_t GetRingBufferType(const HRingBufferType *ringBuffer) {
|
||||
if (ringBuffer == NULL) {
|
||||
return 0;
|
||||
}
|
||||
@ -44,7 +44,7 @@ static uint8_t GetRingBufferType(const HRingBufferType *ringBuffer) {
|
||||
return base->flag & kHRingBufferFlagAllMask;
|
||||
}
|
||||
|
||||
static HRingBufferLenType GetRingBufferTypeSize(const HRingBufferType *ringBuffer) {
|
||||
static inline HRingBufferLenType GetRingBufferTypeSize(const HRingBufferType *ringBuffer) {
|
||||
switch(GetRingBufferType(ringBuffer)) {
|
||||
case kHRingBufferFlag8: return sizeof(uint8_t); break;
|
||||
case kHRingBufferFlag16: return sizeof(uint16_t); break;
|
||||
@ -54,7 +54,7 @@ static HRingBufferLenType GetRingBufferTypeSize(const HRingBufferType *ringBuffe
|
||||
return 0;
|
||||
}
|
||||
|
||||
static HRingBufferDataType GetRingBufferData(const HRingBufferType *ringBuffer, HRingBufferLenType pos) {
|
||||
static inline HRingBufferDataType GetRingBufferData(const HRingBufferType *ringBuffer, HRingBufferLenType pos) {
|
||||
switch(GetRingBufferType(ringBuffer)) {
|
||||
case kHRingBufferFlag8: return ((const _HRingBuffer8 *)ringBuffer)->data[pos]; break;
|
||||
case kHRingBufferFlag16: return ((const _HRingBuffer16 *)ringBuffer)->data[pos]; break;
|
||||
@ -64,7 +64,7 @@ static HRingBufferDataType GetRingBufferData(const HRingBufferType *ringBuffer,
|
||||
return HRING_BUFFER_ERROR;
|
||||
}
|
||||
|
||||
static const uint8_t *GetRingByteData(const HRingBufferType *ringBuffer, HRingBufferLenType pos) {
|
||||
static inline const uint8_t *GetRingByteData(const HRingBufferType *ringBuffer, HRingBufferLenType pos) {
|
||||
const void *result = NULL;
|
||||
switch(GetRingBufferType(ringBuffer)) {
|
||||
case kHRingBufferFlag8: result = &((const _HRingBuffer8 *)ringBuffer)->data[pos]; break;
|
||||
@ -75,7 +75,7 @@ static const uint8_t *GetRingByteData(const HRingBufferType *ringBuffer, HRingBu
|
||||
return (const uint8_t *)result;
|
||||
}
|
||||
|
||||
static void SetRingBufferData(HRingBufferType *ringBuffer, HRingBufferLenType pos, HRingBufferDataType data) {
|
||||
static inline void SetRingBufferData(HRingBufferType *ringBuffer, HRingBufferLenType pos, HRingBufferDataType data) {
|
||||
switch(GetRingBufferType(ringBuffer)) {
|
||||
case kHRingBufferFlag8: ((_HRingBuffer8 *)ringBuffer)->data[pos] = (uint8_t)data; break;
|
||||
case kHRingBufferFlag16: ((_HRingBuffer16 *)ringBuffer)->data[pos] = (uint16_t)data; break;
|
||||
@ -83,7 +83,7 @@ static void SetRingBufferData(HRingBufferType *ringBuffer, HRingBufferLenType po
|
||||
}
|
||||
}
|
||||
|
||||
static uint8_t IsRingBufferFull(const HRingBufferType *ringBuffer) {
|
||||
static inline uint8_t IsRingBufferFull(const HRingBufferType *ringBuffer) {
|
||||
if (ringBuffer == NULL) {
|
||||
return 1;
|
||||
}
|
||||
@ -93,7 +93,7 @@ static uint8_t IsRingBufferFull(const HRingBufferType *ringBuffer) {
|
||||
return (base->flag & kHRingBufferFull) == kHRingBufferFull;
|
||||
}
|
||||
|
||||
static void SetRingBufferFull(HRingBufferType *ringBuffer, uint8_t full) {
|
||||
static inline void SetRingBufferFull(HRingBufferType *ringBuffer, uint8_t full) {
|
||||
InitRingBuffer((HRingBufferType *)ringBuffer);
|
||||
_HRingBufferBase *base = (_HRingBufferBase *)ringBuffer;
|
||||
if (full) {
|
||||
@ -106,7 +106,8 @@ static void SetRingBufferFull(HRingBufferType *ringBuffer, uint8_t full) {
|
||||
static void AdjustReadPos(HRingBufferType *ringBuffer, HRingBufferLenType pos) {
|
||||
const uint8_t isFull = IsRingBufferFull(ringBuffer);
|
||||
_HRingBufferBase *base = (_HRingBufferBase *)ringBuffer;
|
||||
base->start = (base->start + pos) % base->len;
|
||||
base->start += pos;
|
||||
base->start -= (base->start >= base->len) * base->len;
|
||||
if (isFull) {
|
||||
SetRingBufferFull(ringBuffer, 0);
|
||||
}
|
||||
@ -144,7 +145,9 @@ uint8_t HRingBufferAddData(HRingBufferType* buffer, HRingBufferDataType data) {
|
||||
}
|
||||
|
||||
SetRingBufferData(buffer, base->end, data);
|
||||
base->end = (base->end + 1) % base->len;
|
||||
++base->end;
|
||||
base->end -= (base->end >= base->len) * base->len;
|
||||
|
||||
if (base->end == base->start) {
|
||||
SetRingBufferFull(buffer, 1);
|
||||
}
|
||||
@ -156,7 +159,8 @@ uint8_t HRingBufferAddData(HRingBufferType* buffer, HRingBufferDataType data) {
|
||||
uint8_t HRingBufferAddDataOver(HRingBufferType* buffer, HRingBufferDataType data) {
|
||||
if (IsRingBufferFull(buffer)) {
|
||||
_HRingBufferBase *base = (_HRingBufferBase *)buffer;
|
||||
base->start = (base->start + 1) % base->len;
|
||||
++base->start;
|
||||
base->start -= (base->start >= base->len) * base->len;
|
||||
SetRingBufferFull(buffer, 0);
|
||||
}
|
||||
|
||||
@ -186,7 +190,8 @@ HRingBufferDataType HRingBufferPopData(HRingBufferType* buffer) {
|
||||
|
||||
_HRingBufferBase *base = (_HRingBufferBase *)buffer;
|
||||
HRingBufferDataType data = GetRingBufferData(buffer, base->start);
|
||||
base->start = (base->start + 1) % base->len;
|
||||
++base->start;
|
||||
base->start -= (base->start >= base->len) * base->len;
|
||||
if (IsRingBufferFull(buffer)) {
|
||||
SetRingBufferFull(buffer, 0);
|
||||
}
|
||||
|
||||
@ -186,6 +186,10 @@ HTimer_t HTimerAdd(uint8_t id, uint32_t ms, HTimerCallType call, eHTimerFlags fl
|
||||
}
|
||||
|
||||
void HTimerRemove(HTimer_t index) {
|
||||
if (index == HTIMER_INVALID) {
|
||||
return ;
|
||||
}
|
||||
|
||||
const uint8_t id = GET_ID(index);
|
||||
index = GET_INDEX(index);
|
||||
if (id >= HTIMER_REGISTER_MAX) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user