shithub: opus

Download patch

ref: 12fbd8111a7da79855bdde58cf1f135c94397858
parent: f5a1efdc17aebeb7ee890e207f280f3fe4522ca4
author: Michael Klingbeil <klingm@amazon.com>
date: Fri Dec 15 10:48:58 EST 2023

use opus_(re)alloc and opus_free for dnn and DRED related functions

--- a/celt/os_support.h
+++ b/celt/os_support.h
@@ -41,7 +41,7 @@
 #include <string.h>
 #include <stdlib.h>
 
-/** Opus wrapper for malloc(). To do your own dynamic allocation, all you need to do is replace this function and opus_free */
+/** Opus wrapper for malloc(). To do your own dynamic allocation replace this function, opus_realloc, and opus_free */
 #ifndef OVERRIDE_OPUS_ALLOC
 static OPUS_INLINE void *opus_alloc (size_t size)
 {
@@ -49,7 +49,15 @@
 }
 #endif
 
-/** Same as celt_alloc(), except that the area is only needed inside a CELT call (might cause problem with wideband though) */
+#ifndef OVERRIDE_OPUS_REALLOC
+static OPUS_INLINE void *opus_realloc (void *ptr, size_t size)
+{
+   return realloc(ptr, size);
+}
+#endif
+
+/** Used only for non-threadsafe pseudostack.
+    If desired, this can always return the same area of memory rather than allocating a new one every time. */
 #ifndef OVERRIDE_OPUS_ALLOC_SCRATCH
 static OPUS_INLINE void *opus_alloc_scratch (size_t size)
 {
@@ -58,7 +66,7 @@
 }
 #endif
 
-/** Opus wrapper for free(). To do your own dynamic allocation, all you need to do is replace this function and opus_alloc */
+/** Opus wrapper for free(). To do your own dynamic allocation replace this function, opus_realloc, and opus_free */
 #ifndef OVERRIDE_OPUS_FREE
 static OPUS_INLINE void opus_free (void *ptr)
 {
--- a/dnn/fargan.c
+++ b/dnn/fargan.c
@@ -189,7 +189,7 @@
   int ret;
   parse_weights(&list, data, len);
   ret = init_fargan(&st->model, list);
-  free(list);
+  opus_free(list);
   if (ret == 0) return 0;
   else return -1;
 }
--- a/dnn/fwgan.c
+++ b/dnn/fwgan.c
@@ -275,7 +275,7 @@
   int ret;
   parse_weights(&list, data, len);
   ret = init_fwgan(&st->model, list);
-  free(list);
+  opus_free(list);
   if (ret == 0) return 0;
   else return -1;
 }
--- a/dnn/lpcnet.c
+++ b/dnn/lpcnet.c
@@ -205,7 +205,7 @@
   int ret;
   parse_weights(&list, data, len);
   ret = init_lpcnet_model(&st->model, list);
-  free(list);
+  opus_free(list);
   if (ret == 0) return 0;
   else return -1;
 }
@@ -214,7 +214,8 @@
 LPCNetState *lpcnet_create()
 {
     LPCNetState *lpcnet;
-    lpcnet = (LPCNetState *)calloc(lpcnet_get_size(), 1);
+    lpcnet = (LPCNetState *)opus_alloc(lpcnet_get_size(), 1);
+    OPUS_CLEAR(lpcnet, 1);
     lpcnet_init(lpcnet);
     return lpcnet;
 }
@@ -221,7 +222,7 @@
 
 void lpcnet_destroy(LPCNetState *lpcnet)
 {
-    free(lpcnet);
+    opus_free(lpcnet);
 }
 
 void lpcnet_reset_signal(LPCNetState *lpcnet)
--- a/dnn/lpcnet_enc.c
+++ b/dnn/lpcnet_enc.c
@@ -62,13 +62,13 @@
 
 LPCNetEncState *lpcnet_encoder_create(void) {
   LPCNetEncState *st;
-  st = malloc(lpcnet_encoder_get_size());
+  st = opus_alloc(lpcnet_encoder_get_size());
   lpcnet_encoder_init(st);
   return st;
 }
 
 void lpcnet_encoder_destroy(LPCNetEncState *st) {
-  free(st);
+  opus_free(st);
 }
 
 static void frame_analysis(LPCNetEncState *st, kiss_fft_cpx *X, float *Ex, const float *in) {
--- a/dnn/lpcnet_plc.c
+++ b/dnn/lpcnet_plc.c
@@ -76,7 +76,7 @@
   int ret;
   parse_weights(&list, data, len);
   ret = init_plc_model(&st->model, list);
-  free(list);
+  opus_free(list);
   if (ret == 0) {
     ret = lpcnet_encoder_load_model(&st->enc, data, len);
   }
--- a/dnn/parse_lpcnet_weights.c
+++ b/dnn/parse_lpcnet_weights.c
@@ -31,6 +31,7 @@
 #include <string.h>
 #include <stdlib.h>
 #include "nnet.h"
+#include "os_support.h"
 
 #define SPARSE_BLOCK_SIZE 32
 
@@ -55,7 +56,7 @@
 {
   int nb_arrays=0;
   int capacity=20;
-  *list = malloc(capacity*sizeof(WeightArray));
+  *list = opus_alloc(capacity*sizeof(WeightArray));
   while (len > 0) {
     int ret;
     WeightArray array = {NULL, 0, 0, 0};
@@ -64,11 +65,11 @@
       if (nb_arrays+1 >= capacity) {
         /* Make sure there's room for the ending NULL element too. */
         capacity = capacity*3/2;
-        *list = realloc(*list, capacity*sizeof(WeightArray));
+        *list = opus_realloc(*list, capacity*sizeof(WeightArray));
       }
       (*list)[nb_arrays++] = array;
     } else {
-      free(*list);
+      opus_free(*list);
       *list = NULL;
       return -1;
     }
@@ -269,7 +270,7 @@
     printf("found %s: size %d\n", list[i].name, list[i].size);
   }
   printf("%p\n", list[i].name);
-  free(list);
+  opus_free(list);
   munmap(data, len);
   close(fd);
   return 0;
--- a/dnn/pitchdnn.c
+++ b/dnn/pitchdnn.c
@@ -73,7 +73,7 @@
   int ret;
   parse_weights(&list, data, len);
   ret = init_pitchdnn(&st->model, list);
-  free(list);
+  opus_free(list);
   if (ret == 0) return 0;
   else return -1;
 }
--- a/silk/dred_encoder.c
+++ b/silk/dred_encoder.c
@@ -58,7 +58,7 @@
     int ret;
     parse_weights(&list, data, len);
     ret = init_rdovaeenc(&enc->model, list);
-    free(list);
+    opus_free(list);
     if (ret == 0) {
       ret = lpcnet_encoder_load_model(&enc->lpcnet_enc_state, data, len);
     }
--- a/src/opus_decoder.c
+++ b/src/opus_decoder.c
@@ -1189,7 +1189,7 @@
     int ret;
     parse_weights(&list, data, len);
     ret = init_rdovaedec(&dec->model, list);
-    free(list);
+    opus_free(list);
     if (ret == 0) dec->loaded = 1;
     return (ret == 0) ? OPUS_OK : OPUS_BAD_ARG;
 }
@@ -1233,8 +1233,8 @@
 
 void opus_dred_decoder_destroy(OpusDREDDecoder *dec)
 {
-   dec->magic = 0xDE57801D;
-   free(dec);
+   if (dec) dec->magic = 0xDE57801D;
+   opus_free(dec);
 }
 
 int opus_dred_decoder_ctl(OpusDREDDecoder *dred_dec, int request, ...)
@@ -1372,7 +1372,7 @@
 void opus_dred_free(OpusDRED *dec)
 {
 #ifdef ENABLE_DRED
-  free(dec);
+  opus_free(dec);
 #else
   (void)dec;
 #endif
--