Skip to content

Commit

Permalink
AK: Get rid of make_singleton function
Browse files Browse the repository at this point in the history
Just default the InitFunction template argument.
  • Loading branch information
tomuta authored and awesomekling committed Aug 22, 2020
1 parent 8a75e0b commit 5a98e32
Show file tree
Hide file tree
Showing 31 changed files with 42 additions and 46 deletions.
2 changes: 1 addition & 1 deletion AK/FlyString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ struct FlyStringImplTraits : public AK::Traits<StringImpl*> {
}
};

static auto s_table = make_singleton<HashTable<StringImpl*, FlyStringImplTraits>>();
static AK::Singleton<HashTable<StringImpl*, FlyStringImplTraits>> s_table;

static HashTable<StringImpl*, FlyStringImplTraits>& fly_impls()
{
Expand Down
25 changes: 10 additions & 15 deletions AK/Singleton.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,18 @@

namespace AK {

template<typename T, T* (*InitFunction)()>
template<typename T>
struct SingletonInstanceCreator {
static T* create()
{
return new T();
}
};

template<typename T, T* (*InitFunction)() = SingletonInstanceCreator<T>::create>
class Singleton {
AK_MAKE_NONCOPYABLE(Singleton);
AK_MAKE_NONMOVABLE(Singleton);
public:
Singleton() = default;

Expand Down Expand Up @@ -110,18 +119,4 @@ class Singleton {
mutable T* m_obj { nullptr }; // atomic
};

template<typename T>
struct SingletonInstanceCreator {
static T* create()
{
return new T();
}
};

template<typename T>
inline Singleton<T, SingletonInstanceCreator<T>::create> make_singleton()
{
return Singleton<T, SingletonInstanceCreator<T>::create>();
}

}
3 changes: 2 additions & 1 deletion Kernel/CommandLine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,15 @@ const CommandLine& kernel_command_line()

void CommandLine::initialize(const String& string)
{
ASSERT(!s_the);
s_the = new CommandLine(string);
}

CommandLine::CommandLine(const String& string)
: m_string(string)
{
s_the = this;

klog() << "CommandLine: " << string;
for (auto str : m_string.split(' ')) {
if (str == "") {
continue;
Expand Down
2 changes: 1 addition & 1 deletion Kernel/Console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
// Bytes output to 0xE9 end up on the Bochs console. It's very handy.
#define CONSOLE_OUT_TO_E9

static auto s_the = AK::make_singleton<Console>();
static AK::Singleton<Console> s_the;
static Kernel::SpinLock g_console_lock;

void Console::initialize()
Expand Down
2 changes: 1 addition & 1 deletion Kernel/Devices/BXVGADevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ namespace Kernel {
#define VBE_DISPI_ENABLED 0x01
#define VBE_DISPI_LFB_ENABLED 0x40

static auto s_the = AK::make_singleton<BXVGADevice>();
static AK::Singleton<BXVGADevice> s_the;

void BXVGADevice::initialize()
{
Expand Down
2 changes: 1 addition & 1 deletion Kernel/Devices/Device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

namespace Kernel {

static auto s_all_devices = AK::make_singleton<HashMap<u32, Device*>>();
static AK::Singleton<HashMap<u32, Device*>> s_all_devices;

HashMap<u32, Device*>& Device::all_devices()
{
Expand Down
2 changes: 1 addition & 1 deletion Kernel/Devices/KeyboardDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ void KeyboardDevice::handle_irq(const RegisterState&)
}
}

static auto s_the = AK::make_singleton<KeyboardDevice>();
static AK::Singleton<KeyboardDevice> s_the;

void KeyboardDevice::initialize()
{
Expand Down
2 changes: 1 addition & 1 deletion Kernel/Devices/NullDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

namespace Kernel {

static auto s_the = AK::make_singleton<NullDevice>();
static AK::Singleton<NullDevice> s_the;

void NullDevice::initialize()
{
Expand Down
2 changes: 1 addition & 1 deletion Kernel/Devices/PATAChannel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ namespace Kernel {
#define PCI_Mass_Storage_Class 0x1
#define PCI_IDE_Controller_Subclass 0x1

static auto s_pata_lock = AK::make_singleton<Lock>();
static AK::Singleton<Lock> s_pata_lock;

static Lock& s_lock()
{
Expand Down
2 changes: 1 addition & 1 deletion Kernel/Devices/PS2MouseDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ namespace Kernel {

//#define PS2MOUSE_DEBUG

static auto s_the = AK::make_singleton<PS2MouseDevice>();
static AK::Singleton<PS2MouseDevice> s_the;

PS2MouseDevice::PS2MouseDevice()
: IRQHandler(IRQ_MOUSE)
Expand Down
2 changes: 1 addition & 1 deletion Kernel/Devices/SB16.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ void SB16::set_sample_rate(uint16_t hz)
dsp_write((u8)hz);
}

static auto s_the = AK::make_singleton<SB16>();
static AK::Singleton<SB16> s_the;

SB16::SB16()
: IRQHandler(SB16_DEFAULT_IRQ)
Expand Down
2 changes: 1 addition & 1 deletion Kernel/Devices/VMWareBackdoor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class VMWareBackdoorDetector
OwnPtr<VMWareBackdoor> m_backdoor;
};

static auto s_vmware_backdoor = AK::make_singleton<VMWareBackdoorDetector>();
static AK::Singleton<VMWareBackdoorDetector> s_vmware_backdoor;

VMWareBackdoor* VMWareBackdoor::the()
{
Expand Down
2 changes: 1 addition & 1 deletion Kernel/FileSystem/DevPtsFS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ DevPtsFS::~DevPtsFS()
{
}

static auto s_ptys = AK::make_singleton<HashTable<unsigned>>();
static AK::Singleton<HashTable<unsigned>> s_ptys;

bool DevPtsFS::initialize()
{
Expand Down
2 changes: 1 addition & 1 deletion Kernel/FileSystem/FIFO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

namespace Kernel {

static auto s_table = AK::make_singleton<Lockable<HashTable<FIFO*>>>();
static AK::Singleton<Lockable<HashTable<FIFO*>>> s_table;

static Lockable<HashTable<FIFO*>>& all_fifos()
{
Expand Down
2 changes: 1 addition & 1 deletion Kernel/FileSystem/FileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
namespace Kernel {

static u32 s_lastFileSystemID;
static auto s_fs_map = AK::make_singleton<HashMap<u32, FS*>>();
static AK::Singleton<HashMap<u32, FS*>> s_fs_map;

static HashMap<u32, FS*>& all_fses()
{
Expand Down
2 changes: 1 addition & 1 deletion Kernel/FileSystem/Inode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
namespace Kernel {

static SpinLock s_all_inodes_lock;
static auto s_list = AK::make_singleton<InlineLinkedList<Inode>>();
static AK::Singleton<InlineLinkedList<Inode>> s_list;

InlineLinkedList<Inode>& Inode::all_with_lock()
{
Expand Down
2 changes: 1 addition & 1 deletion Kernel/FileSystem/VirtualFileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

namespace Kernel {

static auto s_the = AK::make_singleton<VFS>();
static AK::Singleton<VFS> s_the;
static constexpr int symlink_recursion_limit { 5 }; // FIXME: increase?
static constexpr int root_mount_flags = MS_NODEV | MS_NOSUID | MS_RDONLY;

Expand Down
2 changes: 1 addition & 1 deletion Kernel/Interrupts/APIC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@

namespace Kernel {

static auto s_apic = AK::make_singleton<APIC>();
static AK::Singleton<APIC> s_apic;

class APICIPIInterruptHandler final : public GenericInterruptHandler {
public:
Expand Down
2 changes: 1 addition & 1 deletion Kernel/Net/IPv4Socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@

namespace Kernel {

static auto s_table = AK::make_singleton<Lockable<HashTable<IPv4Socket*>>>();
static AK::Singleton<Lockable<HashTable<IPv4Socket*>>> s_table;

Lockable<HashTable<IPv4Socket*>>& IPv4Socket::all_sockets()
{
Expand Down
2 changes: 1 addition & 1 deletion Kernel/Net/LocalSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

namespace Kernel {

static auto s_list = AK::make_singleton<Lockable<InlineLinkedList<LocalSocket>>>();
static AK::Singleton<Lockable<InlineLinkedList<LocalSocket>>> s_list;

Lockable<InlineLinkedList<LocalSocket>>& LocalSocket::all_sockets()
{
Expand Down
2 changes: 1 addition & 1 deletion Kernel/Net/LoopbackAdapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

namespace Kernel {

static auto s_loopback = AK::make_singleton<LoopbackAdapter>();
static AK::Singleton<LoopbackAdapter> s_loopback;

LoopbackAdapter& LoopbackAdapter::the()
{
Expand Down
2 changes: 1 addition & 1 deletion Kernel/Net/NetworkAdapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

namespace Kernel {

static auto s_table = AK::make_singleton<Lockable<HashTable<NetworkAdapter*>>>();
static AK::Singleton<Lockable<HashTable<NetworkAdapter*>>> s_table;

static Lockable<HashTable<NetworkAdapter*>>& all_adapters()
{
Expand Down
2 changes: 1 addition & 1 deletion Kernel/Net/Routing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

namespace Kernel {

static auto s_arp_table = AK::make_singleton<Lockable<HashMap<IPv4Address, MACAddress>>>();
static AK::Singleton<Lockable<HashMap<IPv4Address, MACAddress>>> s_arp_table;

Lockable<HashMap<IPv4Address, MACAddress>>& arp_table()
{
Expand Down
4 changes: 2 additions & 2 deletions Kernel/Net/TCPSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ void TCPSocket::set_state(State new_state)
}
}

static auto s_socket_closing = AK::make_singleton<Lockable<HashMap<IPv4SocketTuple, RefPtr<TCPSocket>>>>();
static AK::Singleton<Lockable<HashMap<IPv4SocketTuple, RefPtr<TCPSocket>>>> s_socket_closing;

Lockable<HashMap<IPv4SocketTuple, RefPtr<TCPSocket>>>& TCPSocket::closing_sockets()
{
return *s_socket_closing;
}

static auto s_socket_tuples = AK::make_singleton<Lockable<HashMap<IPv4SocketTuple, TCPSocket*>>>();
static AK::Singleton<Lockable<HashMap<IPv4SocketTuple, TCPSocket*>>> s_socket_tuples;

Lockable<HashMap<IPv4SocketTuple, TCPSocket*>>& TCPSocket::sockets_by_tuple()
{
Expand Down
2 changes: 1 addition & 1 deletion Kernel/Net/UDPSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void UDPSocket::for_each(Function<void(const UDPSocket&)> callback)
callback(*it.value);
}

static auto s_map = AK::make_singleton<Lockable<HashMap<u16, UDPSocket*>>>();
static AK::Singleton<Lockable<HashMap<u16, UDPSocket*>>> s_map;

Lockable<HashMap<u16, UDPSocket*>>& UDPSocket::sockets_by_port()
{
Expand Down
2 changes: 1 addition & 1 deletion Kernel/Random.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

namespace Kernel {

static auto s_the = AK::make_singleton<KernelRng>();
static AK::Singleton<KernelRng> s_the;

KernelRng& KernelRng::the()
{
Expand Down
2 changes: 1 addition & 1 deletion Kernel/SharedBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

namespace Kernel {

static auto s_map = AK::make_singleton<Lockable<HashMap<int, NonnullOwnPtr<SharedBuffer>>>>();
static AK::Singleton<Lockable<HashMap<int, NonnullOwnPtr<SharedBuffer>>>> s_map;

Lockable<HashMap<int, NonnullOwnPtr<SharedBuffer>>>& shared_buffers()
{
Expand Down
2 changes: 1 addition & 1 deletion Kernel/TTY/PTYMultiplexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
namespace Kernel {

static const unsigned s_max_pty_pairs = 8;
static auto s_the = AK::make_singleton<PTYMultiplexer>();
static AK::Singleton<PTYMultiplexer> s_the;

PTYMultiplexer& PTYMultiplexer::the()
{
Expand Down
2 changes: 1 addition & 1 deletion Kernel/Time/TimeManagement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

namespace Kernel {

static auto s_the = AK::make_singleton<TimeManagement>();
static AK::Singleton<TimeManagement> s_the;

TimeManagement& TimeManagement::the()
{
Expand Down
2 changes: 1 addition & 1 deletion Kernel/TimerQueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

namespace Kernel {

static auto s_the = AK::make_singleton<TimerQueue>();
static AK::Singleton<TimerQueue> s_the;

TimerQueue& TimerQueue::the()
{
Expand Down
2 changes: 1 addition & 1 deletion Kernel/VM/PageDirectory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ 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 = AK::make_singleton<HashMap<u32, PageDirectory*>>();
static AK::Singleton<HashMap<u32, PageDirectory*>> s_cr3_map;

static HashMap<u32, PageDirectory*>& cr3_map()
{
Expand Down

0 comments on commit 5a98e32

Please sign in to comment.