From 91a1b8d634131714ec89a07496606880ea487ece Mon Sep 17 00:00:00 2001 From: Aaron Giles Date: Fri, 8 Jan 2010 06:05:29 +0000 Subject: 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. --- src/lib/util/bitmap.c | 205 +++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 185 insertions(+), 20 deletions(-) (limited to 'src/lib/util/bitmap.c') 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 +#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 @@ -145,6 +285,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 -- cgit v1.2.3-70-g09d2