Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * Copyright (c) 2021, Daniel Bertalan <dani@danielbertalan.dev>
4 *
5 * SPDX-License-Identifier: BSD-2-Clause
6 */
7
8#include <AK/kmalloc.h>
9
10#if defined(AK_OS_SERENITY) && !defined(KERNEL)
11
12# include <AK/Assertions.h>
13
14// However deceptively simple these functions look, they must not be inlined.
15// Memory allocated in one translation unit has to be deallocatable in another
16// translation unit, so these functions must be the same everywhere.
17// By making these functions global, this invariant is enforced.
18
19void* operator new(size_t size)
20{
21 void* ptr = malloc(size);
22 VERIFY(ptr);
23 return ptr;
24}
25
26void* operator new(size_t size, std::nothrow_t const&) noexcept
27{
28 return malloc(size);
29}
30
31void operator delete(void* ptr) noexcept
32{
33 return free(ptr);
34}
35
36void operator delete(void* ptr, size_t) noexcept
37{
38 return free(ptr);
39}
40
41void* operator new[](size_t size)
42{
43 void* ptr = malloc(size);
44 VERIFY(ptr);
45 return ptr;
46}
47
48void* operator new[](size_t size, std::nothrow_t const&) noexcept
49{
50 return malloc(size);
51}
52
53void operator delete[](void* ptr) noexcept
54{
55 return free(ptr);
56}
57
58void operator delete[](void* ptr, size_t) noexcept
59{
60 return free(ptr);
61}
62
63// This is usually provided by libstdc++ in most cases, and the kernel has its own definition in
64// Kernel/Heap/kmalloc.cpp. If neither of those apply, the following should suffice to not fail during linking.
65namespace AK_REPLACED_STD_NAMESPACE {
66const nothrow_t nothrow;
67}
68
69#endif