/src/arduinojson/src/ArduinoJson/Memory/Allocator.hpp
Line | Count | Source |
1 | | // ArduinoJson - https://arduinojson.org |
2 | | // Copyright © 2014-2026, Benoit BLANCHON |
3 | | // MIT License |
4 | | |
5 | | #pragma once |
6 | | |
7 | | #include <ArduinoJson/Namespace.hpp> |
8 | | |
9 | | #include <stdlib.h> // malloc, free |
10 | | |
11 | | ARDUINOJSON_BEGIN_PUBLIC_NAMESPACE |
12 | | |
13 | | class Allocator { |
14 | | public: |
15 | | virtual void* allocate(size_t size) = 0; |
16 | | virtual void deallocate(void* ptr) = 0; |
17 | | virtual void* reallocate(void* ptr, size_t new_size) = 0; |
18 | | |
19 | | protected: |
20 | | ~Allocator() = default; |
21 | | }; |
22 | | |
23 | | namespace detail { |
24 | | class DefaultAllocator : public Allocator { |
25 | | public: |
26 | 10.2k | void* allocate(size_t size) override { |
27 | 10.2k | return malloc(size); |
28 | 10.2k | } |
29 | | |
30 | 10.2k | void deallocate(void* ptr) override { |
31 | 10.2k | free(ptr); |
32 | 10.2k | } |
33 | | |
34 | 6.24k | void* reallocate(void* ptr, size_t new_size) override { |
35 | 6.24k | return realloc(ptr, new_size); |
36 | 6.24k | } |
37 | | |
38 | 4.88k | static Allocator* instance() { |
39 | 4.88k | static DefaultAllocator allocator; |
40 | 4.88k | return &allocator; |
41 | 4.88k | } |
42 | | |
43 | | private: |
44 | | DefaultAllocator() = default; |
45 | | ~DefaultAllocator() = default; |
46 | | }; |
47 | | } // namespace detail |
48 | | |
49 | | ARDUINOJSON_END_PUBLIC_NAMESPACE |