Skip to content

Commit d4c7e01

Browse files
authored
Pass continuations for success and failure cases (#788)
This PR provides a templated parameter to the allocation routines. This can be used to add special behaviour in both the successful allocation behaviour, and in the failing to allocate cases. The intent of this is two enable two future features * set_new_handler - so that the failure case doesn't just set ENOMEM, and return nullptr. But can handle both the Windows and C++ versions of (_)set_new_handler. * The success handler can be used to add checking, zeroing and in the future storing precise size information in metadata for each allocation.
1 parent 012138e commit d4c7e01

File tree

14 files changed

+142
-116
lines changed

14 files changed

+142
-116
lines changed

fuzzing/snmalloc-fuzzer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,15 +166,15 @@ void snmalloc_random_walk(
166166
case EventKind::AllocZero:
167167
{
168168
auto ptr =
169-
static_cast<char*>(scoped->alloc<snmalloc::YesZero>(e.size_or_index));
169+
static_cast<char*>(scoped->alloc<snmalloc::Zero>(e.size_or_index));
170170
results.emplace_back(0, ptr, e.size_or_index);
171171
break;
172172
}
173173

174174
case EventKind::AllocNoZero:
175175
{
176176
auto ptr =
177-
static_cast<char*>(scoped->alloc<snmalloc::NoZero>(e.size_or_index));
177+
static_cast<char*>(scoped->alloc<snmalloc::Uninit>(e.size_or_index));
178178
std::fill(ptr, ptr + e.size_or_index, e.filler);
179179
results.emplace_back(e.filler, ptr, e.size_or_index);
180180
break;

src/snmalloc/backend/backend.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ namespace snmalloc
5959

6060
if (p == nullptr)
6161
{
62-
errno = ENOMEM;
6362
return nullptr;
6463
}
6564

@@ -112,7 +111,6 @@ namespace snmalloc
112111

113112
if (meta == nullptr)
114113
{
115-
errno = ENOMEM;
116114
return {nullptr, nullptr};
117115
}
118116

@@ -124,7 +122,6 @@ namespace snmalloc
124122
if (p == nullptr)
125123
{
126124
local_state.get_meta_range().dealloc_range(meta_cap, meta_size);
127-
errno = ENOMEM;
128125
#ifdef SNMALLOC_TRACING
129126
message<1024>("Out of memory");
130127
#endif

src/snmalloc/global/globalalloc.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -321,24 +321,24 @@ namespace snmalloc
321321
return sizeclass_full_to_size(entry.get_sizeclass());
322322
}
323323

324-
template<size_t size, ZeroMem zero_mem = NoZero, size_t align = 1>
324+
template<size_t size, typename Conts = Uninit, size_t align = 1>
325325
SNMALLOC_FAST_PATH_INLINE void* alloc()
326326
{
327-
return ThreadAlloc::get().alloc<zero_mem, ThreadAlloc::CheckInit>(
327+
return ThreadAlloc::get().alloc<Conts, ThreadAlloc::CheckInit>(
328328
aligned_size(align, size));
329329
}
330330

331-
template<ZeroMem zero_mem = NoZero, size_t align = 1>
331+
template<typename Conts = Uninit, size_t align = 1>
332332
SNMALLOC_FAST_PATH_INLINE void* alloc(size_t size)
333333
{
334-
return ThreadAlloc::get().alloc<zero_mem, ThreadAlloc::CheckInit>(
334+
return ThreadAlloc::get().alloc<Conts, ThreadAlloc::CheckInit>(
335335
aligned_size(align, size));
336336
}
337337

338-
template<ZeroMem zero_mem = NoZero>
338+
template<typename Conts = Uninit>
339339
SNMALLOC_FAST_PATH_INLINE void* alloc_aligned(size_t align, size_t size)
340340
{
341-
return ThreadAlloc::get().alloc<zero_mem, ThreadAlloc::CheckInit>(
341+
return ThreadAlloc::get().alloc<Conts, ThreadAlloc::CheckInit>(
342342
aligned_size(align, size));
343343
}
344344

src/snmalloc/global/libc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ namespace snmalloc::libc
4747
{
4848
return set_error();
4949
}
50-
return alloc<ZeroMem::YesZero>(sz);
50+
return alloc<Zero>(sz);
5151
}
5252

5353
SNMALLOC_FAST_PATH_INLINE void* realloc(void* ptr, size_t size)

0 commit comments

Comments
 (0)