Use a pointer to free the memory in destroyShared

This commit is contained in:
Arnaud 2026-06-13 20:03:03 +04:00
parent aef4fcc3d4
commit afc51ad5c7
No known key found for this signature in database
GPG Key ID: A6C7C781817146FA
2 changed files with 22 additions and 8 deletions

View File

@ -296,11 +296,8 @@ proc storage_upload_chunk(
initializeLibrary()
checkLibstorageParams(ctx, callback, userData)
let chunk = newSeq[byte](len)
copyMem(addr chunk[0], data, len)
let reqContent = NodeUploadRequest.createShared(
NodeUploadMsgType.CHUNK, sessionId = sessionId, chunk = chunk
NodeUploadMsgType.CHUNK, sessionId = sessionId, chunkData = data, chunkLen = len.int
)
let res = storage_context.sendRequestToStorageThread(
ctx, RequestType.UPLOAD, reqContent, callback, userData

View File

@ -47,7 +47,9 @@ type NodeUploadRequest* = object
operation: NodeUploadMsgType
sessionId: cstring
filepath: cstring
chunk: seq[byte]
# Use a pointer to free the memory in destroyShared
chunk: pointer
chunkLen: int
chunkSize: csize_t
type
@ -68,21 +70,30 @@ proc createShared*(
op: NodeUploadMsgType,
sessionId: cstring = "",
filepath: cstring = "",
chunk: seq[byte] = @[],
chunkData: ptr byte = nil,
chunkLen: int = 0,
chunkSize: csize_t = 0,
): ptr type T =
var ret = createShared(T)
ret[].operation = op
ret[].sessionId = sessionId.alloc()
ret[].filepath = filepath.alloc()
ret[].chunk = chunk
ret[].chunkLen = chunkLen
ret[].chunkSize = chunkSize
if chunkLen > 0 and chunkData != nil:
ret[].chunk = allocShared(chunkLen)
copyMem(ret[].chunk, chunkData, chunkLen)
return ret
proc destroyShared*(self: ptr NodeUploadRequest) =
deallocShared(self[].filepath)
deallocShared(self[].sessionId)
if self[].chunk != nil:
deallocShared(self[].chunk)
deallocShared(self)
proc init(
@ -347,7 +358,13 @@ proc process*(
return err($res.error)
return res
of NodeUploadMsgType.CHUNK:
let res = (await chunk(storage, self.sessionId, self.chunk))
# self.chunk is a raw memory pointer, but chunk() takes a seq[byte],
# so we copy the bytes into a new seq.
var data = newSeq[byte](self.chunkLen)
if self.chunkLen > 0:
copyMem(addr data[0], self.chunk, self.chunkLen)
let res = (await chunk(storage, self.sessionId, data))
if res.isErr:
error "Failed to CHUNK.", error = res.error
return err($res.error)