Got rid of all C.realloc and replaced with my realloc func - tested and working

This commit is contained in:
saxon 2018-08-18 15:48:49 +09:30
parent 54ee353f6b
commit d7d2d3b9d5
1 changed files with 46 additions and 26 deletions

View File

@ -71,6 +71,7 @@ import (
"unsafe" "unsafe"
"github.com/chamaken/cgolmnl/inet" "github.com/chamaken/cgolmnl/inet"
"github.com/cznic/crt"
) )
const ( const (
@ -2585,12 +2586,16 @@ func C_RTMP_ReadPacket(r *C_RTMP, packet *C_RTMPPacket) int32 {
if packet.m_nChannel >= r.m_channelsAllocatedIn { if packet.m_nChannel >= r.m_channelsAllocatedIn {
var n int32 = int32(packet.m_nChannel + 10) var n int32 = int32(packet.m_nChannel + 10)
timestamp := (*int32)(C.realloc(unsafe.Pointer(r.m_channelTimestamp), timestamp := (*int32)(realloc(unsafe.Pointer(r.m_channelTimestamp),
C.size_t(int32(unsafe.Sizeof(n))*n))) 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 var packetPtr *C_RTMPPacket
packets := (**C_RTMPPacket)(C.realloc(unsafe.Pointer(r.m_vecChannelsIn), packets := (**C_RTMPPacket)(realloc(unsafe.Pointer(r.m_vecChannelsIn),
C.size_t(int32(unsafe.Sizeof(packetPtr))*n))) 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 { if timestamp == nil {
//C.free(unsafe.Pointer(r.m_channelTimestamp)) //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 prop C_AMFObjectProperty
var nRes int32 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 nSize -= 3
bError = 0 bError = 0
break break
@ -3031,8 +3036,10 @@ func C_AMFProp_Decode(prop *C_AMFObjectProperty, pBuffer *byte, nSize, bDecodeNa
// amf.c + 1234 // amf.c + 1234
func C_AMF_AddProp(obj *C_AMFObject, prop *C_AMFObjectProperty) { func C_AMF_AddProp(obj *C_AMFObject, prop *C_AMFObjectProperty) {
if (obj.o_num & 0x0f) == 0 { if (obj.o_num & 0x0f) == 0 {
obj.o_props = (*C_AMFObjectProperty)(C.realloc(unsafe.Pointer(obj.o_props), obj.o_props = (*C_AMFObjectProperty)(realloc(unsafe.Pointer(obj.o_props),
C.size_t(int(obj.o_num+16)*int(unsafe.Sizeof(*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( memmove(unsafe.Pointer(&(*(*C_AMFObjectProperty)(incPtr(
unsafe.Pointer(obj.o_props), int(obj.o_num), int(unsafe.Sizeof(*obj.o_props)))))), 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 goHbuf [RTMP_MAX_HEADER_SIZE]byte
var hbuf = unsafe.Pointer(&goHbuf[0]) var hbuf = unsafe.Pointer(&goHbuf[0])
var c byte var c byte
var t int32 var t uint32
var packets unsafe.Pointer var packets unsafe.Pointer
if packet.m_nChannel >= r.m_channelsAllocatedOut { if packet.m_nChannel >= r.m_channelsAllocatedOut {
n := int(packet.m_nChannel + 10) n := int(packet.m_nChannel + 10)
packets = C.realloc(unsafe.Pointer(r.m_vecChannelsOut), C.size_t( //packets = C.realloc(unsafe.Pointer(r.m_vecChannelsOut), C.size_t(
unsafe.Sizeof(packet)*uintptr(n))) //unsafe.Sizeof(packet)*uintptr(n)))
//packets = realloc(unsafe.Pointer(r.m_vecChannelsOut), packets = realloc(unsafe.Pointer(r.m_vecChannelsOut),
//int(unsafe.Sizeof(packet)*uintptr(n))) uint32(unsafe.Sizeof(packet)*uintptr(n)))
if uintptr(packets) == uintptr(0) { if uintptr(packets) == uintptr(0) {
//C.free(unsafe.Pointer(r.m_vecChannelsOut)) //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) r.m_vecChannelsOut = (**C_RTMPPacket)(packets)
C.memset(incPtr(unsafe.Pointer(r.m_vecChannelsOut), int(r.m_channelsAllocatedOut), memset((*byte)(incPtr(unsafe.Pointer(r.m_vecChannelsOut), int(r.m_channelsAllocatedOut),
int(unsafe.Sizeof(packet))), 0, C.size_t(unsafe.Sizeof(packet)* int(unsafe.Sizeof(packet)))), 0, int(unsafe.Sizeof(packet)*
uintptr(n-int(r.m_channelsAllocatedOut)))) uintptr(n-int(r.m_channelsAllocatedOut))))
//memset((*byte)(incPtr(unsafe.Pointer(r.m_vecChannelsOut), int( //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)] nSize = packetSize[int(packet.m_headerType)]
hSize = nSize hSize = nSize
cSize = 0 cSize = 0
t = int32(int(packet.m_nTimeStamp) - last) t = uint32(int(packet.m_nTimeStamp) - last)
if packet.m_body != nil { if packet.m_body != nil {
header = decBytePtr(unsafe.Pointer(packet.m_body), nSize) 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 { if t > 0xffffff {
res = 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 { 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) { func C_AV_queue(vals **C_RTMP_METHOD, num *int32, av *C_AVal, txn int32) {
if (*num & 0x0f) == 0 { if (*num & 0x0f) == 0 {
// TODO: work out what to do with the realloc // TODO: work out what to do with the realloc
//*vals = (*C_RTMP_METHOD)(realloc(unsafe.Pointer(*vals), int((*num+16)* *vals = (*C_RTMP_METHOD)(realloc(unsafe.Pointer(*vals), uint32((*num+16)*
//int(unsafe.Sizeof(*(*vals))))))
*vals = (*C_RTMP_METHOD)(C.realloc(unsafe.Pointer(*vals), C.size_t((*num+16)*
int32(unsafe.Sizeof(*(*vals)))))) 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 := malloc(uintptr(av.av_len + 1))
//tmp := allocate(uintptr(av.av_len + 1)) //tmp := allocate(uintptr(av.av_len + 1))
@ -3870,18 +3877,31 @@ func AMF3CD_AddProp(cd *C.AMF3ClassDef, prop *C_AVal) {
cd.cd_num++ 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 { if ptr == nil {
dest := malloc(uintptr(size)) return malloc(uintptr(newSize))
if ptr != nil {
memmove(dest, ptr, uintptr(size))
} }
return dest 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) { func memmove(to, from unsafe.Pointer, n uintptr) {
if to != nil && from != nil && n != 0 {
copy(ptrToSlice(to, int(n)), ptrToSlice(from, int(n))) copy(ptrToSlice(to, int(n)), ptrToSlice(from, int(n)))
} }
}
// TODO: write test for this func // TODO: write test for this func
func memcmp(a, b unsafe.Pointer, size int) int { func memcmp(a, b unsafe.Pointer, size int) int {