summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom <tomut@yahoo.com>2020-08-20 09:36:06 -0600
committerAndreas Kling <kling@serenityos.org>2020-08-21 11:47:35 +0200
commitf48feae0b2a300992479abf0b2ded85e45ac6045 (patch)
treed0b01169a60261135ee15a8d4a6abd01785a7bec
parent527c8047fe0a08ade2e17fd096ad9b4ebc103ec5 (diff)
downloadserenity-f48feae0b2a300992479abf0b2ded85e45ac6045.zip
Kernel: Switch singletons to use new Singleton class
Fixes #3226
-rw-r--r--Kernel/Console.cpp12
-rw-r--r--Kernel/Console.h1
-rw-r--r--Kernel/Devices/BXVGADevice.cpp9
-rw-r--r--Kernel/Devices/BXVGADevice.h1
-rw-r--r--Kernel/Devices/Device.cpp5
-rw-r--r--Kernel/Devices/KeyboardDevice.cpp11
-rw-r--r--Kernel/Devices/KeyboardDevice.h1
-rw-r--r--Kernel/Devices/NullDevice.cpp10
-rw-r--r--Kernel/Devices/NullDevice.h1
-rw-r--r--Kernel/Devices/PATAChannel.cpp10
-rw-r--r--Kernel/Devices/PS2MouseDevice.cpp9
-rw-r--r--Kernel/Devices/PS2MouseDevice.h1
-rw-r--r--Kernel/Devices/SB16.cpp9
-rw-r--r--Kernel/Devices/SB16.h1
-rw-r--r--Kernel/Devices/VMWareBackdoor.cpp51
-rw-r--r--Kernel/Devices/VMWareBackdoor.h3
-rw-r--r--Kernel/FileSystem/DevPtsFS.cpp17
-rw-r--r--Kernel/FileSystem/FIFO.cpp6
-rw-r--r--Kernel/FileSystem/FileSystem.cpp5
-rw-r--r--Kernel/FileSystem/Inode.cpp7
-rw-r--r--Kernel/FileSystem/VirtualFileSystem.cpp10
-rw-r--r--Kernel/FileSystem/VirtualFileSystem.h1
-rw-r--r--Kernel/Interrupts/APIC.cpp7
-rw-r--r--Kernel/Net/IPv4Socket.cpp6
-rw-r--r--Kernel/Net/LocalSocket.cpp6
-rw-r--r--Kernel/Net/LoopbackAdapter.cpp8
-rw-r--r--Kernel/Net/LoopbackAdapter.h4
-rw-r--r--Kernel/Net/NetworkAdapter.cpp8
-rw-r--r--Kernel/Net/Routing.cpp8
-rw-r--r--Kernel/Net/TCPSocket.cpp6
-rw-r--r--Kernel/Net/UDPSocket.cpp6
-rw-r--r--Kernel/Random.cpp6
-rw-r--r--Kernel/Random.h3
-rw-r--r--Kernel/SharedBuffer.cpp8
-rw-r--r--Kernel/TTY/PTYMultiplexer.cpp5
-rw-r--r--Kernel/TTY/PTYMultiplexer.h4
-rw-r--r--Kernel/Time/TimeManagement.cpp20
-rw-r--r--Kernel/Time/TimeManagement.h2
-rw-r--r--Kernel/TimerQueue.cpp5
-rw-r--r--Kernel/TimerQueue.h3
-rw-r--r--Kernel/VM/MemoryManager.cpp7
-rw-r--r--Kernel/VM/MemoryManager.h2
-rw-r--r--Kernel/VM/PageDirectory.cpp8
-rw-r--r--Kernel/init.cpp17
44 files changed, 184 insertions, 146 deletions
diff --git a/Kernel/Console.cpp b/Kernel/Console.cpp
index 8c8261fd73..f1416cb535 100644
--- a/Kernel/Console.cpp
+++ b/Kernel/Console.cpp
@@ -27,29 +27,33 @@
#include <Kernel/Console.h>
#include <Kernel/IO.h>
#include <Kernel/kstdio.h>
+#include <Kernel/Singleton.h>
#include <Kernel/SpinLock.h>
// Bytes output to 0xE9 end up on the Bochs console. It's very handy.
#define CONSOLE_OUT_TO_E9
-static Console* s_the;
+static auto s_the = Kernel::make_singleton<Console>();
static Kernel::SpinLock g_console_lock;
+void Console::initialize()
+{
+ s_the.ensure_instance();
+}
+
Console& Console::the()
{
- ASSERT(s_the);
return *s_the;
}
bool Console::is_initialized()
{
- return s_the != nullptr;
+ return s_the.is_initialized();
}
Console::Console()
: CharacterDevice(5, 1)
{
- s_the = this;
}
Console::~Console()
diff --git a/Kernel/Console.h b/Kernel/Console.h
index e58733f49e..f06ebf2a6a 100644
--- a/Kernel/Console.h
+++ b/Kernel/Console.h
@@ -34,6 +34,7 @@ class Console final : public Kernel::CharacterDevice {
AK_MAKE_ETERNAL
public:
static Console& the();
+ static void initialize();
static bool is_initialized();
Console();
diff --git a/Kernel/Devices/BXVGADevice.cpp b/Kernel/Devices/BXVGADevice.cpp
index 1e3a4b5168..c31e6ec1f1 100644
--- a/Kernel/Devices/BXVGADevice.cpp
+++ b/Kernel/Devices/BXVGADevice.cpp
@@ -29,6 +29,7 @@
#include <Kernel/IO.h>
#include <Kernel/PCI/Access.h>
#include <Kernel/Process.h>
+#include <Kernel/Singleton.h>
#include <Kernel/VM/AnonymousVMObject.h>
#include <Kernel/VM/MemoryManager.h>
#include <LibC/errno_numbers.h>
@@ -56,7 +57,12 @@ namespace Kernel {
#define VBE_DISPI_ENABLED 0x01
#define VBE_DISPI_LFB_ENABLED 0x40
-static BXVGADevice* s_the;
+static auto s_the = make_singleton<BXVGADevice>();
+
+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();
}
diff --git a/Kernel/Devices/BXVGADevice.h b/Kernel/Devices/BXVGADevice.h
index cdc2016942..213fc1617c 100644
--- a/Kernel/Devices/BXVGADevice.h
+++ b/Kernel/Devices/BXVGADevice.h
@@ -36,6 +36,7 @@ namespace Kernel {
class BXVGADevice final : public BlockDevice {
AK_MAKE_ETERNAL
public:
+ static void initialize();
static BXVGADevice& the();
BXVGADevice();
diff --git a/Kernel/Devices/Device.cpp b/Kernel/Devices/Device.cpp
index e0a49a8631..d89a57506c 100644
--- a/Kernel/Devices/Device.cpp
+++ b/Kernel/Devices/Device.cpp
@@ -26,16 +26,15 @@
#include <Kernel/Devices/Device.h>
#include <Kernel/FileSystem/InodeMetadata.h>
+#include <Kernel/Singleton.h>
#include <LibC/errno_numbers.h>
namespace Kernel {
-static HashMap<u32, Device*>* s_all_devices;
+static auto s_all_devices = make_singleton<HashMap<u32, Device*>>();
HashMap<u32, Device*>& Device::all_devices()
{
- if (s_all_devices == nullptr)
- s_all_devices = new HashMap<u32, Device*>;
return *s_all_devices;
}
diff --git a/Kernel/Devices/KeyboardDevice.cpp b/Kernel/Devices/KeyboardDevice.cpp
index ac1d5ac349..2fd53797e6 100644
--- a/Kernel/Devices/KeyboardDevice.cpp
+++ b/Kernel/Devices/KeyboardDevice.cpp
@@ -31,6 +31,7 @@
#include <Kernel/Arch/i386/CPU.h>
#include <Kernel/Devices/KeyboardDevice.h>
#include <Kernel/IO.h>
+#include <Kernel/Singleton.h>
#include <Kernel/TTY/VirtualConsole.h>
//#define KEYBOARD_DEBUG
@@ -335,11 +336,15 @@ void KeyboardDevice::handle_irq(const RegisterState&)
}
}
-static KeyboardDevice* s_the;
+static auto s_the = make_singleton<KeyboardDevice>();
+
+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)
diff --git a/Kernel/Devices/KeyboardDevice.h b/Kernel/Devices/KeyboardDevice.h
index 94dfb8f02e..74a53a0da6 100644
--- a/Kernel/Devices/KeyboardDevice.h
+++ b/Kernel/Devices/KeyboardDevice.h
@@ -45,6 +45,7 @@ class KeyboardDevice final : public IRQHandler
public:
using Event = KeyEvent;
+ static void initialize();
static KeyboardDevice& the();
virtual ~KeyboardDevice() override;
diff --git a/Kernel/Devices/NullDevice.cpp b/Kernel/Devices/NullDevice.cpp
index 517e1cff18..5e64920acc 100644
--- a/Kernel/Devices/NullDevice.cpp
+++ b/Kernel/Devices/NullDevice.cpp
@@ -26,21 +26,25 @@
#include "NullDevice.h"
#include <AK/StdLibExtras.h>
+#include <Kernel/Singleton.h>
namespace Kernel {
-static NullDevice* s_the;
+static auto s_the = make_singleton<NullDevice>();
+
+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()
diff --git a/Kernel/Devices/NullDevice.h b/Kernel/Devices/NullDevice.h
index a1cf7ce0b6..c7968258f7 100644
--- a/Kernel/Devices/NullDevice.h
+++ b/Kernel/Devices/NullDevice.h
@@ -36,6 +36,7 @@ public:
NullDevice();
virtual ~NullDevice() override;
+ static void initialize();
static NullDevice& the();
private:
diff --git a/Kernel/Devices/PATAChannel.cpp b/Kernel/Devices/PATAChannel.cpp
index e380acd2e6..59295e5373 100644
--- a/Kernel/Devices/PATAChannel.cpp
+++ b/Kernel/Devices/PATAChannel.cpp
@@ -31,6 +31,7 @@
#include <Kernel/FileSystem/ProcFS.h>
#include <Kernel/IO.h>
#include <Kernel/Process.h>
+#include <Kernel/Singleton.h>
#include <Kernel/VM/MemoryManager.h>
namespace Kernel {
@@ -106,13 +107,12 @@ namespace Kernel {
#define PCI_Mass_Storage_Class 0x1
#define PCI_IDE_Controller_Subclass 0x1
+
+static auto s_pata_lock = make_singleton<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)
diff --git a/Kernel/Devices/PS2MouseDevice.cpp b/Kernel/Devices/PS2MouseDevice.cpp
index e4461abaad..dfb5f01bec 100644
--- a/Kernel/Devices/PS2MouseDevice.cpp
+++ b/Kernel/Devices/PS2MouseDevice.cpp
@@ -28,6 +28,7 @@
#include <Kernel/Devices/PS2MouseDevice.h>
#include <Kernel/Devices/VMWareBackdoor.h>
#include <Kernel/IO.h>
+#include <Kernel/Singleton.h>
namespace Kernel {
@@ -56,13 +57,12 @@ namespace Kernel {
//#define PS2MOUSE_DEBUG
-static PS2MouseDevice* s_the;
+static auto s_the = make_singleton<PS2MouseDevice>();
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;
diff --git a/Kernel/Devices/PS2MouseDevice.h b/Kernel/Devices/PS2MouseDevice.h
index 62c5c13aa4..10f382f43d 100644
--- a/Kernel/Devices/PS2MouseDevice.h
+++ b/Kernel/Devices/PS2MouseDevice.h
@@ -40,6 +40,7 @@ public:
PS2MouseDevice();
virtual ~PS2MouseDevice() override;
+ static void create();
static PS2MouseDevice& the();
// ^CharacterDevice
diff --git a/Kernel/Devices/SB16.cpp b/Kernel/Devices/SB16.cpp
index b1ad67bd5a..38f81f2680 100644
--- a/Kernel/Devices/SB16.cpp
+++ b/Kernel/Devices/SB16.cpp
@@ -31,6 +31,7 @@
#include <Kernel/VM/AnonymousVMObject.h>
#include <Kernel/VM/MemoryManager.h>
#include <Kernel/IO.h>
+#include <Kernel/Singleton.h>
//#define SB16_DEBUG
@@ -76,13 +77,12 @@ void SB16::set_sample_rate(uint16_t hz)
dsp_write((u8)hz);
}
-static SB16* s_the;
+static auto s_the = make_singleton<SB16>();
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;
diff --git a/Kernel/Devices/SB16.h b/Kernel/Devices/SB16.h
index 2f2ce81b54..e0d5ba7ca3 100644
--- a/Kernel/Devices/SB16.h
+++ b/Kernel/Devices/SB16.h
@@ -42,6 +42,7 @@ public:
SB16();
virtual ~SB16() override;
+ static void create();
static SB16& the();
// ^CharacterDevice
diff --git a/Kernel/Devices/VMWareBackdoor.cpp b/Kernel/Devices/VMWareBackdoor.cpp
index 20efb8e8fe..8ba15600da 100644
--- a/Kernel/Devices/VMWareBackdoor.cpp
+++ b/Kernel/Devices/VMWareBackdoor.cpp
@@ -25,12 +25,14 @@
*/
#include <AK/Assertions.h>
+#include <AK/OwnPtr.h>
#include <AK/String.h>
#include <Kernel/Arch/i386/CPU.h>
#include <Kernel/CommandLine.h>
#include <Kernel/Devices/VMWareBackdoor.h>
#include <Kernel/API/MousePacket.h>
#include <Kernel/IO.h>
+#include <Kernel/Singleton.h>
namespace Kernel {
@@ -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 auto s_vmware_backdoor = make_singleton<VMWareBackdoorDetector>();
VMWareBackdoor* VMWareBackdoor::the()
{
- return s_vmware_backdoor;
+ return s_vmware_backdoor->get_instance();
}
VMWareBackdoor::VMWareBackdoor()
diff --git a/Kernel/Devices/VMWareBackdoor.h b/Kernel/Devices/VMWareBackdoor.h
index 1325253390..ffe4b752a6 100644
--- a/Kernel/Devices/VMWareBackdoor.h
+++ b/Kernel/Devices/VMWareBackdoor.h
@@ -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 };
};
diff --git a/Kernel/FileSystem/DevPtsFS.cpp b/Kernel/FileSystem/DevPtsFS.cpp
index ac060d026b..b3db215c5d 100644
--- a/Kernel/FileSystem/DevPtsFS.cpp
+++ b/Kernel/FileSystem/DevPtsFS.cpp
@@ -28,6 +28,7 @@
#include <AK/StringView.h>
#include <Kernel/FileSystem/DevPtsFS.h>
#include <Kernel/FileSystem/VirtualFileSystem.h>
+#include <Kernel/Singleton.h>
#include <Kernel/TTY/SlavePTY.h>
namespace Kernel {
@@ -45,14 +46,10 @@ DevPtsFS::~DevPtsFS()
{
}
-static HashTable<unsigned>* ptys;
+static auto s_ptys = make_singleton<HashTable<unsigned>>();
bool DevPtsFS::initialize()
{
- if (ptys == nullptr) {
- ptys = new HashTable<unsigned>();
- }
-
m_root_inode = adopt(*new DevPtsFSInode(*this, 1));
m_root_inode->m_metadata.inode = { fsid(), 1 };
m_root_inode->m_metadata.mode = 0040555;
@@ -104,12 +101,12 @@ RefPtr<Inode> DevPtsFS::get_inode(InodeIdentifier inode_id) const
void DevPtsFS::register_slave_pty(SlavePTY& slave_pty)
{
- ptys->set(slave_pty.index());
+ s_ptys->set(slave_pty.index());
}
void DevPtsFS::unregister_slave_pty(SlavePTY& slave_pty)
{
- ptys->remove(slave_pty.index());
+ s_ptys->remove(slave_pty.index());
}
DevPtsFSInode::DevPtsFSInode(DevPtsFS& fs, unsigned index)
@@ -144,7 +141,7 @@ KResult DevPtsFSInode::traverse_as_directory(Function<bool(const FS::DirectoryEn
callback({ ".", identifier(), 0 });
callback({ "..", identifier(), 0 });
- for (unsigned pty_index : *ptys) {
+ for (unsigned pty_index : *s_ptys) {
String name = String::number(pty_index);
InodeIdentifier identifier = { fsid(), pty_index_to_inode_index(pty_index) };
callback({ name, identifier, 0 });
@@ -157,7 +154,7 @@ KResultOr<size_t> DevPtsFSInode::directory_entry_count() const
{
ASSERT(identifier().index() == 1);
- return 2 + ptys->size();
+ return 2 + s_ptys->size();
}
RefPtr<Inode> DevPtsFSInode::lookup(StringView name)
@@ -170,7 +167,7 @@ RefPtr<Inode> DevPtsFSInode::lookup(StringView name)
auto& fs = static_cast<DevPtsFS&>(this->fs());
auto pty_index = name.to_uint();
- if (pty_index.has_value() && ptys->contains(pty_index.value())) {
+ if (pty_index.has_value() && s_ptys->contains(pty_index.value())) {
return fs.get_inode({ fsid(), pty_index_to_inode_index(pty_index.value()) });
}
diff --git a/Kernel/FileSystem/FIFO.cpp b/Kernel/FileSystem/FIFO.cpp
index 9881b6916b..670266cbce 100644
--- a/Kernel/FileSystem/FIFO.cpp
+++ b/Kernel/FileSystem/FIFO.cpp
@@ -31,17 +31,17 @@
#include <Kernel/FileSystem/FileDescription.h>
#include <Kernel/Lock.h>
#include <Kernel/Process.h>
+#include <Kernel/Singleton.h>
#include <Kernel/Thread.h>
//#define FIFO_DEBUG
namespace Kernel {
+static auto s_table = make_singleton<Lockable<HashTable<FIFO*>>>();
+
static Lockable<HashTable<FIFO*>>& all_fifos()
{
- static Lockable<HashTable<FIFO*>>* s_table;
- if (!s_table)
- s_table = new Lockable<HashTable<FIFO*>>;
return *s_table;
}
diff --git a/Kernel/FileSystem/FileSystem.cpp b/Kernel/FileSystem/FileSystem.cpp
index e4ed6fd98b..450f401253 100644
--- a/Kernel/FileSystem/FileSystem.cpp
+++ b/Kernel/FileSystem/FileSystem.cpp
@@ -31,18 +31,17 @@
#include <Kernel/FileSystem/FileSystem.h>
#include <Kernel/FileSystem/Inode.h>
#include <Kernel/Net/LocalSocket.h>
+#include <Kernel/Singleton.h>
#include <Kernel/VM/MemoryManager.h>
#include <LibC/errno_numbers.h>
namespace Kernel {
static u32 s_lastFileSystemID;
-static HashMap<u32, FS*>* s_fs_map;
+static auto s_fs_map = make_singleton<HashMap<u32, FS*>>();
static HashMap<u32, FS*>& all_fses()
{
- if (!s_fs_map)
- s_fs_map = new HashMap<u32, FS*>();
return *s_fs_map;
}
diff --git a/Kernel/FileSystem/Inode.cpp b/Kernel/FileSystem/Inode.cpp
index 127ad174f6..c136ffc2a0 100644
--- a/Kernel/FileSystem/Inode.cpp
+++ b/Kernel/FileSystem/Inode.cpp
@@ -33,20 +33,19 @@
#include <Kernel/FileSystem/VirtualFileSystem.h>
#include <Kernel/KBufferBuilder.h>
#include <Kernel/Net/LocalSocket.h>
+#include <Kernel/Singleton.h>
#include <Kernel/VM/SharedInodeVMObject.h>
namespace Kernel {
static SpinLock s_all_inodes_lock;
+static auto s_list = make_singleton<InlineLinkedList<Inode>>();
InlineLinkedList<Inode>& Inode::all_with_lock()
{
ASSERT(s_all_inodes_lock.is_locked());
- static InlineLinkedList<Inode>* list;
- if (!list)
- list = new InlineLinkedList<Inode>;
- return *list;
+ return *s_list;
}
void Inode::sync()
diff --git a/Kernel/FileSystem/VirtualFileSystem.cpp b/Kernel/FileSystem/VirtualFileSystem.cpp
index 5febd7c5e6..cc0510a570 100644
--- a/Kernel/FileSystem/VirtualFileSystem.cpp
+++ b/Kernel/FileSystem/VirtualFileSystem.cpp
@@ -34,19 +34,24 @@
#include <Kernel/FileSystem/VirtualFileSystem.h>
#include <Kernel/KSyms.h>
#include <Kernel/Process.h>
+#include <Kernel/Singleton.h>
#include <LibC/errno_numbers.h>
//#define VFS_DEBUG
namespace Kernel {
-static VFS* s_the;
+static auto s_the = make_singleton<VFS>();
static constexpr int symlink_recursion_limit { 5 }; // FIXME: increase?
static constexpr int root_mount_flags = MS_NODEV | MS_NOSUID | MS_RDONLY;
+void VFS::initialize()
+{
+ s_the.ensure_instance();
+}
+
VFS& VFS::the()
{
- ASSERT(s_the);
return *s_the;
}
@@ -55,7 +60,6 @@ VFS::VFS()
#ifdef VFS_DEBUG
klog() << "VFS: Constructing VFS";
#endif
- s_the = this;
}
VFS::~VFS()
diff --git a/Kernel/FileSystem/VirtualFileSystem.h b/Kernel/FileSystem/VirtualFileSystem.h
index 35b17e0da8..1969327698 100644
--- a/Kernel/FileSystem/VirtualFileSystem.h
+++ b/Kernel/FileSystem/VirtualFileSystem.h
@@ -78,6 +78,7 @@ public:
int m_flags;
};
+ static void initialize();
static VFS& the();
VFS();
diff --git a/Kernel/Interrupts/APIC.cpp b/Kernel/Interrupts/APIC.cpp
index 32fc0045de..d3e86af19d 100644
--- a/Kernel/Interrupts/APIC.cpp
+++ b/Kernel/Interrupts/APIC.cpp
@@ -34,6 +34,7 @@
#include <Kernel/IO.h>
#include <Kernel/Interrupts/APIC.h>
#include <Kernel/Interrupts/SpuriousInterruptHandler.h>
+#include <Kernel/Singleton.h>
#include <Kernel/Thread.h>
#include <Kernel/VM/MemoryManager.h>
#include <Kernel/VM/PageDirectory.h>
@@ -68,7 +69,7 @@
namespace Kernel {
-static APIC* s_apic;
+static auto s_apic = make_singleton<APIC>();
class APICIPIInterruptHandler final : public GenericInterruptHandler {
public:
@@ -132,7 +133,7 @@ private:
bool APIC::initialized()
{
- return (s_apic != nullptr);
+ return s_apic.is_initialized();
}
APIC& APIC::the()
@@ -144,7 +145,7 @@ APIC& APIC::the()
void APIC::initialize()
{
ASSERT(!APIC::initialized());
- s_apic = new APIC();
+ s_apic.ensure_instance();
}
PhysicalAddress APIC::get_base()
diff --git a/Kernel/Net/IPv4Socket.cpp b/Kernel/Net/IPv4Socket.cpp
index 6e4fcfcbfa..8286e5a2e8 100644
--- a/Kernel/Net/IPv4Socket.cpp
+++ b/Kernel/Net/IPv4Socket.cpp
@@ -25,6 +25,7 @@
*/
#include <AK/StringBuilder.h>
+#include <Kernel/Singleton.h>
#include <Kernel/FileSystem/FileDescription.h>
#include <Kernel/Net/ARP.h>
#include <Kernel/Net/ICMP.h>
@@ -45,11 +46,10 @@
namespace Kernel {
+static auto s_table = make_singleton<Lockable<HashTable<IPv4Socket*>>>();
+
Lockable<HashTable<IPv4Socket*>>& IPv4Socket::all_sockets()
{
- static Lockable<HashTable<IPv4Socket*>>* s_table;
- if (!s_table)
- s_table = new Lockable<HashTable<IPv4Socket*>>;
return *s_table;
}
diff --git a/Kernel/Net/LocalSocket.cpp b/Kernel/Net/LocalSocket.cpp
index 92f333ad23..c703cc8ac6 100644
--- a/Kernel/Net/LocalSocket.cpp
+++ b/Kernel/Net/LocalSocket.cpp
@@ -29,6 +29,7 @@
#include <Kernel/FileSystem/VirtualFileSystem.h>
#include <Kernel/Net/LocalSocket.h>
#include <Kernel/Process.h>
+#include <Kernel/Singleton.h>
#include <Kernel/StdLib.h>
#include <Kernel/UnixTypes.h>
#include <LibC/errno_numbers.h>
@@ -37,11 +38,10 @@
namespace Kernel {
+static auto s_list = make_singleton<Lockable<InlineLinkedList<LocalSocket>>>();
+
Lockable<InlineLinkedList<LocalSocket>>& LocalSocket::all_sockets()
{
- static Lockable<InlineLinkedList<LocalSocket>>* s_list;
- if (!s_list)
- s_list = new Lockable<InlineLinkedList<LocalSocket>>();
return *s_list;
}
diff --git a/Kernel/Net/LoopbackAdapter.cpp b/Kernel/Net/LoopbackAdapter.cpp
index e42ee66a5e..27faa8bfad 100644
--- a/Kernel/Net/LoopbackAdapter.cpp
+++ b/Kernel/Net/LoopbackAdapter.cpp
@@ -25,15 +25,15 @@
*/
#include <Kernel/Net/LoopbackAdapter.h>
+#include <Kernel/Singleton.h>
namespace Kernel {
+static auto s_loopback = make_singleton<LoopbackAdapter>();
+
LoopbackAdapter& LoopbackAdapter::the()
{
- static LoopbackAdapter* the;
- if (!the)
- the = new LoopbackAdapter;
- return *the;
+ return *s_loopback;
}
LoopbackAdapter::LoopbackAdapter()
diff --git a/Kernel/Net/LoopbackAdapter.h b/Kernel/Net/LoopbackAdapter.h
index b87bb5003f..cebedfe7a3 100644
--- a/Kernel/Net/LoopbackAdapter.h
+++ b/Kernel/Net/LoopbackAdapter.h
@@ -33,15 +33,13 @@ namespace Kernel {
class LoopbackAdapter final : public NetworkAdapter {
AK_MAKE_ETERNAL
public:
+ LoopbackAdapter();
static LoopbackAdapter& the();
virtual ~LoopbackAdapter() override;
virtual void send_raw(ReadonlyBytes) override;
virtual const char* class_name() const override { return "LoopbackAdapter"; }
-
-private:
- LoopbackAdapter();
};
}
diff --git a/Kernel/Net/NetworkAdapter.cpp b/Kernel/Net/NetworkAdapter.cpp
index 7efa7b353e..936bf9a2e7 100644
--- a/Kernel/Net/NetworkAdapter.cpp
+++ b/Kernel/Net/NetworkAdapter.cpp
@@ -33,16 +33,16 @@
#include <Kernel/Net/LoopbackAdapter.h>
#include <Kernel/Net/NetworkAdapter.h>
#include <Kernel/Random.h>
+#include <Kernel/Singleton.h>
#include <Kernel/StdLib.h>
namespace Kernel {
+static auto s_table = make_singleton<Lockable<HashTable<NetworkAdapter*>>>();
+
static Lockable<HashTable<NetworkAdapter*>>& all_adapters()
{
- static Lockable<HashTable<NetworkAdapter*>>* table;
- if (!table)
- table = new Lockable<HashTable<NetworkAdapter*>>;
- return *table;
+ return *s_table;
}
void NetworkAdapter::for_each(Function<void(NetworkAdapter&)> callback)
diff --git a/Kernel/Net/Routing.cpp b/Kernel/Net/Routing.cpp
index b2b9786a2e..5f72ab1166 100644
--- a/Kernel/Net/Routing.cpp
+++ b/Kernel/Net/Routing.cpp
@@ -28,17 +28,17 @@
#include <Kernel/Net/LoopbackAdapter.h>
#include <Kernel/Net/Routing.h>
#include <Kernel/Thread.h>
+#include <Kernel/Singleton.h>
//#define ROUTING_DEBUG
namespace Kernel {
+static auto s_arp_table = make_singleton<Lockable<HashMap<IPv4Address, MACAddress>>>();
+
Lockable<HashMap<IPv4Address, MACAddress>>& arp_table()
{
- static Lockable<HashMap<IPv4Address, MACAddress>>* the;
- if (!the)
- the = new Lockable<HashMap<IPv4Address, MACAddress>>;
- return *the;
+ return *s_arp_table;
}
bool RoutingDecision::is_zero() const
diff --git a/Kernel/Net/TCPSocket.cpp b/Kernel/Net/TCPSocket.cpp
index cafe48370e..7b8c12a49c 100644
--- a/Kernel/Net/TCPSocket.cpp
+++ b/Kernel/Net/TCPSocket.cpp
@@ -33,6 +33,7 @@
#include <Kernel/Net/TCPSocket.h>
#include <Kernel/Process.h>
#include <Kernel/Random.h>
+#include <Kernel/Singleton.h>
//#define TCP_SOCKET_DEBUG
@@ -70,11 +71,10 @@ Lockable<HashMap<IPv4SocketTuple, RefPtr<TCPSocket>>>& TCPSocket::closing_socket
return *s_map;
}
+static auto s_map = make_singleton<Lockable<HashMap<IPv4SocketTuple, TCPSocket*>>>();
+
Lockable<HashMap<IPv4SocketTuple, TCPSocket*>>& TCPSocket::sockets_by_tuple()
{
- static Lockable<HashMap<IPv4SocketTuple, TCPSocket*>>* s_map;
- if (!s_map)
- s_map = new Lockable<HashMap<IPv4SocketTuple, TCPSocket*>>;
return *s_map;
}
diff --git a/Kernel/Net/UDPSocket.cpp b/Kernel/Net/UDPSocket.cpp
index 65091a37c1..ad57696675 100644
--- a/Kernel/Net/UDPSocket.cpp
+++ b/Kernel/Net/UDPSocket.cpp
@@ -31,6 +31,7 @@
#include <Kernel/Net/UDPSocket.h>
#include <Kernel/Process.h>
#include <Kernel/Random.h>
+#include <Kernel/Singleton.h>
namespace Kernel {
@@ -41,11 +42,10 @@ void UDPSocket::for_each(Function<void(const UDPSocket&)> callback)
callback(*it.value);
}
+static auto s_map = make_singleton<Lockable<HashMap<u16, UDPSocket*>>>();
+
Lockable<HashMap<u16, UDPSocket*>>& UDPSocket::sockets_by_port()
{
- static Lockable<HashMap<u16, UDPSocket*>>* s_map;
- if (!s_map)
- s_map = new Lockable<HashMap<u16, UDPSocket*>>;
return *s_map;
}
diff --git a/Kernel/Random.cpp b/Kernel/Random.cpp
index f024865a74..b01b4aa5da 100644
--- a/Kernel/Random.cpp
+++ b/Kernel/Random.cpp
@@ -28,17 +28,15 @@
#include <Kernel/Arch/i386/CPU.h>
#include <Kernel/Devices/RandomDevice.h>
#include <Kernel/Random.h>
+#include <Kernel/Singleton.h>
#include <Kernel/Time/TimeManagement.h>
namespace Kernel {
-static KernelRng* s_the;
+static auto s_the = make_singleton<KernelRng>();
KernelRng& KernelRng::the()
{
- if (!s_the) {
- s_the = new KernelRng;
- }
return *s_the;
}
diff --git a/Kernel/Random.h b/Kernel/Random.h
index 6825319efc..d26f4ea0d2 100644
--- a/Kernel/Random.h
+++ b/Kernel/Random.h
@@ -127,6 +127,7 @@ class KernelRng : public Lockable<FortunaPRNG<Crypto::Cipher::AESCipher, Crypto:
AK_MAKE_ETERNAL;
public:
+ KernelRng();
static KernelRng& the();
void wait_for_entropy();
@@ -134,8 +135,6 @@ public:
void wake_if_ready();
private:
- KernelRng();
-
WaitQueue m_seed_queue;
};
diff --git a/Kernel/SharedBuffer.cpp b/Kernel/SharedBuffer.cpp
index 1989c892db..49401e15e0 100644
--- a/Kernel/SharedBuffer.cpp
+++ b/Kernel/SharedBuffer.cpp
@@ -25,16 +25,16 @@
*/
#include <Kernel/Process.h>
+#include <Kernel/Singleton.h>
#include <Kernel/SharedBuffer.h>
namespace Kernel {
+static auto s_map = make_singleton<Lockable<HashMap<int, NonnullOwnPtr<SharedBuffer>>>>();
+
Lockable<HashMap<int, NonnullOwnPtr<SharedBuffer>>>& shared_buffers()
{
- static Lockable<HashMap<int, NonnullOwnPtr<SharedBuffer>>>* map;
- if (!map)
- map = new Lockable<HashMap<int, NonnullOwnPtr<SharedBuffer>>>;
- return *map;
+ return *s_map;
}
void SharedBuffer::sanity_check(const char* what)
diff --git a/Kernel/TTY/PTYMultiplexer.cpp b/Kernel/TTY/PTYMultiplexer.cpp
index ccc0678e43..4f8d48806e 100644
--- a/Kernel/TTY/PTYMultiplexer.cpp
+++ b/Kernel/TTY/PTYMultiplexer.cpp
@@ -28,6 +28,7 @@
#include "MasterPTY.h"
#include <Kernel/FileSystem/FileDescription.h>
#include <Kernel/Process.h>
+#include <Kernel/Singleton.h>
#include <LibC/errno_numbers.h>
//#define PTMX_DEBUG
@@ -35,18 +36,16 @@
namespace Kernel {
static const unsigned s_max_pty_pairs = 8;
-static PTYMultiplexer* s_the;
+static auto s_the = make_singleton<PTYMultiplexer>();
PTYMultiplexer& PTYMultiplexer::the()
{
- ASSERT(s_the);
return *s_the;
}
PTYMultiplexer::PTYMultiplexer()
: CharacterDevice(5, 2)
{
- s_the = this;
m_freelist.ensure_capacity(s_max_pty_pairs);
for (int i = s_max_pty_pairs; i > 0; --i)
m_freelist.unchecked_append(i - 1);
diff --git a/Kernel/TTY/PTYMultiplexer.h b/Kernel/TTY/PTYMultiplexer.h
index 15bb4bdc19..17698009c5 100644
--- a/Kernel/TTY/PTYMultiplexer.h
+++ b/Kernel/TTY/PTYMultiplexer.h
@@ -40,6 +40,10 @@ public:
PTYMultiplexer();
virtual ~PTYMultiplexer() override;
+ static void initialize()
+ {
+ the();
+ }
static PTYMultiplexer& the();
// ^CharacterDevice
diff --git a/Kernel/Time/TimeManagement.cpp b/Kernel/Time/TimeManagement.cpp
index d97cf26c46..7982c354b5 100644
--- a/Kernel/Time/TimeManagement.cpp
+++ b/Kernel/Time/TimeManagement.cpp
@@ -32,6 +32,7 @@
#include <Kernel/Time/HardwareTimer.h>
#include <Kernel/Time/PIT.h>
#include <Kernel/Time/RTC.h>
+#include <Kernel/Singleton.h>
#include <Kernel/Time/TimeManagement.h>
#include <Kernel/VM/MemoryManager.h>
@@ -39,12 +40,11 @@
namespace Kernel {
-static TimeManagement* s_time_management;
+static auto s_the = make_singleton<TimeManagement>();
TimeManagement& TimeManagement::the()
{
- ASSERT(s_time_management);
- return *s_time_management;
+ return *s_the;
}
bool TimeManagement::is_system_timer(const HardwareTimer& timer) const
@@ -65,11 +65,9 @@ time_t TimeManagement::epoch_time() const
void TimeManagement::initialize()
{
- ASSERT(!s_time_management);
- if (kernel_command_line().lookup("time").value_or("modern") == "legacy")
- s_time_management = new TimeManagement(false);
- else
- s_time_management = new TimeManagement(true);
+ ASSERT(!s_the.is_initialized());
+ s_the.ensure_instance();
+
}
time_t TimeManagement::seconds_since_boot() const
{
@@ -90,8 +88,9 @@ time_t TimeManagement::boot_time() const
return RTC::boot_time();
}
-TimeManagement::TimeManagement(bool probe_non_legacy_hardware_timers)
+TimeManagement::TimeManagement()
{
+ bool probe_non_legacy_hardware_timers = !(kernel_command_line().lookup("time").value_or("modern") == "legacy");
if (ACPI::is_enabled()) {
if (!ACPI::Parser::the()->x86_specific_flags().cmos_rtc_not_present) {
RTC::initialize();
@@ -117,7 +116,8 @@ TimeManagement::TimeManagement(bool probe_non_legacy_hardware_timers)
timeval TimeManagement::now_as_timeval()
{
- return { s_time_management->epoch_time(), (suseconds_t)s_time_management->ticks_this_second() * (suseconds_t)1000 };
+ auto* time_management = s_the.ptr();
+ return { time_management->epoch_time(), (suseconds_t)time_management->ticks_this_second() * (suseconds_t)1000 };
}
Vector<HardwareTimer*> TimeManagement::scan_and_initialize_periodic_timers()
diff --git a/Kernel/Time/TimeManagement.h b/Kernel/Time/TimeManagement.h
index c09159cc6c..5d16060ff3 100644
--- a/Kernel/Time/TimeManagement.h
+++ b/Kernel/Time/TimeManagement.h
@@ -42,6 +42,7 @@ class TimeManagement {
AK_MAKE_ETERNAL;
public:
+ TimeManagement();
static bool initialized();
static void initialize();
static TimeManagement& the();
@@ -63,7 +64,6 @@ public:
static timeval now_as_timeval();
private:
- explicit TimeManagement(bool probe_non_legacy_hardware_timers);
bool probe_and_set_legacy_hardware_timers();
bool probe_and_set_non_legacy_hardware_timers();
Vector<HardwareTimer*> scan_and_initialize_periodic_timers();
diff --git a/Kernel/TimerQueue.cpp b/Kernel/TimerQueue.cpp
index cac513039f..41197d6861 100644
--- a/Kernel/TimerQueue.cpp
+++ b/Kernel/TimerQueue.cpp
@@ -28,17 +28,16 @@
#include <AK/NonnullOwnPtr.h>
#include <AK/OwnPtr.h>
#include <Kernel/Scheduler.h>
+#include <Kernel/Singleton.h>
#include <Kernel/Time/TimeManagement.h>
#include <Kernel/TimerQueue.h>
namespace Kernel {
-static TimerQueue* s_the;
+static auto s_the = make_singleton<TimerQueue>();
TimerQueue& TimerQueue::the()
{
- if (!s_the)
- s_the = new TimerQueue;
return *s_the;
}
diff --git a/Kernel/TimerQueue.h b/Kernel/TimerQueue.h
index 449609da94..eec220462e 100644
--- a/Kernel/TimerQueue.h
+++ b/Kernel/TimerQueue.h
@@ -56,6 +56,7 @@ struct Timer {
class TimerQueue {
public:
+ TimerQueue();
static TimerQueue& the();
TimerId add_timer(NonnullOwnPtr<Timer>&&);
@@ -64,8 +65,6 @@ public:
void fire();
private:
- TimerQueue();
-
void update_next_timer_due();
u64 microseconds_to_ticks(u64 micro_seconds) { return micro_seconds * (m_ticks_per_second / 1'000'000); }
diff --git a/Kernel/VM/MemoryManager.cpp b/Kernel/VM/MemoryManager.cpp
index c39735225e..977285dcd8 100644
--- a/Kernel/VM/MemoryManager.cpp
+++ b/Kernel/VM/MemoryManager.cpp
@@ -39,6 +39,7 @@
#include <Kernel/VM/PhysicalRegion.h>
#include <Kernel/VM/PurgeableVMObject.h>
#include <Kernel/VM/SharedInodeVMObject.h>
+#include <Kernel/Singleton.h>
#include <Kernel/StdLib.h>
//#define MM_DEBUG
@@ -50,7 +51,7 @@ extern FlatPtr end_of_kernel_bss;
namespace Kernel {
-static MemoryManager* s_the;
+static auto s_the = make_singleton<MemoryManager>();
RecursiveSpinLock s_mm_lock;
MemoryManager& MM
@@ -60,6 +61,8 @@ MemoryManager& MM
MemoryManager::MemoryManager()
{
+ ASSERT(!s_the.is_initialized());
+
ScopedSpinLock lock(s_mm_lock);
m_kernel_page_directory = PageDirectory::create_kernel_page_directory();
parse_memory_map();
@@ -217,7 +220,7 @@ void MemoryManager::initialize(u32 cpu)
Processor::current().set_mm_data(*mm_data);
if (cpu == 0)
- s_the = new MemoryManager;
+ s_the.ensure_instance();
}
Region* MemoryManager::kernel_region_from_vaddr(VirtualAddress vaddr)
diff --git a/Kernel/VM/MemoryManager.h b/Kernel/VM/MemoryManager.h
index a7a324ed0d..13e796e251 100644
--- a/Kernel/VM/MemoryManager.h
+++ b/Kernel/VM/MemoryManager.h
@@ -85,6 +85,7 @@ class MemoryManager {
friend Optional<KBuffer> procfs$memstat(InodeIdentifier);
public:
+ MemoryManager();
static MemoryManager& the();
static void initialize(u32 cpu);
@@ -160,7 +161,6 @@ public:
PageDirectory& kernel_page_directory() { return *m_kernel_page_directory; }
private:
- MemoryManager();
~MemoryManager();
enum class AccessSpace { Kernel,
diff --git a/Kernel/VM/PageDirectory.cpp b/Kernel/VM/PageDirectory.cpp
index 838c6e104f..1b15853d94 100644
--- a/Kernel/VM/PageDirectory.cpp
+++ b/Kernel/VM/PageDirectory.cpp
@@ -27,6 +27,7 @@
#include <AK/Memory.h>
#include <Kernel/Process.h>
#include <Kernel/Random.h>
+#include <Kernel/Singleton.h>
#include <Kernel/Thread.h>
#include <Kernel/VM/MemoryManager.h>
#include <Kernel/VM/PageDirectory.h>
@@ -37,13 +38,12 @@ static const FlatPtr userspace_range_base = 0x00800000;
static const FlatPtr userspace_range_ceiling = 0xbe000000;
static const FlatPtr kernelspace_range_base = 0xc0800000;
+static auto s_cr3_map = make_singleton<HashMap<u32, PageDirectory*>>();
+
static HashMap<u32, PageDirectory*>& cr3_map()
{
ASSERT_INTERRUPTS_DISABLED();
- static HashMap<u32, PageDirectory*>* map;
- if (!map)
- map = new HashMap<u32, PageDirectory*>;
- return *map;
+ return *s_cr3_map;
}
RefPtr<PageDirectory> PageDirectory::find_by_cr3(u32 cr3)
diff --git a/Kernel/init.cpp b/Kernel/init.cpp
index 608b8715a1..200d852c8a 100644
--- a/Kernel/init.cpp
+++ b/Kernel/init.cpp
@@ -135,10 +135,10 @@ extern "C" [[noreturn]] void init()
InterruptManagement::initialize();
ACPI::initialize();
- new VFS;
- new KeyboardDevice;
- new PS2MouseDevice;
- new Console;
+ VFS::initialize();
+ KeyboardDevice::initialize();
+ PS2MouseDevice::create();
+ Console::initialize();
klog() << "Starting SerenityOS...";
@@ -146,7 +146,7 @@ extern "C" [[noreturn]] void init()
TimeManagement::initialize();
- new NullDevice;
+ NullDevice::initialize();
if (!get_serial_debug())
new SerialDevice(SERIAL_COM1_ADDR, 64);
new SerialDevice(SERIAL_COM2_ADDR, 65);
@@ -228,7 +228,7 @@ void init_stage2()
});
if (bxvga_found) {
- new BXVGADevice;
+ BXVGADevice::initialize();
} else {
if (multiboot_info_ptr->framebuffer_type == MULTIBOOT_FRAMEBUFFER_TYPE_RGB || multiboot_info_ptr->framebuffer_type == MULTIBOOT_FRAMEBUFFER_TYPE_EGA_TEXT) {
new MBVGADevice(
@@ -237,7 +237,7 @@ void init_stage2()
multiboot_info_ptr->framebuffer_width,
multiboot_info_ptr->framebuffer_height);
} else {
- new BXVGADevice;
+ BXVGADevice::initialize();
}
}
}
@@ -252,9 +252,8 @@ void init_stage2()
new ZeroDevice;
new FullDevice;
new RandomDevice;
- new PTYMultiplexer;
+ PTYMultiplexer::initialize();
new SB16;
- VMWareBackdoor::initialize();
bool force_pio = kernel_command_line().contains("force_pio");