Skip to content

Commit

Permalink
Free cgo data
Browse files Browse the repository at this point in the history
  • Loading branch information
hkalodner authored and joshuacolvin0 committed Nov 30, 2021
1 parent c8c8efd commit cffb3a0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
4 changes: 2 additions & 2 deletions packages/arb-avm-cpp/cmachine/arbcore.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ func (ac *ArbCore) DeliverMessages(previousMessageCount *big.Int, previousSeqBat
previousMessageCountPtr := unsafeDataPointer(math.U256Bytes(previousMessageCount))
previousSeqBatchAccPtr := unsafeDataPointer(previousSeqBatchAcc.Bytes())
seqBatchItemsSlice := sequencerBatchItemsToByteSliceArray(seqBatchItems)
defer C.free(seqBatchItemsSlice.slices)
defer freeByteSliceArray(seqBatchItemsSlice)
delayedMessagesSlice := delayedMessagesToByteSliceArray(delayedMessages)
defer C.free(delayedMessagesSlice.slices)
defer freeByteSliceArray(delayedMessagesSlice)

var cReorgSeqBatchItemCount unsafe.Pointer
if reorgSeqBatchItemCount != nil {
Expand Down
15 changes: 2 additions & 13 deletions packages/arb-avm-cpp/cmachine/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,17 +171,6 @@ func (m *Machine) ExecuteAssertion(
)
}

// Note: the slices field of the returned struct needs manually freed by C.free
func bytesArrayToByteSliceArray(bytes [][]byte) C.struct_ByteSliceArrayStruct {
byteSlices := encodeByteSliceList(bytes)
sliceArrayData := C.malloc(C.size_t(C.sizeof_struct_ByteSliceStruct * len(byteSlices)))
sliceArray := (*[1 << 30]C.struct_ByteSliceStruct)(sliceArrayData)[:len(byteSlices):len(byteSlices)]
for i, data := range byteSlices {
sliceArray[i] = data
}
return C.struct_ByteSliceArrayStruct{slices: sliceArrayData, count: C.int(len(byteSlices))}
}

func (m *Machine) ExecuteAssertionAdvanced(
maxGas uint64,
goOverGas bool,
Expand All @@ -196,11 +185,11 @@ func (m *Machine) ExecuteAssertionAdvanced(
C.machineExecutionConfigSetMaxGas(conf, C.uint64_t(maxGas), boolToCInt(goOverGas))

msgData := bytesArrayToByteSliceArray(encodeMachineInboxMessages(messages))
defer C.free(msgData.slices)
defer freeByteSliceArray(msgData)
C.machineExecutionConfigSetInboxMessages(conf, msgData)

sideloadsData := bytesArrayToByteSliceArray(encodeInboxMessages(sideloads))
defer C.free(sideloadsData.slices)
defer freeByteSliceArray(sideloadsData)
C.machineExecutionConfigSetSideloads(conf, sideloadsData)

C.machineExecutionConfigSetStopOnSideload(conf, boolToCInt(stopOnSideload))
Expand Down
19 changes: 19 additions & 0 deletions packages/arb-avm-cpp/cmachine/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,25 @@ func receiveByteSliceArray(sliceArray C.ByteSliceArray) [][]byte {
return slices
}

// Note: the slices field of the returned struct needs manually freed by C.free
func bytesArrayToByteSliceArray(bytes [][]byte) C.struct_ByteSliceArrayStruct {
byteSlices := encodeByteSliceList(bytes)
sliceArrayData := C.malloc(C.size_t(C.sizeof_struct_ByteSliceStruct * len(byteSlices)))
sliceArray := (*[1 << 30]C.struct_ByteSliceStruct)(sliceArrayData)[:len(byteSlices):len(byteSlices)]
for i, data := range byteSlices {
sliceArray[i] = data
}
return C.struct_ByteSliceArrayStruct{slices: sliceArrayData, count: C.int(len(byteSlices))}
}

func freeByteSliceArray(sliceArray C.struct_ByteSliceArrayStruct) {
dataSlices := (*[1 << 30]C.struct_ByteSliceStruct)(unsafe.Pointer(sliceArray.slices))[:sliceArray.count:sliceArray.count]
for _, slice := range dataSlices {
C.free(slice.data)
}
C.free(sliceArray.slices)
}

func toByteSliceView(data []byte) C.ByteSlice {
return C.struct_ByteSliceStruct{data: C.CBytes(data), length: C.int(len(data))}
}
Expand Down

0 comments on commit cffb3a0

Please sign in to comment.