diff options
Diffstat (limited to '')
-rw-r--r-- | libnetdata/string/Makefile.am | 8 | ||||
-rw-r--r-- | libnetdata/string/README.md | 25 | ||||
-rw-r--r-- | src/libnetdata/string/string.c (renamed from libnetdata/string/string.c) | 23 | ||||
-rw-r--r-- | src/libnetdata/string/string.h (renamed from libnetdata/string/string.h) | 3 | ||||
-rw-r--r-- | src/libnetdata/string/utf8.h (renamed from libnetdata/string/utf8.h) | 0 |
5 files changed, 24 insertions, 35 deletions
diff --git a/libnetdata/string/Makefile.am b/libnetdata/string/Makefile.am deleted file mode 100644 index 161784b8f..000000000 --- a/libnetdata/string/Makefile.am +++ /dev/null @@ -1,8 +0,0 @@ -# SPDX-License-Identifier: GPL-3.0-or-later - -AUTOMAKE_OPTIONS = subdir-objects -MAINTAINERCLEANFILES = $(srcdir)/Makefile.in - -dist_noinst_DATA = \ - README.md \ - $(NULL) diff --git a/libnetdata/string/README.md b/libnetdata/string/README.md deleted file mode 100644 index 4fd442507..000000000 --- a/libnetdata/string/README.md +++ /dev/null @@ -1,25 +0,0 @@ -<!-- -title: "String" -custom_edit_url: https://github.com/netdata/netdata/edit/master/libnetdata/string/README.md -sidebar_label: "String" -learn_status: "Published" -learn_topic_type: "Tasks" -learn_rel_path: "Developers/libnetdata" ---> - -# STRING - -STRING provides a way to allocate and free text strings, while de-duplicating them. - -It can be used similarly to libc string functions: - - - `strdup()` and `strdupz()` become `string_strdupz()`. - - `strlen()` becomes `string_strlen()` (and it does not walkthrough the bytes of the string). - - `free()` and `freez()` become `string_freez()`. - -There is also a special `string_dup()` function that increases the reference counter of a STRING, avoiding the -index lookup to find it. - -Once there is a `STRING *`, the actual `const char *` can be accessed with `string2str()`. - -All STRING should be constant. Changing the contents of a `const char *` that has been acquired by `string2str()` should never happen. diff --git a/libnetdata/string/string.c b/src/libnetdata/string/string.c index e1c8352a5..0b4a6470d 100644 --- a/libnetdata/string/string.c +++ b/src/libnetdata/string/string.c @@ -88,8 +88,8 @@ void string_statistics(size_t *inserts, size_t *deletes, size_t *searches, size_ } } -#define string_entry_acquire(se) __atomic_add_fetch(&((se)->refcount), 1, __ATOMIC_SEQ_CST); -#define string_entry_release(se) __atomic_sub_fetch(&((se)->refcount), 1, __ATOMIC_SEQ_CST); +#define string_entry_acquire(se) __atomic_add_fetch(&((se)->refcount), 1, __ATOMIC_SEQ_CST) +#define string_entry_release(se) __atomic_sub_fetch(&((se)->refcount), 1, __ATOMIC_SEQ_CST) static inline bool string_entry_check_and_acquire(STRING *se) { #ifdef NETDATA_INTERNAL_CHECKS @@ -307,6 +307,25 @@ STRING *string_strdupz(const char *str) { return string; } +STRING *string_strndupz(const char *str, size_t len) { + if(unlikely(!str || !*str || !len)) return NULL; + +#ifdef NETDATA_INTERNAL_CHECKS + uint8_t partition = string_partition_str(str); +#endif + + char buf[len + 1]; + memcpy(buf, str, len); + buf[len] = '\0'; + + STRING *string = string_index_search(buf, len + 1); + while(!string) + string = string_index_insert(buf, len + 1); + + string_stats_atomic_increment(partition, active_references); + return string; +} + void string_freez(STRING *string) { if(unlikely(!string)) return; diff --git a/libnetdata/string/string.h b/src/libnetdata/string/string.h index ba0e3876b..f2ff9666c 100644 --- a/libnetdata/string/string.h +++ b/src/libnetdata/string/string.h @@ -8,7 +8,10 @@ // STRING implementation typedef struct netdata_string STRING; + STRING *string_strdupz(const char *str); +STRING *string_strndupz(const char *str, size_t len); + STRING *string_dup(STRING *string); void string_freez(STRING *string); size_t string_strlen(STRING *string); diff --git a/libnetdata/string/utf8.h b/src/libnetdata/string/utf8.h index 3e6c8c288..3e6c8c288 100644 --- a/libnetdata/string/utf8.h +++ b/src/libnetdata/string/utf8.h |