zeroshade commented on a change in pull request #11206:
URL: https://github.com/apache/arrow/pull/11206#discussion_r718527172



##########
File path: go/arrow/memory/cgo_allocator.go
##########
@@ -0,0 +1,108 @@
+// 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.
+
+// +build cgo
+// +build ccalloc
+
+package memory
+
+import (
+       "runtime"
+
+       cga "github.com/apache/arrow/go/arrow/memory/internal/cgoalloc"
+)
+
+// CgoArrowAllocator is an allocator which exposes the C++ memory pool class
+// from the Arrow C++ Library as an allocator for memory buffers to use in Go.
+// The build tag 'ccalloc' must be used in order to include it as it requires
+// linking against the arrow library.
+//
+// The primary reason to use this would be as an allocator when dealing with
+// exporting data across the cdata interface in order to ensure that the memory
+// is allocated safely on the C side so it can be held on the CGO side beyond
+// the context of a single function call. If the memory in use isn't allocated
+// on the C side, then it is not safe for any pointers to data to be held 
outside
+// of Go beyond the context of a single Cgo function call as it will be 
invisible
+// to the Go garbage collector and could potentially get moved without being 
updated.

Review comment:
       When making a CGO call, during the execution of it the Go garbage 
collector will pin memory being used so that any pointers to go memory will 
remain valid during the length of that cgo call. But the documentation is very 
clear that it is *not* safe to let C/C++ maintain pointers to Go memory beyond 
the context of that single call because after the cgo call returns, the Go 
garbage collector is free to move memory around as necessary. Normally the 
garbage collector can update any references in Go when it does this so that 
everything continues to work just fine, but it cannot update any pointers in 
C/C++ when it does this. 
   
   So the global container of exported arrays used by `releaseData` serves two 
purposes:
   
   1. Ensuring that during the context of a single CGO call that there is a 
maintained reference to the Go objects so that it keeps the garbage collector 
from cleaning it up
   2. Allowing C/C++ to call `releaseData` to free the memory if we're handing 
it off, or allowing the ability to hand off memory to let C/C++ own it and 
control when it is released. this is only safe if the underlying buffers were 
allocated by C and not Go allocated memory.
   
   For more information about this specifically in terms of passing pointers 
around: https://pkg.go.dev/cmd/cgo#hdr-Passing_pointers is the documentation on 
CGO that i'm referencing.




-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to