Wrote realloc func

This commit is contained in:
saxon 2018-07-28 11:05:51 +09:30
parent 5cca4d45a5
commit 5cba7d7c02
1 changed files with 17 additions and 10 deletions

View File

@ -148,10 +148,6 @@ const (
int64Size = 8 int64Size = 8
) )
func memmove(to, from unsafe.Pointer, n uintptr) {
copy(ptrToSlice(to, int(n)), ptrToSlice(from, int(n)))
}
// av_setDataFrame is a static const global in rtmp.c // av_setDataFrame is a static const global in rtmp.c
var ( var (
setDataFrame = AVC("@setDataFrame") setDataFrame = AVC("@setDataFrame")
@ -1104,8 +1100,8 @@ func sendPacket(r *C.RTMP, packet *C.RTMPPacket, queue int) int {
log.Println("Resize") log.Println("Resize")
n := int(packet.m_nChannel + 10) n := int(packet.m_nChannel + 10)
// TODO: port this // TODO: port this
packets = C.realloc(unsafe.Pointer(r.m_vecChannelsOut), packets = realloc(unsafe.Pointer(r.m_vecChannelsOut),
C.size_t(unsafe.Sizeof(packet)*uintptr(n))) int(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))
@ -1241,7 +1237,7 @@ func sendPacket(r *C.RTMP, packet *C.RTMPPacket, queue int) int {
if chunks > 1 { if chunks > 1 {
tlen = chunks*(cSize+1) + nSize + hSize tlen = chunks*(cSize+1) + nSize + hSize
// TODO: figure out how to do this in go // TODO: figure out how to do this in go
tbuf = C.malloc(C.size_t(tlen)) tbuf = allocate(uintptr(tlen))
if tbuf == nil { if tbuf == nil {
return 0 return 0
@ -1345,7 +1341,7 @@ func sendPacket(r *C.RTMP, packet *C.RTMPPacket, queue int) int {
*(**C.RTMPPacket)(incPtr(unsafe.Pointer(r.m_vecChannelsOut), *(**C.RTMPPacket)(incPtr(unsafe.Pointer(r.m_vecChannelsOut),
int(packet.m_nChannel), int(unsafe.Sizeof(packet)))) = int(packet.m_nChannel), int(unsafe.Sizeof(packet)))) =
(*C.RTMPPacket)(C.malloc(C.size_t(unsafe.Sizeof(*packet)))) (*C.RTMPPacket)(allocate(unsafe.Sizeof(*packet)))
} }
memmove(unsafe.Pointer(*(**C.RTMPPacket)(incPtr(unsafe.Pointer(r.m_vecChannelsOut), memmove(unsafe.Pointer(*(**C.RTMPPacket)(incPtr(unsafe.Pointer(r.m_vecChannelsOut),
@ -1404,10 +1400,10 @@ func sockBufSend(sb *C.RTMPSockBuf, buf *byte, l int32) int32 {
func avQueue(vals **C.RTMP_METHOD, num *int, av *C.AVal, txn int) { func avQueue(vals **C.RTMP_METHOD, num *int, av *C.AVal, txn int) {
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)(C.realloc(unsafe.Pointer(*vals), C.size_t((*num+16)* *vals = (*C.RTMP_METHOD)(realloc(unsafe.Pointer(*vals), int((*num+16)*
int(unsafe.Sizeof(*(*vals)))))) int(unsafe.Sizeof(*(*vals))))))
} }
tmp := unsafe.Pointer(C.malloc(C.size_t(av.av_len + 1))) tmp := allocate(uintptr(av.av_len + 1))
memmove(tmp, unsafe.Pointer(av.av_val), uintptr(av.av_len)) memmove(tmp, unsafe.Pointer(av.av_val), uintptr(av.av_len))
*indxBytePtr(tmp, int(av.av_len)) = '\000' *indxBytePtr(tmp, int(av.av_len)) = '\000'
@ -1417,6 +1413,7 @@ func avQueue(vals **C.RTMP_METHOD, num *int, av *C.AVal, txn int) {
int(unsafe.Sizeof(*(*vals)))))).name.av_len = av.av_len int(unsafe.Sizeof(*(*vals)))))).name.av_len = av.av_len
(*(*C.RTMP_METHOD)(incPtr(unsafe.Pointer(*vals), *num, (*(*C.RTMP_METHOD)(incPtr(unsafe.Pointer(*vals), *num,
int(unsafe.Sizeof(*(*vals)))))).name.av_val = (*C.char)(tmp) int(unsafe.Sizeof(*(*vals)))))).name.av_val = (*C.char)(tmp)
*num++
} }
func amfEncodeNamedNumber(output *byte, outend *byte, strName *C.AVal, dVal float64) *byte { func amfEncodeNamedNumber(output *byte, outend *byte, strName *C.AVal, dVal float64) *byte {
@ -1603,6 +1600,16 @@ func amfEncodeInt32(output *byte, outend *byte, nVal int32) *byte {
return (*byte)(incBytePtr(outputPtr, 4)) return (*byte)(incBytePtr(outputPtr, 4))
} }
func realloc(ptr unsafe.Pointer, size int) unsafe.Pointer {
dest := allocate(uintptr(size))
memmove(dest, ptr, uintptr(size))
return dest
}
func memmove(to, from unsafe.Pointer, n uintptr) {
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 {
for i := 0; i < size; i++ { for i := 0; i < size; i++ {