Now using av_malloc/av_free instead of malloc/free

This commit is contained in:
Quentin Renard
2025-02-16 14:16:13 +01:00
parent 685441141d
commit dca9296441
4 changed files with 8 additions and 6 deletions

View File

@@ -12,11 +12,11 @@ import (
func stringFromC(len int, fn func(buf *C.char, size C.size_t) error) (string, error) { func stringFromC(len int, fn func(buf *C.char, size C.size_t) error) (string, error) {
size := C.size_t(len) size := C.size_t(len)
buf := (*C.char)(C.malloc(size)) buf := (*C.char)(C.av_malloc(size))
if buf == nil { if buf == nil {
return "", errors.New("astiav: buf is nil") return "", errors.New("astiav: buf is nil")
} }
defer C.free(unsafe.Pointer(buf)) defer C.av_free(unsafe.Pointer(buf))
if err := fn(buf, size); err != nil { if err := fn(buf, size); err != nil {
return "", err return "", err
} }

View File

@@ -57,7 +57,7 @@ func AllocIOContext(bufferSize int, writable bool, readFunc IOContextReadFunc, s
}() }()
// Since go doesn't allow c to store pointers to go data, we need to create this C pointer // Since go doesn't allow c to store pointers to go data, we need to create this C pointer
handlerID := C.malloc(C.size_t(1)) handlerID := C.av_malloc(C.size_t(1))
if handlerID == nil { if handlerID == nil {
err = errors.New("astiav: allocating handler id failed") err = errors.New("astiav: allocating handler id failed")
return return
@@ -66,7 +66,7 @@ func AllocIOContext(bufferSize int, writable bool, readFunc IOContextReadFunc, s
// Make sure handler id is freed in case of error // Make sure handler id is freed in case of error
defer func() { defer func() {
if err != nil { if err != nil {
C.free(handlerID) C.av_free(handlerID)
} }
}() }()

View File

@@ -1,4 +1,5 @@
#include <libavformat/avio.h> #include <libavformat/avio.h>
#include <libavutil/mem.h>
#include <stdlib.h> #include <stdlib.h>
int astiavInterruptCallback(void *ret) int astiavInterruptCallback(void *ret)
@@ -8,7 +9,7 @@ int astiavInterruptCallback(void *ret)
AVIOInterruptCB* astiavNewInterruptCallback(int *ret) AVIOInterruptCB* astiavNewInterruptCallback(int *ret)
{ {
AVIOInterruptCB* c = malloc(sizeof(AVIOInterruptCB)); AVIOInterruptCB* c = av_malloc(sizeof(AVIOInterruptCB));
c->callback = astiavInterruptCallback; c->callback = astiavInterruptCallback;
c->opaque = ret; c->opaque = ret;
return c; return c;

View File

@@ -1,6 +1,7 @@
package astiav package astiav
//#include "io_interrupter.h" //#include "io_interrupter.h"
//#include <libavutil/mem.h>
//#include <stdlib.h> //#include <stdlib.h>
import "C" import "C"
import "unsafe" import "unsafe"
@@ -18,7 +19,7 @@ func NewIOInterrupter() *IOInterrupter {
func (i *IOInterrupter) Free() { func (i *IOInterrupter) Free() {
if i.c != nil { if i.c != nil {
C.free(unsafe.Pointer(i.c)) C.av_free(unsafe.Pointer(i.c))
i.c = nil i.c = nil
} }
} }