2020-01-18 11:38:21 +03:00
|
|
|
/*
|
2021-08-07 23:32:45 +03:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2021-06-29 23:57:27 +03:00
|
|
|
* Copyright (c) 2021, Daniel Bertalan <dani@danielbertalan.dev>
|
2020-01-18 11:38:21 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2018-10-10 12:53:07 +03:00
|
|
|
#pragma once
|
|
|
|
|
2021-08-07 23:32:45 +03:00
|
|
|
#include <AK/Checked.h>
|
|
|
|
|
2021-06-29 23:57:27 +03:00
|
|
|
#if defined(KERNEL)
|
|
|
|
# include <Kernel/Heap/kmalloc.h>
|
|
|
|
#else
|
2020-02-25 17:58:24 +03:00
|
|
|
# include <new>
|
2021-06-29 23:57:27 +03:00
|
|
|
# include <stdlib.h>
|
|
|
|
|
|
|
|
# define kcalloc calloc
|
|
|
|
# define kmalloc malloc
|
|
|
|
# define kmalloc_good_size malloc_good_size
|
|
|
|
# define kfree free
|
2021-07-11 14:21:38 +03:00
|
|
|
|
|
|
|
inline void kfree_sized(void* ptr, size_t)
|
|
|
|
{
|
|
|
|
kfree(ptr);
|
|
|
|
}
|
2021-06-29 23:57:27 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef __serenity__
|
|
|
|
# include <AK/Types.h>
|
2021-05-15 11:06:41 +03:00
|
|
|
|
|
|
|
# ifndef AK_OS_MACOS
|
2021-05-30 12:10:39 +03:00
|
|
|
extern "C" {
|
2021-05-15 11:06:41 +03:00
|
|
|
inline size_t malloc_good_size(size_t size) { return size; }
|
2021-05-30 12:10:39 +03:00
|
|
|
}
|
2021-05-15 11:06:41 +03:00
|
|
|
# else
|
|
|
|
# include <malloc/malloc.h>
|
|
|
|
# endif
|
2020-02-25 17:58:24 +03:00
|
|
|
#endif
|
|
|
|
|
2019-04-20 13:58:02 +03:00
|
|
|
#ifdef KERNEL
|
2019-05-28 12:53:16 +03:00
|
|
|
# define AK_MAKE_ETERNAL \
|
|
|
|
public: \
|
|
|
|
void* operator new(size_t size) { return kmalloc_eternal(size); } \
|
2021-08-30 10:55:24 +03:00
|
|
|
void operator delete(void*, size_t) { VERIFY_NOT_REACHED(); } \
|
2019-05-28 12:53:16 +03:00
|
|
|
\
|
|
|
|
private:
|
2018-11-01 01:19:15 +03:00
|
|
|
#else
|
2019-05-28 12:53:16 +03:00
|
|
|
# define AK_MAKE_ETERNAL
|
2018-11-01 01:19:15 +03:00
|
|
|
#endif
|
|
|
|
|
AK+Kernel: Make fallible allocations compiler-agnostic
In standard C++, operators `new` and `new[]` are guaranteed to return a
valid (non-null) pointer and throw an exception if the allocation
couldn't be performed. Based on this, compilers did not check the
returned pointer before attempting to use them for object construction.
To avoid this, the allocator operators were changed to be `noexcept` in
PR #7026, which made GCC emit the desired null checks. Unfortunately,
this is a non-standard feature which meant that Clang would not accept
these function definitions, as it did not match its expected
declaration.
To make compiling using Clang possible, the special "nothrow" versions
of `new` are implemented in this commit. These take a tag type of
`std::nothrow_t` (used for disambiguating from placement new/etc.), and
are allowed by the standard to return null. There is a global variable,
`std::nothrow`, declared with this type, which is also exported into the
global namespace.
To perform fallible allocations, the following syntax should be used:
```cpp
auto ptr = new (nothrow) T;
```
As we don't support exceptions in the kernel, the only way of uphold the
"throwing" new's guarantee is to abort if the allocation couldn't be
performed. Once we have proper OOM handling in the kernel, this should
only be used for critical allocations, where we wouldn't be able to
recover from allocation failures anyway.
2021-06-20 10:39:20 +03:00
|
|
|
using std::nothrow;
|
2021-08-07 23:32:45 +03:00
|
|
|
|
|
|
|
inline void* kmalloc_array(Checked<size_t> a, Checked<size_t> b)
|
|
|
|
{
|
|
|
|
auto size = a * b;
|
|
|
|
VERIFY(!size.has_overflow());
|
|
|
|
return kmalloc(size.value());
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void* kmalloc_array(Checked<size_t> a, Checked<size_t> b, Checked<size_t> c)
|
|
|
|
{
|
|
|
|
auto size = a * b * c;
|
|
|
|
VERIFY(!size.has_overflow());
|
|
|
|
return kmalloc(size.value());
|
|
|
|
}
|