diff options
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; |