diff options
author | Max Horn | 2009-03-10 19:08:07 +0000 |
---|---|---|
committer | Max Horn | 2009-03-10 19:08:07 +0000 |
commit | 4da6fbbb76987ea6f2c7cef70da6a9e497fddf2d (patch) | |
tree | 0da84a11ed4d1ee363700a841c561f4816cb299f /engines/sci | |
parent | 39359479ea4e3a2bf7a335a51c9e28c7515669ff (diff) | |
download | scummvm-rg350-4da6fbbb76987ea6f2c7cef70da6a9e497fddf2d.tar.gz scummvm-rg350-4da6fbbb76987ea6f2c7cef70da6a9e497fddf2d.tar.bz2 scummvm-rg350-4da6fbbb76987ea6f2c7cef70da6a9e497fddf2d.zip |
SCI: Memory 'ref counting' code is only used by song iterators -> move it there, make it private (to be replaced one day by something else, e.g. Common::SharedPtr)
svn-id: r39305
Diffstat (limited to 'engines/sci')
-rw-r--r-- | engines/sci/sci_memory.cpp | 72 | ||||
-rw-r--r-- | engines/sci/sci_memory.h | 52 | ||||
-rw-r--r-- | engines/sci/sfx/iterator.cpp | 97 |
3 files changed, 98 insertions, 123 deletions
diff --git a/engines/sci/sci_memory.cpp b/engines/sci/sci_memory.cpp index 3fb320dad0..d8e49a5fe9 100644 --- a/engines/sci/sci_memory.cpp +++ b/engines/sci/sci_memory.cpp @@ -115,76 +115,4 @@ char *sci_strndup(const char *src, size_t length) { 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 index 49b75e1685..d3bc4a16d9 100644 --- a/engines/sci/sci_memory.h +++ b/engines/sci/sci_memory.h @@ -24,20 +24,7 @@ */ -/** 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 - ** - **/ +// This header file defines a portable library for allocating memory safely. #ifndef SCI_SCI_MEMORY_H @@ -99,43 +86,6 @@ extern char *sci_strndup(const char *src, size_t length); ** 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_SCI_MEMORY_H diff --git a/engines/sci/sfx/iterator.cpp b/engines/sci/sfx/iterator.cpp index ab868b05d8..c6fefabbf0 100644 --- a/engines/sci/sfx/iterator.cpp +++ b/engines/sci/sfx/iterator.cpp @@ -36,6 +36,103 @@ namespace Sci { +/****************************************/ +/* Refcounting garbage collected memory */ +/****************************************/ + +#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 + +/* 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(). +*/ +static 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; +} + +/* Adds another reference to refcounted memory +** Parameters: (void *) data: The data to add a reference to +** Returns : (void *) data +*/ +static 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; +} + +/* 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 +*/ +static 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 + } +} + +/* 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. +*/ +static void *sci_refcount_memdup(void *data, size_t len) { + void *dest = sci_refcount_alloc(len); + memcpy(dest, data, len); + return dest; +} + + + static const int MIDI_cmdlen[16] = {0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 1, 1, 2, 0 }; |