1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-14 14:32:42 +01:00

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.
This commit is contained in:
John Ericson 2025-11-10 01:39:57 -05:00
parent b8a9862f80
commit 110e1d0aab
17 changed files with 113 additions and 103 deletions

View file

@ -1,6 +1,6 @@
#include "nix/expr/tests/libexpr.hh" #include "nix/expr/tests/libexpr.hh"
#include "nix/expr/value-to-json.hh" #include "nix/expr/value-to-json.hh"
#include "nix/expr/static-string-data.hh" #include "nix/expr/string-data-static.hh"
namespace nix { namespace nix {
// Testing the conversion to JSON // Testing the conversion to JSON

View file

@ -1,5 +1,5 @@
#include "nix/expr/tests/libexpr.hh" #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/value.hh"
#include "nix/expr/print.hh" #include "nix/expr/print.hh"

View file

@ -1,5 +1,5 @@
#include "nix/expr/value.hh" #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 "nix/store/tests/libstore.hh"
#include <gtest/gtest.h> #include <gtest/gtest.h>

View file

@ -31,7 +31,8 @@ headers = [ config_pub_h ] + files(
'print.hh', 'print.hh',
'repl-exit-status.hh', 'repl-exit-status.hh',
'search-path.hh', 'search-path.hh',
'static-string-data.hh', 'string-data.hh',
'string-data-static.hh',
'symbol-table.hh', 'symbol-table.hh',
'value-to-json.hh', 'value-to-json.hh',
'value-to-xml.hh', 'value-to-xml.hh',

View file

@ -12,7 +12,7 @@
#include "nix/expr/value.hh" #include "nix/expr/value.hh"
#include "nix/expr/symbol-table.hh" #include "nix/expr/symbol-table.hh"
#include "nix/expr/eval-error.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/util/pos-idx.hh"
#include "nix/expr/counter.hh" #include "nix/expr/counter.hh"
#include "nix/util/pos-table.hh" #include "nix/util/pos-table.hh"

View file

@ -5,7 +5,7 @@
#include "nix/expr/eval.hh" #include "nix/expr/eval.hh"
#include "nix/expr/value.hh" #include "nix/expr/value.hh"
#include "nix/expr/static-string-data.hh" #include "nix/expr/string-data-static.hh"
namespace nix { namespace nix {

View file

@ -1,7 +1,7 @@
#pragma once #pragma once
///@file ///@file
#include "nix/expr/value.hh" #include "nix/expr/string-data.hh"
namespace nix { namespace nix {

View file

@ -0,0 +1,96 @@
#pragma once
///@file
#include <cstddef>
#include <cstring>
#include <memory_resource>
#include <string_view>
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<size_t N>
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

View file

@ -3,7 +3,7 @@
#include <memory_resource> #include <memory_resource>
#include "nix/expr/value.hh" #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/chunked-vector.hh"
#include "nix/util/error.hh" #include "nix/util/error.hh"

View file

@ -3,16 +3,14 @@
#include <bit> #include <bit>
#include <cassert> #include <cassert>
#include <cstddef>
#include <cstring>
#include <memory> #include <memory>
#include <memory_resource>
#include <span> #include <span>
#include <string_view> #include <string_view>
#include <type_traits> #include <type_traits>
#include <concepts> #include <concepts>
#include "nix/expr/eval-gc.hh" #include "nix/expr/eval-gc.hh"
#include "nix/expr/string-data.hh"
#include "nix/expr/value/context.hh" #include "nix/expr/value/context.hh"
#include "nix/util/source-path.hh" #include "nix/util/source-path.hh"
#include "nix/expr/print-options.hh" #include "nix/expr/print-options.hh"
@ -192,91 +190,6 @@ public:
friend struct Value; 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<size_t N>
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 { namespace detail {
/** /**

View file

@ -5,7 +5,7 @@
#include "nix/expr/eval-settings.hh" #include "nix/expr/eval-settings.hh"
#include "nix/expr/gc-small-vector.hh" #include "nix/expr/gc-small-vector.hh"
#include "nix/expr/json-to-value.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/globals.hh"
#include "nix/store/names.hh" #include "nix/store/names.hh"
#include "nix/store/path-references.hh" #include "nix/store/path-references.hh"

View file

@ -1,6 +1,6 @@
#include "nix/expr/primops.hh" #include "nix/expr/primops.hh"
#include "nix/expr/eval-inline.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/derivations.hh"
#include "nix/store/store-api.hh" #include "nix/store/store-api.hh"
#include "nix/store/globals.hh" #include "nix/store/globals.hh"

View file

@ -1,5 +1,5 @@
#include "nix/expr/primops.hh" #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/store-open.hh"
#include "nix/store/realisation.hh" #include "nix/store/realisation.hh"
#include "nix/store/make-content-addressed.hh" #include "nix/store/make-content-addressed.hh"

View file

@ -2,7 +2,7 @@
#include "nix/expr/primops.hh" #include "nix/expr/primops.hh"
#include "nix/expr/eval-inline.hh" #include "nix/expr/eval-inline.hh"
#include "nix/expr/eval-settings.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/store/store-api.hh"
#include "nix/fetchers/fetchers.hh" #include "nix/fetchers/fetchers.hh"
#include "nix/store/filetransfer.hh" #include "nix/store/filetransfer.hh"

View file

@ -1,6 +1,6 @@
#include "nix/expr/primops.hh" #include "nix/expr/primops.hh"
#include "nix/expr/eval-inline.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" #include "expr-config-private.hh"

View file

@ -19,7 +19,7 @@
#include "nix/expr/eval-settings.hh" #include "nix/expr/eval-settings.hh"
#include "nix/expr/symbol-table.hh" #include "nix/expr/symbol-table.hh"
#include "nix/expr/value.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/attrs.hh"
#include "nix/fetchers/fetchers.hh" #include "nix/fetchers/fetchers.hh"
#include "nix/util/configuration.hh" #include "nix/util/configuration.hh"

View file

@ -9,7 +9,7 @@
#include "nix/expr/eval-inline.hh" #include "nix/expr/eval-inline.hh"
#include "nix/store/profiles.hh" #include "nix/store/profiles.hh"
#include "nix/expr/print-ambiguous.hh" #include "nix/expr/print-ambiguous.hh"
#include "nix/expr/static-string-data.hh" #include "nix/expr/string-data-static.hh"
#include <limits> #include <limits>
#include <sstream> #include <sstream>