Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change malloc/realloc/free with macros so they can be user defined #114

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions minimp3.h
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
22 changes: 11 additions & 11 deletions minimp3_ex.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
Expand Down Expand Up @@ -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));
}

Expand Down Expand Up @@ -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
Expand Down