| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | #pragma once |
| 4 | |
| 5 | #include <mos/mos_global.h> |
| 6 | #include <mos/string_view.hpp> |
| 7 | #include <mos/type_utils.hpp> |
| 8 | |
| 9 | MOSAPI void *do_kmalloc(size_t size); |
| 10 | MOSAPI void *do_kcalloc(size_t nmemb, size_t size); |
| 11 | MOSAPI void *do_krealloc(void *ptr, size_t size); |
| 12 | MOSAPI void do_kfree(const void *ptr); |
| 13 | |
| 14 | namespace mos |
| 15 | { |
| 16 | template<typename TItem, bool HasTypeName = HasTypeName<TItem>> |
| 17 | struct default_allocator |
| 18 | { |
| 19 | }; |
| 20 | |
| 21 | template<typename TItem> |
| 22 | struct default_allocator<TItem, false> |
| 23 | { |
| 24 | static TItem *allocate(size_t size) |
| 25 | { |
| 26 | return static_cast<TItem *>(do_kmalloc(size)); |
| 27 | } |
| 28 | |
| 29 | static void free(TItem *ptr, size_t = 0) |
| 30 | { |
| 31 | do_kfree(ptr); |
| 32 | } |
| 33 | }; |
| 34 | |
| 35 | template<typename TItem> |
| 36 | struct default_allocator<TItem, true> |
| 37 | { |
| 38 | static TItem *allocate(size_t size) |
| 39 | { |
| 40 | return create<TItem>(size); |
| 41 | } |
| 42 | |
| 43 | static void free(TItem *ptr, size_t = 0) |
| 44 | { |
| 45 | do_kfree(ptr); |
| 46 | } |
| 47 | }; |
| 48 | |
| 49 | } // namespace mos |
| 50 | |