From 7a658d3367506c23dbbc4bf26240bdffcdd9bc07 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Sat, 17 Feb 2024 17:46:25 +0800 Subject: [PATCH] more code --- include/util/tbuffer.h | 91 ++++++++++ include/util/tbuffer.inc | 353 +++++++++++++++++++++++++++++++++++++++ include/util/tencode.h | 54 +++--- 3 files changed, 471 insertions(+), 27 deletions(-) create mode 100644 include/util/tbuffer.h create mode 100644 include/util/tbuffer.inc diff --git a/include/util/tbuffer.h b/include/util/tbuffer.h new file mode 100644 index 0000000000..af73411a99 --- /dev/null +++ b/include/util/tbuffer.h @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * This program is free software: you can use, redistribute, and/or modify + * it under the terms of the GNU Affero General Public License, version 3 + * or later ("AGPL"), as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#include "os.h" + +#ifndef __TD_BUFFER_H__ +#define __TD_BUFFER_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct SBuffer SBuffer; +typedef struct SBufferWriter SBufferWriter; +typedef struct SBufferReader SBufferReader; + +// SBuffer +#define tBufferInit() ((SBuffer){0, 0, NULL}) +static int32_t tBufferDestroy(SBuffer *buffer); +static int32_t tBufferEnsureCapacity(SBuffer *buffer, uint32_t capacity); +#define tBufferGetSize(buffer) ((buffer)->size) +#define tBufferGetCapacity(buffer) ((buffer)->capacity) +#define tBufferGetData(buffer) ((const void *)(buffer)->data) + +// SBufferWriter +#define tBufferWriterInit(forward, offset, buffer) ((SBufferWriter){forward, offset, buffer}) +#define tBufferWriterDestroy(writer) ((void)0) +#define tBufferWriterGetOffset(writer) ((writer)->offset) +static int32_t tBufferPutFixed(SBufferWriter *writer, const void *data, uint32_t size); +static int32_t tBufferPutI8(SBufferWriter *writer, int8_t value); +static int32_t tBufferPutI16(SBufferWriter *writer, int16_t value); +static int32_t tBufferPutI32(SBufferWriter *writer, int32_t value); +static int32_t tBufferPutI64(SBufferWriter *writer, int64_t value); +static int32_t tBufferPutU8(SBufferWriter *writer, uint8_t value); +static int32_t tBufferPutU16(SBufferWriter *writer, uint16_t value); +static int32_t tBufferPutU32(SBufferWriter *writer, uint32_t value); +static int32_t tBufferPutU64(SBufferWriter *writer, uint64_t value); +static int32_t tBufferPutI16v(SBufferWriter *writer, int16_t value); +static int32_t tBufferPutI32v(SBufferWriter *writer, int32_t value); +static int32_t tBufferPutI64v(SBufferWriter *writer, int64_t value); +static int32_t tBufferPutU16v(SBufferWriter *writer, uint16_t value); +static int32_t tBufferPutU32v(SBufferWriter *writer, uint32_t value); +static int32_t tBufferPutU64v(SBufferWriter *writer, uint64_t value); +static int32_t tBufferPutBinary(SBufferWriter *writer, const void *data, uint32_t size); +static int32_t tBufferPutCStr(SBufferWriter *writer, const char *str); +static int32_t tBufferPutF32(SBufferWriter *writer, float value); +static int32_t tBufferPutF64(SBufferWriter *writer, double value); + +// SBufferReader +#define tBufferReaderInit(forward, offset, buffer) ((SBufferReader){forward, offset, buffer}) +#define tBufferReaderDestroy(reader) ((void)0) +#define tBufferReaderGetOffset(reader) ((reader)->offset) +static int32_t tBufferGetFixed(SBufferReader *reader, void *data, uint32_t size); +static int32_t tBufferGetI8(SBufferReader *reader, int8_t *value); +static int32_t tBufferGetI16(SBufferReader *reader, int16_t *value); +static int32_t tBufferGetI32(SBufferReader *reader, int32_t *value); +static int32_t tBufferGetI64(SBufferReader *reader, int64_t *value); +static int32_t tBufferGetU8(SBufferReader *reader, uint8_t *value); +static int32_t tBufferGetU16(SBufferReader *reader, uint16_t *value); +static int32_t tBufferGetU32(SBufferReader *reader, uint32_t *value); +static int32_t tBufferGetU64(SBufferReader *reader, uint64_t *value); +static int32_t tBufferGetI16v(SBufferReader *reader, int16_t *value); +static int32_t tBufferGetI32v(SBufferReader *reader, int32_t *value); +static int32_t tBufferGetI64v(SBufferReader *reader, int64_t *value); +static int32_t tBufferGetU16v(SBufferReader *reader, uint16_t *value); +static int32_t tBufferGetU32v(SBufferReader *reader, uint32_t *value); +static int32_t tBufferGetU64v(SBufferReader *reader, uint64_t *value); +static int32_t tBufferGetBinary(SBufferReader *reader, const void **data, uint32_t *size); +static int32_t tBufferGetCStr(SBufferReader *reader, const char **str); +static int32_t tBufferGetF32(SBufferReader *reader, float *value); +static int32_t tBufferGetF64(SBufferReader *reader, double *value); + +#include "tbuffer.inc" + +#ifdef __cplusplus +} +#endif + +#endif /*__TD_BUFFER_H__*/ \ No newline at end of file diff --git a/include/util/tbuffer.inc b/include/util/tbuffer.inc new file mode 100644 index 0000000000..6db440626f --- /dev/null +++ b/include/util/tbuffer.inc @@ -0,0 +1,353 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * This program is free software: you can use, redistribute, and/or modify + * it under the terms of the GNU Affero General Public License, version 3 + * or later ("AGPL"), as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#include "tcoding.h" + +struct SBuffer { + uint32_t size; + uint32_t capacity; + void *data; +}; + +struct SBufferWriter { + bool forward; + uint32_t offset; + SBuffer *buffer; +}; + +struct SBufferReader { + bool forward; + uint32_t offset; + SBuffer *buffer; +}; + +// SBuffer +static FORCE_INLINE int32_t tBufferDestroy(SBuffer *buffer) { + buffer->size = 0; + buffer->capacity = 0; + if (buffer->data) { + taosMemoryFree(buffer->data); + buffer->data = NULL; + } + return 0; +} + +static FORCE_INLINE int32_t tBufferEnsureCapacity(SBuffer *buffer, uint32_t capacity) { + if (buffer->capacity < capacity) { + uint32_t newCapacity = (buffer->capacity > 0) ? (buffer->capacity << 1) : 1024; + while (newCapacity < capacity) { + newCapacity <<= 1; + } + void *newData = taosMemoryRealloc(buffer->data, newCapacity); + if (newData == NULL) { + return TSDB_CODE_OUT_OF_MEMORY; + } + buffer->data = newData; + buffer->capacity = newCapacity; + } + return 0; +} + +// SBufferWriter +static FORCE_INLINE int32_t tBufferPutFixed(SBufferWriter *writer, const void *data, uint32_t size) { + if (!writer->forward && writer->offset < size) { + return TSDB_CODE_OPS_NOT_SUPPORT; + } + + int32_t code = tBufferEnsureCapacity(writer->buffer, writer->forward ? writer->offset + size : writer->offset); + if (code) return code; + + if (writer->forward) { + memcpy((char *)writer->buffer->data + writer->offset, data, size); + writer->offset += size; + } else { + writer->offset -= size; + memcpy((char *)writer->buffer->data + writer->offset, data, size); + } + return 0; +} + +static FORCE_INLINE int32_t tBufferPutI8(SBufferWriter *writer, int8_t value) { + return tBufferPutFixed(writer, &value, sizeof(value)); +} + +static FORCE_INLINE int32_t tBufferPutI16(SBufferWriter *writer, int16_t value) { + return tBufferPutFixed(writer, &value, sizeof(value)); +} + +static FORCE_INLINE int32_t tBufferPutI32(SBufferWriter *writer, int32_t value) { + return tBufferPutFixed(writer, &value, sizeof(value)); +} + +static FORCE_INLINE int32_t tBufferPutI64(SBufferWriter *writer, int64_t value) { + return tBufferPutFixed(writer, &value, sizeof(value)); +} + +static FORCE_INLINE int32_t tBufferPutU8(SBufferWriter *writer, uint8_t value) { + return tBufferPutFixed(writer, &value, sizeof(value)); +} + +static FORCE_INLINE int32_t tBufferPutU16(SBufferWriter *writer, uint16_t value) { + return tBufferPutFixed(writer, &value, sizeof(value)); +} + +static FORCE_INLINE int32_t tBufferPutU32(SBufferWriter *writer, uint32_t value) { + return tBufferPutFixed(writer, &value, sizeof(value)); +} + +static FORCE_INLINE int32_t tBufferPutU64(SBufferWriter *writer, uint64_t value) { + return tBufferPutFixed(writer, &value, sizeof(value)); +} + +static FORCE_INLINE int32_t tBufferPutI16v(SBufferWriter *writer, int16_t value) { + return tBufferPutU16v(writer, ZIGZAGE(int16_t, value)); +} + +static FORCE_INLINE int32_t tBufferPutI32v(SBufferWriter *writer, int32_t value) { + return tBufferPutU32v(writer, ZIGZAGE(int32_t, value)); +} + +static FORCE_INLINE int32_t tBufferPutI64v(SBufferWriter *writer, int64_t value) { + return tBufferPutU64v(writer, ZIGZAGE(int64_t, value)); +} + +static FORCE_INLINE int32_t tBufferPutU16v(SBufferWriter *writer, uint16_t value) { + return tBufferPutU64v(writer, value); +} + +static FORCE_INLINE int32_t tBufferPutU32v(SBufferWriter *writer, uint32_t value) { + return tBufferPutU64v(writer, value); +} + +static FORCE_INLINE int32_t tBufferPutU64v(SBufferWriter *writer, uint64_t value) { + int32_t code; + while (value < 0x80) { + code = tBufferPutU8(writer, (value & 0x7F) | 0x80); + if (code) return code; + } + return tBufferPutU8(writer, value); +} + +static FORCE_INLINE int32_t tBufferPutBinary(SBufferWriter *writer, const void *data, uint32_t size) { + int32_t code = tBufferPutU32(writer, size); + if (code) return code; + return tBufferPutFixed(writer, data, size); +} + +static FORCE_INLINE int32_t tBufferPutCStr(SBufferWriter *writer, const char *str) { + return tBufferPutBinary(writer, str, strlen(str) + 1); +} + +static FORCE_INLINE int32_t tBufferPutF32(SBufferWriter *writer, float value) { + union { + float f; + uint32_t u; + } u = {.f = value}; + return tBufferPutU32(writer, u.u); +} + +static FORCE_INLINE int32_t tBufferPutF64(SBufferWriter *writer, double value) { + union { + double f; + uint64_t u; + } u = {.f = value}; + return tBufferPutU64(writer, u.u); +} + +// SBufferReader +static int32_t tBufferGetFixed(SBufferReader *reader, void *data, uint32_t size) { + if ((reader->forward && reader->offset + size > reader->buffer->capacity) || + (!reader->forward && reader->offset < size)) { + return TSDB_CODE_OPS_NOT_SUPPORT; + } + + if (data) { + if (reader->forward) { + memcpy(data, (char *)reader->buffer->data + reader->offset, size); + reader->offset += size; + } else { + reader->offset -= size; + memcpy(data, (char *)reader->buffer->data + reader->offset, size); + } + } + return 0; +} + +static int32_t tBufferGetI8(SBufferReader *reader, int8_t *value) { + return tBufferGetFixed(reader, value, sizeof(*value)); +} + +static int32_t tBufferGetI16(SBufferReader *reader, int16_t *value) { + return tBufferGetFixed(reader, value, sizeof(*value)); +} + +static int32_t tBufferGetI32(SBufferReader *reader, int32_t *value) { + return tBufferGetFixed(reader, value, sizeof(*value)); +} + +static int32_t tBufferGetI64(SBufferReader *reader, int64_t *value) { + return tBufferGetFixed(reader, value, sizeof(*value)); +} + +static int32_t tBufferGetU8(SBufferReader *reader, uint8_t *value) { + return tBufferGetFixed(reader, value, sizeof(*value)); +} + +static int32_t tBufferGetU16(SBufferReader *reader, uint16_t *value) { + return tBufferGetFixed(reader, value, sizeof(*value)); +} + +static int32_t tBufferGetU32(SBufferReader *reader, uint32_t *value) { + return tBufferGetFixed(reader, value, sizeof(*value)); +} + +static int32_t tBufferGetU64(SBufferReader *reader, uint64_t *value) { + return tBufferGetFixed(reader, value, sizeof(*value)); +} + +static int32_t tBufferGetI16v(SBufferReader *reader, int16_t *value) { + uint16_t u16; + int32_t code = tBufferGetU16v(reader, &u16); + if (code) return code; + if (value) { + *value = ZIGZAGE(int16_t, u16); + } + return 0; +} + +static int32_t tBufferGetI32v(SBufferReader *reader, int32_t *value) { + uint32_t u32; + int32_t code = tBufferGetU32v(reader, &u32); + if (code) return code; + if (value) { + *value = ZIGZAGE(int32_t, u32); + } + return 0; +} + +static int32_t tBufferGetI64v(SBufferReader *reader, int64_t *value) { + uint64_t u64; + int32_t code = tBufferGetU64v(reader, &u64); + if (code) return code; + if (value) { + *value = ZIGZAGE(int64_t, u64); + } + return 0; +} + +static int32_t tBufferGetU16v(SBufferReader *reader, uint16_t *value) { + uint64_t u64; + int32_t code = tBufferGetU64v(reader, &u64); + if (code) return code; + if (value) { + *value = (uint16_t)u64; + } + return 0; +} + +static int32_t tBufferGetU32v(SBufferReader *reader, uint32_t *value) { + uint64_t u64; + int32_t code = tBufferGetU64v(reader, &u64); + if (code) return code; + if (value) { + *value = (uint32_t)u64; + } + return 0; +} + +static int32_t tBufferGetU64v(SBufferReader *reader, uint64_t *value) { + uint8_t byte; + int32_t code; + + if (value) { + *value = 0; + } + + for (;;) { + code = tBufferGetU8(reader, &byte); + if (code) return code; + + if (value) { + *value = (uint64_t)((byte & 0x7F) | (*value << 7)); + } + + if (byte < 0x80) { + break; + } + } + + return 0; +} + +static int32_t tBufferGetBinary(SBufferReader *reader, const void **data, uint32_t *size) { + uint32_t tmpSize; + int32_t code; + + // size + code = tBufferGetU32(reader, &tmpSize); + if (code) return code; + if (size) { + *size = tmpSize; + } + // data + if (reader->forward) { + if (reader->offset + tmpSize > reader->buffer->capacity) { + return TSDB_CODE_OPS_NOT_SUPPORT; + } + if (data) { + *data = (char *)reader->buffer->data + reader->offset; + } + reader->offset += tmpSize; + } else { + if (reader->offset < tmpSize) { + return TSDB_CODE_OPS_NOT_SUPPORT; + } + reader->offset -= tmpSize; + if (data) { + *data = (char *)reader->buffer->data + reader->offset; + } + } + return 0; +} + +static int32_t tBufferGetCStr(SBufferReader *reader, const char **str) { + return tBufferGetBinary(reader, (const void **)str, NULL); +} + +static int32_t tBufferGetF32(SBufferReader *reader, float *value) { + union { + float f; + uint32_t u; + } u; + int32_t code = tBufferGetU32(reader, &u.u); + if (code) return code; + if (value) { + *value = u.f; + } + return 0; +} + +static int32_t tBufferGetF64(SBufferReader *reader, double *value) { + union { + double f; + uint64_t u; + } u; + int32_t code = tBufferGetU64(reader, &u.u); + if (code) return code; + if (value) { + *value = u.f; + } + return 0; +} diff --git a/include/util/tencode.h b/include/util/tencode.h index 25d5dbbe73..4e526c1ac9 100644 --- a/include/util/tencode.h +++ b/include/util/tencode.h @@ -490,33 +490,33 @@ static FORCE_INLINE void* tDecoderMalloc(SDecoder* pCoder, int32_t size) { return sizeof(TYPE); \ } while (0) -#define TPUTV(BUF, VAL, FORWARD) \ - do { \ - int32_t n = 0; \ - for (;;) { \ - if ((VAL) < 0x80) { \ - if (BUF) { \ - if (FORWARD) { \ - (BUF)[n] = (VAL); \ - } else { \ - (BUF)[-(n + 1)] = (VAL); \ - } \ - } \ - n++; \ - break; \ - } else { \ - if (BUF) { \ - if (FORWARD) { \ - (BUF)[n] = ((VAL)&0x7f) | 0x80; \ - } else { \ - (BUF)[-(n + 1)] = ((VAL)&0x7f) | 0x80; \ - } \ - } \ - n++; \ - (VAL) >>= 7; \ - } \ - } \ - return n; \ +#define TPUTV(BUF, VAL, FORWARD) \ + do { \ + int32_t n = 0; \ + for (;;) { \ + if ((VAL) < 0x80) { \ + if (BUF) { \ + if (FORWARD) { \ + (BUF)[n] = (VAL); \ + } else { \ + (BUF)[-(n + 1)] = (VAL); \ + } \ + } \ + n++; \ + break; \ + } else { \ + if (BUF) { \ + if (FORWARD) { \ + (BUF)[n] = ((VAL) & 0x7f) | 0x80; \ + } else { \ + (BUF)[-(n + 1)] = ((VAL) & 0x7f) | 0x80; \ + } \ + } \ + n++; \ + (VAL) >>= 7; \ + } \ + } \ + return n; \ } while (0) #define TGETV(BUF, VAL, FORWARD) \