No need for osd_malloc, osd_malloc_array and osd_free (nw)

MALLOC_DEBUG not applicable anymore since we use new to allocate in 99.9% of cases
This commit is contained in:
Miodrag Milanovic 2016-11-11 14:34:46 +01:00
parent 5c0edceec3
commit 7c765ea147
25 changed files with 44 additions and 343 deletions

View File

@ -111,10 +111,6 @@ if _OPTIONS["targetos"]=="windows" then
"_WIN32_WINNT=0x0501",
}
configuration { "Debug" }
defines {
"MALLOC_DEBUG",
}
configuration { }
elseif _OPTIONS["targetos"]=="linux" then

View File

@ -12,11 +12,6 @@ configuration { "mingw* or vs*" }
"main=utf8_main",
}
configuration { "Debug" }
defines {
"MALLOC_DEBUG",
}
configuration { "vs*" }
flags {
"Unicode",

View File

@ -331,31 +331,3 @@ int main( int argc, char *argv[] )
free (filebuf);
return 0;
}
void *osd_malloc_array(size_t size)
{
return osd_malloc(size);
}
void *malloc_array_file_line(size_t size, const char *file, int line)
{
// allocate the memory and fail if we can't
return osd_malloc_array(size);
}
void free_file_line( void *memory, const char *file, int line )
{
osd_free( memory );
}
void osd_free( void *memory )
{
#undef free
free( memory );
}
void *osd_malloc( size_t size )
{
#undef malloc
return malloc( size );
}

View File

@ -129,7 +129,7 @@ void ram_device::device_validity_check(validity_checker &valid) const
p += 1;
}
osd_free(s);
free(s);
}
} else {

View File

@ -157,7 +157,7 @@ void media_identifier::identify_file(const char *name)
if (filerr == osd_file::error::NONE && length > 0)
{
identify_data(name, reinterpret_cast<uint8_t *>(data), length);
osd_free(data);
free(data);
}
}
}

View File

@ -900,7 +900,7 @@ bool mame_ui_manager::can_paste()
// free the string if allocated
if (text != nullptr)
osd_free(text);
free(text);
// did we have text?
return text != nullptr;
@ -923,7 +923,7 @@ void mame_ui_manager::paste()
machine().ioport().natkeyboard().post_utf8(text);
// free the string
osd_free(text);
free(text);
}
}

View File

@ -1207,7 +1207,7 @@ osd_file::error core_file::load(std::string const &filename, void **data, std::u
return osd_file::error::OUT_OF_MEMORY;
// allocate memory
*data = osd_malloc(size);
*data = malloc(size);
length = std::uint32_t(size);
// read the data

View File

@ -109,7 +109,7 @@ int core_strwildcmp(const char *sp1, const char *sp2)
/*-------------------------------------------------
core_strdup - string duplication via osd_malloc
core_strdup - string duplication via malloc
-------------------------------------------------*/
char *core_strdup(const char *str)
@ -117,7 +117,7 @@ char *core_strdup(const char *str)
char *cpy = nullptr;
if (str != nullptr)
{
cpy = (char *)osd_malloc_array(strlen(str) + 1);
cpy = (char *)malloc(strlen(str) + 1);
if (cpy != nullptr)
strcpy(cpy, str);
}

View File

@ -49,7 +49,7 @@ int core_strnicmp(const char *s1, const char *s2, size_t n);
#define strncasecmp MUST_USE_CORE_STRNICMP_INSTEAD
/* since strdup is not part of the standard, we use this instead - free with osd_free() */
/* since strdup is not part of the standard, we use this instead - free with free() */
char *core_strdup(const char *str);
/* this macro prevents people from using strdup directly */

View File

@ -85,11 +85,11 @@
static osd_shared_mem *osd_sharedmem_alloc(const char *path, int create, size_t size)
{
int fd;
osd_shared_mem *os_shmem = (osd_shared_mem *) osd_malloc(sizeof(osd_shared_mem));
osd_shared_mem *os_shmem = (osd_shared_mem *)malloc(sizeof(osd_shared_mem));
if (create)
{
char *buf = (char *) osd_malloc_array(size);
char *buf = (char *) malloc(size);
memset(buf,0, size);
fd = open(path, O_RDWR | O_CREAT, S_IRWXU);
@ -101,12 +101,12 @@ static osd_shared_mem *osd_sharedmem_alloc(const char *path, int create, size_t
fd = open(path, O_RDWR);
if (fd == -1)
{
osd_free(os_shmem);
free(os_shmem);
return nullptr;
}
os_shmem->creator = 0;
}
os_shmem->fn = (char *) osd_malloc_array(strlen(path)+1);
os_shmem->fn = (char *) malloc(strlen(path)+1);
strcpy(os_shmem->fn, path);
assert(fd != -1);
@ -122,8 +122,8 @@ static void osd_sharedmem_free(osd_shared_mem *os_shmem)
munmap(os_shmem->ptr, os_shmem->size);
if (os_shmem->creator)
unlink(os_shmem->fn);
osd_free(os_shmem->fn);
osd_free(os_shmem);
free(os_shmem->fn);
free(os_shmem);
}
static void *osd_sharedmem_ptr(osd_shared_mem *os_shmem)
@ -133,19 +133,19 @@ static void *osd_sharedmem_ptr(osd_shared_mem *os_shmem)
#else
static osd_shared_mem *osd_sharedmem_alloc(const char *path, int create, size_t size)
{
osd_shared_mem *os_shmem = (osd_shared_mem *) osd_malloc(sizeof(osd_shared_mem));
osd_shared_mem *os_shmem = (osd_shared_mem *) malloc(sizeof(osd_shared_mem));
os_shmem->creator = 0;
os_shmem->ptr = (void *) osd_malloc_array(size);
os_shmem->ptr = (void *) malloc(size);
os_shmem->size = size;
return os_shmem;
}
static void osd_sharedmem_free(osd_shared_mem *os_shmem)
{
osd_free(os_shmem->ptr);
osd_free(os_shmem);
free(os_shmem->ptr);
free(os_shmem);
}
static void *osd_sharedmem_ptr(osd_shared_mem *os_shmem)

View File

@ -276,7 +276,7 @@ osd_file::error osd_file::open(std::string const &path, std::uint32_t openflags,
}
}
// if we still failed, clean up and osd_free
// if we still failed, clean up and free
if (fd < 0)
{
return errno_to_file_error(errno);

View File

@ -197,7 +197,7 @@ osd_directory_entry *osd_stat(const std::string &path)
// create an osd_directory_entry; be sure to make sure that the caller can
// free all resources by just freeing the resulting osd_directory_entry
result = (osd_directory_entry *)osd_malloc_array(sizeof(*result) + path.length() + 1);
result = (osd_directory_entry *)malloc(sizeof(*result) + path.length() + 1);
strcpy((char *)(result + 1), path.c_str());
result->name = (char *)(result + 1);
result->type = ENTTYPE_NONE;

View File

@ -11,9 +11,6 @@
//
// - osd_ticks
// - osd_sleep
// - osd_malloc
// - osd_malloc_array
// - osd_free
//============================================================
#ifndef __OSDLIB__
@ -60,7 +57,7 @@ int osd_setenv(const char *name, const char *value, int overwrite);
Return value:
the returned string needs to be osd_free()-ed!
the returned string needs to be free-ed!
-----------------------------------------------------------------------------*/
char *osd_get_clipboard_text(void);

View File

@ -57,48 +57,6 @@ void osd_process_kill()
kill(getpid(), SIGKILL);
}
//============================================================
// osd_malloc
//============================================================
void *osd_malloc(size_t size)
{
#ifndef MALLOC_DEBUG
return malloc(size);
#else
#error "MALLOC_DEBUG not yet supported"
#endif
}
//============================================================
// osd_malloc_array
//============================================================
void *osd_malloc_array(size_t size)
{
#ifndef MALLOC_DEBUG
return malloc(size);
#else
#error "MALLOC_DEBUG not yet supported"
#endif
}
//============================================================
// osd_free
//============================================================
void osd_free(void *ptr)
{
#ifndef MALLOC_DEBUG
free(ptr);
#else
#error "MALLOC_DEBUG not yet supported"
#endif
}
//============================================================
// osd_alloc_executable
//
@ -205,7 +163,7 @@ char *osd_get_clipboard_text(void)
CFIndex const length = CFDataGetLength(data_ref);
CFRange const range = CFRangeMake(0, length);
result = reinterpret_cast<char *>(osd_malloc_array(length + 1));
result = reinterpret_cast<char *>(malloc(length + 1));
if (result)
{
CFDataGetBytes(data_ref, range, reinterpret_cast<unsigned char *>(result));

View File

@ -55,47 +55,6 @@ void osd_process_kill()
kill(getpid(), SIGKILL);
}
//============================================================
// osd_malloc
//============================================================
void *osd_malloc(size_t size)
{
#ifndef MALLOC_DEBUG
return malloc(size);
#else
#error "MALLOC_DEBUG not yet supported"
#endif
}
//============================================================
// osd_malloc_array
//============================================================
void *osd_malloc_array(size_t size)
{
#ifndef MALLOC_DEBUG
return malloc(size);
#else
#error "MALLOC_DEBUG not yet supported"
#endif
}
//============================================================
// osd_free
//============================================================
void osd_free(void *ptr)
{
#ifndef MALLOC_DEBUG
free(ptr);
#else
#error "MALLOC_DEBUG not yet supported"
#endif
}
//============================================================
// osd_alloc_executable
//
@ -160,7 +119,7 @@ char *osd_get_clipboard_text(void)
if (SDL_HasClipboardText())
{
char *temp = SDL_GetClipboardText();
result = (char *) osd_malloc_array(strlen(temp) + 1);
result = (char *) malloc(strlen(temp) + 1);
strcpy(result, temp);
SDL_free(temp);
}

View File

@ -111,36 +111,6 @@ void osd_process_kill()
TerminateProcess(GetCurrentProcess(), -1);
}
//============================================================
// osd_malloc
//============================================================
void *osd_malloc(size_t size)
{
return malloc(size);
}
//============================================================
// osd_malloc_array
//============================================================
void *osd_malloc_array(size_t size)
{
return malloc(size);
}
//============================================================
// osd_free
//============================================================
void osd_free(void *ptr)
{
free(ptr);
}
//============================================================
// osd_alloc_executable
//

View File

@ -84,7 +84,7 @@ int osd_setenv(const char *name, const char *value, int overwrite)
if (osd_getenv(name) != nullptr)
return 0;
}
buf = (char *) osd_malloc_array(strlen(name)+strlen(value)+2);
buf = (char *) malloc(strlen(name)+strlen(value)+2);
sprintf(buf, "%s=%s", name, value);
result = putenv(buf);
@ -106,99 +106,6 @@ void osd_process_kill()
TerminateProcess(GetCurrentProcess(), -1);
}
//============================================================
// osd_malloc
//============================================================
void *osd_malloc(size_t size)
{
#ifndef MALLOC_DEBUG
return malloc(size);
#else
// add in space for the size and offset
size += MAX_ALIGNMENT + sizeof(size_t) + 2;
size &= ~size_t(1);
// basic objects just come from the heap
uint8_t *const block = reinterpret_cast<uint8_t *>(HeapAlloc(GetProcessHeap(), 0, size));
if (block == nullptr)
return nullptr;
uint8_t *const result = reinterpret_cast<uint8_t *>(reinterpret_cast<uintptr_t>(block + sizeof(size_t) + MAX_ALIGNMENT) & ~(uintptr_t(MAX_ALIGNMENT) - 1));
// store the size and return and pointer to the data afterward
*reinterpret_cast<size_t *>(block) = size;
*(result - 1) = result - block;
return result;
#endif
}
//============================================================
// osd_malloc_array
//============================================================
void *osd_malloc_array(size_t size)
{
#ifndef MALLOC_DEBUG
return malloc(size);
#else
// add in space for the size and offset
size += MAX_ALIGNMENT + sizeof(size_t) + 2;
size &= ~size_t(1);
// round the size up to a page boundary
size_t const rounded_size = ((size + sizeof(void *) + PAGE_SIZE - 1) / PAGE_SIZE) * PAGE_SIZE;
// reserve that much memory, plus two guard pages
void *page_base = VirtualAlloc(nullptr, rounded_size + 2 * PAGE_SIZE, MEM_RESERVE, PAGE_NOACCESS);
if (page_base == nullptr)
return nullptr;
// now allow access to everything but the first and last pages
page_base = VirtualAlloc(reinterpret_cast<uint8_t *>(page_base) + PAGE_SIZE, rounded_size, MEM_COMMIT, PAGE_READWRITE);
if (page_base == nullptr)
return nullptr;
// work backwards from the page base to get to the block base
uint8_t *const block = GUARD_ALIGN_START ? reinterpret_cast<uint8_t *>(page_base) : (reinterpret_cast<uint8_t *>(page_base) + rounded_size - size);
uint8_t *const result = reinterpret_cast<uint8_t *>(reinterpret_cast<uintptr_t>(block + sizeof(size_t) + MAX_ALIGNMENT) & ~(uintptr_t(MAX_ALIGNMENT) - 1));
// store the size at the start with a flag indicating it has a guard page
*reinterpret_cast<size_t *>(block) = size | 1;
*(result - 1) = result - block;
return result;
#endif
}
//============================================================
// osd_free
//============================================================
void osd_free(void *ptr)
{
#ifndef MALLOC_DEBUG
free(ptr);
#else
uint8_t const offset = *(reinterpret_cast<uint8_t *>(ptr) - 1);
uint8_t *const block = reinterpret_cast<uint8_t *>(ptr) - offset;
size_t const size = *reinterpret_cast<size_t *>(block);
if ((size & 0x1) == 0)
{
// if no guard page, just free the pointer
HeapFree(GetProcessHeap(), 0, block);
}
else
{
// large items need more care
ULONG_PTR const page_base = reinterpret_cast<ULONG_PTR>(block) & ~(PAGE_SIZE - 1);
VirtualFree(reinterpret_cast<void *>(page_base - PAGE_SIZE), 0, MEM_RELEASE);
}
#endif
}
//============================================================
// osd_alloc_executable
//
@ -275,7 +182,7 @@ static char *get_clipboard_text_by_format(UINT format, std::string (*convert)(LP
std::string s = (*convert)(data);
// copy the string
result = (char *) osd_malloc(s.size() + 1);
result = (char *) malloc(s.size() + 1);
if (result != nullptr)
memcpy(result, s.data(), (s.size() + 1) * sizeof(*result));

View File

@ -193,7 +193,7 @@ osd_common_t::osd_common_t(osd_options &options)
osd_common_t::~osd_common_t()
{
for(unsigned int i= 0; i < m_video_names.size(); ++i)
osd_free(const_cast<char*>(m_video_names[i]));
free(const_cast<char*>(m_video_names[i]));
//m_video_options,reset();
osd_output::pop(this);
}

View File

@ -684,59 +684,6 @@ void osd_work_item_release(osd_work_item *item);
MISCELLANEOUS INTERFACES
***************************************************************************/
/*-----------------------------------------------------------------------------
osd_malloc: allocate memory
Parameters:
size - the number of bytes to allocate
Return value:
a pointer to the allocated memory
Notes:
This is just a hook to do OS-specific allocation trickery.
It can be safely written as a wrapper to malloc().
-----------------------------------------------------------------------------*/
void *osd_malloc(size_t size);
/*-----------------------------------------------------------------------------
osd_malloc_array: allocate memory, hinting tha this memory contains an
array
Parameters:
size - the number of bytes to allocate
Return value:
a pointer to the allocated memory
Notes:
This is just a hook to do OS-specific allocation trickery.
It can be safely written as a wrapper to malloc().
-----------------------------------------------------------------------------*/
void *osd_malloc_array(size_t size);
/*-----------------------------------------------------------------------------
osd_free: free memory allocated by osd_malloc
Parameters:
ptr - the pointer returned from osd_mallo
Return value:
None
-----------------------------------------------------------------------------*/
void osd_free(void *ptr);
/*-----------------------------------------------------------------------------
osd_alloc_executable: allocate memory that can contain executable code

View File

@ -28,7 +28,7 @@ static char *utf8_from_latin1(const char *src)
}
/* allocate space for result, twice the source len to be safe */
buffer = (char *) osd_malloc(strlen(src) * 2 + 1);
buffer = (char *) malloc(strlen(src) * 2 + 1);
/* point to the start */
bufptr = buffer;
@ -73,7 +73,7 @@ static char *latin1_from_utf8(const char *src)
}
/* allocate space for result */
buffer = (char *) osd_malloc(strlen(src) + 1);
buffer = (char *) malloc(strlen(src) + 1);
/* point to the start */
bufptr = buffer;

View File

@ -1332,7 +1332,7 @@ done:
if (alloc_path != nullptr)
free(alloc_path);
if (new_fname != nullptr)
osd_free(new_fname);
free(new_fname);
return err;
}
@ -1376,7 +1376,7 @@ done:
if (alloc_path != nullptr)
free(alloc_path);
if (new_fname != nullptr)
osd_free(new_fname);
free(new_fname);
return err;
}
@ -1481,7 +1481,7 @@ done:
if (alloc_path)
free(alloc_path);
if (new_fname)
osd_free(new_fname);
free(new_fname);
return err;
}
@ -1920,7 +1920,7 @@ done:
if (alloc_dest != nullptr)
free(alloc_dest);
if (new_fname != nullptr)
osd_free(new_fname);
free(new_fname);
return err;
}
@ -1968,7 +1968,7 @@ imgtoolerr_t imgtool::partition::put_file(const char *newfname, const char *fork
done:
/* clean up */
if (alloc_newfname != nullptr)
osd_free(alloc_newfname);
free(alloc_newfname);
return err;
}
@ -2016,7 +2016,7 @@ done:
if (alloc_path)
free(alloc_path);
if (new_fname)
osd_free(new_fname);
free(new_fname);
return err;
}
@ -2060,7 +2060,7 @@ done:
if (alloc_path)
free(alloc_path);
if (new_fname)
osd_free(new_fname);
free(new_fname);
return err;
}
@ -2105,7 +2105,7 @@ done:
if (alloc_path)
free(alloc_path);
if (new_path)
osd_free(new_path);
free(new_path);
return err;
}
@ -2150,7 +2150,7 @@ done:
if (alloc_path)
free(alloc_path);
if (new_path)
osd_free(new_path);
free(new_path);
return err;
}
@ -2466,7 +2466,7 @@ done:
if (alloc_path != nullptr)
free(alloc_path);
if (new_path != nullptr)
osd_free(new_path);
free(new_path);
return err;
}
@ -2509,7 +2509,7 @@ imgtoolerr_t imgtool::directory::get_next(imgtool_dirent &ent)
return IMGTOOLERR_BADFILENAME;
snprintf(ent.filename, ARRAY_LENGTH(ent.filename), "%s", new_fname);
osd_free(new_fname);
free(new_fname);
}
// don't trust the module!

View File

@ -238,7 +238,7 @@ int main(int argc, char *argv[])
if (util::core_file::load(tempfilename.c_str(), &buffer, bufsize) == osd_file::error::NONE)
{
tempheader.assign((const char *)buffer, bufsize);
osd_free(buffer);
free(buffer);
}
/* verify the template */

View File

@ -343,7 +343,7 @@ static int join_file(const char *filename, const char *outname, int write_output
printf(" verified\n");
// release allocated memory
osd_free(splitbuffer);
free(splitbuffer);
splitbuffer = nullptr;
}
if (write_output)
@ -366,7 +366,7 @@ cleanup:
remove(outfilename.c_str());
}
if (splitbuffer != nullptr)
osd_free(splitbuffer);
free(splitbuffer);
return error;
}

View File

@ -282,7 +282,7 @@ int main(int argc, char *argv[])
if (util::core_file::load(tempfilename.c_str(), &buffer, bufsize) == osd_file::error::NONE)
{
tempheader.assign((const char *)buffer, bufsize);
osd_free(buffer);
free(buffer);
}
// verify the template

View File

@ -707,7 +707,7 @@ int main(int argc, char *argv[])
result = 1;
}
osd_free(data);
free(data);
return result;
}