aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/public/udf/udf_allocator.h
blob: 147ea08a7dc2b64f0aac0ecff3f5d1a768d28c5a (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
#pragma once
#include "udf_version.h"
#include <util/system/types.h>
#include <new>
#include <cstddef>
#include <limits>

#if UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 37)
extern "C" void* UdfArrowAllocate(ui64 size);
extern "C" void* UdfArrowReallocate(const void* mem, ui64 prevSize, ui64 size);
extern "C" void UdfArrowFree(const void* mem, ui64 size);
#endif

#if UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 8)
extern "C" void* UdfAllocateWithSize(ui64 size);
extern "C" void UdfFreeWithSize(const void* mem, ui64 size);
extern "C" [[deprecated("Use UdfAllocateWithSize() instead")]] void* UdfAllocate(ui64 size);
extern "C" [[deprecated("Use UdfFreeWithSize() instead")]] void UdfFree(const void* mem);
#else
extern "C" void* UdfAllocate(ui64 size);
extern "C" void UdfFree(const void* mem);
#endif

namespace NYql {
namespace NUdf {

template <typename Type>
struct TStdAllocatorForUdf
{
    typedef Type value_type;
    typedef Type* pointer;
    typedef const Type* const_pointer;
    typedef Type& reference;
    typedef const Type& const_reference;
    typedef size_t size_type;
    typedef ptrdiff_t difference_type;

    TStdAllocatorForUdf() noexcept = default;
    ~TStdAllocatorForUdf() noexcept = default;

    template<typename U> TStdAllocatorForUdf(const TStdAllocatorForUdf<U>&) noexcept {};
    template<typename U> struct rebind { typedef TStdAllocatorForUdf<U> other; };
    template<typename U> bool operator==(const TStdAllocatorForUdf<U>&) const { return true; };
    template<typename U> bool operator!=(const TStdAllocatorForUdf<U>&) const { return false; }

    static pointer allocate(size_type n, const void* = nullptr)
    {
#if UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 8)
        return static_cast<pointer>(UdfAllocateWithSize(n * sizeof(value_type)));
#else
        return static_cast<pointer>(UdfAllocate(n * sizeof(value_type)));
#endif
    }

    static void deallocate(const_pointer p, size_type n) noexcept
    {
#if UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 8)
        (void)(n);
        return UdfFreeWithSize(static_cast<const void*>(p), n * sizeof(value_type));
#else
        (void)(n);
        return UdfFree(static_cast<const void*>(p));
#endif
    }
};

struct TWithUdfAllocator {
    void* operator new(size_t sz) {
#if UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 8)
        return UdfAllocateWithSize(sz);
#else
        return UdfAllocate(sz);
#endif
    }

    void* operator new[](size_t sz) {
#if UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 8)
        return UdfAllocateWithSize(sz);
#else
        return UdfAllocate(sz);
#endif
    }

#if UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 8)
    void operator delete(void *mem, std::size_t sz) noexcept {
        return UdfFreeWithSize(mem, sz);
    }

    void operator delete[](void *mem, std::size_t sz) noexcept {
        return UdfFreeWithSize(mem, sz);
    }
#else
    void operator delete(void *mem) noexcept {
        return UdfFree(mem);
    }

    void operator delete[](void *mem) noexcept {
        return UdfFree(mem);
    }
#endif
};

} // namespace NUdf
} // namespace NYql