diff --git a/minimp3.h b/minimp3.h index 3220ae1..cd7f524 100644 --- a/minimp3.h +++ b/minimp3.h @@ -84,6 +84,21 @@ int mp3dec_decode_frame(mp3dec_t *dec, const uint8_t *mp3, int mp3_bytes, mp3d_s #define MINIMP3_MIN(a, b) ((a) > (b) ? (b) : (a)) #define MINIMP3_MAX(a, b) ((a) < (b) ? (b) : (a)) +#if defined(MINIMP3_MALLOC) && defined(MINIMP3_FREE) && defined(MINIMP3_REALLOC) +// ok +#elif !defined(MINIMP3_MALLOC) && !defined(MINIMP3_FREE) && !defined(MINIMP3_REALLOC) +// ok +#else +#error "Must define all or none of MINIMP3_MALLOC, MINIMP3_FREE, and MINIMP3_REALLOC." +#endif + +#ifndef MINIMP3_MALLOC +#define MINIMP3_MALLOC(sz) malloc(sz) +#define MINIMP3_REALLOC(p,newsz) realloc(p,newsz) +#define MINIMP3_FREE(p) free(p) +#endif + + #if !defined(MINIMP3_NO_SIMD) #if !defined(MINIMP3_ONLY_SIMD) && (defined(_M_X64) || defined(__x86_64__) || defined(__aarch64__) || defined(_M_ARM64)) diff --git a/minimp3_ex.h b/minimp3_ex.h index 2871705..497be92 100644 --- a/minimp3_ex.h +++ b/minimp3_ex.h @@ -408,7 +408,7 @@ int mp3dec_load_cb(mp3dec_t *dec, mp3dec_io_t *io, uint8_t *buf, size_t buf_size allocated += detected_samples*sizeof(mp3d_sample_t); else allocated += (buf_size/frame_info.frame_bytes)*samples*sizeof(mp3d_sample_t); - info->buffer = (mp3d_sample_t*)malloc(allocated); + info->buffer = (mp3d_sample_t*)MINIMP3_MALLOC(allocated); if (!info->buffer) return MP3D_E_MEMORY; /* save info */ @@ -422,7 +422,7 @@ int mp3dec_load_cb(mp3dec_t *dec, mp3dec_io_t *io, uint8_t *buf, size_t buf_size if ((allocated - info->samples*sizeof(mp3d_sample_t)) < MINIMP3_MAX_SAMPLES_PER_FRAME*sizeof(mp3d_sample_t)) { allocated *= 2; - mp3d_sample_t *alloc_buf = (mp3d_sample_t*)realloc(info->buffer, allocated); + mp3d_sample_t *alloc_buf = (mp3d_sample_t*)MINIMP3_REALLOC(info->buffer, allocated); if (!alloc_buf) return MP3D_E_MEMORY; info->buffer = alloc_buf; @@ -489,7 +489,7 @@ int mp3dec_load_cb(mp3dec_t *dec, mp3dec_io_t *io, uint8_t *buf, size_t buf_size /* reallocate to normal buffer size */ if (allocated != info->samples*sizeof(mp3d_sample_t)) { - mp3d_sample_t *alloc_buf = (mp3d_sample_t*)realloc(info->buffer, info->samples*sizeof(mp3d_sample_t)); + mp3d_sample_t *alloc_buf = (mp3d_sample_t*)MINIMP3_REALLOC(info->buffer, info->samples*sizeof(mp3d_sample_t)); if (!alloc_buf && info->samples) return MP3D_E_MEMORY; info->buffer = alloc_buf; @@ -654,7 +654,7 @@ static int mp3dec_load_index(void *user_data, const uint8_t *frame, int frame_si dec->index.capacity = 4096; else dec->index.capacity *= 2; - mp3dec_frame_t *alloc_buf = (mp3dec_frame_t *)realloc((void*)dec->index.frames, sizeof(mp3dec_frame_t)*dec->index.capacity); + mp3dec_frame_t *alloc_buf = (mp3dec_frame_t *)MINIMP3_REALLOC((void*)dec->index.frames, sizeof(mp3dec_frame_t)*dec->index.capacity); if (!alloc_buf) return MP3D_E_MEMORY; dec->index.frames = alloc_buf; @@ -984,7 +984,7 @@ int mp3dec_ex_open_cb(mp3dec_ex_t *dec, mp3dec_io_t *io, int flags) return ret; #else dec->file.size = MINIMP3_IO_SIZE; - dec->file.buffer = (const uint8_t*)malloc(dec->file.size); + dec->file.buffer = (const uint8_t*)MINIMP3_MALLOC(dec->file.size); if (!dec->file.buffer) return MP3D_E_MEMORY; #endif @@ -1219,7 +1219,7 @@ static int mp3dec_open_file_w(const wchar_t *file_name, mp3dec_map_info_t *map_i static void mp3dec_close_file(mp3dec_map_info_t *map_info) { if (map_info->buffer) - free((void *)map_info->buffer); + MINIMP3_FREE((void *)map_info->buffer); map_info->buffer = 0; map_info->size = 0; } @@ -1242,7 +1242,7 @@ static int mp3dec_open_file(const char *file_name, mp3dec_map_info_t *map_info) map_info->size = (size_t)size; if (fseek(file, 0, SEEK_SET)) goto error; - map_info->buffer = (uint8_t *)malloc(map_info->size); + map_info->buffer = (uint8_t *)MINIMP3_MALLOC(map_info->size); if (!map_info->buffer) { res = MP3D_E_MEMORY; @@ -1333,12 +1333,12 @@ void mp3dec_ex_close(mp3dec_ex_t *dec) mp3dec_close_ring(&dec->file); #else if (dec->io && dec->file.buffer) - free((void*)dec->file.buffer); + MINIMP3_FREE((void*)dec->file.buffer); #endif if (dec->is_file) mp3dec_close_file(&dec->file); if (dec->index.frames) - free(dec->index.frames); + MINIMP3_FREE(dec->index.frames); memset(dec, 0, sizeof(*dec)); } @@ -1386,10 +1386,10 @@ void mp3dec_ex_close(mp3dec_ex_t *dec) mp3dec_close_ring(&dec->file); #else if (dec->io && dec->file.buffer) - free((void*)dec->file.buffer); + MINIMP3_FREE((void*)dec->file.buffer); #endif if (dec->index.frames) - free(dec->index.frames); + MINIMP3_FREE(dec->index.frames); memset(dec, 0, sizeof(*dec)); } #endif