[ 
https://issues.apache.org/jira/browse/ARROW-2435?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16436730#comment-16436730
 ] 

ASF GitHub Bot commented on ARROW-2435:
---------------------------------------

liurenjie1024 commented on a change in pull request #1875: ARROW-2435: [Rust] 
Add memory pool abstraction.
URL: https://github.com/apache/arrow/pull/1875#discussion_r181276036
 
 

 ##########
 File path: rust/src/memory_pool.rs
 ##########
 @@ -0,0 +1,90 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use libc;
+use std::mem;
+use std::cmp;
+
+use super::error::ArrowError;
+use super::error::Result;
+
+const ALIGNMENT: usize = 64;
+
+/// Memory pool for allocating memory. It's also responsible for tracking 
memory usage.
+pub trait MemoryPool {
+    /// Allocate memory.
+    /// The implementation should ensures that allocated memory is aligned.
+    fn allocate(&self, size: usize) -> Result<*mut u8>;
+
+    /// Reallocate memory.
+    /// If the implementation doesn't support reallocating aligned memory, it 
allocates new memory
+    /// and copied old memory to it.
+    fn reallocate(&self, old_size: usize, new_size: usize, pointer: *mut u8) 
-> Result<*mut u8>;
+
+    /// Free memory.
+    fn free(&self, ptr: *mut u8);
+}
+
+/// Implementation of memory pool using libc api.
+#[allow(dead_code)]
+struct LibcMemoryPool;
+
+impl MemoryPool for LibcMemoryPool {
+    fn allocate(&self, size: usize) -> Result<*mut u8> {
+        unsafe {
+            let mut page: *mut libc::c_void = mem::uninitialized();
+            let result = libc::posix_memalign(&mut page, ALIGNMENT, size);
+            match result {
+                0 => Ok(mem::transmute::<*mut libc::c_void, *mut u8>(page)),
+                _ => Err(ArrowError::MemoryError(
+                    "Failed to allocate memory".to_string(),
+                )),
+            }
+        }
+    }
+
+    fn reallocate(&self, old_size: usize, new_size: usize, pointer: *mut u8) 
-> Result<*mut u8> {
+        unsafe {
+            let old_src = mem::transmute::<*mut u8, *mut 
libc::c_void>(pointer);
+            let result = self.allocate(new_size)?;
+            let dst = mem::transmute::<*mut u8, *mut libc::c_void>(result);
+            libc::memcpy(dst, old_src, cmp::min(old_size, new_size));
+            libc::free(old_src);
+            Ok(result)
+        }
+    }
+
+    fn free(&self, ptr: *mut u8) {
+        unsafe { libc::free(mem::transmute::<*mut u8, *mut libc::c_void>(ptr)) 
}
+    }
+}
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    #[test]
+    fn test_allocate() {
+        let memory_pool = LibcMemoryPool {};
+
+        for _ in 0..10 {
+            let p = memory_pool.allocate(1024).unwrap();
+            // make sure this is 64-byte aligned
+            assert_eq!(0, (p as usize) % 64);
+        }
 
 Review comment:
   Hi, @andygrove 
   
   Test  added. I tried also to test free but it seems that there is no easy 
way to test that.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on 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


> [Rust] Add memory pool abstraction.
> -----------------------------------
>
>                 Key: ARROW-2435
>                 URL: https://issues.apache.org/jira/browse/ARROW-2435
>             Project: Apache Arrow
>          Issue Type: Improvement
>          Components: Rust
>    Affects Versions: 0.9.0
>            Reporter: Renjie Liu
>            Assignee: Renjie Liu
>            Priority: Major
>              Labels: pull-request-available
>
> Add memory pool abstraction as the c++ api.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to