cpp-msgpack-light 0.3.0
A light library to serialize MessagePack.
Loading...
Searching...
No Matches
allocator_wrapper.h
Go to the documentation of this file.
1/*
2 * Copyright 2024 MusicScience37 (Kenta Kabashima)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
20#pragma once
21
22#include <cstddef>
23#include <utility> // IWYU pragma: keep
24
26
27namespace msgpack_light::details {
28
34template <typename Allocator>
36public:
42 explicit allocator_wrapper(Allocator allocator)
43 : allocator_(std::move(allocator)) {}
44
51 [[nodiscard]] object_data* allocate_object_data(std::size_t size) {
52 return static_cast<object_data*>(allocator_.allocate(
53 size * sizeof(object_data), alignof(object_data)));
54 }
55
61 void deallocate_object_data(object_data* ptr) noexcept {
62 allocator_.deallocate(ptr);
63 }
64
72 std::size_t size) {
73 return static_cast<key_value_pair_data*>(allocator_.allocate(
74 size * sizeof(key_value_pair_data), alignof(key_value_pair_data)));
75 }
76
83 allocator_.deallocate(ptr);
84 }
85
92 [[nodiscard]] unsigned char* allocate_unsigned_char(std::size_t size) {
93 return static_cast<unsigned char*>(allocator_.allocate(size, 1U));
94 }
95
101 void deallocate_unsigned_char(unsigned char* ptr) noexcept {
102 allocator_.deallocate(ptr);
103 }
104
111 [[nodiscard]] char* allocate_char(std::size_t size) {
112 return static_cast<char*>(allocator_.allocate(size, 1U));
113 }
114
120 void deallocate_char(char* ptr) noexcept { allocator_.deallocate(ptr); }
121
122private:
124 Allocator allocator_;
125};
126
127} // namespace msgpack_light::details
void deallocate_unsigned_char(unsigned char *ptr) noexcept
Deallocate unsigned char instances.
char * allocate_char(std::size_t size)
Allocate char instances.
void deallocate_char(char *ptr) noexcept
Deallocate char instances.
allocator_wrapper(Allocator allocator)
Constructor.
void deallocate_object_data(object_data *ptr) noexcept
Deallocate object data.
object_data * allocate_object_data(std::size_t size)
Allocate object data.
key_value_pair_data * allocate_key_value_pair_data(std::size_t size)
Allocate key-value pair data.
unsigned char * allocate_unsigned_char(std::size_t size)
Allocate unsigned char instances.
void deallocate_key_value_pair_data(key_value_pair_data *ptr) noexcept
Deallocate key-value pair data.
Namespace of internal implementations.
Definition binary.h:35
STL namespace.
Definition of object_data struct.
Struct of data of key-value pairs in maps.
Struct of data of objects in MessagePack.
Definition object_data.h:94