vertexclique commented on a change in pull request #7061:
URL: https://github.com/apache/arrow/pull/7061#discussion_r419000857



##########
File path: rust/arrow/src/memory.rs
##########
@@ -20,34 +20,56 @@
 
 use std::alloc::Layout;
 use std::mem::align_of;
+use std::ptr::NonNull;
 
 pub const ALIGNMENT: usize = 64;
 
+///
+/// As you can see this is global and lives as long as the program lives.
+/// Be careful to not write anything to this pointer in any scenario.
+/// If you use allocation methods shown here you won't have any problems.
+const BYPASS_PTR: NonNull<u8> = unsafe { NonNull::new_unchecked(0xDEADBEEF as 
*mut u8) };
+
 pub fn allocate_aligned(size: usize) -> *mut u8 {
     unsafe {
-        let layout = Layout::from_size_align_unchecked(size, ALIGNMENT);
-        std::alloc::alloc_zeroed(layout)
+        if size == 0 {
+            // In a perfect world, there is no need to request zero size 
allocation.
+            // Currently, passing zero sized layout to alloc is UB.
+            // This will dodge allocator api for any type.
+            BYPASS_PTR.as_mut()
+        } else {
+            let layout = Layout::from_size_align_unchecked(size, ALIGNMENT);
+            std::alloc::alloc_zeroed(layout)
+        }
     }
 }
 
-pub unsafe fn free_aligned(p: *mut u8, size: usize) {
-    std::alloc::dealloc(p, Layout::from_size_align_unchecked(size, ALIGNMENT));
+pub unsafe fn free_aligned(ptr: *mut u8, size: usize) {
+    if size != 0x00 && ptr != BYPASS_PTR.as_mut() {
+        std::alloc::dealloc(ptr, Layout::from_size_align_unchecked(size, 
ALIGNMENT));
+    }
 }
 
 pub unsafe fn reallocate(ptr: *mut u8, old_size: usize, new_size: usize) -> 
*mut u8 {
-    let new_ptr = std::alloc::realloc(
-        ptr,
-        Layout::from_size_align_unchecked(old_size, ALIGNMENT),
-        new_size,
-    );
-    if !new_ptr.is_null() && new_size > old_size {
-        new_ptr.add(old_size).write_bytes(0, new_size - old_size);
+    if old_size == 0x00 && ptr == BYPASS_PTR.as_mut() {

Review comment:
       Addressed.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to