1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 03:07:36 +00:00

Kernel: Switch singletons to use new Singleton class

MemoryManager cannot use the Singleton class because
MemoryManager::initialize is called before the global constructors
are run. That caused the Singleton to be re-initialized, causing
it to create another MemoryManager instance.

Fixes #3226
This commit is contained in:
Tom 2020-08-24 19:35:19 -06:00 committed by Andreas Kling
parent ba6e4fb77f
commit d89582880e
46 changed files with 221 additions and 170 deletions

View file

@ -25,6 +25,7 @@
*/
#include <AK/Checked.h>
#include <AK/Singleton.h>
#include <Kernel/Devices/BXVGADevice.h>
#include <Kernel/IO.h>
#include <Kernel/PCI/Access.h>
@ -56,7 +57,12 @@ namespace Kernel {
#define VBE_DISPI_ENABLED 0x01
#define VBE_DISPI_LFB_ENABLED 0x40
static BXVGADevice* s_the;
static AK::Singleton<BXVGADevice> s_the;
void BXVGADevice::initialize()
{
s_the.ensure_instance();
}
BXVGADevice& BXVGADevice::the()
{
@ -67,7 +73,6 @@ BXVGADevice::BXVGADevice()
: BlockDevice(29, 0)
{
s_the = this;
m_framebuffer_address = PhysicalAddress(find_framebuffer_address());
set_safe_resolution();
}

View file

@ -36,6 +36,7 @@ namespace Kernel {
class BXVGADevice final : public BlockDevice {
AK_MAKE_ETERNAL
public:
static void initialize();
static BXVGADevice& the();
BXVGADevice();

View file

@ -24,18 +24,17 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <AK/Singleton.h>
#include <Kernel/Devices/Device.h>
#include <Kernel/FileSystem/InodeMetadata.h>
#include <LibC/errno_numbers.h>
namespace Kernel {
static HashMap<u32, Device*>* s_all_devices;
static AK::Singleton<HashMap<u32, Device*>> s_all_devices;
HashMap<u32, Device*>& Device::all_devices()
{
if (s_all_devices == nullptr)
s_all_devices = new HashMap<u32, Device*>;
return *s_all_devices;
}

View file

@ -26,6 +26,7 @@
#include <AK/Assertions.h>
#include <AK/ByteBuffer.h>
#include <AK/Singleton.h>
#include <AK/StringView.h>
#include <AK/Types.h>
#include <Kernel/Arch/i386/CPU.h>
@ -335,11 +336,15 @@ void KeyboardDevice::handle_irq(const RegisterState&)
}
}
static KeyboardDevice* s_the;
static AK::Singleton<KeyboardDevice> s_the;
void KeyboardDevice::initialize()
{
s_the.ensure_instance();
}
KeyboardDevice& KeyboardDevice::the()
{
ASSERT(s_the);
return *s_the;
}
@ -347,8 +352,6 @@ KeyboardDevice::KeyboardDevice()
: IRQHandler(IRQ_KEYBOARD)
, CharacterDevice(85, 1)
{
s_the = this;
// Empty the buffer of any pending data.
// I don't care what you've been pressing until now!
while (IO::in8(I8042_STATUS) & I8042_BUFFER_FULL)

View file

@ -45,6 +45,7 @@ class KeyboardDevice final : public IRQHandler
public:
using Event = KeyEvent;
static void initialize();
static KeyboardDevice& the();
virtual ~KeyboardDevice() override;

View file

@ -25,22 +25,26 @@
*/
#include "NullDevice.h"
#include <AK/Singleton.h>
#include <AK/StdLibExtras.h>
namespace Kernel {
static NullDevice* s_the;
static AK::Singleton<NullDevice> s_the;
void NullDevice::initialize()
{
s_the.ensure_instance();
}
NullDevice& NullDevice::the()
{
ASSERT(s_the);
return *s_the;
}
NullDevice::NullDevice()
: CharacterDevice(1, 3)
{
s_the = this;
}
NullDevice::~NullDevice()

View file

@ -36,6 +36,7 @@ public:
NullDevice();
virtual ~NullDevice() override;
static void initialize();
static NullDevice& the();
private:

View file

@ -25,6 +25,7 @@
*/
#include <AK/ByteBuffer.h>
#include <AK/Singleton.h>
#include <AK/StringView.h>
#include <Kernel/Devices/PATAChannel.h>
#include <Kernel/Devices/PATADiskDevice.h>
@ -106,13 +107,12 @@ namespace Kernel {
#define PCI_Mass_Storage_Class 0x1
#define PCI_IDE_Controller_Subclass 0x1
static AK::Singleton<Lock> s_pata_lock;
static Lock& s_lock()
{
static Lock* lock;
if (!lock)
lock = new Lock;
return *lock;
return *s_pata_lock;
};
OwnPtr<PATAChannel> PATAChannel::create(ChannelType type, bool force_pio)

View file

@ -25,6 +25,7 @@
*/
#include <AK/Memory.h>
#include <AK/Singleton.h>
#include <Kernel/Devices/PS2MouseDevice.h>
#include <Kernel/Devices/VMWareBackdoor.h>
#include <Kernel/IO.h>
@ -56,13 +57,12 @@ namespace Kernel {
//#define PS2MOUSE_DEBUG
static PS2MouseDevice* s_the;
static AK::Singleton<PS2MouseDevice> s_the;
PS2MouseDevice::PS2MouseDevice()
: IRQHandler(IRQ_MOUSE)
, CharacterDevice(10, 1)
{
s_the = this;
initialize();
}
@ -70,6 +70,11 @@ PS2MouseDevice::~PS2MouseDevice()
{
}
void PS2MouseDevice::create()
{
s_the.ensure_instance();
}
PS2MouseDevice& PS2MouseDevice::the()
{
return *s_the;

View file

@ -40,6 +40,7 @@ public:
PS2MouseDevice();
virtual ~PS2MouseDevice() override;
static void create();
static PS2MouseDevice& the();
// ^CharacterDevice

View file

@ -25,6 +25,7 @@
*/
#include <AK/Memory.h>
#include <AK/Singleton.h>
#include <AK/StringView.h>
#include <Kernel/Devices/SB16.h>
#include <Kernel/Thread.h>
@ -76,13 +77,12 @@ void SB16::set_sample_rate(uint16_t hz)
dsp_write((u8)hz);
}
static SB16* s_the;
static AK::Singleton<SB16> s_the;
SB16::SB16()
: IRQHandler(SB16_DEFAULT_IRQ)
, CharacterDevice(42, 42) // ### ?
{
s_the = this;
initialize();
}
@ -90,6 +90,11 @@ SB16::~SB16()
{
}
void SB16::create()
{
s_the.ensure_instance();
}
SB16& SB16::the()
{
return *s_the;

View file

@ -42,6 +42,7 @@ public:
SB16();
virtual ~SB16() override;
static void create();
static SB16& the();
// ^CharacterDevice

View file

@ -25,6 +25,8 @@
*/
#include <AK/Assertions.h>
#include <AK/OwnPtr.h>
#include <AK/Singleton.h>
#include <AK/String.h>
#include <Kernel/Arch/i386/CPU.h>
#include <Kernel/CommandLine.h>
@ -80,33 +82,40 @@ inline void vmware_high_bandwidth_get(VMWareCommand& command)
: "+a"(command.ax), "+b"(command.bx), "+c"(command.cx), "+d"(command.dx), "+S"(command.si), "+D"(command.di));
}
static VMWareBackdoor* s_vmware_backdoor;
static bool detect_presence()
class VMWareBackdoorDetector
{
VMWareCommand command;
command.bx = ~VMWARE_MAGIC;
command.command = VMWARE_CMD_GETVERSION;
vmware_out(command);
if (command.bx != VMWARE_MAGIC || command.ax == 0xFFFFFFFF)
return false;
return true;
}
public:
VMWareBackdoorDetector()
{
if (detect_presence())
m_backdoor = make<VMWareBackdoor>();
}
VMWareBackdoor* VMWareBackdoor::initialize()
{
ASSERT(s_vmware_backdoor == nullptr);
if (!detect_presence())
return nullptr;
VMWareBackdoor* get_instance()
{
return m_backdoor.ptr();
}
s_vmware_backdoor = new VMWareBackdoor;
klog() << "VMWare backdoor opened.";
return s_vmware_backdoor;
}
private:
static bool detect_presence()
{
VMWareCommand command;
command.bx = ~VMWARE_MAGIC;
command.command = VMWARE_CMD_GETVERSION;
vmware_out(command);
if (command.bx != VMWARE_MAGIC || command.ax == 0xFFFFFFFF)
return false;
return true;
}
OwnPtr<VMWareBackdoor> m_backdoor;
};
static AK::Singleton<VMWareBackdoorDetector> s_vmware_backdoor;
VMWareBackdoor* VMWareBackdoor::the()
{
return s_vmware_backdoor;
return s_vmware_backdoor->get_instance();
}
VMWareBackdoor::VMWareBackdoor()

View file

@ -63,9 +63,9 @@ class VMWareBackdoor {
AK_MAKE_ETERNAL;
public:
VMWareBackdoor();
static VMWareBackdoor* the();
static VMWareBackdoor* initialize();
bool vmmouse_is_absolute() const;
void enable_absolute_vmmouse();
void disable_absolute_vmmouse();
@ -76,7 +76,6 @@ public:
private:
void send_high_bandwidth(VMWareCommand& command);
void get_high_bandwidth(VMWareCommand& command);
VMWareBackdoor();
bool detect_vmmouse();
bool m_vmmouse_absolute { false };
};