aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/actors/util/rc_buf_backend.h
blob: f7089a302ca075f4a86ebf1e1877af44728bf0cf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#pragma once

#include <atomic>

#include <library/cpp/deprecated/atomic/atomic.h>

#include <util/system/types.h>
#include <util/system/compiler.h>
#include <util/generic/array_ref.h>
#include <util/system/sys_alloc.h>

namespace NDetail {

struct TRcBufInternalBackend {
public:
    struct TCookies {
        using TSelf = TCookies;
        std::atomic<const char*> Begin;
        std::atomic<const char*> End;

        static size_t BytesToAligned(size_t size) {
            bool misaligned = size % alignof(TSelf);
            return misaligned ? alignof(TSelf) - size % alignof(TSelf) : 0;
        }

        static size_t BytesToAlloc(size_t size) {
            return size + BytesToAligned(size) + sizeof(TSelf);
        }
    };
private:
    // to be binary compatible with TSharedData
    struct THeader : public TCookies {
        TAtomic RefCount;
        ui64 Zero = 0;
    };

    enum : size_t {
        HeaderSize = sizeof(THeader),
        OverheadSize = HeaderSize,
        MaxDataSize = (std::numeric_limits<size_t>::max() - OverheadSize)
    };

public:
    TRcBufInternalBackend() noexcept
        : Data_(nullptr)
        , Size_(0)
    { }

    ~TRcBufInternalBackend() noexcept {
        Release();
    }

    TRcBufInternalBackend(const TRcBufInternalBackend& other) noexcept
        : Data_(other.Data_)
        , Size_(other.Size_)
    {
        AddRef();
    }

    TRcBufInternalBackend(TRcBufInternalBackend&& other) noexcept
        : Data_(other.Data_)
        , Size_(other.Size_)
    {
        other.Data_ = nullptr;
        other.Size_ = 0;
    }

    TRcBufInternalBackend& operator=(const TRcBufInternalBackend& other) noexcept {
        if (this != &other) {
            Release();
            Data_ = other.Data_;
            Size_ = other.Size_;
            AddRef();
        }
        return *this;
    }

    TRcBufInternalBackend& operator=(TRcBufInternalBackend&& other) noexcept {
        if (this != &other) {
            Release();
            Data_ = other.Data_;
            Size_ = other.Size_;
            other.Data_ = nullptr;
            other.Size_ = 0;
        }
        return *this;
    }

    Y_FORCE_INLINE explicit operator bool() const { return Size_ > 0; }

    Y_FORCE_INLINE char* mutable_data() { return Data(); }
    Y_FORCE_INLINE char* mutable_begin() { return Data(); }
    Y_FORCE_INLINE char* mutable_end() { return Data() + Size_; }

    Y_FORCE_INLINE const char* data() const { return Data(); }
    Y_FORCE_INLINE const char* begin() const { return Data(); }
    Y_FORCE_INLINE const char* end() const { return Data() + Size_; }

    Y_FORCE_INLINE size_t size() const { return Size_; }

    /**
     * Copies data to new allocated buffer if data is shared
     * New container loses original owner
     * Returns pointer to mutable buffer
     */
    char* Detach() {
        if (IsShared()) {
            *this = TRcBufInternalBackend::Copy(data(), size());
        }
        return Data_;
    }

    bool IsPrivate() const {
        return Data_ ? IsPrivate(Header()) : true;
    }

    bool IsShared() const {
        return !IsPrivate();
    }

    TString ToString() const {
        return TString(data(), size());
    }

    TCookies* GetCookies() {
        return Header();
    }

    /**
     * Attach to pre-allocated data with a preceding THeader
     */
    static TRcBufInternalBackend AttachUnsafe(char* data, size_t size) noexcept {
        TRcBufInternalBackend result;
        result.Data_ = data;
        result.Size_ = size;
        return result;
    }

    /**
     * Make uninitialized buffer of the specified size
     */
    static TRcBufInternalBackend Uninitialized(size_t size, size_t headroom = 0, size_t tailroom = 0) {
        size_t fullSize = checkedSum(size, checkedSum(headroom, tailroom));
        return AttachUnsafe(Allocate(size, headroom, tailroom), fullSize);
    }

    /**
     * Make a copy of the specified data
     */
    static TRcBufInternalBackend Copy(const void* data, size_t size) {
        TRcBufInternalBackend result = Uninitialized(size);
        if (size) {
            ::memcpy(result.Data(), data, size);
        }
        return result;
    }

private:
    Y_FORCE_INLINE THeader* Header() const noexcept {
        Y_VERIFY_DEBUG(Data_);
        return reinterpret_cast<THeader*>(Data_);
    }

    Y_FORCE_INLINE char* Data() const noexcept {
        Y_VERIFY_DEBUG(Data_);
        return Data_ + OverheadSize;
    }

    static bool IsPrivate(THeader* header) noexcept {
        return 1 == AtomicGet(header->RefCount);
    }

    void AddRef() noexcept {
        if (Data_) {
            AtomicIncrement(Header()->RefCount);
        }
    }

    void Release() noexcept {
        if (Data_) {
            auto* header = Header();
            if (IsPrivate(header) || 0 == AtomicDecrement(header->RefCount)) {
                Deallocate(Data_);
            }
        }
    }

private:
    static size_t checkedSum(size_t a, size_t b) {
        if (a > std::numeric_limits<size_t>::max() - b) {
            throw std::length_error("Allocate size overflow");
        }
        return a + b;
    }

    static char* Allocate(size_t size, size_t headroom = 0, size_t tailroom = 0) {
        char* data = nullptr;
        size_t fullSize = checkedSum(size, checkedSum(headroom, tailroom));
        if (fullSize > 0) {
            if (fullSize >= MaxDataSize) {
                throw std::length_error("Allocate size overflow");
            }
            auto allocSize = OverheadSize + fullSize;
            char* raw = reinterpret_cast<char*>(y_allocate(allocSize));

            auto* header = reinterpret_cast<THeader*>(raw);
            header->Begin = raw + OverheadSize + headroom;
            header->End = raw + allocSize - tailroom;
            header->RefCount = 1;

            data = raw;
        }

        return data;
    }

    static void Deallocate(char* data) noexcept {
        if (data) {
            char* raw = data;

            y_deallocate(raw);
        }
    }

private:
    char* Data_;
    size_t Size_;
};

} // namespace NDetail