serenity/AK/kmalloc.h
Daniel Bertalan a482a3e609 AK: Declare operators new and delete as global functions
This fixes a build issue introduced in 23d66fe, where the compiler
statically detected that that mismatching new and delete operators were
used.

Clang generates a warning for this, for the reasons described in the
comment in `AK/kmalloc.cpp`, but GCC does not.

Besides moving the allocator functions into a `.cpp` file, declarations
in `AK/kmalloc.cpp` were reordered to have imports at the top, in order
to make the code more readable.
2021-07-05 20:23:42 +02:00

46 lines
1 KiB
C++

/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, Daniel Bertalan <dani@danielbertalan.dev>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#if defined(KERNEL)
# include <Kernel/Heap/kmalloc.h>
#else
# include <new>
# include <stdlib.h>
# define kcalloc calloc
# define kmalloc malloc
# define kmalloc_good_size malloc_good_size
# define kfree free
# define krealloc realloc
#endif
#ifndef __serenity__
# include <AK/Types.h>
# ifndef AK_OS_MACOS
extern "C" {
inline size_t malloc_good_size(size_t size) { return size; }
}
# else
# include <malloc/malloc.h>
# endif
#endif
#ifdef KERNEL
# define AK_MAKE_ETERNAL \
public: \
void* operator new(size_t size) { return kmalloc_eternal(size); } \
\
private:
#else
# define AK_MAKE_ETERNAL
#endif
using std::nothrow;