From 110e1d0aab04ce24a9d09e4aa031f5f9b2dd98f1 Mon Sep 17 00:00:00 2001 From: John Ericson Date: Mon, 10 Nov 2025 01:39:57 -0500 Subject: [PATCH] Move `StringData` to its own header Now that it isn't just used for `Value`, it doesn't really belong in there. Rename `static-string-data.hh` to share the same prefix to keep them close together when sorting lines, also. --- src/libexpr-tests/json.cc | 2 +- src/libexpr-tests/value/print.cc | 2 +- src/libexpr-tests/value/value.cc | 2 +- src/libexpr/include/nix/expr/meson.build | 3 +- src/libexpr/include/nix/expr/nixexpr.hh | 2 +- src/libexpr/include/nix/expr/parser-state.hh | 2 +- ...c-string-data.hh => string-data-static.hh} | 2 +- src/libexpr/include/nix/expr/string-data.hh | 96 +++++++++++++++++++ src/libexpr/include/nix/expr/symbol-table.hh | 2 +- src/libexpr/include/nix/expr/value.hh | 89 +---------------- src/libexpr/primops.cc | 2 +- src/libexpr/primops/context.cc | 2 +- src/libexpr/primops/fetchClosure.cc | 2 +- src/libexpr/primops/fetchTree.cc | 2 +- src/libexpr/primops/fromTOML.cc | 2 +- src/libflake/flake-primops.cc | 2 +- src/nix/nix-env/user-env.cc | 2 +- 17 files changed, 113 insertions(+), 103 deletions(-) rename src/libexpr/include/nix/expr/{static-string-data.hh => string-data-static.hh} (96%) create mode 100644 src/libexpr/include/nix/expr/string-data.hh diff --git a/src/libexpr-tests/json.cc b/src/libexpr-tests/json.cc index aa71c4d86..6b9c829c2 100644 --- a/src/libexpr-tests/json.cc +++ b/src/libexpr-tests/json.cc @@ -1,6 +1,6 @@ #include "nix/expr/tests/libexpr.hh" #include "nix/expr/value-to-json.hh" -#include "nix/expr/static-string-data.hh" +#include "nix/expr/string-data-static.hh" namespace nix { // Testing the conversion to JSON diff --git a/src/libexpr-tests/value/print.cc b/src/libexpr-tests/value/print.cc index 0456835b4..c683cfd97 100644 --- a/src/libexpr-tests/value/print.cc +++ b/src/libexpr-tests/value/print.cc @@ -1,5 +1,5 @@ #include "nix/expr/tests/libexpr.hh" -#include "nix/expr/static-string-data.hh" +#include "nix/expr/string-data-static.hh" #include "nix/expr/value.hh" #include "nix/expr/print.hh" diff --git a/src/libexpr-tests/value/value.cc b/src/libexpr-tests/value/value.cc index 420db0f31..7f614df68 100644 --- a/src/libexpr-tests/value/value.cc +++ b/src/libexpr-tests/value/value.cc @@ -1,5 +1,5 @@ #include "nix/expr/value.hh" -#include "nix/expr/static-string-data.hh" +#include "nix/expr/string-data-static.hh" #include "nix/store/tests/libstore.hh" #include diff --git a/src/libexpr/include/nix/expr/meson.build b/src/libexpr/include/nix/expr/meson.build index 2b0fbc406..8034dc69b 100644 --- a/src/libexpr/include/nix/expr/meson.build +++ b/src/libexpr/include/nix/expr/meson.build @@ -31,7 +31,8 @@ headers = [ config_pub_h ] + files( 'print.hh', 'repl-exit-status.hh', 'search-path.hh', - 'static-string-data.hh', + 'string-data.hh', + 'string-data-static.hh', 'symbol-table.hh', 'value-to-json.hh', 'value-to-xml.hh', diff --git a/src/libexpr/include/nix/expr/nixexpr.hh b/src/libexpr/include/nix/expr/nixexpr.hh index 3f7c883f3..d801de5c2 100644 --- a/src/libexpr/include/nix/expr/nixexpr.hh +++ b/src/libexpr/include/nix/expr/nixexpr.hh @@ -12,7 +12,7 @@ #include "nix/expr/value.hh" #include "nix/expr/symbol-table.hh" #include "nix/expr/eval-error.hh" -#include "nix/expr/static-string-data.hh" +#include "nix/expr/string-data-static.hh" #include "nix/util/pos-idx.hh" #include "nix/expr/counter.hh" #include "nix/util/pos-table.hh" diff --git a/src/libexpr/include/nix/expr/parser-state.hh b/src/libexpr/include/nix/expr/parser-state.hh index 661584ea0..7dd587be8 100644 --- a/src/libexpr/include/nix/expr/parser-state.hh +++ b/src/libexpr/include/nix/expr/parser-state.hh @@ -5,7 +5,7 @@ #include "nix/expr/eval.hh" #include "nix/expr/value.hh" -#include "nix/expr/static-string-data.hh" +#include "nix/expr/string-data-static.hh" namespace nix { diff --git a/src/libexpr/include/nix/expr/static-string-data.hh b/src/libexpr/include/nix/expr/string-data-static.hh similarity index 96% rename from src/libexpr/include/nix/expr/static-string-data.hh rename to src/libexpr/include/nix/expr/string-data-static.hh index 93b5d46a0..d589232c2 100644 --- a/src/libexpr/include/nix/expr/static-string-data.hh +++ b/src/libexpr/include/nix/expr/string-data-static.hh @@ -1,7 +1,7 @@ #pragma once ///@file -#include "nix/expr/value.hh" +#include "nix/expr/string-data.hh" namespace nix { diff --git a/src/libexpr/include/nix/expr/string-data.hh b/src/libexpr/include/nix/expr/string-data.hh new file mode 100644 index 000000000..b967de325 --- /dev/null +++ b/src/libexpr/include/nix/expr/string-data.hh @@ -0,0 +1,96 @@ +#pragma once +///@file + +#include +#include +#include +#include + +namespace nix { + +class StringData +{ +public: + using size_type = std::size_t; + + size_type size_; + char data_[]; + + /* + * This in particular ensures that we cannot have a `StringData` + * that we use by value, which is just what we want! + * + * Dynamically sized types aren't a thing in C++ and even flexible array + * members are a language extension and beyond the realm of standard C++. + * Technically, sizeof data_ member is 0 and the intended way to use flexible + * array members is to allocate sizeof(StrindData) + count * sizeof(char) bytes + * and the compiler will consider alignment restrictions for the FAM. + * + */ + + StringData(StringData &&) = delete; + StringData & operator=(StringData &&) = delete; + StringData(const StringData &) = delete; + StringData & operator=(const StringData &) = delete; + ~StringData() = default; + +private: + StringData() = delete; + + explicit StringData(size_type size) + : size_(size) + { + } + +public: + /** + * Allocate StringData on the (possibly) GC-managed heap and copy + * the contents of s to it. + */ + static const StringData & make(std::string_view s); + + /** + * Allocate StringData on the (possibly) GC-managed heap. + * @param size Length of the string (without the NUL terminator). + */ + static StringData & alloc(size_t size); + + size_t size() const + { + return size_; + } + + char * data() noexcept + { + return data_; + } + + const char * data() const noexcept + { + return data_; + } + + const char * c_str() const noexcept + { + return data_; + } + + constexpr std::string_view view() const noexcept + { + return std::string_view(data_, size_); + } + + template + struct Static; + + static StringData & make(std::pmr::memory_resource & resource, std::string_view s) + { + auto & res = + *new (resource.allocate(sizeof(StringData) + s.size() + 1, alignof(StringData))) StringData(s.size()); + std::memcpy(res.data_, s.data(), s.size()); + res.data_[s.size()] = '\0'; + return res; + } +}; + +} // namespace nix diff --git a/src/libexpr/include/nix/expr/symbol-table.hh b/src/libexpr/include/nix/expr/symbol-table.hh index ccee8bee0..660a90842 100644 --- a/src/libexpr/include/nix/expr/symbol-table.hh +++ b/src/libexpr/include/nix/expr/symbol-table.hh @@ -3,7 +3,7 @@ #include #include "nix/expr/value.hh" -#include "nix/expr/static-string-data.hh" +#include "nix/expr/string-data-static.hh" #include "nix/util/chunked-vector.hh" #include "nix/util/error.hh" diff --git a/src/libexpr/include/nix/expr/value.hh b/src/libexpr/include/nix/expr/value.hh index 54a735fbd..ccec94c0a 100644 --- a/src/libexpr/include/nix/expr/value.hh +++ b/src/libexpr/include/nix/expr/value.hh @@ -3,16 +3,14 @@ #include #include -#include -#include #include -#include #include #include #include #include #include "nix/expr/eval-gc.hh" +#include "nix/expr/string-data.hh" #include "nix/expr/value/context.hh" #include "nix/util/source-path.hh" #include "nix/expr/print-options.hh" @@ -192,91 +190,6 @@ public: friend struct Value; }; -class StringData -{ -public: - using size_type = std::size_t; - - size_type size_; - char data_[]; - - /* - * This in particular ensures that we cannot have a `StringData` - * that we use by value, which is just what we want! - * - * Dynamically sized types aren't a thing in C++ and even flexible array - * members are a language extension and beyond the realm of standard C++. - * Technically, sizeof data_ member is 0 and the intended way to use flexible - * array members is to allocate sizeof(StrindData) + count * sizeof(char) bytes - * and the compiler will consider alignment restrictions for the FAM. - * - */ - - StringData(StringData &&) = delete; - StringData & operator=(StringData &&) = delete; - StringData(const StringData &) = delete; - StringData & operator=(const StringData &) = delete; - ~StringData() = default; - -private: - StringData() = delete; - - explicit StringData(size_type size) - : size_(size) - { - } - -public: - /** - * Allocate StringData on the (possibly) GC-managed heap and copy - * the contents of s to it. - */ - static const StringData & make(std::string_view s); - - /** - * Allocate StringData on the (possibly) GC-managed heap. - * @param size Length of the string (without the NUL terminator). - */ - static StringData & alloc(size_t size); - - size_t size() const - { - return size_; - } - - char * data() noexcept - { - return data_; - } - - const char * data() const noexcept - { - return data_; - } - - const char * c_str() const noexcept - { - return data_; - } - - constexpr std::string_view view() const noexcept - { - return std::string_view(data_, size_); - } - - template - struct Static; - - static StringData & make(std::pmr::memory_resource & resource, std::string_view s) - { - auto & res = - *new (resource.allocate(sizeof(StringData) + s.size() + 1, alignof(StringData))) StringData(s.size()); - std::memcpy(res.data_, s.data(), s.size()); - res.data_[s.size()] = '\0'; - return res; - } -}; - namespace detail { /** diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index d2489478d..e42002153 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -5,7 +5,7 @@ #include "nix/expr/eval-settings.hh" #include "nix/expr/gc-small-vector.hh" #include "nix/expr/json-to-value.hh" -#include "nix/expr/static-string-data.hh" +#include "nix/expr/string-data-static.hh" #include "nix/store/globals.hh" #include "nix/store/names.hh" #include "nix/store/path-references.hh" diff --git a/src/libexpr/primops/context.cc b/src/libexpr/primops/context.cc index 2d3957e2c..0eb96fa74 100644 --- a/src/libexpr/primops/context.cc +++ b/src/libexpr/primops/context.cc @@ -1,6 +1,6 @@ #include "nix/expr/primops.hh" #include "nix/expr/eval-inline.hh" -#include "nix/expr/static-string-data.hh" +#include "nix/expr/string-data-static.hh" #include "nix/store/derivations.hh" #include "nix/store/store-api.hh" #include "nix/store/globals.hh" diff --git a/src/libexpr/primops/fetchClosure.cc b/src/libexpr/primops/fetchClosure.cc index 4e2631f23..912bf4e4e 100644 --- a/src/libexpr/primops/fetchClosure.cc +++ b/src/libexpr/primops/fetchClosure.cc @@ -1,5 +1,5 @@ #include "nix/expr/primops.hh" -#include "nix/expr/static-string-data.hh" +#include "nix/expr/string-data-static.hh" #include "nix/store/store-open.hh" #include "nix/store/realisation.hh" #include "nix/store/make-content-addressed.hh" diff --git a/src/libexpr/primops/fetchTree.cc b/src/libexpr/primops/fetchTree.cc index dbcc5ca77..8e12a232b 100644 --- a/src/libexpr/primops/fetchTree.cc +++ b/src/libexpr/primops/fetchTree.cc @@ -2,7 +2,7 @@ #include "nix/expr/primops.hh" #include "nix/expr/eval-inline.hh" #include "nix/expr/eval-settings.hh" -#include "nix/expr/static-string-data.hh" +#include "nix/expr/string-data-static.hh" #include "nix/store/store-api.hh" #include "nix/fetchers/fetchers.hh" #include "nix/store/filetransfer.hh" diff --git a/src/libexpr/primops/fromTOML.cc b/src/libexpr/primops/fromTOML.cc index 94385ce82..27770065d 100644 --- a/src/libexpr/primops/fromTOML.cc +++ b/src/libexpr/primops/fromTOML.cc @@ -1,6 +1,6 @@ #include "nix/expr/primops.hh" #include "nix/expr/eval-inline.hh" -#include "nix/expr/static-string-data.hh" +#include "nix/expr/string-data-static.hh" #include "expr-config-private.hh" diff --git a/src/libflake/flake-primops.cc b/src/libflake/flake-primops.cc index 36be61564..19d3f604c 100644 --- a/src/libflake/flake-primops.cc +++ b/src/libflake/flake-primops.cc @@ -19,7 +19,7 @@ #include "nix/expr/eval-settings.hh" #include "nix/expr/symbol-table.hh" #include "nix/expr/value.hh" -#include "nix/expr/static-string-data.hh" +#include "nix/expr/string-data-static.hh" #include "nix/fetchers/attrs.hh" #include "nix/fetchers/fetchers.hh" #include "nix/util/configuration.hh" diff --git a/src/nix/nix-env/user-env.cc b/src/nix/nix-env/user-env.cc index 21fdf25bc..4405ce9f2 100644 --- a/src/nix/nix-env/user-env.cc +++ b/src/nix/nix-env/user-env.cc @@ -9,7 +9,7 @@ #include "nix/expr/eval-inline.hh" #include "nix/store/profiles.hh" #include "nix/expr/print-ambiguous.hh" -#include "nix/expr/static-string-data.hh" +#include "nix/expr/string-data-static.hh" #include #include