2022-01-09 12:01:27 +00:00
|
|
|
/*
|
2022-01-13 11:07:00 +00:00
|
|
|
* Copyright (c) 2022, kleines Filmröllchen <filmroellchen@serenityos.org>.
|
2022-01-09 12:01:27 +00:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Forward.h>
|
|
|
|
#include <AK/Noncopyable.h>
|
|
|
|
|
2024-06-17 22:12:53 +00:00
|
|
|
#if defined(AK_OS_SERENITY)
|
2023-01-03 14:20:56 +00:00
|
|
|
# include <mallocdefs.h>
|
2022-01-09 12:01:27 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
class NoAllocationGuard {
|
|
|
|
AK_MAKE_NONCOPYABLE(NoAllocationGuard);
|
|
|
|
AK_MAKE_NONMOVABLE(NoAllocationGuard);
|
|
|
|
|
|
|
|
public:
|
|
|
|
NoAllocationGuard()
|
|
|
|
: m_allocation_enabled_previously(get_thread_allocation_state())
|
|
|
|
{
|
|
|
|
set_thread_allocation_state(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
~NoAllocationGuard()
|
|
|
|
{
|
|
|
|
set_thread_allocation_state(m_allocation_enabled_previously);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static bool get_thread_allocation_state()
|
|
|
|
{
|
2024-06-17 22:12:53 +00:00
|
|
|
#if defined(AK_OS_SERENITY)
|
2022-01-12 22:24:57 +00:00
|
|
|
// This extern thread-local lives in our LibC, which doesn't exist on other systems.
|
2022-01-09 12:01:27 +00:00
|
|
|
return s_allocation_enabled;
|
2022-01-12 22:24:57 +00:00
|
|
|
#else
|
|
|
|
return true;
|
2022-01-09 12:01:27 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void set_thread_allocation_state(bool value)
|
|
|
|
{
|
2024-06-17 22:12:53 +00:00
|
|
|
#if defined(AK_OS_SERENITY)
|
2022-01-09 12:01:27 +00:00
|
|
|
s_allocation_enabled = value;
|
2022-01-12 22:24:57 +00:00
|
|
|
#else
|
|
|
|
(void)value;
|
2022-01-09 12:01:27 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-01-12 21:33:43 +00:00
|
|
|
bool m_allocation_enabled_previously { true };
|
2022-01-09 12:01:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-11-26 11:18:30 +00:00
|
|
|
#if USING_AK_GLOBALLY
|
2022-01-09 12:01:27 +00:00
|
|
|
using AK::NoAllocationGuard;
|
2022-11-26 11:18:30 +00:00
|
|
|
#endif
|