diff options
author | 2010-01-08 06:05:29 +0000 | |
---|---|---|
committer | 2010-01-08 06:05:29 +0000 | |
commit | 91a1b8d634131714ec89a07496606880ea487ece (patch) | |
tree | be1623ecd9688f97d426efe03688289e4a25f6b5 /src/lib | |
parent | 26ef6355153f38264122c168438d09d1811b9530 (diff) |
NOTE: This change requires two new osd functions: osd_malloc() and
osd_free(). They take the same parameters as malloc() and free().
Renamed mamecore.h -> emucore.h.
New C++-aware memory manager, implemented in emualloc.*. This is a
simple manager that allows you to add any type of object to a
resource pool. Most commonly, allocated objects are added, and so
a set of allocation macros is provided to allow you to manage
objects in a particular pool:
pool_alloc(p, t) = allocate object of type 't' and add to pool 'p'
pool_alloc_clear(p, t) = same as above, but clear the memory first
pool_alloc_array(p, t, c) = allocate an array of 'c' objects of type
't' and add to pool 'p'
pool_alloc_array_clear(p, t, c) = same, but with clearing
pool_free(p, v) = free object 'v' and remove it from the pool
Note that pool_alloc[_clear] is roughly equivalent to "new t" and
pool_alloc_array[_clear] is roughly equivalent to "new t[c]". Also
note that pool_free works for single objects and arrays.
There is a single global_resource_pool defined which should be used
for any global allocations. It has equivalent macros to the pool_*
macros above that automatically target the global pool.
In addition, the memory module defines global new/delete overrides
that access file and line number parameters so that allocations can
be tracked. Currently this tracking is only done if MAME_DEBUG is
enabled. In debug builds, any unfreed memory will be printed at
the end of the session.
emualloc.h also has #defines to disable malloc/free/realloc/calloc.
Since emualloc.h is included by emucore.h, this means pretty much
all code within the emulator is forced to use the new allocators.
Although straight new/delete do work, their use is discouraged, as
any allocations made with them will not be tracked.
Changed the familar auto_alloc_* macros to map to the resource pool
model described above. The running_machine is now a class and contains
a resource pool which is automatically destructed upon deletion. If
you are a driver writer, all your allocations should be done with
auto_alloc_*.
Changed all drivers and files in the core using malloc/realloc or the
old alloc_*_or_die macros to use (preferably) the auto_alloc_* macros
instead, or the global_alloc_* macros if necessary.
Added simple C++ wrappers for astring and bitmap_t, as these need
proper constructors/destructors to be used for auto_alloc_astring and
auto_alloc_bitmap.
Removed references to the winalloc prefix file. Most of its
functionality has moved into the core, save for the guard page
allocations, which are now implemented in osd_alloc and osd_free.
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/util/astring.c | 67 | ||||
-rw-r--r-- | src/lib/util/astring.h | 33 | ||||
-rw-r--r-- | src/lib/util/bitmap.c | 205 | ||||
-rw-r--r-- | src/lib/util/bitmap.h | 31 | ||||
-rw-r--r-- | src/lib/util/pool.c | 8 |
5 files changed, 299 insertions, 45 deletions
diff --git a/src/lib/util/astring.c b/src/lib/util/astring.c index 18f8968cf21..7ce911dfa30 100644 --- a/src/lib/util/astring.c +++ b/src/lib/util/astring.c @@ -48,24 +48,16 @@ /*************************************************************************** - TYPE DEFINITIONS -***************************************************************************/ - -struct _astring -{ - char * text; - int alloclen; - char smallbuf[256 - sizeof(int) - sizeof(char *)]; -}; - - - -/*************************************************************************** GLOBAL VARIABLES ***************************************************************************/ static const char dummy_text[2] = { 0 }; + +#ifdef __cplusplus +static const astring dummy_astring; +#else static const astring dummy_astring = { (char *)dummy_text, 1, { 0 } }; +#endif @@ -143,6 +135,53 @@ INLINE void normalize_substr(int *start, int *count, int length) ASTRING ALLOCATION ***************************************************************************/ +#ifdef __cplusplus + +/*------------------------------------------------- + astring - basic constructor +-------------------------------------------------*/ + +astring::astring() +{ + /* initialize base fields by hand */ + text = smallbuf; + alloclen = ARRAY_LENGTH(smallbuf); + smallbuf[0] = 0; +} + + +/*------------------------------------------------- + ~astring - basic destructor +-------------------------------------------------*/ + +astring::~astring() +{ + if (text != smallbuf) + free(text); +} + + +/*------------------------------------------------- + astring_alloc - allocate a new astring +-------------------------------------------------*/ + +astring *astring_alloc(void) +{ + return new astring; +} + + +/*------------------------------------------------- + astring_free - free an astring +-------------------------------------------------*/ + +void astring_free(astring *str) +{ + delete str; +} + +#else + /*------------------------------------------------- astring_alloc - allocate a new astring -------------------------------------------------*/ @@ -180,6 +219,8 @@ void astring_free(astring *str) free(str); } +#endif + /*************************************************************************** diff --git a/src/lib/util/astring.h b/src/lib/util/astring.h index 1ac8f449e0d..84cd030cf47 100644 --- a/src/lib/util/astring.h +++ b/src/lib/util/astring.h @@ -42,15 +42,44 @@ #ifndef __ASTRING_H__ #define __ASTRING_H__ -#include "pool.h" #include <stdarg.h> +#include "osdcomm.h" /*************************************************************************** TYPE DEFINITIONS ***************************************************************************/ -typedef struct _astring astring; +/* base astring structure */ +typedef struct _astring_base astring_base; +struct _astring_base +{ + char * text; + int alloclen; + char smallbuf[64 - sizeof(int) - sizeof(char *)]; +}; + + +#ifdef __cplusplus + +/* class for C++ */ +class astring : public astring_base +{ +private: + astring(const astring &); + astring &operator=(const astring &); + +public: + astring(); + ~astring(); +}; + +#else + +/* direct map for C */ +typedef astring_base astring; + +#endif diff --git a/src/lib/util/bitmap.c b/src/lib/util/bitmap.c index 8eb7cbd2bbd..3f40e9f3a20 100644 --- a/src/lib/util/bitmap.c +++ b/src/lib/util/bitmap.c @@ -39,12 +39,152 @@ #include "bitmap.h" +#ifdef __cplusplus +#include <new> +#endif /*************************************************************************** BITMAP ALLOCATION/CONFIGURATION ***************************************************************************/ +#ifdef __cplusplus + +/*------------------------------------------------- + bitmap_t - basic constructor +-------------------------------------------------*/ + +bitmap_t::bitmap_t() +{ + /* initialize base fields by hand */ + alloc = NULL; + base = NULL; + rowpixels = 0; + width = 0; + height = 0; + format = BITMAP_FORMAT_INVALID; + bpp = 0; + palette = NULL; + cliprect.min_x = cliprect.min_y = 0; + cliprect.max_x = cliprect.max_y = 0; +} + + +bitmap_t::bitmap_t(int _width, int _height, bitmap_format _format, int _xslop, int _yslop) +{ + /* initialize base fields by hand */ + alloc = NULL; + base = NULL; + rowpixels = (_width + 2 * _xslop + 7) & ~7; + width = _width; + height = _height; + format = _format; + bpp = bitmap_format_to_bpp(_format); + palette = NULL; + cliprect.min_x = cliprect.min_y = 0; + cliprect.max_x = width - 1; + cliprect.max_y = height - 1; + + /* fail if invalid format */ + if (bpp == 0) + throw std::bad_alloc(); + + /* allocate memory for the bitmap itself */ + size_t allocbytes = rowpixels * (height + 2 * _yslop) * bpp / 8; + alloc = malloc(allocbytes); + if (alloc == NULL) + throw std::bad_alloc(); + + /* clear to 0 by default */ + memset(alloc, 0, allocbytes); + + /* compute the base */ + base = (UINT8 *)alloc + (rowpixels * _yslop + _xslop) * (bpp / 8); +} + + +bitmap_t::bitmap_t(void *_base, int _width, int _height, int _rowpixels, bitmap_format _format) +{ + /* initialize base fields by hand */ + alloc = NULL; + base = _base; + rowpixels = _rowpixels; + width = _width; + height = _height; + format = _format; + bpp = bitmap_format_to_bpp(_format); + palette = NULL; + cliprect.min_x = cliprect.min_y = 0; + cliprect.max_x = width - 1; + cliprect.max_y = height - 1; + + /* fail if invalid format */ + if (bpp == 0) + throw std::bad_alloc(); +} + + +/*------------------------------------------------- + ~bitmap_t - basic destructor +-------------------------------------------------*/ + +bitmap_t::~bitmap_t() +{ + /* dereference the palette */ + if (palette != NULL) + palette_deref(palette); + + /* free any allocated memory */ + if (alloc != NULL) + free(alloc); +} + + +/*------------------------------------------------- + bitmap_alloc -- allocate memory for a new + bitmap of the given format +-------------------------------------------------*/ + +bitmap_t *bitmap_alloc(int width, int height, bitmap_format format) +{ + return new bitmap_t(width, height, format); +} + + +/*------------------------------------------------- + bitmap_alloc_slop -- allocate a new bitmap with + additional slop on the borders +-------------------------------------------------*/ + +bitmap_t *bitmap_alloc_slop(int width, int height, int xslop, int yslop, bitmap_format format) +{ + return new bitmap_t(width, height, format, xslop, yslop); +} + + +/*------------------------------------------------- + bitmap_wrap -- wrap an existing memory buffer + as a bitmap +-------------------------------------------------*/ + +bitmap_t *bitmap_wrap(void *base, int width, int height, int rowpixels, bitmap_format format) +{ + return new bitmap_t(base, width, height, rowpixels, format); +} + + +/*------------------------------------------------- + bitmap_free -- release memory allocated for + a bitmap +-------------------------------------------------*/ + +void bitmap_free(bitmap_t *bitmap) +{ + delete bitmap; +} + +#else + /*------------------------------------------------- bitmap_alloc -- allocate memory for a new bitmap of the given format @@ -146,6 +286,51 @@ bitmap_t *bitmap_wrap(void *base, int width, int height, int rowpixels, bitmap_f /*------------------------------------------------- + bitmap_free -- release memory allocated for + a bitmap +-------------------------------------------------*/ + +void bitmap_free(bitmap_t *bitmap) +{ + /* dereference the palette */ + if (bitmap->palette != NULL) + palette_deref(bitmap->palette); + + /* free any allocated memory */ + if (bitmap->alloc != NULL) + free(bitmap->alloc); + + /* free the bitmap */ + free(bitmap); +} + +#endif + + +/*------------------------------------------------- + bitmap_clone_existing -- clone an existing + bitmap by copying its fields; the target + bitmap does not own the memory +-------------------------------------------------*/ + +void bitmap_clone_existing(bitmap_t *bitmap, const bitmap_t *srcbitmap) +{ + if (bitmap->alloc != NULL) + free(bitmap->alloc); + bitmap->alloc = NULL; + + bitmap->base = srcbitmap->base; + bitmap->rowpixels = srcbitmap->rowpixels; + bitmap->width = srcbitmap->width; + bitmap->height = srcbitmap->height; + bitmap->format = srcbitmap->format; + bitmap->bpp = srcbitmap->bpp; + bitmap->palette = srcbitmap->palette; + bitmap->cliprect = srcbitmap->cliprect; +} + + +/*------------------------------------------------- bitmap_set_palette -- associate a palette with a bitmap -------------------------------------------------*/ @@ -168,26 +353,6 @@ void bitmap_set_palette(bitmap_t *bitmap, palette_t *palette) } -/*------------------------------------------------- - bitmap_free -- release memory allocated for - a bitmap --------------------------------------------------*/ - -void bitmap_free(bitmap_t *bitmap) -{ - /* dereference the palette */ - if (bitmap->palette != NULL) - palette_deref(bitmap->palette); - - /* free any allocated memory */ - if (bitmap->alloc != NULL) - free(bitmap->alloc); - - /* free the bitmap */ - free(bitmap); -} - - /*************************************************************************** BITMAP DRAWING diff --git a/src/lib/util/bitmap.h b/src/lib/util/bitmap.h index 06a515867a4..b95f176ebd0 100644 --- a/src/lib/util/bitmap.h +++ b/src/lib/util/bitmap.h @@ -79,8 +79,8 @@ struct _rectangle /* bitmaps describe a rectangular array of pixels */ -typedef struct _bitmap_t bitmap_t; -struct _bitmap_t +typedef struct _bitmap_base bitmap_base; +struct _bitmap_base { void * alloc; /* pointer to allocated pixel memory */ void * base; /* pointer to pixel (0,0) (adjusted for padding) */ @@ -94,6 +94,30 @@ struct _bitmap_t }; +#ifdef __cplusplus + +/* class for C++ */ +class bitmap_t : public bitmap_base +{ +private: + bitmap_t(const bitmap_t &); + bitmap_t &operator=(const bitmap_t &); + +public: + bitmap_t(); + bitmap_t(int _width, int _height, bitmap_format _format, int _xslop = 0, int _yslop = 0); + bitmap_t(void *_base, int _width, int _height, int _rowpixels, bitmap_format _format); + ~bitmap_t(); +}; + +#else + +/* direct map for C */ +typedef bitmap_base bitmap_t; + +#endif + + /*************************************************************************** MACROS @@ -132,6 +156,9 @@ void bitmap_set_palette(bitmap_t *bitmap, palette_t *palette); /* free allocated data for a bitmap */ void bitmap_free(bitmap_t *bitmap); +/* clone an existing bitmap by copying its fields; the target bitmap does not own the memory */ +void bitmap_clone_existing(bitmap_t *bitmap, const bitmap_t *srcbitmap); + /* ----- bitmap drawing ----- */ diff --git a/src/lib/util/pool.c b/src/lib/util/pool.c index 48b5791b304..b73d948dccf 100644 --- a/src/lib/util/pool.c +++ b/src/lib/util/pool.c @@ -497,11 +497,7 @@ void pool_iterate_end(object_pool_iterator *iter) void *pool_malloc_file_line(object_pool *pool, size_t size, const char *file, int line) { -#ifdef MALLOC_DEBUG - void *ptr = malloc_file_line(size, file, line); -#else void *ptr = malloc(size); -#endif return pool_object_add_file_line(pool, OBJTYPE_MEMORY, ptr, size, file, line); } @@ -515,11 +511,7 @@ void *pool_realloc_file_line(object_pool *pool, void *ptr, size_t size, const ch { if (ptr != NULL) pool_object_remove(pool, ptr, FALSE); -#ifdef MALLOC_DEBUG - ptr = realloc_file_line(ptr, size, file, line); -#else ptr = realloc(ptr, size); -#endif if (size != 0) pool_object_add_file_line(pool, OBJTYPE_MEMORY, ptr, size, file, line); return ptr; |