Skip to content

Commit e6f3c18

Browse files
authored
Merge pull request #28 from phil-opp/fix-alloc-ref
AllocRef::alloc is now safe and allows zero-sized allocations
2 parents 28f92da + f148923 commit e6f3c18

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/lib.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#![feature(const_fn)]
2-
#![cfg_attr(feature = "alloc_ref", feature(allocator_api))]
2+
#![cfg_attr(feature = "alloc_ref", feature(allocator_api, alloc_layout_extra))]
33
#![no_std]
44

55
#[cfg(test)]
@@ -133,15 +133,20 @@ impl Heap {
133133

134134
#[cfg(feature = "alloc_ref")]
135135
unsafe impl AllocRef for Heap {
136-
unsafe fn alloc(&mut self, layout: Layout) -> Result<(NonNull<u8>, usize), AllocErr> {
136+
fn alloc(&mut self, layout: Layout) -> Result<(NonNull<u8>, usize), AllocErr> {
137+
if layout.size() == 0 {
138+
return Ok((layout.dangling(), 0));
139+
}
137140
match self.allocate_first_fit(layout) {
138141
Ok(ptr) => Ok((ptr, layout.size())),
139142
Err(()) => Err(AllocErr),
140143
}
141144
}
142145

143146
unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout) {
144-
self.deallocate(ptr, layout)
147+
if layout.size() != 0 {
148+
self.deallocate(ptr, layout);
149+
}
145150
}
146151
}
147152

0 commit comments

Comments
 (0)