1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 3 /* CMetrics 4 * ======== 5 * Copyright 2021 Eduardo Silva <eduardo@calyptia.com> 6 * 7 * Licensed under the Apache License, Version 2.0 (the "License"); 8 * you may not use this file except in compliance with the License. 9 * You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, software 14 * distributed under the License is distributed on an "AS IS" BASIS, 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 * See the License for the specific language governing permissions and 17 * limitations under the License. 18 */ 19 20 #ifndef CMT_SDS_H 21 #define CMT_SDS_H 22 23 /* 24 * This interface is a minimized version of Fluent Bit SDS just for easily 25 * string storage 26 */ 27 28 #include <stdlib.h> 29 #include <string.h> 30 #include <inttypes.h> 31 32 #include <cmetrics/cmt_log.h> 33 34 #define CMT_SDS_HEADER_SIZE (sizeof(uint64_t) + sizeof(uint64_t)) 35 36 typedef char *cmt_sds_t; 37 38 #pragma pack(push, 1) 39 struct cmt_sds { 40 uint64_t len; /* used */ 41 uint64_t alloc; /* excluding the header and null terminator */ 42 char buf[]; 43 }; 44 #pragma pack(pop) 45 46 #define CMT_SDS_HEADER(s) ((struct cmt_sds *) (s - CMT_SDS_HEADER_SIZE)) 47 48 size_t cmt_sds_avail(cmt_sds_t s); 49 cmt_sds_t sds_alloc(size_t size); 50 size_t cmt_sds_alloc(cmt_sds_t s); 51 cmt_sds_t cmt_sds_increase(cmt_sds_t s, size_t len); 52 size_t cmt_sds_len(cmt_sds_t s); 53 cmt_sds_t cmt_sds_create_len(const char *str, int len); 54 cmt_sds_t cmt_sds_create(const char *str); 55 void cmt_sds_destroy(cmt_sds_t s); 56 cmt_sds_t cmt_sds_cat(cmt_sds_t s, const char *str, int len); 57 cmt_sds_t cmt_sds_create_size(size_t size); 58 void cmt_sds_set_len(cmt_sds_t s, size_t len); 59 void cmt_sds_cat_safe(cmt_sds_t *buf, const char *str, int len); 60 61 #endif