MCPcopy Create free account
hub / github.com/HaxeFoundation/hxcpp / alloc

Method alloc

src/hx/thread/Scratch.cpp:5–41  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3#include "ThreadImpl.hpp"
4
5hx::thread::Scratch hx::thread::Scratch::alloc(int bytes)
6{
7 auto current = reinterpret_cast<hx::thread::ThreadImpl_obj*>(hx::thread::Thread_obj::current().GetPtr());
8 auto required = bytes + sizeof(int);
9
10 // If there is not enough space in the thread array then do a standard malloc.
11 // Maybe we want some warning log message so the user knows it's hitting a slower path?
12 if (required > static_cast<uint64_t>(current->scratch->length) - current->cursor)
13 {
14 auto alloc = std::malloc(required);
15 auto storage = new(alloc) int;
16 auto view = cpp::marshal::View<uint8_t>(static_cast<uint8_t*>(alloc) + sizeof(int), bytes);
17
18 std::memset(alloc, 0, required);
19
20 return
21 hx::thread::Scratch(storage, view, [](cpp::marshal::View<uint8_t> view) {
22 std::free(view.ptr.ptr - sizeof(int));
23 });
24 }
25 else
26 {
27 auto storage = new(current->scratch->GetBase() + current->cursor) int;
28 auto view = cpp::marshal::View<uint8_t>(current->scratch->GetBase() + current->cursor + sizeof(int), bytes);
29
30 current->cursor += bytes + sizeof(int);
31
32 return
33 hx::thread::Scratch(storage, view, [](cpp::marshal::View<uint8_t> view) {
34 view.fill(0);
35
36 auto current = reinterpret_cast<hx::thread::ThreadImpl_obj*>(hx::thread::Thread_obj::current().GetPtr());
37
38 current->cursor -= view.length + sizeof(int);
39 });
40 }
41}
42
43hx::thread::Scratch::Scratch(int* _count, cpp::marshal::View<uint8_t> _view, ReleaseFunc _release) : count(_count), view(_view), release(_release)
44{

Callers

nothing calls this directly

Calls 3

ScratchClass · 0.85
GetPtrMethod · 0.80
fillMethod · 0.80

Tested by

no test coverage detected