On Fri, Feb 2, 2024 at 10:05 AM Chong Yuan <chong.y.y...@gmail.com> wrote:
>
> Here is the example in our gorocksdb:
>
> // Get returns the data associated with the key from the database.
>
> func (db *DB) Get(opts *ReadOptions, key []byte) (*Slice, error) {
>
>                var (
>
>                               cErr    *C.char
>
>                               cValLen C.size_t
>
>                               cKey    = byteToChar(key)
>
>                )
>
>                cValue := C.rocksdb_get(db.c, opts.c, cKey, 
> C.size_t(len(key)), &cValLen, &cErr)
>
>                runtime.KeepAlive(key)  //<==this is the customization we 
> added.
>
>                if cErr != nil {
>
>                               defer C.rocksdb_free(unsafe.Pointer(cErr))
>
>                               return nil, errors.New(C.GoString(cErr))
>
>                }
>
>                return NewSlice(cValue, cValLen), nil
>
> }
>
> // byteToChar returns *C.char from byte slice.
>
> func byteToChar(b []byte) *C.char {
>
>                var c *C.char
>
>                if len(b) > 0 {
>
>                               c = (*C.char)(unsafe.Pointer(&b[0]))
>
>                }
>
>                return c
>
> }
>
> My question is: if the keepalive(key) is necessary above. Will the "key" be 
> GC-ed when calling into C.rocksdb_get(). Does the compiler know there’s a 
> reference to “key” (using unsafe.pointer) and not collect “key” when running 
> GC?

That call to runtime.KeepAlive does not look necessary.  Pointers
passed to cgo functions are kept alive for the duration of the call.
The pointer returned by byteToChar will keep the slice's backing array
alive.

Ian

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAOyqgcWv-JY_iA-TQW3Vvob-T9wmPxHW5fFPrreBJdvG-z%3D7YA%40mail.gmail.com.

Reply via email to