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

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

xhochy closed pull request #1875: ARROW-2435: [Rust] Add memory pool 
abstraction.
URL: https://github.com/apache/arrow/pull/1875
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/.gitignore b/.gitignore
index c902ba39c..d9c69e954 100644
--- a/.gitignore
+++ b/.gitignore
@@ -30,6 +30,7 @@ MANIFEST
 *.vcxproj
 *.vcxproj.*
 *.sln
+*.iml
 
 cpp/.idea/
 python/.eggs/
diff --git a/rust/src/error.rs b/rust/src/error.rs
index 6a342e063..d82ee1190 100644
--- a/rust/src/error.rs
+++ b/rust/src/error.rs
@@ -20,3 +20,5 @@ pub enum ArrowError {
     MemoryError(String),
     ParseError(String),
 }
+
+pub type Result<T> = ::std::result::Result<T, ArrowError>;
diff --git a/rust/src/lib.rs b/rust/src/lib.rs
index 6ab3daabb..2d2274029 100644
--- a/rust/src/lib.rs
+++ b/rust/src/lib.rs
@@ -29,3 +29,4 @@ pub mod datatypes;
 pub mod error;
 pub mod list;
 pub mod memory;
+pub mod memory_pool;
diff --git a/rust/src/memory_pool.rs b/rust/src/memory_pool.rs
new file mode 100644
index 000000000..acfcc3071
--- /dev/null
+++ b/rust/src/memory_pool.rs
@@ -0,0 +1,105 @@
+// 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) % ALIGNMENT);
+            memory_pool.free(p);
+        }
+    }
+
+    #[test]
+    fn test_reallocate() {
+        let memory_pool = LibcMemoryPool {};
+
+        for _ in 0..10 {
+            let p1 = memory_pool.allocate(1024).unwrap();
+            let p2 = memory_pool.reallocate(1024, 2048, p1).unwrap();
+            // make sure this is 64-byte aligned
+            assert_eq!(0, (p1 as usize) % ALIGNMENT);
+            assert_eq!(0, (p2 as usize) % ALIGNMENT);
+            memory_pool.free(p2);
+        }
+    }
+}


 

----------------------------------------------------------------
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
>             Fix For: 0.10.0
>
>
> Add memory pool abstraction as the c++ api.



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

Reply via email to