From d7d2d3b9d5107acbbd01fad4a7a6c86cb06b2084 Mon Sep 17 00:00:00 2001 From: saxon Date: Sat, 18 Aug 2018 15:48:49 +0930 Subject: [PATCH] Got rid of all C.realloc and replaced with my realloc func - tested and working --- rtmp/rtmp.go | 72 +++++++++++++++++++++++++++++++++------------------- 1 file changed, 46 insertions(+), 26 deletions(-) diff --git a/rtmp/rtmp.go b/rtmp/rtmp.go index fa881f79..ceea4b62 100644 --- a/rtmp/rtmp.go +++ b/rtmp/rtmp.go @@ -71,6 +71,7 @@ import ( "unsafe" "github.com/chamaken/cgolmnl/inet" + "github.com/cznic/crt" ) const ( @@ -2585,12 +2586,16 @@ func C_RTMP_ReadPacket(r *C_RTMP, packet *C_RTMPPacket) int32 { if packet.m_nChannel >= r.m_channelsAllocatedIn { var n int32 = int32(packet.m_nChannel + 10) - timestamp := (*int32)(C.realloc(unsafe.Pointer(r.m_channelTimestamp), - C.size_t(int32(unsafe.Sizeof(n))*n))) + timestamp := (*int32)(realloc(unsafe.Pointer(r.m_channelTimestamp), + uint32(int32(unsafe.Sizeof(n))*n))) + //timestamp := (*int32)(C.realloc(unsafe.Pointer(r.m_channelTimestamp), + //C.size_t(int32(unsafe.Sizeof(n))*n))) var packetPtr *C_RTMPPacket - packets := (**C_RTMPPacket)(C.realloc(unsafe.Pointer(r.m_vecChannelsIn), - C.size_t(int32(unsafe.Sizeof(packetPtr))*n))) + packets := (**C_RTMPPacket)(realloc(unsafe.Pointer(r.m_vecChannelsIn), + uint32(int32(unsafe.Sizeof(packetPtr))*n))) + //packets := (**C_RTMPPacket)(C.realloc(unsafe.Pointer(r.m_vecChannelsIn), + //C.size_t(int32(unsafe.Sizeof(packetPtr))*n))) if timestamp == nil { //C.free(unsafe.Pointer(r.m_channelTimestamp)) } @@ -2768,7 +2773,7 @@ func C_AMF_Decode(obj *C_AMFObject, pBuffer *byte, nSize int32, bDecodeName int3 var prop C_AMFObjectProperty var nRes int32 - if nSize >= 3 && C_AMF_DecodeInt24(pBuffer) == C.AMF_OBJECT_END { + if nSize >= 3 && C_AMF_DecodeInt24(pBuffer) == AMF_OBJECT_END { nSize -= 3 bError = 0 break @@ -3031,8 +3036,10 @@ func C_AMFProp_Decode(prop *C_AMFObjectProperty, pBuffer *byte, nSize, bDecodeNa // amf.c + 1234 func C_AMF_AddProp(obj *C_AMFObject, prop *C_AMFObjectProperty) { if (obj.o_num & 0x0f) == 0 { - obj.o_props = (*C_AMFObjectProperty)(C.realloc(unsafe.Pointer(obj.o_props), - C.size_t(int(obj.o_num+16)*int(unsafe.Sizeof(*obj.o_props))))) + obj.o_props = (*C_AMFObjectProperty)(realloc(unsafe.Pointer(obj.o_props), + uint32(int(obj.o_num+16)*int(unsafe.Sizeof(*obj.o_props))))) + //obj.o_props = (*C_AMFObjectProperty)(C.realloc(unsafe.Pointer(obj.o_props), + //C.size_t(int(obj.o_num+16)*int(unsafe.Sizeof(*obj.o_props))))) } memmove(unsafe.Pointer(&(*(*C_AMFObjectProperty)(incPtr( unsafe.Pointer(obj.o_props), int(obj.o_num), int(unsafe.Sizeof(*obj.o_props)))))), @@ -3220,15 +3227,15 @@ func C_RTMP_SendPacket(r *C_RTMP, packet *C_RTMPPacket, queue int) int { var goHbuf [RTMP_MAX_HEADER_SIZE]byte var hbuf = unsafe.Pointer(&goHbuf[0]) var c byte - var t int32 + var t uint32 var packets unsafe.Pointer if packet.m_nChannel >= r.m_channelsAllocatedOut { n := int(packet.m_nChannel + 10) - packets = C.realloc(unsafe.Pointer(r.m_vecChannelsOut), C.size_t( - unsafe.Sizeof(packet)*uintptr(n))) - //packets = realloc(unsafe.Pointer(r.m_vecChannelsOut), - //int(unsafe.Sizeof(packet)*uintptr(n))) + //packets = C.realloc(unsafe.Pointer(r.m_vecChannelsOut), C.size_t( + //unsafe.Sizeof(packet)*uintptr(n))) + packets = realloc(unsafe.Pointer(r.m_vecChannelsOut), + uint32(unsafe.Sizeof(packet)*uintptr(n))) if uintptr(packets) == uintptr(0) { //C.free(unsafe.Pointer(r.m_vecChannelsOut)) @@ -3238,8 +3245,8 @@ func C_RTMP_SendPacket(r *C_RTMP, packet *C_RTMPPacket, queue int) int { } r.m_vecChannelsOut = (**C_RTMPPacket)(packets) - C.memset(incPtr(unsafe.Pointer(r.m_vecChannelsOut), int(r.m_channelsAllocatedOut), - int(unsafe.Sizeof(packet))), 0, C.size_t(unsafe.Sizeof(packet)* + memset((*byte)(incPtr(unsafe.Pointer(r.m_vecChannelsOut), int(r.m_channelsAllocatedOut), + int(unsafe.Sizeof(packet)))), 0, int(unsafe.Sizeof(packet)* uintptr(n-int(r.m_channelsAllocatedOut)))) //memset((*byte)(incPtr(unsafe.Pointer(r.m_vecChannelsOut), int( @@ -3276,7 +3283,7 @@ func C_RTMP_SendPacket(r *C_RTMP, packet *C_RTMPPacket, queue int) int { nSize = packetSize[int(packet.m_headerType)] hSize = nSize cSize = 0 - t = int32(int(packet.m_nTimeStamp) - last) + t = uint32(int(packet.m_nTimeStamp) - last) if packet.m_body != nil { header = decBytePtr(unsafe.Pointer(packet.m_body), nSize) @@ -3335,7 +3342,7 @@ func C_RTMP_SendPacket(r *C_RTMP, packet *C_RTMPPacket, queue int) int { if t > 0xffffff { res = 0xffffff } - hptr = unsafe.Pointer(C_AMF_EncodeInt24((*byte)(hptr), (*byte)(hend), res)) + hptr = unsafe.Pointer(C_AMF_EncodeInt24((*byte)(hptr), (*byte)(hend), int32(res))) } if nSize > 4 { @@ -3527,10 +3534,10 @@ func C_RTMPSockBuf_Send(sb *C_RTMPSockBuf, buf *byte, l int32) int32 { func C_AV_queue(vals **C_RTMP_METHOD, num *int32, av *C_AVal, txn int32) { if (*num & 0x0f) == 0 { // TODO: work out what to do with the realloc - //*vals = (*C_RTMP_METHOD)(realloc(unsafe.Pointer(*vals), int((*num+16)* - //int(unsafe.Sizeof(*(*vals)))))) - *vals = (*C_RTMP_METHOD)(C.realloc(unsafe.Pointer(*vals), C.size_t((*num+16)* + *vals = (*C_RTMP_METHOD)(realloc(unsafe.Pointer(*vals), uint32((*num+16)* int32(unsafe.Sizeof(*(*vals)))))) + //*vals = (*C_RTMP_METHOD)(C.realloc(unsafe.Pointer(*vals), C.size_t((*num+16)* + //int32(unsafe.Sizeof(*(*vals)))))) } tmp := malloc(uintptr(av.av_len + 1)) //tmp := allocate(uintptr(av.av_len + 1)) @@ -3870,17 +3877,30 @@ func AMF3CD_AddProp(cd *C.AMF3ClassDef, prop *C_AVal) { cd.cd_num++ } */ +func realloc(ptr unsafe.Pointer, newSize uint32) unsafe.Pointer { + return unsafe.Pointer(crt.Xrealloc(crt.TLS(uintptr(unsafe.Pointer(nil))), + uintptr(ptr), uint64(newSize))) + /* + var curSize uint32 + var newPtr unsafe.Pointer -func realloc(ptr unsafe.Pointer, size int) unsafe.Pointer { - dest := malloc(uintptr(size)) - if ptr != nil { - memmove(dest, ptr, uintptr(size)) - } - return dest + if ptr == nil { + return malloc(uintptr(newSize)) + } + if newSize <= curSize { + return ptr + } + newPtr = malloc(uintptr(newSize)) + memmove(ptr, newPtr, uintptr(newSize)) + ptr = nil + return newPtr + */ } func memmove(to, from unsafe.Pointer, n uintptr) { - copy(ptrToSlice(to, int(n)), ptrToSlice(from, int(n))) + if to != nil && from != nil && n != 0 { + copy(ptrToSlice(to, int(n)), ptrToSlice(from, int(n))) + } } // TODO: write test for this func