From 57497c6ab24e3aacf41a67a49e77d18d339cb799 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Fri, 6 Oct 2023 16:13:42 +0100 Subject: [PATCH] LibCore: Add a Core::System wrapper for memory allocation Allocating raw memory isn't something we do often, but it does happen. Let's make it comfier. --- Userland/Libraries/LibCore/System.cpp | 8 ++++++++ Userland/Libraries/LibCore/System.h | 2 ++ 2 files changed, 10 insertions(+) diff --git a/Userland/Libraries/LibCore/System.cpp b/Userland/Libraries/LibCore/System.cpp index 01b882c76c..c7cc62c1aa 100644 --- a/Userland/Libraries/LibCore/System.cpp +++ b/Userland/Libraries/LibCore/System.cpp @@ -1837,4 +1837,12 @@ ErrorOr current_executable_path() return String::from_utf8({ path, strlen(path) }); } +ErrorOr allocate(size_t count, size_t size) +{ + auto* data = static_cast(calloc(count, size)); + if (!data) + return Error::from_errno(errno); + return Bytes { data, size * count }; +} + } diff --git a/Userland/Libraries/LibCore/System.h b/Userland/Libraries/LibCore/System.h index c905dae160..1774be6de7 100644 --- a/Userland/Libraries/LibCore/System.h +++ b/Userland/Libraries/LibCore/System.h @@ -280,4 +280,6 @@ char** environment(); ErrorOr current_executable_path(); +ErrorOr allocate(size_t count, size_t size); + }