diff options
author | Michael Klingbeil <klingm@amazon.com> | 2023-12-15 23:48:58 +0300 |
---|---|---|
committer | Michael Klingbeil <klingm@amazon.com> | 2023-12-15 23:48:58 +0300 |
commit | 12fbd8111a7da79855bdde58cf1f135c94397858 (patch) | |
tree | c3538a0d1eb83862f9e177f26a1db3dac0fac013 | |
parent | f5a1efdc17aebeb7ee890e207f280f3fe4522ca4 (diff) |
use opus_(re)alloc and opus_free for dnn and DRED related functionsopus-ng-fix-alloc
-rw-r--r-- | celt/os_support.h | 14 | ||||
-rw-r--r-- | dnn/fargan.c | 2 | ||||
-rw-r--r-- | dnn/fwgan.c | 2 | ||||
-rw-r--r-- | dnn/lpcnet.c | 7 | ||||
-rw-r--r-- | dnn/lpcnet_enc.c | 4 | ||||
-rw-r--r-- | dnn/lpcnet_plc.c | 2 | ||||
-rw-r--r-- | dnn/parse_lpcnet_weights.c | 9 | ||||
-rw-r--r-- | dnn/pitchdnn.c | 2 | ||||
-rw-r--r-- | silk/dred_encoder.c | 2 | ||||
-rw-r--r-- | src/opus_decoder.c | 8 |
10 files changed, 31 insertions, 21 deletions
diff --git a/celt/os_support.h b/celt/os_support.h index 009bf861..7d2d3781 100644 --- 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 @@ static OPUS_INLINE void *opus_alloc (size_t size) } #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 @@ static OPUS_INLINE void *opus_alloc_scratch (size_t size) } #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) { diff --git a/dnn/fargan.c b/dnn/fargan.c index 451fcacb..aced8cb8 100644 --- a/dnn/fargan.c +++ b/dnn/fargan.c @@ -189,7 +189,7 @@ int fargan_load_model(FARGANState *st, const unsigned char *data, int len) { 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; } diff --git a/dnn/fwgan.c b/dnn/fwgan.c index 562ffe93..301c73f6 100644 --- a/dnn/fwgan.c +++ b/dnn/fwgan.c @@ -275,7 +275,7 @@ int fwgan_load_model(FWGANState *st, const unsigned char *data, int len) { 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; } diff --git a/dnn/lpcnet.c b/dnn/lpcnet.c index ff43e301..52e81b07 100644 --- a/dnn/lpcnet.c +++ b/dnn/lpcnet.c @@ -205,7 +205,7 @@ int lpcnet_load_model(LPCNetState *st, const unsigned char *data, int len) { 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,14 +214,15 @@ int lpcnet_load_model(LPCNetState *st, const unsigned char *data, int len) { 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; } void lpcnet_destroy(LPCNetState *lpcnet) { - free(lpcnet); + opus_free(lpcnet); } void lpcnet_reset_signal(LPCNetState *lpcnet) diff --git a/dnn/lpcnet_enc.c b/dnn/lpcnet_enc.c index e720c958..1cdbf76a 100644 --- a/dnn/lpcnet_enc.c +++ b/dnn/lpcnet_enc.c @@ -62,13 +62,13 @@ int lpcnet_encoder_load_model(LPCNetEncState *st, const unsigned char *data, int 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) { diff --git a/dnn/lpcnet_plc.c b/dnn/lpcnet_plc.c index b713110f..ec8ad7e9 100644 --- a/dnn/lpcnet_plc.c +++ b/dnn/lpcnet_plc.c @@ -76,7 +76,7 @@ int lpcnet_plc_load_model(LPCNetPLCState *st, const unsigned char *data, int len 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); } diff --git a/dnn/parse_lpcnet_weights.c b/dnn/parse_lpcnet_weights.c index be2dafdc..c2108593 100644 --- 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 parse_weights(WeightArray **list, const unsigned char *data, int len) { 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 @@ int parse_weights(WeightArray **list, const unsigned char *data, int len) 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 @@ int main() 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; diff --git a/dnn/pitchdnn.c b/dnn/pitchdnn.c index ae95ca32..b04a0ebe 100644 --- a/dnn/pitchdnn.c +++ b/dnn/pitchdnn.c @@ -73,7 +73,7 @@ int pitchdnn_load_model(PitchDNNState *st, const unsigned char *data, int len) { 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; } diff --git a/silk/dred_encoder.c b/silk/dred_encoder.c index f791115a..0e1bb33f 100644 --- a/silk/dred_encoder.c +++ b/silk/dred_encoder.c @@ -58,7 +58,7 @@ int dred_encoder_load_model(DREDEnc* enc, const unsigned char *data, int len) 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); } diff --git a/src/opus_decoder.c b/src/opus_decoder.c index 1e0a1da4..596c2dd0 100644 --- a/src/opus_decoder.c +++ b/src/opus_decoder.c @@ -1189,7 +1189,7 @@ int dred_decoder_load_model(OpusDREDDecoder *dec, const unsigned char *data, int 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 @@ OpusDREDDecoder *opus_dred_decoder_create(int *error) 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 @@ OpusDRED *opus_dred_alloc(int *error) void opus_dred_free(OpusDRED *dec) { #ifdef ENABLE_DRED - free(dec); + opus_free(dec); #else (void)dec; #endif |