From 1e4db221e5fe6867e4558528b0d93db2c1de4170 Mon Sep 17 00:00:00 2001 From: Max Horn Date: Tue, 24 Feb 2009 05:39:10 +0000 Subject: SCI: Moved engines/sci/include/sci_memory.h and engines/sci/scicore/sci_memory.cpp into engines/sci/; moved macros from the header into the source file svn-id: r38832 --- engines/sci/engine/aatree.cpp | 2 +- engines/sci/engine/savegame.cfsml | 2 +- engines/sci/engine/savegame.cpp | 4 +- engines/sci/engine/scriptconsole.cpp | 2 +- engines/sci/engine/sys_strings.cpp | 2 +- engines/sci/gfx/gfx_pixmap_scale.cpp | 2 +- engines/sci/gfx/gfx_system.h | 2 +- engines/sci/gfx/gfx_tools.cpp | 2 +- engines/sci/gfx/menubar.cpp | 2 +- engines/sci/gfx/operations.cpp | 2 +- engines/sci/gfx/resource/sci_font.cpp | 2 +- engines/sci/gfx/resource/sci_pal_1.cpp | 2 +- engines/sci/gfx/resource/sci_pic_0.cpp | 2 +- engines/sci/gfx/resource/sci_resmgr.cpp | 2 +- engines/sci/gfx/resource/sci_view_0.cpp | 2 +- engines/sci/gfx/resource/sci_view_1.cpp | 2 +- engines/sci/gfx/sbtree.cpp | 2 +- engines/sci/gfx/widgets.cpp | 2 +- engines/sci/include/heapmgr.h | 2 +- engines/sci/include/sci_memory.h | 171 ---------------------------- engines/sci/include/sciconsole.h | 2 +- engines/sci/module.mk | 2 +- engines/sci/sci_memory.cpp | 190 ++++++++++++++++++++++++++++++++ engines/sci/sci_memory.h | 141 ++++++++++++++++++++++++ engines/sci/scicore/decompress0.cpp | 2 +- engines/sci/scicore/decompress01.cpp | 2 +- engines/sci/scicore/decompress1.cpp | 2 +- engines/sci/scicore/decompress11.cpp | 2 +- engines/sci/scicore/resource.cpp | 2 +- engines/sci/scicore/resource_map.cpp | 2 +- engines/sci/scicore/resource_patch.cpp | 2 +- engines/sci/scicore/sci_memory.cpp | 160 --------------------------- engines/sci/scicore/sciconsole.cpp | 2 +- engines/sci/sfx/iterator.cpp | 2 +- engines/sci/sfx/mixer/soft.cpp | 2 +- engines/sci/sfx/pcm-iterator.cpp | 2 +- engines/sci/sfx/seq/instrument-map.cpp | 2 +- engines/sci/sfx/softseq/amiga.cpp | 2 +- engines/sci/sfx/songlib.cpp | 2 +- 39 files changed, 367 insertions(+), 367 deletions(-) delete mode 100644 engines/sci/include/sci_memory.h create mode 100644 engines/sci/sci_memory.cpp create mode 100644 engines/sci/sci_memory.h delete mode 100644 engines/sci/scicore/sci_memory.cpp (limited to 'engines') diff --git a/engines/sci/engine/aatree.cpp b/engines/sci/engine/aatree.cpp index 3cd3a01691..4cbe1b5c0a 100644 --- a/engines/sci/engine/aatree.cpp +++ b/engines/sci/engine/aatree.cpp @@ -25,7 +25,7 @@ #include "sci/engine/aatree.h" -#include "sci/include/sci_memory.h" +#include "sci/sci_memory.h" namespace Sci { diff --git a/engines/sci/engine/savegame.cfsml b/engines/sci/engine/savegame.cfsml index 3d5da9b89e..ff55fe0a55 100644 --- a/engines/sci/engine/savegame.cfsml +++ b/engines/sci/engine/savegame.cfsml @@ -32,7 +32,7 @@ #include // FIXME: For struct tm #include "common/stream.h" #include "common/system.h" -#include "sci/include/sci_memory.h" +#include "sci/sci_memory.h" #include "sci/gfx/gfx_operations.h" #include "sci/gfx/menubar.h" #include "sci/sfx/sfx_engine.h" diff --git a/engines/sci/engine/savegame.cpp b/engines/sci/engine/savegame.cpp index 363f519759..5c5980ff06 100644 --- a/engines/sci/engine/savegame.cpp +++ b/engines/sci/engine/savegame.cpp @@ -32,7 +32,7 @@ #include // FIXME: For struct tm #include "common/stream.h" #include "common/system.h" -#include "sci/include/sci_memory.h" +#include "sci/sci_memory.h" #include "sci/gfx/gfx_operations.h" #include "sci/gfx/menubar.h" #include "sci/sfx/sfx_engine.h" @@ -237,7 +237,7 @@ RECORD synonym_t "synonym_t" { #line 102 "engines/sci/engine/savegame.cfsml" #include // We need va_lists -#include "sci/include/sci_memory.h" +#include "sci/sci_memory.h" #ifdef CFSML_DEBUG_MALLOC /* diff --git a/engines/sci/engine/scriptconsole.cpp b/engines/sci/engine/scriptconsole.cpp index b81b2794d3..9be395ed5e 100644 --- a/engines/sci/engine/scriptconsole.cpp +++ b/engines/sci/engine/scriptconsole.cpp @@ -26,7 +26,7 @@ /* Second half of the console implementation: VM dependent stuff */ /* Remember, it doesn't have to be fast. */ -#include "sci/include/sci_memory.h" +#include "sci/sci_memory.h" #include "sci/include/engine.h" namespace Sci { diff --git a/engines/sci/engine/sys_strings.cpp b/engines/sci/engine/sys_strings.cpp index 601b94a4b5..bcb7dd9219 100644 --- a/engines/sci/engine/sys_strings.cpp +++ b/engines/sci/engine/sys_strings.cpp @@ -25,7 +25,7 @@ #include "sci/tools.h" #include "sci/include/sys_strings.h" -#include "sci/include/sci_memory.h" +#include "sci/sci_memory.h" namespace Sci { diff --git a/engines/sci/gfx/gfx_pixmap_scale.cpp b/engines/sci/gfx/gfx_pixmap_scale.cpp index 139e35d24b..eacc78e47c 100644 --- a/engines/sci/gfx/gfx_pixmap_scale.cpp +++ b/engines/sci/gfx/gfx_pixmap_scale.cpp @@ -29,7 +29,7 @@ ** EXTRA_BYTE_OFFSET: Extra source byte offset for copying (used on big-endian machines in 24 bit mode) */ -#include "sci/include/sci_memory.h" +#include "sci/sci_memory.h" namespace Sci { diff --git a/engines/sci/gfx/gfx_system.h b/engines/sci/gfx/gfx_system.h index 9f8fda0cbe..efc293caaf 100644 --- a/engines/sci/gfx/gfx_system.h +++ b/engines/sci/gfx/gfx_system.h @@ -28,7 +28,7 @@ #include "common/scummsys.h" #include "common/rect.h" -#include "sci/include/sci_memory.h" +#include "sci/sci_memory.h" #include "sci/tools.h" namespace Sci { diff --git a/engines/sci/gfx/gfx_tools.cpp b/engines/sci/gfx/gfx_tools.cpp index 56338f4440..fe3e99b6a8 100644 --- a/engines/sci/gfx/gfx_tools.cpp +++ b/engines/sci/gfx/gfx_tools.cpp @@ -23,7 +23,7 @@ * */ -#include "sci/include/sci_memory.h" +#include "sci/sci_memory.h" #include "sci/gfx/gfx_tools.h" namespace Sci { diff --git a/engines/sci/gfx/menubar.cpp b/engines/sci/gfx/menubar.cpp index c125f48593..c81a8064dc 100644 --- a/engines/sci/gfx/menubar.cpp +++ b/engines/sci/gfx/menubar.cpp @@ -28,7 +28,7 @@ ** used for any actual actions on behalf of the interpreter. */ -#include "sci/include/sci_memory.h" +#include "sci/sci_memory.h" #include "sci/include/engine.h" #include "sci/gfx/menubar.h" diff --git a/engines/sci/gfx/operations.cpp b/engines/sci/gfx/operations.cpp index 16fbdec4a1..6250d89660 100644 --- a/engines/sci/gfx/operations.cpp +++ b/engines/sci/gfx/operations.cpp @@ -25,7 +25,7 @@ // Graphical operations, called from the widget state manager -#include "sci/include/sci_memory.h" +#include "sci/sci_memory.h" #include "sci/gfx/gfx_operations.h" #include "common/system.h" diff --git a/engines/sci/gfx/resource/sci_font.cpp b/engines/sci/gfx/resource/sci_font.cpp index a6d3abcb9d..e37306405e 100644 --- a/engines/sci/gfx/resource/sci_font.cpp +++ b/engines/sci/gfx/resource/sci_font.cpp @@ -23,7 +23,7 @@ * */ -#include "sci/include/sci_memory.h" +#include "sci/sci_memory.h" #include "sci/gfx/gfx_system.h" #include "sci/gfx/gfx_resource.h" #include "sci/gfx/gfx_tools.h" diff --git a/engines/sci/gfx/resource/sci_pal_1.cpp b/engines/sci/gfx/resource/sci_pal_1.cpp index 544f6db4f3..fdd11e2474 100644 --- a/engines/sci/gfx/resource/sci_pal_1.cpp +++ b/engines/sci/gfx/resource/sci_pal_1.cpp @@ -26,7 +26,7 @@ /* SCI1 palette resource defrobnicator */ #include "common/file.h" -#include "sci/include/sci_memory.h" +#include "sci/sci_memory.h" #include "sci/gfx/gfx_system.h" #include "sci/gfx/gfx_resource.h" diff --git a/engines/sci/gfx/resource/sci_pic_0.cpp b/engines/sci/gfx/resource/sci_pic_0.cpp index d64e46f32d..4e470a9435 100644 --- a/engines/sci/gfx/resource/sci_pic_0.cpp +++ b/engines/sci/gfx/resource/sci_pic_0.cpp @@ -24,7 +24,7 @@ */ #include // for time() to seed rand() via srand() -#include "sci/include/sci_memory.h" +#include "sci/sci_memory.h" #include "sci/gfx/gfx_resource.h" #include "sci/gfx/gfx_tools.h" diff --git a/engines/sci/gfx/resource/sci_resmgr.cpp b/engines/sci/gfx/resource/sci_resmgr.cpp index d64b777afa..4e2b1e2410 100644 --- a/engines/sci/gfx/resource/sci_resmgr.cpp +++ b/engines/sci/gfx/resource/sci_resmgr.cpp @@ -25,7 +25,7 @@ // The interpreter-specific part of the resource manager, for SCI -#include "sci/include/sci_memory.h" +#include "sci/sci_memory.h" #include "sci/include/sciresource.h" #include "sci/gfx/gfx_widgets.h" #include "sci/gfx/gfx_resmgr.h" diff --git a/engines/sci/gfx/resource/sci_view_0.cpp b/engines/sci/gfx/resource/sci_view_0.cpp index a22f0659b0..c5717b27b8 100644 --- a/engines/sci/gfx/resource/sci_view_0.cpp +++ b/engines/sci/gfx/resource/sci_view_0.cpp @@ -25,7 +25,7 @@ #include "common/endian.h" -#include "sci/include/sci_memory.h" +#include "sci/sci_memory.h" #include "sci/gfx/gfx_system.h" #include "sci/gfx/gfx_resource.h" #include "sci/gfx/gfx_tools.h" diff --git a/engines/sci/gfx/resource/sci_view_1.cpp b/engines/sci/gfx/resource/sci_view_1.cpp index 82ba6160a2..d013721735 100644 --- a/engines/sci/gfx/resource/sci_view_1.cpp +++ b/engines/sci/gfx/resource/sci_view_1.cpp @@ -27,7 +27,7 @@ #include "common/endian.h" -#include "sci/include/sci_memory.h" +#include "sci/sci_memory.h" #include "sci/gfx/gfx_system.h" #include "sci/gfx/gfx_resource.h" #include "sci/gfx/gfx_tools.h" diff --git a/engines/sci/gfx/sbtree.cpp b/engines/sci/gfx/sbtree.cpp index a791f0650a..711d7a9bb9 100644 --- a/engines/sci/gfx/sbtree.cpp +++ b/engines/sci/gfx/sbtree.cpp @@ -26,7 +26,7 @@ // Static binary lookup tree lookup -#include "sci/include/sci_memory.h" +#include "sci/sci_memory.h" #include "sci/gfx/sbtree.h" namespace Sci { diff --git a/engines/sci/gfx/widgets.cpp b/engines/sci/gfx/widgets.cpp index 93aedcc68d..02bf37b314 100644 --- a/engines/sci/gfx/widgets.cpp +++ b/engines/sci/gfx/widgets.cpp @@ -23,7 +23,7 @@ * */ -#include "sci/include/sci_memory.h" +#include "sci/sci_memory.h" #include "sci/gfx/gfx_widgets.h" namespace Sci { diff --git a/engines/sci/include/heapmgr.h b/engines/sci/include/heapmgr.h index 918a6a07fa..eb42b16acf 100644 --- a/engines/sci/include/heapmgr.h +++ b/engines/sci/include/heapmgr.h @@ -29,7 +29,7 @@ #define _FREESCI_HEAPMGR_H_ #include "sci/tools.h" -#include "sci/include/sci_memory.h" +#include "sci/sci_memory.h" namespace Sci { diff --git a/engines/sci/include/sci_memory.h b/engines/sci/include/sci_memory.h deleted file mode 100644 index d9ba069719..0000000000 --- a/engines/sci/include/sci_memory.h +++ /dev/null @@ -1,171 +0,0 @@ -/* ScummVM - Graphic Adventure Engine - * - * ScummVM is the legal property of its developers, whose names - * are too numerous to list here. Please refer to the COPYRIGHT - * file distributed with this source distribution. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - * $URL$ - * $Id$ - * - */ - - -/** This header file defines a portable library for allocating memory safely - ** throughout FreeSCI. - ** Implementations of basic functions found here are in this file and - ** $(SRCDIR)/src/scicore/sci_memory.c - * - ************** - * - * Sets behaviour if memory allocation call fails. - * UNCHECKED_MALLOCS: use C library routine without checks - * (nothing defined): check mallocs and exit immediately on fail (recommended) - * - ** -- Alex Angas - ** - **/ - - -#ifndef _SCI_MEMORY_H -#define _SCI_MEMORY_H - -#include "common/scummsys.h" - -namespace Sci { - -/********** the memory allocation macros **********/ - -#ifdef UNCHECKED_MALLOCS - -#define ALLOC_MEM(alloc_statement, size, filename, linenum, funcname)\ -do {\ - alloc_statement;\ -} while (0); - -#else /* !UNCHECKED_MALLOCS */ - -#define ALLOC_MEM(alloc_statement, size, filename, linenum, funcname)\ -do {\ - if (size == 0) {\ - warning("Allocating zero bytes of memory [%s (%s) : %u]", filename, funcname, linenum);\ - } else if (!(size > 0)) {\ - error("Cannot allocate negative bytes of memory [%s (%s) : %u]", filename, funcname, linenum);\ - }\ -\ - alloc_statement; /* attempt to allocate the memory */\ -\ - if (res == NULL) {\ - /* exit immediately */\ - error("Memory allocation of %lu bytes failed [%s (%s) : %u]", size, filename, funcname, linenum);\ - }\ -} while (0); - -#endif /* !UNCHECKED_MALLOCS */ - - -/********** memory allocation routines **********/ - -extern void *sci_malloc(size_t size); -/* Allocates the specified amount of memory. -** Parameters: (size_t) size: Number of bytes to allocate -** Returns : (void *) A pointer to the allocated memory chunk -** To free this string, use the free() command. -** If the call fails, behaviour is dependent on the definition of SCI_ALLOC. -*/ - -extern void *sci_calloc(size_t num, size_t size); -/* Allocates num * size bytes of zeroed-out memory. -** Parameters: (size_t) num: Number of elements to allocate -** (size_t) size: Amount of memory per element to allocate -** Returns : (void *) A pointer to the allocated memory chunk -** To free this string, use the free() command. -** See _SCI_MALLOC() for more information if call fails. -*/ - -extern void *sci_realloc(void *ptr, size_t size); -/* Increases the size of an allocated memory chunk. -** Parameters: (void *) ptr: The original pointer -** (size_t) size: New size of the memory chunk -** Returns : (void *) A possibly new pointer, containing 'size' -** bytes of memory and everything contained in the original 'ptr' -** (possibly minus some trailing data if the new memory area is -** smaller than the old one). -** To free this string, use the free() command. -** See _SCI_MALLOC() for more information if call fails. -*/ - - -extern char *sci_strdup(const char *src); -/* Duplicates a string. -** Parameters: (const char *) src: The original pointer -** Returns : (char *) a pointer to the storage location for the copied -** string. -** To free this string, use the free() command. -** See _SCI_MALLOC() for more information if call fails. -*/ - - -extern char *sci_strndup(const char *src, size_t length); -/* Copies a string into a newly allocated memory part, up to a certain length. -** Parameters: (char *) src: The source string -** (int) length: The maximum length of the string (not counting -** a trailing \0). -** Returns : (char *) The resulting copy, allocated with sci_malloc(). -** To free this string, use the free() command. -** See _SCI_MALLOC() for more information if call fails. -*/ - -/****************************************/ -/* Refcounting garbage collected memory */ -/****************************************/ - -/* Refcounting memory calls are a little slower than the others, -** and using it improperly may cause memory leaks. It conserves -** memory, though. */ - -extern void *sci_refcount_alloc(size_t length); -/* Allocates "garbage" memory -** Parameters: (size_t) length: Number of bytes to allocate -** Returns : (void *) The allocated memory -** Memory allocated in this fashion will be marked as holding one reference. -** It cannot be freed with 'free()', only by using sci_refcount_decref(). -*/ - -extern void *sci_refcount_incref(void *data); -/* Adds another reference to refcounted memory -** Parameters: (void *) data: The data to add a reference to -** Returns : (void *) data -*/ - -extern void sci_refcount_decref(void *data); -/* Decrements the reference count for refcounted memory -** Parameters: (void *) data: The data to add a reference to -** Returns : (void *) data -** If the refcount reaches zero, the memory will be deallocated -*/ - -extern void *sci_refcount_memdup(void *data, size_t len); -/* Duplicates non-refcounted memory into a refcounted block -** Parameters: (void *) data: The memory to copy from -** (size_t) len: The number of bytes to copy/allocate -** Returns : (void *) Newly allocated refcounted memory -** The number of references accounted for will be one. -*/ - -} // End of namespace Sci - -#endif /* _SCI_MEMORY_H */ diff --git a/engines/sci/include/sciconsole.h b/engines/sci/include/sciconsole.h index 8888e5927d..92c80601be 100644 --- a/engines/sci/include/sciconsole.h +++ b/engines/sci/include/sciconsole.h @@ -34,7 +34,7 @@ #include "common/scummsys.h" -#include "sci/include/sci_memory.h" +#include "sci/sci_memory.h" #include "sci/tools.h" #include "sci/include/vm_types.h" diff --git a/engines/sci/module.mk b/engines/sci/module.mk index 876a36911c..8803549f3d 100644 --- a/engines/sci/module.mk +++ b/engines/sci/module.mk @@ -5,6 +5,7 @@ MODULE_OBJS = \ detection.o \ exereader.o \ sci.o \ + sci_memory.o \ tools.o \ engine/aatree.o \ engine/game.o \ @@ -58,7 +59,6 @@ MODULE_OBJS = \ scicore/resource.o \ scicore/resource_map.o \ scicore/resource_patch.o \ - scicore/sci_memory.o \ scicore/script.o \ scicore/versions.o \ scicore/vocab.o \ diff --git a/engines/sci/sci_memory.cpp b/engines/sci/sci_memory.cpp new file mode 100644 index 0000000000..3fb320dad0 --- /dev/null +++ b/engines/sci/sci_memory.cpp @@ -0,0 +1,190 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * $URL$ + * $Id$ + * + */ + +#include "common/util.h" +#include "sci/tools.h" +#include "sci/sci_memory.h" + +namespace Sci { + +/********** the memory allocation macros **********/ + +#ifdef UNCHECKED_MALLOCS + +#define ALLOC_MEM(alloc_statement, size, filename, linenum, funcname)\ +do {\ + alloc_statement;\ +} while (0); + +#else /* !UNCHECKED_MALLOCS */ + +#define ALLOC_MEM(alloc_statement, size, filename, linenum, funcname)\ +do {\ + if (size == 0) {\ + warning("Allocating zero bytes of memory [%s (%s) : %u]", filename, funcname, linenum);\ + } else if (!(size > 0)) {\ + error("Cannot allocate negative bytes of memory [%s (%s) : %u]", filename, funcname, linenum);\ + }\ +\ + alloc_statement; /* attempt to allocate the memory */\ +\ + if (res == NULL) {\ + /* exit immediately */\ + error("Memory allocation of %lu bytes failed [%s (%s) : %u]", size, filename, funcname, linenum);\ + }\ +} while (0); + +#endif /* !UNCHECKED_MALLOCS */ + + +void * sci_malloc(size_t size) { + void *res; + + ALLOC_MEM((res = malloc(size)), size, __FILE__, __LINE__, "") + return res; +} + + +void * sci_calloc(size_t num, size_t size) { + void *res; + + ALLOC_MEM((res = calloc(num, size)), num * size, __FILE__, __LINE__, "") + return res; +} + +void *sci_realloc(void *ptr, size_t size) { + void *res; + + ALLOC_MEM((res = realloc(ptr, size)), size, __FILE__, __LINE__, "") + return res; +} + +char *sci_strdup(const char *src) { + void *res; + + if (!src) { + fprintf(stderr, "_SCI_STRDUP() [%s (%s) : %u]\n", + __FILE__, "", __LINE__); + fprintf(stderr, " attempt to strdup NULL pointer\n"); + BREAKPOINT(); + } + ALLOC_MEM((res = strdup(src)), strlen(src), __FILE__, __LINE__, "") + + return (char*)res; +} + +char *sci_strndup(const char *src, size_t length) { + void *res; + char *strres; + size_t rlen = (int)MIN(strlen(src), length) + 1; + + if (!src) { + fprintf(stderr, "_SCI_STRNDUP() [%s (%s) : %u]\n", + __FILE__, "", __LINE__); + fprintf(stderr, " attempt to strndup NULL pointer\n"); + BREAKPOINT(); + } + ALLOC_MEM((res = malloc(rlen)), rlen, __FILE__, __LINE__, "") + + strres = (char *)res; + strncpy(strres, src, rlen); + strres[rlen - 1] = 0; + + return strres; +} + +//-------- Refcounting ---------- + +#define REFCOUNT_OVERHEAD (sizeof(uint32) * 3) +#define REFCOUNT_MAGIC_LIVE_1 0xebdc1741 +#define REFCOUNT_MAGIC_LIVE_2 0x17015ac9 +#define REFCOUNT_MAGIC_DEAD_1 0x11dead11 +#define REFCOUNT_MAGIC_DEAD_2 0x22dead22 + +#define REFCOUNT_CHECK(p) ((((uint32 *)(p))[-3] == REFCOUNT_MAGIC_LIVE_2) && (((uint32 *)(p))[-1] == REFCOUNT_MAGIC_LIVE_1)) + +#define REFCOUNT(p) (((uint32 *)p)[-2]) + +#undef TRACE_REFCOUNT + +extern void *sci_refcount_alloc(size_t length) { + uint32 *data = (uint32 *)sci_malloc(REFCOUNT_OVERHEAD + length); +#ifdef TRACE_REFCOUNT + fprintf(stderr, "[] REF: Real-alloc at %p\n", data); +#endif + data += 3; + + data[-1] = REFCOUNT_MAGIC_LIVE_1; + data[-3] = REFCOUNT_MAGIC_LIVE_2; + REFCOUNT(data) = 1; +#ifdef TRACE_REFCOUNT + fprintf(stderr, "[] REF: Alloc'd %p (ref=%d) OK=%d\n", data, REFCOUNT(data), + REFCOUNT_CHECK(data)); +#endif + return data; +} + +extern void *sci_refcount_incref(void *data) { + if (!REFCOUNT_CHECK(data)) { + BREAKPOINT(); + } else + REFCOUNT(data)++; + +#ifdef TRACE_REFCOUNT + fprintf(stderr, "[] REF: Inc'ing %p (now ref=%d)\n", data, REFCOUNT(data)); +#endif + return data; +} + +extern void sci_refcount_decref(void *data) { +#ifdef TRACE_REFCOUNT + fprintf(stderr, "[] REF: Dec'ing %p (prev ref=%d) OK=%d\n", data, REFCOUNT(data), + REFCOUNT_CHECK(data)); +#endif + if (!REFCOUNT_CHECK(data)) { + BREAKPOINT(); + } else if (--REFCOUNT(data) == 0) { + uint32 *fdata = (uint32 *)data; + + fdata[-1] = REFCOUNT_MAGIC_DEAD_1; + fdata[-3] = REFCOUNT_MAGIC_DEAD_2; + +#ifdef TRACE_REFCOUNT + fprintf(stderr, "[] REF: Freeing (%p)...\n", fdata - 3); +#endif + free(fdata - 3); +#ifdef TRACE_REFCOUNT + fprintf(stderr, "[] REF: Done.\n"); +#endif + } +} + +extern void *sci_refcount_memdup(void *data, size_t len) { + void *dest = sci_refcount_alloc(len); + memcpy(dest, data, len); + return dest; +} + +} // End of namespace Sci diff --git a/engines/sci/sci_memory.h b/engines/sci/sci_memory.h new file mode 100644 index 0000000000..9f47e9aa15 --- /dev/null +++ b/engines/sci/sci_memory.h @@ -0,0 +1,141 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * $URL$ + * $Id$ + * + */ + + +/** This header file defines a portable library for allocating memory safely + ** throughout FreeSCI. + ** Implementations of basic functions found here are in this file and + ** $(SRCDIR)/src/scicore/sci_memory.c + * + ************** + * + * Sets behaviour if memory allocation call fails. + * UNCHECKED_MALLOCS: use C library routine without checks + * (nothing defined): check mallocs and exit immediately on fail (recommended) + * + ** -- Alex Angas + ** + **/ + + +#ifndef _SCI_MEMORY_H +#define _SCI_MEMORY_H + +#include "common/scummsys.h" + +namespace Sci { + +/********** memory allocation routines **********/ + +extern void *sci_malloc(size_t size); +/* Allocates the specified amount of memory. +** Parameters: (size_t) size: Number of bytes to allocate +** Returns : (void *) A pointer to the allocated memory chunk +** To free this string, use the free() command. +** If the call fails, behaviour is dependent on the definition of SCI_ALLOC. +*/ + +extern void *sci_calloc(size_t num, size_t size); +/* Allocates num * size bytes of zeroed-out memory. +** Parameters: (size_t) num: Number of elements to allocate +** (size_t) size: Amount of memory per element to allocate +** Returns : (void *) A pointer to the allocated memory chunk +** To free this string, use the free() command. +** See _SCI_MALLOC() for more information if call fails. +*/ + +extern void *sci_realloc(void *ptr, size_t size); +/* Increases the size of an allocated memory chunk. +** Parameters: (void *) ptr: The original pointer +** (size_t) size: New size of the memory chunk +** Returns : (void *) A possibly new pointer, containing 'size' +** bytes of memory and everything contained in the original 'ptr' +** (possibly minus some trailing data if the new memory area is +** smaller than the old one). +** To free this string, use the free() command. +** See _SCI_MALLOC() for more information if call fails. +*/ + + +extern char *sci_strdup(const char *src); +/* Duplicates a string. +** Parameters: (const char *) src: The original pointer +** Returns : (char *) a pointer to the storage location for the copied +** string. +** To free this string, use the free() command. +** See _SCI_MALLOC() for more information if call fails. +*/ + + +extern char *sci_strndup(const char *src, size_t length); +/* Copies a string into a newly allocated memory part, up to a certain length. +** Parameters: (char *) src: The source string +** (int) length: The maximum length of the string (not counting +** a trailing \0). +** Returns : (char *) The resulting copy, allocated with sci_malloc(). +** To free this string, use the free() command. +** See _SCI_MALLOC() for more information if call fails. +*/ + +/****************************************/ +/* Refcounting garbage collected memory */ +/****************************************/ + +/* Refcounting memory calls are a little slower than the others, +** and using it improperly may cause memory leaks. It conserves +** memory, though. */ + +extern void *sci_refcount_alloc(size_t length); +/* Allocates "garbage" memory +** Parameters: (size_t) length: Number of bytes to allocate +** Returns : (void *) The allocated memory +** Memory allocated in this fashion will be marked as holding one reference. +** It cannot be freed with 'free()', only by using sci_refcount_decref(). +*/ + +extern void *sci_refcount_incref(void *data); +/* Adds another reference to refcounted memory +** Parameters: (void *) data: The data to add a reference to +** Returns : (void *) data +*/ + +extern void sci_refcount_decref(void *data); +/* Decrements the reference count for refcounted memory +** Parameters: (void *) data: The data to add a reference to +** Returns : (void *) data +** If the refcount reaches zero, the memory will be deallocated +*/ + +extern void *sci_refcount_memdup(void *data, size_t len); +/* Duplicates non-refcounted memory into a refcounted block +** Parameters: (void *) data: The memory to copy from +** (size_t) len: The number of bytes to copy/allocate +** Returns : (void *) Newly allocated refcounted memory +** The number of references accounted for will be one. +*/ + +} // End of namespace Sci + +#endif /* _SCI_MEMORY_H */ diff --git a/engines/sci/scicore/decompress0.cpp b/engines/sci/scicore/decompress0.cpp index 3863bfa508..2f6bcd117f 100644 --- a/engines/sci/scicore/decompress0.cpp +++ b/engines/sci/scicore/decompress0.cpp @@ -30,7 +30,7 @@ #include "common/stream.h" #include "common/endian.h" -#include "sci/include/sci_memory.h" +#include "sci/sci_memory.h" #include "sci/include/sciresource.h" namespace Sci { diff --git a/engines/sci/scicore/decompress01.cpp b/engines/sci/scicore/decompress01.cpp index 2ae70aaddd..f318d7b197 100644 --- a/engines/sci/scicore/decompress01.cpp +++ b/engines/sci/scicore/decompress01.cpp @@ -28,7 +28,7 @@ #include "common/stream.h" #include "common/endian.h" -#include "sci/include/sci_memory.h" +#include "sci/sci_memory.h" #include "sci/include/sciresource.h" namespace Sci { diff --git a/engines/sci/scicore/decompress1.cpp b/engines/sci/scicore/decompress1.cpp index ccf8d1704f..7b9924f9ec 100644 --- a/engines/sci/scicore/decompress1.cpp +++ b/engines/sci/scicore/decompress1.cpp @@ -28,7 +28,7 @@ #include "common/stream.h" #include "common/util.h" -#include "sci/include/sci_memory.h" +#include "sci/sci_memory.h" #include "sci/include/sciresource.h" namespace Sci { diff --git a/engines/sci/scicore/decompress11.cpp b/engines/sci/scicore/decompress11.cpp index 4f7a74b20e..13364be262 100644 --- a/engines/sci/scicore/decompress11.cpp +++ b/engines/sci/scicore/decompress11.cpp @@ -27,7 +27,7 @@ #include "common/stream.h" -#include "sci/include/sci_memory.h" +#include "sci/sci_memory.h" #include "sci/include/sciresource.h" namespace Sci { diff --git a/engines/sci/scicore/resource.cpp b/engines/sci/scicore/resource.cpp index b1523f66b6..dc58db5a5e 100644 --- a/engines/sci/scicore/resource.cpp +++ b/engines/sci/scicore/resource.cpp @@ -30,7 +30,7 @@ #include "common/util.h" #include "sci/tools.h" -#include "sci/include/sci_memory.h" +#include "sci/sci_memory.h" #include "sci/include/sciresource.h" #include "sci/include/vocabulary.h" diff --git a/engines/sci/scicore/resource_map.cpp b/engines/sci/scicore/resource_map.cpp index 6406c60c98..e06fe11578 100644 --- a/engines/sci/scicore/resource_map.cpp +++ b/engines/sci/scicore/resource_map.cpp @@ -23,7 +23,7 @@ * */ -#include "sci/include/sci_memory.h" +#include "sci/sci_memory.h" #include "sci/include/sciresource.h" #include "sci/tools.h" diff --git a/engines/sci/scicore/resource_patch.cpp b/engines/sci/scicore/resource_patch.cpp index 8545987d8f..c062a2f782 100644 --- a/engines/sci/scicore/resource_patch.cpp +++ b/engines/sci/scicore/resource_patch.cpp @@ -27,7 +27,7 @@ #include "common/file.h" #include "sci/include/sciresource.h" -#include "sci/include/sci_memory.h" +#include "sci/sci_memory.h" namespace Sci { diff --git a/engines/sci/scicore/sci_memory.cpp b/engines/sci/scicore/sci_memory.cpp deleted file mode 100644 index 446c1bb1a9..0000000000 --- a/engines/sci/scicore/sci_memory.cpp +++ /dev/null @@ -1,160 +0,0 @@ -/* ScummVM - Graphic Adventure Engine - * - * ScummVM is the legal property of its developers, whose names - * are too numerous to list here. Please refer to the COPYRIGHT - * file distributed with this source distribution. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - * $URL$ - * $Id$ - * - */ - -#include "common/util.h" -#include "sci/tools.h" -#include "sci/include/sci_memory.h" - -namespace Sci { - -void * sci_malloc(size_t size) { - void *res; - - ALLOC_MEM((res = malloc(size)), size, __FILE__, __LINE__, "") - return res; -} - - -void * sci_calloc(size_t num, size_t size) { - void *res; - - ALLOC_MEM((res = calloc(num, size)), num * size, __FILE__, __LINE__, "") - return res; -} - -void *sci_realloc(void *ptr, size_t size) { - void *res; - - ALLOC_MEM((res = realloc(ptr, size)), size, __FILE__, __LINE__, "") - return res; -} - -char *sci_strdup(const char *src) { - void *res; - - if (!src) { - fprintf(stderr, "_SCI_STRDUP() [%s (%s) : %u]\n", - __FILE__, "", __LINE__); - fprintf(stderr, " attempt to strdup NULL pointer\n"); - BREAKPOINT(); - } - ALLOC_MEM((res = strdup(src)), strlen(src), __FILE__, __LINE__, "") - - return (char*)res; -} - -char *sci_strndup(const char *src, size_t length) { - void *res; - char *strres; - size_t rlen = (int)MIN(strlen(src), length) + 1; - - if (!src) { - fprintf(stderr, "_SCI_STRNDUP() [%s (%s) : %u]\n", - __FILE__, "", __LINE__); - fprintf(stderr, " attempt to strndup NULL pointer\n"); - BREAKPOINT(); - } - ALLOC_MEM((res = malloc(rlen)), rlen, __FILE__, __LINE__, "") - - strres = (char *)res; - strncpy(strres, src, rlen); - strres[rlen - 1] = 0; - - return strres; -} - -//-------- Refcounting ---------- - -#define REFCOUNT_OVERHEAD (sizeof(uint32) * 3) -#define REFCOUNT_MAGIC_LIVE_1 0xebdc1741 -#define REFCOUNT_MAGIC_LIVE_2 0x17015ac9 -#define REFCOUNT_MAGIC_DEAD_1 0x11dead11 -#define REFCOUNT_MAGIC_DEAD_2 0x22dead22 - -#define REFCOUNT_CHECK(p) ((((uint32 *)(p))[-3] == REFCOUNT_MAGIC_LIVE_2) && (((uint32 *)(p))[-1] == REFCOUNT_MAGIC_LIVE_1)) - -#define REFCOUNT(p) (((uint32 *)p)[-2]) - -#undef TRACE_REFCOUNT - -extern void *sci_refcount_alloc(size_t length) { - uint32 *data = (uint32 *)sci_malloc(REFCOUNT_OVERHEAD + length); -#ifdef TRACE_REFCOUNT - fprintf(stderr, "[] REF: Real-alloc at %p\n", data); -#endif - data += 3; - - data[-1] = REFCOUNT_MAGIC_LIVE_1; - data[-3] = REFCOUNT_MAGIC_LIVE_2; - REFCOUNT(data) = 1; -#ifdef TRACE_REFCOUNT - fprintf(stderr, "[] REF: Alloc'd %p (ref=%d) OK=%d\n", data, REFCOUNT(data), - REFCOUNT_CHECK(data)); -#endif - return data; -} - -extern void *sci_refcount_incref(void *data) { - if (!REFCOUNT_CHECK(data)) { - BREAKPOINT(); - } else - REFCOUNT(data)++; - -#ifdef TRACE_REFCOUNT - fprintf(stderr, "[] REF: Inc'ing %p (now ref=%d)\n", data, REFCOUNT(data)); -#endif - return data; -} - -extern void sci_refcount_decref(void *data) { -#ifdef TRACE_REFCOUNT - fprintf(stderr, "[] REF: Dec'ing %p (prev ref=%d) OK=%d\n", data, REFCOUNT(data), - REFCOUNT_CHECK(data)); -#endif - if (!REFCOUNT_CHECK(data)) { - BREAKPOINT(); - } else if (--REFCOUNT(data) == 0) { - uint32 *fdata = (uint32 *)data; - - fdata[-1] = REFCOUNT_MAGIC_DEAD_1; - fdata[-3] = REFCOUNT_MAGIC_DEAD_2; - -#ifdef TRACE_REFCOUNT - fprintf(stderr, "[] REF: Freeing (%p)...\n", fdata - 3); -#endif - free(fdata - 3); -#ifdef TRACE_REFCOUNT - fprintf(stderr, "[] REF: Done.\n"); -#endif - } -} - -extern void *sci_refcount_memdup(void *data, size_t len) { - void *dest = sci_refcount_alloc(len); - memcpy(dest, data, len); - return dest; -} - -} // End of namespace Sci diff --git a/engines/sci/scicore/sciconsole.cpp b/engines/sci/scicore/sciconsole.cpp index 8db1b930fb..7b2aa3ce23 100644 --- a/engines/sci/scicore/sciconsole.cpp +++ b/engines/sci/scicore/sciconsole.cpp @@ -26,7 +26,7 @@ /* First part of the console implmentation: VM independent stuff */ /* Remember, it doesn't have to be fast. */ -#include "sci/include/sci_memory.h" +#include "sci/sci_memory.h" #include "sci/include/engine.h" namespace Sci { diff --git a/engines/sci/sfx/iterator.cpp b/engines/sci/sfx/iterator.cpp index 90964ee9c6..092bed9288 100644 --- a/engines/sci/sfx/iterator.cpp +++ b/engines/sci/sfx/iterator.cpp @@ -30,7 +30,7 @@ #include "sci/sfx/sfx_iterator_internal.h" #include "sci/sfx/sfx_player.h" #include "sci/tools.h" -#include "sci/include/sci_memory.h" +#include "sci/sci_memory.h" namespace Sci { diff --git a/engines/sci/sfx/mixer/soft.cpp b/engines/sci/sfx/mixer/soft.cpp index 209fcf0633..ed351be9bb 100644 --- a/engines/sci/sfx/mixer/soft.cpp +++ b/engines/sci/sfx/mixer/soft.cpp @@ -27,7 +27,7 @@ #include "sci/tools.h" #include "sci/sfx/mixer.h" -#include "sci/include/sci_memory.h" +#include "sci/sci_memory.h" namespace Sci { diff --git a/engines/sci/sfx/pcm-iterator.cpp b/engines/sci/sfx/pcm-iterator.cpp index 93514ffd51..193f00045f 100644 --- a/engines/sci/sfx/pcm-iterator.cpp +++ b/engines/sci/sfx/pcm-iterator.cpp @@ -25,7 +25,7 @@ #include "sci/sfx/sfx_iterator.h" #include "sci/tools.h" /* for BREAKPOINT */ -#include "sci/include/sci_memory.h" +#include "sci/sci_memory.h" namespace Sci { diff --git a/engines/sci/sfx/seq/instrument-map.cpp b/engines/sci/sfx/seq/instrument-map.cpp index 4fd3094745..b24a8c2e0f 100644 --- a/engines/sci/sfx/seq/instrument-map.cpp +++ b/engines/sci/sfx/seq/instrument-map.cpp @@ -25,7 +25,7 @@ #include "common/scummsys.h" #include "sci/include/sci_midi.h" -#include "sci/include/sci_memory.h" +#include "sci/sci_memory.h" #include "sci/sfx/seq/instrument-map.h" #include "sci/sfx/sfx_engine.h" diff --git a/engines/sci/sfx/softseq/amiga.cpp b/engines/sci/sfx/softseq/amiga.cpp index 938a778e6d..acd9fc444f 100644 --- a/engines/sci/sfx/softseq/amiga.cpp +++ b/engines/sci/sfx/softseq/amiga.cpp @@ -24,7 +24,7 @@ */ #include "sci/tools.h" -#include "sci/include/sci_memory.h" +#include "sci/sci_memory.h" #include "sci/sfx/softseq.h" #include "common/file.h" diff --git a/engines/sci/sfx/songlib.cpp b/engines/sci/sfx/songlib.cpp index d98e387a96..5a53affe18 100644 --- a/engines/sci/sfx/songlib.cpp +++ b/engines/sci/sfx/songlib.cpp @@ -25,7 +25,7 @@ #include #include "sci/sfx/sfx_engine.h" -#include "sci/include/sci_memory.h" +#include "sci/sci_memory.h" namespace Sci { -- cgit v1.2.3