// // // #include #include #include #include #include #include #include "sds.h" #include "sdsalloc.h" //static int SDS_HDR5_SIZE = SDS_HDR(sdshdr5); int sds_hdr_size(char type) { switch (type & SDS_TYPE_MASK) { case SDS_TYPE_5: return sizeof(struct sdshdr5); case SDS_TYPE_8: return sizeof(struct sdshdr8); case SDS_TYPE_16: return sizeof(struct sdshdr16); case SDS_TYPE_32: return sizeof(struct sdshdr32); case SDS_TYPE_64: return sizeof(struct sdshdr64); } return 0; } //static char sds_req_type(size_t string_size) { // if (string_size < 1 << 5) // return SDS_TYPE_5; // if (string_size < 1 << 8) // return SDS_TYPE_8; // if (string_size < 1 << 16) // return SDS_TYPE_16; //#if (LONG_MAX == LLONG_MAX) // if (string_size < 1ll << 32) // return SDS_TYPE_32; //#endif // return SDS_TYPE_64; //} size_t sds_getlen(sds s) { unsigned char flags = s[-1]; switch (flags & SDS_TYPE_MASK) { case SDS_TYPE_5: return SDS_TYPE_5_LEN(flags); case SDS_TYPE_8: return SDS_HDR(8, s)->len; case SDS_TYPE_16: return SDS_HDR(16, s)->len; case SDS_TYPE_32: return SDS_HDR(32, s)->len; case SDS_TYPE_64: return SDS_HDR(64, s)->len; } return 0; } size_t sds_avail(const sds s) { unsigned char flags = s[-1]; switch (flags & SDS_TYPE_MASK) { case SDS_TYPE_5: { return 0; } case SDS_TYPE_8: { SDS_HDR_VAR(8, s); return sh->alloc - sh->len; } case SDS_TYPE_16: { SDS_HDR_VAR(16, s); return sh->alloc - sh->len; } case SDS_TYPE_32: { SDS_HDR_VAR(32, s); return sh->alloc - sh->len; } case SDS_TYPE_64: { SDS_HDR_VAR(64, s); return sh->alloc - sh->len; } } return 0; } int sds_type(const sds s) { unsigned char flags = s[-1]; return flags & SDS_TYPE_MASK; } int sds_get_hdr_size(const sds s) { unsigned char flags = s[-1]; switch (flags & SDS_TYPE_MASK) { case SDS_TYPE_5: return sizeof(struct sdshdr5); case SDS_TYPE_8: return sizeof(struct sdshdr8); case SDS_TYPE_16: return sizeof(struct sdshdr16); case SDS_TYPE_32: return sizeof(struct sdshdr32); case SDS_TYPE_64: return sizeof(struct sdshdr64); } return 0; } /* sdsalloc() = sdsavail() + sdslen() */ size_t sds_alloc(const sds s) { unsigned char flags = s[-1]; switch (flags & SDS_TYPE_MASK) { case SDS_TYPE_5: return SDS_TYPE_5_LEN(flags); case SDS_TYPE_8: return SDS_HDR(8, s)->alloc; case SDS_TYPE_16: return SDS_HDR(16, s)->alloc; case SDS_TYPE_32: return SDS_HDR(32, s)->alloc; case SDS_TYPE_64: return SDS_HDR(64, s)->alloc; } return 0; }