improved alloc-dealloc structure

This commit is contained in:
Gabriel mermelstein 2025-04-17 11:40:16 +03:00
parent f70532403a
commit fe41628527
No known key found for this signature in database
GPG Key ID: 82B8134785FEAE0D
3 changed files with 13 additions and 28 deletions

View File

@ -226,18 +226,11 @@ proc WrapOutgoingMessage(
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
return RET_ERR
var msg = allocSharedSeqFromCArray(cast[ptr byte](message), messageLen.int)
let msgId = messageId.alloc()
defer:
deallocSharedSeq(msg)
deallocShared(msgId)
handleRequest(
ctx,
RequestType.MESSAGE,
SdsMessageRequest.createShared(
SdsMessageMsgType.WRAP_MESSAGE, msg, messageLen, msgId
SdsMessageMsgType.WRAP_MESSAGE, message, messageLen, messageId
),
callback,
userData,
@ -258,15 +251,12 @@ proc UnwrapReceivedMessage(
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
return RET_ERR
var msg = allocSharedSeqFromCArray(cast[ptr byte](message), messageLen.int)
defer:
deallocSharedSeq(msg)
handleRequest(
ctx,
RequestType.MESSAGE,
SdsMessageRequest.createShared(SdsMessageMsgType.UNWRAP_MESSAGE, msg, messageLen),
SdsMessageRequest.createShared(
SdsMessageMsgType.UNWRAP_MESSAGE, message, messageLen
),
callback,
userData,
)
@ -286,11 +276,6 @@ proc MarkDependenciesMet(
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
return RET_ERR
var messageIds = allocSharedSeqFromCArray(cast[ptr cstring](messageIds), count.int)
defer:
deallocSharedSeq(messageIds)
handleRequest(
ctx,
RequestType.DEPENDENCIES,

View File

@ -15,18 +15,17 @@ type SdsDependenciesRequest* = object
proc createShared*(
T: type SdsDependenciesRequest,
op: SdsDependenciesMsgType,
messageIds: SharedSeq[cstring],
messageIds: pointer,
count: csize_t = 0,
): ptr type T =
var ret = createShared(T)
ret[].operation = op
ret[].messageIds = messageIds # check if alloc is needed
ret[].count = count
ret[].messageIds = allocSharedSeqFromCArray(cast[ptr cstring](messageIds), count.int)
return ret
proc destroyShared(self: ptr SdsDependenciesRequest) =
#deallocShared(self[].message)
#deallocShared(self[].messageId)
deallocSharedSeq(self[].messageIds)
deallocShared(self)
proc process*(

View File

@ -21,20 +21,21 @@ type SdsUnwrapResponse* = object
proc createShared*(
T: type SdsMessageRequest,
op: SdsMessageMsgType,
message: SharedSeq[byte],
message: pointer,
messageLen: csize_t = 0,
messageId: cstring = "",
): ptr type T =
var ret = createShared(T)
ret[].operation = op
ret[].message = message # check if alloc is needed
ret[].messageLen = messageLen
ret[].messageId = messageId # check if alloc is needed
ret[].messageId = messageId.alloc()
ret[].message = allocSharedSeqFromCArray(cast[ptr byte](message), messageLen.int)
return ret
proc destroyShared(self: ptr SdsMessageRequest) =
#deallocShared(self[].message)
#deallocShared(self[].messageId)
deallocSharedSeq(self[].message)
deallocShared(self[].messageId)
deallocShared(self)
proc process*(