Skip to content

Commit df45ce6

Browse files
committed
JSON: Add stateless allocator which uses thread local heap
Signed-off-by: Abhijat Malviya <[email protected]>
1 parent e291181 commit df45ce6

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

src/core/json/json_object.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,42 @@ inline thread_local PMR_NS::memory_resource* tl_mr = nullptr;
2929

3030
void InitJSONTLHeap(PMR_NS::memory_resource* mr);
3131

32+
template <typename T> class JSONConsAllocator {
33+
public:
34+
using value_type = T;
35+
using size_type = std::size_t;
36+
using difference_type = std::ptrdiff_t;
37+
using is_always_equal = std::true_type;
38+
39+
template <typename U> JSONConsAllocator(const JSONConsAllocator<U>&) noexcept {
40+
}
41+
42+
JSONConsAllocator() noexcept = default;
43+
44+
static value_type* allocate(size_type n) {
45+
void* ptr = detail::tl_mr->allocate(n * sizeof(value_type), alignof(value_type));
46+
return static_cast<value_type*>(ptr);
47+
}
48+
49+
static void deallocate(value_type* ptr, size_type n) noexcept {
50+
detail::tl_mr->deallocate(ptr, n * sizeof(value_type), alignof(value_type));
51+
}
52+
53+
static PMR_NS::memory_resource* resource() {
54+
return detail::tl_mr;
55+
}
56+
};
57+
58+
template <typename T, typename U>
59+
bool operator==(const JSONConsAllocator<T>&, const JSONConsAllocator<U>&) noexcept {
60+
return true;
61+
}
62+
63+
template <typename T, typename U>
64+
bool operator!=(const JSONConsAllocator<T>&, const JSONConsAllocator<U>&) noexcept {
65+
return false;
66+
}
67+
3268
using JsonType = jsoncons::pmr::json;
3369

3470
// Build a json object from string. If the string is not legal json, will return nullopt

0 commit comments

Comments
 (0)