aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/actors/util/shared_data.cpp
blob: 209ca7bcbdef614397b25118517d8c281d5ee97e (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
#include "shared_data.h"

#include "memory_tracker.h"

#include <util/system/sys_alloc.h>
#include <util/system/sanitizers.h>

namespace NActors {

    static constexpr char MemoryLabelSharedData[] = "Tablet/TSharedData/Buffers";

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

            auto* privateHeader = reinterpret_cast<TPrivateHeader*>(raw);
            privateHeader->AllocSize = allocSize;
            NActors::NMemory::TLabel<MemoryLabelSharedData>::Add(allocSize);

            auto* header = reinterpret_cast<THeader*>(raw + PrivateHeaderSize);
            header->RefCount = 1;
            header->Owner = nullptr;

            data = raw + OverheadSize;
            NSan::Poison(data, size);
        }
        return data;
    }

    void TSharedData::Deallocate(char* data) noexcept {
        if (data) {
            char* raw = data - OverheadSize;

            auto* privateHeader = reinterpret_cast<TPrivateHeader*>(raw);
            NActors::NMemory::TLabel<MemoryLabelSharedData>::Sub(privateHeader->AllocSize);

            auto* header = reinterpret_cast<THeader*>(raw + PrivateHeaderSize);
            Y_VERIFY_DEBUG(header->Owner == nullptr);

            y_deallocate(raw);
        }
    }

}