Skip to content

Commit dff7df2

Browse files
committed
Remove duplicate code from allocator_api
1 parent 26f6041 commit dff7df2

File tree

1 file changed

+8
-29
lines changed

1 file changed

+8
-29
lines changed

src/lib.rs

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ mod llff {
7676
critical_section::with(|cs| self.heap.borrow(cs).borrow_mut().free())
7777
}
7878

79-
unsafe fn alloc(&self, layout: Layout) -> Option<NonNull<u8>> {
79+
fn alloc(&self, layout: Layout) -> Option<NonNull<u8>> {
8080
critical_section::with(|cs| {
8181
self.heap
8282
.borrow(cs)
@@ -119,25 +119,15 @@ mod llff {
119119
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
120120
match layout.size() {
121121
0 => Ok(NonNull::slice_from_raw_parts(layout.dangling(), 0)),
122-
size => critical_section::with(|cs| {
123-
self.heap
124-
.borrow(cs)
125-
.borrow_mut()
126-
.allocate_first_fit(layout)
127-
.map(|allocation| NonNull::slice_from_raw_parts(allocation, size))
128-
.map_err(|_| AllocError)
122+
size => self.alloc(layout).map_or(Err(AllocError), |allocation| {
123+
Ok(NonNull::slice_from_raw_parts(allocation, size))
129124
}),
130125
}
131126
}
132127

133128
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
134129
if layout.size() != 0 {
135-
critical_section::with(|cs| {
136-
self.heap
137-
.borrow(cs)
138-
.borrow_mut()
139-
.deallocate(NonNull::new_unchecked(ptr.as_ptr()), layout)
140-
});
130+
self.dealloc(ptr.as_ptr(), layout);
141131
}
142132
}
143133
}
@@ -201,7 +191,7 @@ mod tlsf {
201191
});
202192
}
203193

204-
unsafe fn alloc(&self, layout: Layout) -> Option<NonNull<u8>> {
194+
fn alloc(&self, layout: Layout) -> Option<NonNull<u8>> {
205195
critical_section::with(|cs| self.heap.borrow(cs).borrow_mut().allocate(layout))
206196
}
207197

@@ -238,26 +228,15 @@ mod tlsf {
238228
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
239229
match layout.size() {
240230
0 => Ok(NonNull::slice_from_raw_parts(layout.dangling(), 0)),
241-
size => critical_section::with(|cs| {
242-
self.heap
243-
.borrow(cs)
244-
.borrow_mut()
245-
.allocate(layout)
246-
.map_or(Err(AllocError), |allocation| {
247-
Ok(NonNull::slice_from_raw_parts(allocation, size))
248-
})
231+
size => self.alloc(layout).map_or(Err(AllocError), |allocation| {
232+
Ok(NonNull::slice_from_raw_parts(allocation, size))
249233
}),
250234
}
251235
}
252236

253237
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
254238
if layout.size() != 0 {
255-
critical_section::with(|cs| {
256-
self.heap
257-
.borrow(cs)
258-
.borrow_mut()
259-
.deallocate(NonNull::new_unchecked(ptr.as_ptr()), layout.align())
260-
});
239+
self.dealloc(ptr.as_ptr(), layout);
261240
}
262241
}
263242
}

0 commit comments

Comments
 (0)