Merge branch 'malloc-usable-size' into 'master'
Implement malloc_usable_size See merge request redox-os/relibc!860
This commit is contained in:
+1
-1
Submodule dlmalloc-rs updated: e4ba849349...3b0ee3b467
@@ -40,3 +40,9 @@ pub unsafe extern "C" fn pvalloc(size: size_t) -> *mut c_void {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// See <https://man7.org/linux/man-pages/man3/malloc_usable_size.3.html>.
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn malloc_usable_size(ptr: *mut c_void) -> size_t {
|
||||
platform::alloc_usable_size(ptr)
|
||||
}
|
||||
|
||||
@@ -97,3 +97,10 @@ pub unsafe fn free(ptr: *mut c_void) {
|
||||
}
|
||||
(*ALLOCATOR.get()).lock().free(ptr.cast())
|
||||
}
|
||||
|
||||
pub unsafe fn alloc_usable_size(ptr: *mut c_void) -> size_t {
|
||||
if ptr.is_null() {
|
||||
return 0;
|
||||
}
|
||||
(*ALLOCATOR.get()).lock().usable_size(ptr.cast())
|
||||
}
|
||||
|
||||
@@ -49,6 +49,7 @@ EXPECT_NAMES=\
|
||||
locale/duplocale \
|
||||
locale/newlocale \
|
||||
locale/setlocale \
|
||||
malloc/usable_size \
|
||||
math \
|
||||
regex \
|
||||
select \
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
malloc: 27 bytes
|
||||
malloc_usable_size: 48 bytes
|
||||
@@ -0,0 +1,2 @@
|
||||
malloc: 27 bytes
|
||||
malloc_usable_size: 48 bytes
|
||||
@@ -0,0 +1,31 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <malloc.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
int main(void) {
|
||||
size_t request_size = 27;
|
||||
char *ptr = (char *)malloc(request_size);
|
||||
|
||||
if (!ptr) {
|
||||
fprintf(stderr, "Allocation failed\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
size_t actual_size = malloc_usable_size(ptr);
|
||||
|
||||
printf("malloc: %zu bytes\n", request_size);
|
||||
printf("malloc_usable_size: %zu bytes\n", actual_size);
|
||||
|
||||
assert(actual_size >= request_size);
|
||||
memset(ptr, 'A', actual_size);
|
||||
ptr[actual_size - 1] = '\0';
|
||||
|
||||
assert(ptr[0] == 'A');
|
||||
assert(ptr[actual_size - 2] == 'A');
|
||||
assert(ptr[actual_size - 1] == '\0');
|
||||
free(ptr);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user