Skip to content

Commit

Permalink
rbd/encryption: Avoid a C memory allocation in EncryptionLoad2 API
Browse files Browse the repository at this point in the history
Instead of creating a slice based on a pre-allocated C memory array we
could directly go for a slice created via the built-in function make().

Signed-off-by: Anoop C S <anoopcs@cryptolab.net>
  • Loading branch information
anoopcs9 committed Feb 17, 2025
1 parent 48cf0b4 commit b3db13f
Showing 1 changed file with 3 additions and 6 deletions.
9 changes: 3 additions & 6 deletions rbd/encryption_load2.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,25 +81,22 @@ func (image *Image) EncryptionLoad2(opts []EncryptionOptions) error {
}

length := len(opts)
cspecs := (*C.rbd_encryption_spec_t)(C.malloc(
C.size_t(C.sizeof_rbd_encryption_spec_t * length)))
specs := unsafe.Slice(cspecs, length)
cspecs := make([]C.rbd_encryption_spec_t, length)
freeFuncs := make([]func(), length)

for idx, option := range opts {
f := option.(encryptionOptions2).writeEncryptionSpec(&specs[idx])
f := option.(encryptionOptions2).writeEncryptionSpec(&cspecs[idx])
freeFuncs[idx] = f
}
defer func() {
for _, f := range freeFuncs {
f()
}
C.free(unsafe.Pointer(cspecs))
}()

ret := C.rbd_encryption_load2(
image.image,
cspecs,
(*C.rbd_encryption_spec_t)(unsafe.Pointer(&cspecs[0])),
C.size_t(length))
return getError(ret)
}

0 comments on commit b3db13f

Please sign in to comment.