mirror of
https://github.com/holub/mame
synced 2025-04-22 16:31:49 +03:00
Add realloc implementation as required by the C standard (nw)
This commit is contained in:
parent
3da02a7f64
commit
a9b60b05ac
@ -222,6 +222,44 @@ void free_file_line(void *memory, const char *file, int line, bool array)
|
||||
osd_free(memory);
|
||||
}
|
||||
|
||||
void *realloc_internal(void *memory, size_t size, const char *file, int line, bool array)
|
||||
{
|
||||
fprintf(stderr, "realloc_internal called for %p in %s(%d)!\n", memory, file, line);
|
||||
if(size == 0) {
|
||||
return nullptr;
|
||||
}
|
||||
if(memory == nullptr) {
|
||||
return malloc_file_line(size, file, line, array, false, false);
|
||||
}
|
||||
// find the memory entry
|
||||
memory_entry *entry = memory_entry::find(memory);
|
||||
|
||||
// warn about untracked reallocs
|
||||
if (entry == nullptr)
|
||||
{
|
||||
fprintf(stderr, "Error: attempt to realloc untracked memory %p in %s(%d)!\n", memory, file, line);
|
||||
osd_break_into_debugger("Error: attempt to realloc untracked memory");
|
||||
return memory;
|
||||
}
|
||||
|
||||
// this is used internally and should always be an array
|
||||
if(!array || !entry->m_array)
|
||||
{
|
||||
fprintf(stderr, "Error: attempt to realloc non-array memory %p in %s(%d). realloc_internal should never be called directly!\n", memory, file, line);
|
||||
osd_break_into_debugger("Error: attempt to realloc non-array memory");
|
||||
}
|
||||
|
||||
size_t o_size = entry->m_size;
|
||||
void *new_buf = malloc_file_line(size, file, line, array, false, false);
|
||||
if(new_buf == nullptr) {
|
||||
fprintf(stderr, "Error: realloc: unable to allocate new buffer %p in %s(%d)!\n", memory, file, line);
|
||||
return nullptr;
|
||||
}
|
||||
memcpy(new_buf, memory, (o_size < size) ? o_size : size);
|
||||
free_file_line(memory, file, line, array);
|
||||
return new_buf;
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------
|
||||
// track_memory - enables or disables the memory
|
||||
|
@ -43,6 +43,10 @@ void *malloc_file_line(size_t size, const char *file, int line, bool array, bool
|
||||
void free_file_line(void *memory, const char *file, int line, bool array);
|
||||
inline void free_file_line(const void *memory, const char *file, int line, bool array) { free_file_line(const_cast<void *>(memory), file, line, array); }
|
||||
|
||||
// realloc with file and line number info for internal use
|
||||
void *realloc_internal(void *memory, size_t size, const char *file, int line, bool array);
|
||||
|
||||
|
||||
// called from the exit path of any code that wants to check for unfreed memory
|
||||
void track_memory(bool track);
|
||||
UINT64 next_memory_id();
|
||||
@ -91,7 +95,7 @@ extern const zeromem_t zeromem;
|
||||
#undef free
|
||||
|
||||
#define malloc(x) malloc_file_line(x, __FILE__, __LINE__, true, false, false)
|
||||
#define realloc(x,y) __error_realloc_is_dangerous__
|
||||
#define realloc(x,y) realloc_internal(x, y, __FILE__, __LINE__, true, false)
|
||||
#define free(x) free_file_line(x, __FILE__, __LINE__, true)
|
||||
|
||||
#if !defined(_MSC_VER) || _MSC_VER < 1900 // < VS2015
|
||||
|
Loading…
Reference in New Issue
Block a user