浏览代码

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.
Sam Atkins 1 年之前
父节点
当前提交
57497c6ab2
共有 2 个文件被更改,包括 10 次插入0 次删除
  1. 8 0
      Userland/Libraries/LibCore/System.cpp
  2. 2 0
      Userland/Libraries/LibCore/System.h

+ 8 - 0
Userland/Libraries/LibCore/System.cpp

@@ -1837,4 +1837,12 @@ ErrorOr<String> current_executable_path()
     return String::from_utf8({ path, strlen(path) });
 }
 
+ErrorOr<Bytes> allocate(size_t count, size_t size)
+{
+    auto* data = static_cast<u8*>(calloc(count, size));
+    if (!data)
+        return Error::from_errno(errno);
+    return Bytes { data, size * count };
+}
+
 }

+ 2 - 0
Userland/Libraries/LibCore/System.h

@@ -280,4 +280,6 @@ char** environment();
 
 ErrorOr<String> current_executable_path();
 
+ErrorOr<Bytes> allocate(size_t count, size_t size);
+
 }