@@ -9,6 +9,7 @@ package kernel
99
1010// Bridge function: exported Go function that C library can call
1111extern int go_writer_callback_bridge(void* bytes, size_t size, void* userdata);
12+ extern int go_pre_alloc_writer_callback_bridge(void* bytes, size_t size, void* userdata);
1213*/
1314import "C"
1415import (
@@ -41,7 +42,14 @@ func go_writer_callback_bridge(bytes unsafe.Pointer, size C.size_t, userdata uns
4142// writeToBytes is a helper function that uses a callback pattern to collect bytes
4243// It takes a function that calls the C API with the writer callback
4344func writeToBytes (writerFunc func (C.btck_WriteBytes , unsafe.Pointer ) C.int ) ([]byte , error ) {
45+ return writeToPreAllocBytes (writerFunc , 0 )
46+ }
47+
48+ func writeToPreAllocBytes (writerFunc func (C.btck_WriteBytes , unsafe.Pointer ) C.int , preallocSize int ) ([]byte , error ) {
4449 callbackData := & writerCallbackData {}
50+ if preallocSize > 0 {
51+ callbackData .buffer = make ([]byte , 0 , preallocSize )
52+ }
4553
4654 // Create cgo handle for the callback data
4755 handle := cgo .NewHandle (callbackData )
@@ -57,6 +65,5 @@ func writeToBytes(writerFunc func(C.btck_WriteBytes, unsafe.Pointer) C.int) ([]b
5765 return nil , callbackData .err
5866 }
5967
60- // Return exactly the bytes that were written (slice the buffer to actual size)
6168 return callbackData .buffer , nil
6269}
0 commit comments