From abae59767c98557a1abd17c1431aafcf6a379963 Mon Sep 17 00:00:00 2001 From: Aaron Culliney Date: Wed, 30 Dec 2015 22:16:57 -0800 Subject: [PATCH] Enable simple heap memory fencing/scribbling in debug builds --- Android/jni/jnicrash.c | 2 +- Android/jni/jnihooks.c | 4 +- src/audio/AY8910.c | 22 +++--- src/audio/mockingboard.c | 4 +- src/audio/playqueue.c | 8 +-- src/audio/soundcore-openal.c | 12 ++-- src/audio/soundcore-opensles.c | 14 ++-- src/audio/speaker.c | 4 +- src/common.h | 15 +--- src/disk.c | 5 +- src/json_parse.c | 10 +-- src/memmngt.h | 124 +++++++++++++++++++++++++++++++++ src/meta/debug.c | 4 +- src/meta/debug.l | 4 +- src/meta/debugger.c | 3 +- src/test/testcommon.c | 4 +- src/test/testdisk.c | 38 +++++----- src/test/testtrace.c | 14 ++-- src/video/glalert.c | 10 ++- src/video/glhudmodel.c | 2 +- src/video/glnode.c | 2 +- src/video/gltouchjoy.c | 8 +-- src/video/gltouchkbd.c | 10 +-- src/video/gltouchmenu.c | 8 +-- src/video/glvideo.c | 36 +++++----- src/video_util/imageUtil.m | 8 +-- src/video_util/modelUtil.c | 36 +++++----- src/video_util/sourceUtil.c | 9 +-- 28 files changed, 265 insertions(+), 155 deletions(-) create mode 100644 src/memmngt.h diff --git a/Android/jni/jnicrash.c b/Android/jni/jnicrash.c index 582804f3..92c2d95a 100644 --- a/Android/jni/jnicrash.c +++ b/Android/jni/jnicrash.c @@ -131,7 +131,7 @@ void Java_org_deadc0de_apple2ix_Apple2CrashHandler_nativeProcessCrash(JNIEnv *en } if (symbolsPath) { - FREE(symbolsPath); + ASPRINTF_FREE(symbolsPath); } (*env)->ReleaseStringUTFChars(env, jCrashPath, crashPath); diff --git a/Android/jni/jnihooks.c b/Android/jni/jnihooks.c index aa661791..225c8777 100644 --- a/Android/jni/jnihooks.c +++ b/Android/jni/jnihooks.c @@ -342,7 +342,7 @@ void Java_org_deadc0de_apple2ix_Apple2Activity_nativeChooseDisk(JNIEnv *env, jcl } else { video_backend->animation_showDiskChosen(drive); } - FREE(gzPath); + ASPRINTF_FREE(gzPath); } else { video_backend->animation_showDiskChosen(drive); } @@ -390,7 +390,7 @@ jstring Java_org_deadc0de_apple2ix_Apple2Activity_nativeLoadState(JNIEnv *env, j asprintf(&str, "{ disk1 = \"%s\"; readOnly1 = %s; disk2 = \"%s\"; readOnly2 = %s }", (disk1 ?: ""), readOnly1 ? "true" : "false", (disk2 ?: ""), readOnly2 ? "true" : "false"); if (str) { jstr = (*env)->NewStringUTF(env, str); - FREE(str); + ASPRINTF_FREE(str); } return jstr; diff --git a/src/audio/AY8910.c b/src/audio/AY8910.c index f2939a12..a5df3be8 100644 --- a/src/audio/AY8910.c +++ b/src/audio/AY8910.c @@ -243,14 +243,14 @@ static void sound_init( CAY8910 *_this, const char *device, unsigned long nSampl sound_generator_framesiz = sound_generator_freq / (int)hz; #if 0 - if( ( sound_buf = (libspectrum_signed_word*) malloc( sizeof( libspectrum_signed_word ) * + if( ( sound_buf = (libspectrum_signed_word*) MALLOC( sizeof( libspectrum_signed_word ) * sound_generator_framesiz * sound_channels ) ) == NULL || ( tape_buf = - malloc( sizeof( libspectrum_signed_word ) * + MALLOC( sizeof( libspectrum_signed_word ) * sound_generator_framesiz ) ) == NULL ) { if( sound_buf ) { - free( sound_buf ); + FREE( sound_buf ); sound_buf = NULL; } sound_end(_this); @@ -263,14 +263,14 @@ static void sound_init( CAY8910 *_this, const char *device, unsigned long nSampl #ifdef HAVE_SAMPLERATE if( settings_current.sound_hifi ) { - if( ( convert_input_buffer = malloc( sizeof( float ) * + if( ( convert_input_buffer = MALLOC( sizeof( float ) * sound_generator_framesiz * sound_channels ) ) == NULL || ( convert_output_buffer = - malloc( sizeof( float ) * sound_framesiz * sound_channels ) ) == + MALLOC( sizeof( float ) * sound_framesiz * sound_channels ) ) == NULL ) { if( convert_input_buffer ) { - free( convert_input_buffer ); + FREE( convert_input_buffer ); convert_input_buffer = NULL; } sound_end(_this); @@ -373,17 +373,17 @@ static void sound_end( CAY8910 *_this ) #if 0 if( sound_enabled ) { if( sound_buf ) { - free( sound_buf ); + FREE( sound_buf ); sound_buf = NULL; - free( tape_buf ); + FREE( tape_buf ); tape_buf = NULL; } if( convert_input_buffer ) { - free( convert_input_buffer ); + FREE( convert_input_buffer ); convert_input_buffer = NULL; } if( convert_output_buffer ) { - free( convert_output_buffer ); + FREE( convert_output_buffer ); convert_output_buffer = NULL; } #ifdef HAVE_SAMPLERATE @@ -397,7 +397,7 @@ static void sound_end( CAY8910 *_this ) #if 0 if( sound_buf ) { - free( sound_buf ); + FREE( sound_buf ); sound_buf = NULL; } #endif diff --git a/src/audio/mockingboard.c b/src/audio/mockingboard.c index 882ca35e..02682e43 100644 --- a/src/audio/mockingboard.c +++ b/src/audio/mockingboard.c @@ -1413,7 +1413,7 @@ static bool MB_DSInit() SAMPLE_RATE = audio_backend->systemSettings.sampleRateHz; MB_BUF_SIZE = audio_backend->systemSettings.stereoBufferSizeSamples * audio_backend->systemSettings.bytesPerSample * MB_CHANNELS; g_dwDSBufferSize = MB_BUF_SIZE; - g_nMixBuffer = malloc(MB_BUF_SIZE / audio_backend->systemSettings.bytesPerSample); + g_nMixBuffer = MALLOC(MB_BUF_SIZE / audio_backend->systemSettings.bytesPerSample); #ifndef APPLE2IX bool bRes = DSZeroVoiceBuffer(&MockingboardVoice, (char*)"MB", g_dwDSBufferSize); @@ -1722,7 +1722,7 @@ void MB_Initialize() for(i=0; inext; - free(p); + FREE(p); } } @@ -226,13 +226,13 @@ PlayQueue_s *playq_createPlayQueue(const long *nodeIdPtr, unsigned long numBuffe assert(numBuffers <= MAX_PLAYQ_BUFFERS); do { - playq = calloc(1, sizeof(PlayQueue_s)); + playq = CALLOC(1, sizeof(PlayQueue_s)); if (!playq) { ERRLOG("no memory"); break; } - PQList_s *list = calloc(1, sizeof(PQList_s)); + PQList_s *list = CALLOC(1, sizeof(PQList_s)); playq->_internal = list; if (!list) { ERRLOG("no memory"); @@ -241,7 +241,7 @@ PlayQueue_s *playq_createPlayQueue(const long *nodeIdPtr, unsigned long numBuffe bool allocSuccess = true; for (unsigned long i=0; inode.nodeId = nodeIdPtr[i]; if (!listNode) { diff --git a/src/audio/soundcore-openal.c b/src/audio/soundcore-openal.c index b795d66d..71ce6fa0 100644 --- a/src/audio/soundcore-openal.c +++ b/src/audio/soundcore-openal.c @@ -357,7 +357,7 @@ static ALVoice *_openal_createVoice(unsigned long numChannels) { ALVoice *voice = NULL; do { - voice = calloc(1, sizeof(*voice)); + voice = CALLOC(1, sizeof(*voice)); if (voice == NULL) { ERRLOG("OOPS, Out of memory!"); break; @@ -424,7 +424,7 @@ static ALVoice *_openal_createVoice(unsigned long numChannels) { unsigned long maxSamples = openal_audio_backend.systemSettings.monoBufferSizeSamples * numChannels; voice->buffersize = maxSamples * openal_audio_backend.systemSettings.bytesPerSample; - voice->data = calloc(1, voice->buffersize); + voice->data = CALLOC(1, voice->buffersize); if (voice->data == NULL) { ERRLOG("OOPS, Error allocating %d bytes", voice->buffersize); break; @@ -488,7 +488,7 @@ static long openal_createSoundBuffer(const AudioContext_s *audio_context, INOUT } ALVoices immutableNode = { /*const*/.source = voice->source }; - ALVoices *vnode = calloc(1, sizeof(ALVoices)); + ALVoices *vnode = CALLOC(1, sizeof(ALVoices)); if (!vnode) { ERRLOG("OOPS, Not enough memory"); break; @@ -497,7 +497,7 @@ static long openal_createSoundBuffer(const AudioContext_s *audio_context, INOUT vnode->voice = voice; HASH_ADD_INT(voices, source, vnode); - if ((*soundbuf_struct = calloc(1, sizeof(AudioBuffer_s))) == NULL) { + if ((*soundbuf_struct = CALLOC(1, sizeof(AudioBuffer_s))) == NULL) { ERRLOG("OOPS, Not enough memory"); break; } @@ -566,7 +566,7 @@ static long openal_systemSetup(INOUT AudioContext_s **audio_context) { LOG("WARNING - AL_SOFT_buffer_samples extension not supported... Proceeding anyway..."); } - if ((*audio_context = calloc(1, sizeof(AudioContext_s))) == NULL) { + if ((*audio_context = CALLOC(1, sizeof(AudioContext_s))) == NULL) { ERRLOG("OOPS, Not enough memory"); break; } @@ -580,7 +580,7 @@ static long openal_systemSetup(INOUT AudioContext_s **audio_context) { if (result) { if (ctx) { - AudioContext_s *ctxPtr = calloc(1, sizeof(AudioContext_s)); + AudioContext_s *ctxPtr = CALLOC(1, sizeof(AudioContext_s)); ctxPtr->_internal = ctx; openal_systemShutdown(&ctxPtr); } diff --git a/src/audio/soundcore-opensles.c b/src/audio/soundcore-opensles.c index b4af766e..e96901f7 100644 --- a/src/audio/soundcore-opensles.c +++ b/src/audio/soundcore-opensles.c @@ -426,14 +426,14 @@ static long opensl_createSoundBuffer(const AudioContext_s *audio_context, INOUT voice->ringBuffer = prevBuffer; } else { LOG("Creating new SLVoice ..."); - voice = calloc(1, sizeof(*voice)); + voice = CALLOC(1, sizeof(*voice)); if (voice == NULL) { ERRLOG("OOPS, Out of memory!"); break; } voice->bufferSize = bufferSize; // Allocate enough space for the temp buffer (including a maximum allowed overflow) - voice->ringBuffer = calloc(1, voice->bufferSize + ctx->submitSize/*max overflow*/); + voice->ringBuffer = CALLOC(1, voice->bufferSize + ctx->submitSize/*max overflow*/); if (voice->ringBuffer == NULL) { ERRLOG("OOPS, Error allocating %lu bytes", (unsigned long)voice->bufferSize+ctx->submitSize); break; @@ -444,7 +444,7 @@ static long opensl_createSoundBuffer(const AudioContext_s *audio_context, INOUT voice->ctx = ctx; - if ((*soundbuf_struct = calloc(1, sizeof(AudioBuffer_s))) == NULL) { + if ((*soundbuf_struct = CALLOC(1, sizeof(AudioBuffer_s))) == NULL) { ERRLOG("OOPS, Not enough memory"); break; } @@ -556,14 +556,14 @@ static long opensles_systemSetup(INOUT AudioContext_s **audio_context) { // // Engine creation ... // - ctx = calloc(1, sizeof(EngineContext_s)); + ctx = CALLOC(1, sizeof(EngineContext_s)); if (!ctx) { result = -1; break; } ctx->submitSize = android_stereoBufferSubmitSizeSamples * opensles_audio_backend.systemSettings.bytesPerSample * NUM_CHANNELS; - ctx->mixBuf = calloc(1, ctx->submitSize); + ctx->mixBuf = CALLOC(1, ctx->submitSize); if (ctx->mixBuf == NULL) { ERRLOG("OOPS, Error allocating %lu bytes", (unsigned long)ctx->submitSize); break; @@ -608,7 +608,7 @@ static long opensles_systemSetup(INOUT AudioContext_s **audio_context) { } // create soundcore API wrapper - if ((*audio_context = calloc(1, sizeof(AudioContext_s))) == NULL) { + if ((*audio_context = CALLOC(1, sizeof(AudioContext_s))) == NULL) { result = -1; ERRLOG("OOPS, Not enough memory"); break; @@ -707,7 +707,7 @@ static long opensles_systemSetup(INOUT AudioContext_s **audio_context) { if (result != SL_RESULT_SUCCESS) { if (ctx) { - AudioContext_s *ctxPtr = calloc(1, sizeof(AudioContext_s)); + AudioContext_s *ctxPtr = CALLOC(1, sizeof(AudioContext_s)); ctxPtr->_internal = ctx; opensles_systemShutdown(&ctxPtr); } diff --git a/src/audio/speaker.c b/src/audio/speaker.c index 43bce41a..f181ff9c 100644 --- a/src/audio/speaker.c +++ b/src/audio/speaker.c @@ -368,14 +368,14 @@ void speaker_init(void) { remainder_buffer_size_max = ((CLK_6502_INT*(unsigned long)CPU_SCALE_FASTEST)/audio_backend->systemSettings.sampleRateHz)+1; - samples_buffer = calloc(1, channelsSampleRateHz * sizeof(int16_t)); + samples_buffer = CALLOC(1, channelsSampleRateHz * sizeof(int16_t)); if (!samples_buffer) { err = -1; break; } samples_buffer_idx = bufferSizeIdealMax; - remainder_buffer = malloc(remainder_buffer_size_max * sizeof(int16_t)); + remainder_buffer = MALLOC(remainder_buffer_size_max * sizeof(int16_t)); if (!remainder_buffer) { err = -1; break; diff --git a/src/common.h b/src/common.h index ee75c5c0..599dfd63 100644 --- a/src/common.h +++ b/src/common.h @@ -319,19 +319,8 @@ static const char *log_end = "\n"; *ptr = '\0'; \ } while (0); -#define FREE(x) \ - do { \ - free((x)); \ - (x) = NULL; \ - } while (0) - -#ifdef __APPLE__ -#define CFRELEASE(x) \ - do { \ - CFRelease((x)); \ - (x) = NULL; \ - } while (0) -#endif +// memory management +#include "memmngt.h" // branch prediction #define LIKELY(x) __builtin_expect((x), true) diff --git a/src/disk.c b/src/disk.c index c5b5a7a2..85521439 100644 --- a/src/disk.c +++ b/src/disk.c @@ -756,7 +756,7 @@ const char *disk6_eject(int drive) { } } - FREE(disk6.disk[drive].file_name); + STRDUP_FREE(disk6.disk[drive].file_name); memset(&disk6.disk[drive], 0x0, sizeof(disk6.disk[drive])); disk6.disk[drive].fd = -1; @@ -1076,7 +1076,7 @@ bool disk6_loadState(StateHelper_s *helper) { if (namelen) { unsigned long gzlen = (_GZLEN+1); - char *namebuf = malloc(namelen+gzlen+1); + char *namebuf = MALLOC(namelen+gzlen+1); if (!helper->load(fd, namebuf, namelen)) { FREE(namebuf); break; @@ -1088,6 +1088,7 @@ bool disk6_loadState(StateHelper_s *helper) { namebuf[namelen+gzlen] = '\0'; LOG("LOAD disk[%lu] : (%u) %s", i, namelen, namebuf); if (disk6_insert(i, namebuf, disk6.disk[i].is_protected)) { + FREE(namebuf); break; } } diff --git a/src/json_parse.c b/src/json_parse.c index 8391cf64..67f3a790 100644 --- a/src/json_parse.c +++ b/src/json_parse.c @@ -38,7 +38,7 @@ int json_createFromFile(const char *filePath, INOUT JSON_s *parsedData) { } jsonLen = JSON_LENGTH*2; - jsonString = malloc(jsonLen); + jsonString = MALLOC(jsonLen); if (jsonString == NULL) { ERRLOG("WHOA : %s", strerror(errno)); break; @@ -56,7 +56,7 @@ int json_createFromFile(const char *filePath, INOUT JSON_s *parsedData) { if (jsonLen - jsonIdx < JSON_LENGTH) { //LOG("reallocating json string ..."); jsonLen <<= 1; - char *newString = realloc(jsonString, jsonLen); + char *newString = REALLOC(jsonString, jsonLen); if (!newString) { ERRLOG("WHOA2 : %s", strerror(errno)); bytesRead = -1; @@ -83,7 +83,7 @@ int json_createFromFile(const char *filePath, INOUT JSON_s *parsedData) { unsigned int numTokens = DEFAULT_NUMTOK; do { if (!jsonTokens) { - jsonTokens = calloc(numTokens, sizeof(jsmntok_t)); + jsonTokens = CALLOC(numTokens, sizeof(jsmntok_t)); if (!jsonTokens) { ERRLOG("WHOA3 : %s", strerror(errno)); break; @@ -91,12 +91,12 @@ int json_createFromFile(const char *filePath, INOUT JSON_s *parsedData) { } else { //LOG("reallocating json tokens ..."); numTokens <<= 1; - jsmntok_t *newTokens = realloc(jsonTokens, numTokens * sizeof(jsmntok_t)); - memset(newTokens, '\0', numTokens * sizeof(jsmntok_t)); + jsmntok_t *newTokens = REALLOC(jsonTokens, numTokens * sizeof(jsmntok_t)); if (!newTokens) { ERRLOG("WHOA4 : %s", strerror(errno)); break; } + memset(newTokens, '\0', numTokens * sizeof(jsmntok_t)); jsonTokens = newTokens; } jsmn_init(&parser); diff --git a/src/memmngt.h b/src/memmngt.h new file mode 100644 index 00000000..88636140 --- /dev/null +++ b/src/memmngt.h @@ -0,0 +1,124 @@ +/* + * Apple // emulator for *ix + * + * This software package is subject to the GNU General Public License + * version 3 or later (your choice) as published by the Free Software + * Foundation. + * + * Copyright 2013-2015 Aaron Culliney + * + */ + +#ifndef _MEMMNGT_H_ +#define _MEMMNGT_H_ + +// Simple memory management routines + +#define _FREE(ptr, free_func) \ + do { \ + free_func((ptr)); \ + /* WARNING : code may depend on NULLification, even in release builds */ \ + (ptr) = NULL; \ + } while (0) + +#define ASPRINTF_FREE(ptr) _FREE((ptr), free) +#define STRDUP_FREE(ptr) _FREE((ptr), free) + +#ifdef NDEBUG +# define MALLOC(size) malloc((size)) +# define CALLOC(nmemb, size) calloc((nmemb), (size)) +# define REALLOC(ptr, size) realloc((ptr), (size)) +# define FREE(ptr) _FREE((ptr), free) +#else + +// NOTE: debug builds use a simplistic inline *alloc() fence and a scribbling free() to pinpoint out-of-bounds heap +// writes. We still need to use Valgrind to pinpoint oob-reads and other issues =) + +# define MALLOC(size) _a2_malloc((size)) +# define CALLOC(nmemb, size) _a2_calloc((nmemb), (size)) +# define REALLOC(ptr, size) _a2_realloc((ptr), (size)) +# define FREE(ptr) _FREE((ptr), _a2_free) + +# define _BUF_SENTINEL 0xDEADC0DEUL +# define _BUF_FENCE_SZ (sizeof(_BUF_SENTINEL)) + +static inline void *_a2_malloc(size_t size) { + const size_t totalSize = sizeof(size_t)+_BUF_FENCE_SZ+size+_BUF_FENCE_SZ; + char *p = (char *)malloc(totalSize); + if (p) { + *((size_t *)p) = totalSize; + *((uint32_t *)(p+sizeof(size_t))) = _BUF_SENTINEL; + *((uint32_t *)(p+totalSize-_BUF_FENCE_SZ)) = _BUF_SENTINEL; + p += sizeof(size_t)+_BUF_FENCE_SZ; + } + return p; +} + +static inline void *_a2_calloc(size_t nmemb, size_t size) { + size *= nmemb; + const size_t totalSize = sizeof(size_t)+_BUF_FENCE_SZ+size+_BUF_FENCE_SZ; + char *p = (char *)calloc(totalSize, 1); + if (p) { + *((size_t *)p) = totalSize; + *((uint32_t *)(p+sizeof(size_t))) = _BUF_SENTINEL; + *((uint32_t *)(p+totalSize-_BUF_FENCE_SZ)) = _BUF_SENTINEL; + p += sizeof(size_t)+_BUF_FENCE_SZ; + } + return p; +} + +static inline void _a2_free(void *ptr) { + char *p = (char *)ptr; + if (!p) { + return; + } + p = p-_BUF_FENCE_SZ-sizeof(size_t); + const size_t totalSize = *((size_t *)p); + assert( *((uint32_t *)(p+sizeof(size_t))) == _BUF_SENTINEL && "1st memory sentinel invalid!" ); + assert( *((uint32_t *)(p+totalSize-_BUF_FENCE_SZ)) == _BUF_SENTINEL && "2nd memory sentinel invalid!" ); + memset(p, 0xAA, totalSize); + free(p); +} + +static inline void *_a2_realloc(void *ptr, size_t size) { + char *p = (char *)ptr; + if (!p) { + return _a2_malloc(size); + } + if (size == 0) { + FREE(ptr); + return NULL; + } + + // verify prior allocation is sane + p = p-_BUF_FENCE_SZ-sizeof(size_t); + const size_t totalSizeBefore = *((size_t *)p); + assert( *((uint32_t *)(p+sizeof(size_t))) == _BUF_SENTINEL && "1st memory sentinel invalid!" ); + assert( *((uint32_t *)(p+totalSizeBefore-_BUF_FENCE_SZ)) == _BUF_SENTINEL && "2nd memory sentinel invalid!" ); + + const size_t totalSizeAfter = sizeof(size_t)+_BUF_FENCE_SZ+size+_BUF_FENCE_SZ; + assert(totalSizeAfter > totalSizeBefore && "FIXME fenced realloc() to smaller sizes not implemented!"); + + p = (char *)realloc(p, totalSizeAfter); + if (p) { + *((size_t *)p) = totalSizeAfter; + assert( *((uint32_t *)(p+sizeof(size_t))) == _BUF_SENTINEL && "1st memory sentinel invalid!" ); + *((uint32_t *)(p+totalSizeAfter-_BUF_FENCE_SZ)) = _BUF_SENTINEL; + p += sizeof(size_t)+_BUF_FENCE_SZ; + } + + return p; +} + +#endif + +#ifdef __APPLE__ +#define CFRELEASE(x) \ + do { \ + CFRelease((x)); \ + (x) = NULL; \ + } while (0) +#endif + +#endif // whole file + diff --git a/src/meta/debug.c b/src/meta/debug.c index 3d8bc25a..8482a323 100644 --- a/src/meta/debug.c +++ b/src/meta/debug.c @@ -2367,7 +2367,7 @@ YY_RULE_SETUP char *buf = NULL; asprintf(&buf, "%s/%s", getenv("HOME"), "cputrace.txt"); cpu65_trace_toggle(buf); - free(buf); + ASPRINTF_FREE(buf); #else LOG("CPU tracing not enabled..."); #endif @@ -2382,7 +2382,7 @@ YY_RULE_SETUP char *buf = NULL; asprintf(&buf, "%s/%s", getenv("HOME"), "disktrace.txt"); c_toggle_disk_trace_6(buf, NULL); - free(buf); + ASPRINTF_FREE(buf); #else LOG("Disk tracing not enabled..."); #endif diff --git a/src/meta/debug.l b/src/meta/debug.l index a6e823e6..5ff34c05 100644 --- a/src/meta/debug.l +++ b/src/meta/debug.l @@ -854,7 +854,7 @@ ADDRS [0-9a-fA-F]+ char *buf = NULL; asprintf(&buf, "%s/%s", getenv("HOME"), "cputrace.txt"); cpu65_trace_toggle(buf); - free(buf); + ASPRINTF_FREE(buf); #else LOG("CPU tracing not enabled..."); #endif @@ -865,7 +865,7 @@ ADDRS [0-9a-fA-F]+ char *buf = NULL; asprintf(&buf, "%s/%s", getenv("HOME"), "disktrace.txt"); c_toggle_disk_trace_6(buf, NULL); - free(buf); + ASPRINTF_FREE(buf); #else LOG("Disk tracing not enabled..."); #endif diff --git a/src/meta/debugger.c b/src/meta/debugger.c index a7fd3640..1b70c04c 100644 --- a/src/meta/debugger.c +++ b/src/meta/debugger.c @@ -1195,8 +1195,7 @@ bool c_debugger_should_break() { if (pthread_self() != cpu_thread_id) { // OOPS ... ERRLOG("should only call this from cpu thread, bailing..."); - void *ptr = NULL; - free(ptr); + RELEASE_BREAK(); } if (at_haltpt()) { diff --git a/src/test/testcommon.c b/src/test/testcommon.c index df7dd7a6..5baa104d 100644 --- a/src/test/testcommon.c +++ b/src/test/testcommon.c @@ -140,7 +140,7 @@ int test_setup_boot_disk(const char *fileName, int readonly) { CFRELEASE(fileURL); CFIndex length = CFStringGetLength(filePath); CFIndex maxSize = CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8); - disk = (char *)malloc(maxSize); + disk = (char *)MALLOC(maxSize); if (!CFStringGetCString(filePath, disk, maxSize, kCFStringEncodingUTF8)) { FREE(disk); } @@ -184,7 +184,7 @@ int test_setup_boot_disk(const char *fileName, int readonly) { while (*path) { char *disk = *path; ++path; - FREE(disk); + ASPRINTF_FREE(disk); } return err; diff --git a/src/test/testdisk.c b/src/test/testdisk.c index b2ac7bbd..0a942d58 100644 --- a/src/test/testdisk.c +++ b/src/test/testdisk.c @@ -72,7 +72,7 @@ TEST test_boot_disk_bytes() { ASSERT(expectedSize == EXPECTED_DISK_TRACE_FILE_SIZE); fseek(fp, 0, SEEK_SET); - unsigned char *buf = malloc(EXPECTED_DISK_TRACE_FILE_SIZE); + unsigned char *buf = MALLOC(EXPECTED_DISK_TRACE_FILE_SIZE); if (fread(buf, 1, EXPECTED_DISK_TRACE_FILE_SIZE, fp) != EXPECTED_DISK_TRACE_FILE_SIZE) { ASSERT(false); } @@ -120,7 +120,7 @@ TEST test_boot_disk_bytes_nib() { ASSERT(expectedSize == EXPECTED_DISK_TRACE_NIB_FILE_SIZE); fseek(fp, 0, SEEK_SET); - unsigned char *buf = malloc(EXPECTED_DISK_TRACE_NIB_FILE_SIZE); + unsigned char *buf = MALLOC(EXPECTED_DISK_TRACE_NIB_FILE_SIZE); if (fread(buf, 1, EXPECTED_DISK_TRACE_NIB_FILE_SIZE, fp) != EXPECTED_DISK_TRACE_NIB_FILE_SIZE) { ASSERT(false); } @@ -173,7 +173,7 @@ TEST test_boot_disk_bytes_po() { ASSERT(expectedSize == EXPECTED_DISK_TRACE_PO_FILE_SIZE); fseek(fp, 0, SEEK_SET); - unsigned char *buf = malloc(EXPECTED_DISK_TRACE_PO_FILE_SIZE); + unsigned char *buf = MALLOC(EXPECTED_DISK_TRACE_PO_FILE_SIZE); if (fread(buf, 1, EXPECTED_DISK_TRACE_PO_FILE_SIZE, fp) != EXPECTED_DISK_TRACE_PO_FILE_SIZE) { ASSERT(false); } @@ -368,7 +368,7 @@ TEST test_disk_bytes_savehello_dsk() { ASSERT(expectedSize == EXPECTED_DISKWRITE_TRACE_DSK_FILE_SIZE); fseek(fp, 0, SEEK_SET); - unsigned char *buf = malloc(EXPECTED_DISKWRITE_TRACE_DSK_FILE_SIZE); + unsigned char *buf = MALLOC(EXPECTED_DISKWRITE_TRACE_DSK_FILE_SIZE); if (fread(buf, 1, EXPECTED_DISKWRITE_TRACE_DSK_FILE_SIZE, fp) != EXPECTED_DISKWRITE_TRACE_DSK_FILE_SIZE) { ASSERT(false); } @@ -404,7 +404,7 @@ TEST test_disk_bytes_savehello_dsk() { ASSERT(expectedSize == DSK_SIZE); fseek(fp, 0, SEEK_SET); - unsigned char *buf = malloc(DSK_SIZE); + unsigned char *buf = MALLOC(DSK_SIZE); if (fread(buf, 1, DSK_SIZE, fp) != DSK_SIZE) { ASSERT(false); } @@ -462,7 +462,7 @@ TEST test_disk_bytes_savehello_nib() { ASSERT(expectedSize == EXPECTED_DISKWRITE_TRACE_NIB_FILE_SIZE); fseek(fp, 0, SEEK_SET); - unsigned char *buf = malloc(EXPECTED_DISKWRITE_TRACE_NIB_FILE_SIZE); + unsigned char *buf = MALLOC(EXPECTED_DISKWRITE_TRACE_NIB_FILE_SIZE); if (fread(buf, 1, EXPECTED_DISKWRITE_TRACE_NIB_FILE_SIZE, fp) != EXPECTED_DISKWRITE_TRACE_NIB_FILE_SIZE) { ASSERT(false); } @@ -498,7 +498,7 @@ TEST test_disk_bytes_savehello_nib() { ASSERT(expectedSize == NIB_SIZE); fseek(fp, 0, SEEK_SET); - unsigned char *buf = malloc(NIB_SIZE); + unsigned char *buf = MALLOC(NIB_SIZE); if (fread(buf, 1, NIB_SIZE, fp) != NIB_SIZE) { ASSERT(false); } @@ -556,7 +556,7 @@ TEST test_disk_bytes_savehello_po() { ASSERT(expectedSize == EXPECTED_DISKWRITE_TRACE_PO_FILE_SIZE); fseek(fp, 0, SEEK_SET); - unsigned char *buf = malloc(EXPECTED_DISKWRITE_TRACE_PO_FILE_SIZE); + unsigned char *buf = MALLOC(EXPECTED_DISKWRITE_TRACE_PO_FILE_SIZE); if (fread(buf, 1, EXPECTED_DISKWRITE_TRACE_PO_FILE_SIZE, fp) != EXPECTED_DISKWRITE_TRACE_PO_FILE_SIZE) { ASSERT(false); } @@ -592,7 +592,7 @@ TEST test_disk_bytes_savehello_po() { ASSERT(expectedSize == DSK_SIZE); fseek(fp, 0, SEEK_SET); - unsigned char *buf = malloc(DSK_SIZE); + unsigned char *buf = MALLOC(DSK_SIZE); if (fread(buf, 1, DSK_SIZE, fp) != DSK_SIZE) { ASSERT(false); } @@ -669,7 +669,7 @@ TEST test_outofspace_dsk() { ASSERT(expectedSize == EXPECTED_OOS_DSK_TRACE_FILE_SIZE); fseek(fp, 0, SEEK_SET); - unsigned char *buf = malloc(EXPECTED_OOS_DSK_TRACE_FILE_SIZE); + unsigned char *buf = MALLOC(EXPECTED_OOS_DSK_TRACE_FILE_SIZE); if (fread(buf, 1, EXPECTED_OOS_DSK_TRACE_FILE_SIZE, fp) != EXPECTED_OOS_DSK_TRACE_FILE_SIZE) { ASSERT(false); } @@ -705,7 +705,7 @@ TEST test_outofspace_dsk() { ASSERT(expectedSize == DSK_SIZE); fseek(fp, 0, SEEK_SET); - unsigned char *buf = malloc(DSK_SIZE); + unsigned char *buf = MALLOC(DSK_SIZE); if (fread(buf, 1, DSK_SIZE, fp) != DSK_SIZE) { ASSERT(false); } @@ -760,7 +760,7 @@ TEST test_outofspace_nib() { ASSERT(expectedSize == NIB_SIZE); fseek(fp, 0, SEEK_SET); - unsigned char *buf = malloc(NIB_SIZE); + unsigned char *buf = MALLOC(NIB_SIZE); if (fread(buf, 1, NIB_SIZE, fp) != NIB_SIZE) { ASSERT(false); } @@ -815,7 +815,7 @@ TEST test_outofspace_po() { ASSERT(expectedSize == DSK_SIZE); fseek(fp, 0, SEEK_SET); - unsigned char *buf = malloc(DSK_SIZE); + unsigned char *buf = MALLOC(DSK_SIZE); if (fread(buf, 1, DSK_SIZE, fp) != DSK_SIZE) { ASSERT(false); } @@ -937,7 +937,7 @@ TEST test_bload_trace_dsk() { ASSERT(expectedSize == EXPECTED_BLOAD_TRACE_DSK_FILE_SIZE); fseek(fp, 0, SEEK_SET); - unsigned char *buf = malloc(EXPECTED_BLOAD_TRACE_DSK_FILE_SIZE); + unsigned char *buf = MALLOC(EXPECTED_BLOAD_TRACE_DSK_FILE_SIZE); if (fread(buf, 1, EXPECTED_BLOAD_TRACE_DSK_FILE_SIZE, fp) != EXPECTED_BLOAD_TRACE_DSK_FILE_SIZE) { ASSERT(false); } @@ -1055,7 +1055,7 @@ TEST test_bload_trace_nib() { ASSERT(expectedSize == EXPECTED_BLOAD_TRACE_NIB_FILE_SIZE); fseek(fp, 0, SEEK_SET); - unsigned char *buf = malloc(EXPECTED_BLOAD_TRACE_NIB_FILE_SIZE); + unsigned char *buf = MALLOC(EXPECTED_BLOAD_TRACE_NIB_FILE_SIZE); if (fread(buf, 1, EXPECTED_BLOAD_TRACE_NIB_FILE_SIZE, fp) != EXPECTED_BLOAD_TRACE_NIB_FILE_SIZE) { ASSERT(false); } @@ -1173,7 +1173,7 @@ TEST test_bload_trace_po() { ASSERT(expectedSize == EXPECTED_BLOAD_TRACE_PO_FILE_SIZE); fseek(fp, 0, SEEK_SET); - unsigned char *buf = malloc(EXPECTED_BLOAD_TRACE_PO_FILE_SIZE); + unsigned char *buf = MALLOC(EXPECTED_BLOAD_TRACE_PO_FILE_SIZE); if (fread(buf, 1, EXPECTED_BLOAD_TRACE_PO_FILE_SIZE, fp) != EXPECTED_BLOAD_TRACE_PO_FILE_SIZE) { ASSERT(false); } @@ -1288,7 +1288,7 @@ TEST test_data_stability_dsk() { ASSERT(expectedSize == EXPECTED_STABILITY_DSK_FILE_SIZE); fseek(fp, 0, SEEK_SET); - unsigned char *buf = malloc(EXPECTED_STABILITY_DSK_FILE_SIZE); + unsigned char *buf = MALLOC(EXPECTED_STABILITY_DSK_FILE_SIZE); if (fread(buf, 1, EXPECTED_STABILITY_DSK_FILE_SIZE, fp) != EXPECTED_STABILITY_DSK_FILE_SIZE) { ASSERT(false); } @@ -1323,7 +1323,7 @@ TEST test_data_stability_nib() { ASSERT(expectedSize == EXPECTED_STABILITY_NIB_FILE_SIZE); fseek(fp, 0, SEEK_SET); - unsigned char *buf = malloc(EXPECTED_STABILITY_NIB_FILE_SIZE); + unsigned char *buf = MALLOC(EXPECTED_STABILITY_NIB_FILE_SIZE); if (fread(buf, 1, EXPECTED_STABILITY_NIB_FILE_SIZE, fp) != EXPECTED_STABILITY_NIB_FILE_SIZE) { ASSERT(false); } @@ -1358,7 +1358,7 @@ TEST test_data_stability_po() { ASSERT(expectedSize == EXPECTED_STABILITY_PO_FILE_SIZE); fseek(fp, 0, SEEK_SET); - unsigned char *buf = malloc(EXPECTED_STABILITY_PO_FILE_SIZE); + unsigned char *buf = MALLOC(EXPECTED_STABILITY_PO_FILE_SIZE); if (fread(buf, 1, EXPECTED_STABILITY_PO_FILE_SIZE, fp) != EXPECTED_STABILITY_PO_FILE_SIZE) { ASSERT(false); } diff --git a/src/test/testtrace.c b/src/test/testtrace.c index a13d4bc9..eb8a62ff 100644 --- a/src/test/testtrace.c +++ b/src/test/testtrace.c @@ -70,7 +70,7 @@ TEST test_boot_disk_cputrace() { long expectedSize = ftell(fp); ASSERT(expectedSize == EXPECTED_CPU_TRACE_FILE_SIZE); fseek(fp, 0, SEEK_SET); - unsigned char *buf = malloc(EXPECTED_CPU_TRACE_FILE_SIZE); + unsigned char *buf = MALLOC(EXPECTED_CPU_TRACE_FILE_SIZE); if (fread(buf, 1, EXPECTED_CPU_TRACE_FILE_SIZE, fp) != EXPECTED_CPU_TRACE_FILE_SIZE) { ASSERT(false); } @@ -121,7 +121,7 @@ TEST test_cputrace_hello_dsk() { long expectedSize = ftell(fp); ASSERT(expectedSize == EXPECTED_CPUTRACE_HELLO_FILE_SIZE); fseek(fp, 0, SEEK_SET); - unsigned char *buf = malloc(EXPECTED_CPUTRACE_HELLO_FILE_SIZE); + unsigned char *buf = MALLOC(EXPECTED_CPUTRACE_HELLO_FILE_SIZE); if (fread(buf, 1, EXPECTED_CPUTRACE_HELLO_FILE_SIZE, fp) != EXPECTED_CPUTRACE_HELLO_FILE_SIZE) { ASSERT(false); } @@ -171,7 +171,7 @@ TEST test_cputrace_hello_nib() { long expectedSize = ftell(fp); ASSERT(expectedSize == EXPECTED_CPUTRACE_HELLO_NIB_FILE_SIZE); fseek(fp, 0, SEEK_SET); - unsigned char *buf = malloc(EXPECTED_CPUTRACE_HELLO_NIB_FILE_SIZE); + unsigned char *buf = MALLOC(EXPECTED_CPUTRACE_HELLO_NIB_FILE_SIZE); if (fread(buf, 1, EXPECTED_CPUTRACE_HELLO_NIB_FILE_SIZE, fp) != EXPECTED_CPUTRACE_HELLO_NIB_FILE_SIZE) { ASSERT(false); } @@ -221,7 +221,7 @@ TEST test_cputrace_hello_po() { long expectedSize = ftell(fp); ASSERT(expectedSize == EXPECTED_CPUTRACE_HELLO_PO_FILE_SIZE); fseek(fp, 0, SEEK_SET); - unsigned char *buf = malloc(EXPECTED_CPUTRACE_HELLO_PO_FILE_SIZE); + unsigned char *buf = MALLOC(EXPECTED_CPUTRACE_HELLO_PO_FILE_SIZE); if (fread(buf, 1, EXPECTED_CPUTRACE_HELLO_PO_FILE_SIZE, fp) != EXPECTED_CPUTRACE_HELLO_PO_FILE_SIZE) { ASSERT(false); } @@ -267,7 +267,7 @@ TEST test_boot_disk_vmtrace() { ASSERT(expectedSize == EXPECTED_VM_TRACE_FILE_SIZE); fseek(fp, 0, SEEK_SET); - unsigned char *buf = malloc(EXPECTED_VM_TRACE_FILE_SIZE); + unsigned char *buf = MALLOC(EXPECTED_VM_TRACE_FILE_SIZE); if (fread(buf, 1, EXPECTED_VM_TRACE_FILE_SIZE, fp) != EXPECTED_VM_TRACE_FILE_SIZE) { ASSERT(false); } @@ -315,7 +315,7 @@ TEST test_boot_disk_vmtrace_nib() { ASSERT(expectedSize == EXPECTED_VM_TRACE_NIB_FILE_SIZE); fseek(fp, 0, SEEK_SET); - unsigned char *buf = malloc(EXPECTED_VM_TRACE_NIB_FILE_SIZE); + unsigned char *buf = MALLOC(EXPECTED_VM_TRACE_NIB_FILE_SIZE); if (fread(buf, 1, EXPECTED_VM_TRACE_NIB_FILE_SIZE, fp) != EXPECTED_VM_TRACE_NIB_FILE_SIZE) { ASSERT(false); } @@ -363,7 +363,7 @@ TEST test_boot_disk_vmtrace_po() { ASSERT(expectedSize == EXPECTED_VM_TRACE_PO_FILE_SIZE); fseek(fp, 0, SEEK_SET); - unsigned char *buf = malloc(EXPECTED_VM_TRACE_PO_FILE_SIZE); + unsigned char *buf = MALLOC(EXPECTED_VM_TRACE_PO_FILE_SIZE); if (fread(buf, 1, EXPECTED_VM_TRACE_PO_FILE_SIZE, fp) != EXPECTED_VM_TRACE_PO_FILE_SIZE) { ASSERT(false); } diff --git a/src/video/glalert.c b/src/video/glalert.c index 75898dfe..d47620be 100644 --- a/src/video/glalert.c +++ b/src/video/glalert.c @@ -27,7 +27,7 @@ static GLModel *messageModel = NULL; // ---------------------------------------------------------------------------- static void *_create_alert(void) { - GLModelHUDElement *hudElement = (GLModelHUDElement *)calloc(sizeof(GLModelHUDElement), 1); + GLModelHUDElement *hudElement = (GLModelHUDElement *)CALLOC(sizeof(GLModelHUDElement), 1); if (hudElement) { hudElement->colorScheme = RED_ON_BLACK; hudElement->blackIsTransparent = false; @@ -47,9 +47,7 @@ static inline void _set_alpha(unsigned int dstIdx) { } static void _alertToModel(char *message, unsigned int messageCols, unsigned int messageRows) { - if (!message) { - return; - } + assert(message); isEnabled = false; @@ -78,7 +76,7 @@ static void _alertToModel(char *message, unsigned int messageCols, unsigned int hudElement->tpl = message; hudElement->pixWidth = fbWidth; hudElement->pixHeight = fbHeight; - hudElement->pixels = calloc(fbWidth * fbHeight, 1); + hudElement->pixels = MALLOC(fbWidth * fbHeight); if (!hudElement->pixels) { LOG("OOPS cannot create animation message intermediate framebuffer!"); break; @@ -209,7 +207,7 @@ static void _animation_showMessage(char *messageTemplate, unsigned int cols, uns const unsigned int framedStride = framedCols+1/*\0*/; const unsigned int sourceStride = cols+1/*\0*/; - char *message = calloc(framedStride*framedRows, 1); + char *message = CALLOC(framedStride*framedRows, 1); if (!message) { LOG("OOPS cannot create memory for animation message!"); return; diff --git a/src/video/glhudmodel.c b/src/video/glhudmodel.c index 011a964d..4cae0b20 100644 --- a/src/video/glhudmodel.c +++ b/src/video/glhudmodel.c @@ -13,7 +13,7 @@ #include "glvideo.h" void *glhud_createDefault(void) { - GLModelHUDElement *hudElement = (GLModelHUDElement *)calloc(sizeof(GLModelHUDElement), 1); + GLModelHUDElement *hudElement = (GLModelHUDElement *)CALLOC(sizeof(GLModelHUDElement), 1); if (hudElement) { hudElement->colorScheme = RED_ON_BLACK; } diff --git a/src/video/glnode.c b/src/video/glnode.c index 20bd426e..c44f4c50 100644 --- a/src/video/glnode.c +++ b/src/video/glnode.c @@ -32,7 +32,7 @@ static glnode_array_node_s *tail = NULL; void glnode_registerNode(glnode_render_order_t order, GLNode node) { pthread_mutex_lock(&mutex); - glnode_array_node_s *arrayNode = malloc(sizeof(glnode_array_node_s)); + glnode_array_node_s *arrayNode = MALLOC(sizeof(glnode_array_node_s)); assert(arrayNode); arrayNode->next = NULL; arrayNode->last = NULL; diff --git a/src/video/gltouchjoy.c b/src/video/gltouchjoy.c index 867780d3..b86cd379 100644 --- a/src/video/gltouchjoy.c +++ b/src/video/gltouchjoy.c @@ -118,12 +118,12 @@ static void _setup_axis_object(GLModel *parent) { }; const unsigned int size = sizeof(axisTemplate); - hudElement->tpl = calloc(size, 1); + hudElement->tpl = CALLOC(size, 1); hudElement->tplWidth = AXIS_TEMPLATE_COLS; hudElement->tplHeight = AXIS_TEMPLATE_ROWS; memcpy(hudElement->tpl, axisTemplate, size); - hudElement->pixels = calloc(AXIS_FB_WIDTH * AXIS_FB_HEIGHT, 1); + hudElement->pixels = CALLOC(AXIS_FB_WIDTH * AXIS_FB_HEIGHT, 1); hudElement->pixWidth = AXIS_FB_WIDTH; hudElement->pixHeight = AXIS_FB_HEIGHT; } @@ -166,12 +166,12 @@ static void _setup_button_object(GLModel *parent) { }; const unsigned int size = sizeof(buttonTemplate); - hudElement->tpl = calloc(size, 1); + hudElement->tpl = CALLOC(size, 1); hudElement->tplWidth = BUTTON_TEMPLATE_COLS; hudElement->tplHeight = BUTTON_TEMPLATE_ROWS; memcpy(hudElement->tpl, buttonTemplate, size); - hudElement->pixels = calloc(BUTTON_FB_WIDTH * BUTTON_FB_HEIGHT, 1); + hudElement->pixels = CALLOC(BUTTON_FB_WIDTH * BUTTON_FB_HEIGHT, 1); hudElement->pixWidth = BUTTON_FB_WIDTH; hudElement->pixHeight = BUTTON_FB_HEIGHT; } diff --git a/src/video/gltouchkbd.c b/src/video/gltouchkbd.c index cd5e5dfb..54133747 100644 --- a/src/video/gltouchkbd.c +++ b/src/video/gltouchkbd.c @@ -448,11 +448,11 @@ static void _setup_touchkbd_hud(GLModel *parent) { hudKeyboard->pixWidth = KBD_FB_WIDTH; hudKeyboard->pixHeight = KBD_FB_HEIGHT; - const unsigned int size = sizeof(kbdTemplateUCase/* assuming all the same */); - hudKeyboard->tpl = calloc(size, 1); - hudKeyboard->pixels = calloc(KBD_FB_WIDTH * KBD_FB_HEIGHT, 1); + const unsigned int size = sizeof(kbdTemplateUCase/* assuming all the same dimensions */); + hudKeyboard->tpl = MALLOC(size); + hudKeyboard->pixels = MALLOC(KBD_FB_WIDTH * KBD_FB_HEIGHT); - memcpy(hudKeyboard->tpl, kbdTemplateUCase[0], sizeof(kbdTemplateUCase/* assuming all the same size */)); + memcpy(hudKeyboard->tpl, kbdTemplateUCase[0], sizeof(kbdTemplateUCase/* assuming all the same dimensions */)); // setup normal color pixels hudKeyboard->colorScheme = RED_ON_BLACK; @@ -460,7 +460,7 @@ static void _setup_touchkbd_hud(GLModel *parent) { } static void *_create_touchkbd_hud(void) { - GLModelHUDKeyboard *hudKeyboard = (GLModelHUDKeyboard *)calloc(sizeof(GLModelHUDKeyboard), 1); + GLModelHUDKeyboard *hudKeyboard = (GLModelHUDKeyboard *)CALLOC(sizeof(GLModelHUDKeyboard), 1); if (hudKeyboard) { hudKeyboard->blackIsTransparent = true; hudKeyboard->opaquePixelHalo = true; diff --git a/src/video/gltouchmenu.c b/src/video/gltouchmenu.c index c367f15e..73654fc9 100644 --- a/src/video/gltouchmenu.c +++ b/src/video/gltouchmenu.c @@ -344,15 +344,15 @@ static void _setup_touchmenu(GLModel *parent) { } const unsigned int size = sizeof(topMenuTemplate); - hudMenu->tpl = calloc(size, 1); - hudMenu->pixels = calloc(MENU_FB_WIDTH * MENU_FB_HEIGHT, 1); - hudMenu->pixelsAlt = calloc(MENU_FB_WIDTH * MENU_FB_HEIGHT, 1); + hudMenu->tpl = CALLOC(size, 1); + hudMenu->pixels = CALLOC(MENU_FB_WIDTH * MENU_FB_HEIGHT, 1); + hudMenu->pixelsAlt = CALLOC(MENU_FB_WIDTH * MENU_FB_HEIGHT, 1); _present_menu(parent); } static void *_create_touchmenu(void) { - GLModelHUDMenu *hudMenu = (GLModelHUDMenu *)calloc(sizeof(GLModelHUDMenu), 1); + GLModelHUDMenu *hudMenu = (GLModelHUDMenu *)CALLOC(sizeof(GLModelHUDMenu), 1); if (hudMenu) { hudMenu->blackIsTransparent = true; hudMenu->opaquePixelHalo = true; diff --git a/src/video/glvideo.c b/src/video/glvideo.c index e5916bf9..4c0485a2 100644 --- a/src/video/glvideo.c +++ b/src/video/glvideo.c @@ -90,17 +90,17 @@ static void _create_CRT_model(void) { #endif }; - GLModel *crt = calloc(1, sizeof(GLModel)); + GLModel *crt = CALLOC(1, sizeof(GLModel)); crt->numVertices = 4; crt->numElements = 6; - crt->positions = malloc(sizeof(crt_positions)); + crt->positions = MALLOC(sizeof(crt_positions)); memcpy(crt->positions, &crt_positions[0], sizeof(crt_positions)); crt->positionType = GL_FLOAT; crt->positionSize = 4; // x,y,z coordinates crt->positionArraySize = sizeof(crt_positions); - crt->texCoords = malloc(sizeof(crt_texcoords)); + crt->texCoords = MALLOC(sizeof(crt_texcoords)); memcpy(crt->texCoords, &crt_texcoords[0], sizeof(crt_texcoords)); crt->texcoordType = GL_FLOAT; crt->texcoordSize = 2; // s,t coordinates @@ -111,7 +111,7 @@ static void _create_CRT_model(void) { crt->normalSize = GL_NONE; crt->normalArraySize = 0; - crt->elements = malloc(sizeof(indices)); + crt->elements = MALLOC(sizeof(indices)); memcpy(crt->elements, &indices[0], sizeof(indices)); crt->elementType = GL_UNSIGNED_SHORT; crt->elementArraySize = sizeof(indices); @@ -315,7 +315,7 @@ static GLuint _build_program(demoSource *vertexSource, demoSource *fragmentSourc ////////////////////////////////////// // Allocate memory for the source string including the version preprocessor information - sourceString = malloc(vertexSource->byteSize + versionStringSize); + sourceString = MALLOC(vertexSource->byteSize + versionStringSize); // Prepend our vertex shader source string with the supported GLSL version so // the shader will work on ES, Legacy, and OpenGL 3.2 Core Profile contexts @@ -332,10 +332,10 @@ static GLuint _build_program(demoSource *vertexSource, demoSource *fragmentSourc glGetShaderiv(vertexShader, GL_INFO_LOG_LENGTH, &logLength); if (logLength > 0) { - GLchar *log = (GLchar *)malloc(logLength); + GLchar *log = (GLchar *)MALLOC(logLength); glGetShaderInfoLog(vertexShader, logLength, &logLength, log); LOG("Vtx Shader compile log:%s\n", log); - free(log); + FREE(log); } glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &status); @@ -344,8 +344,7 @@ static GLuint _build_program(demoSource *vertexSource, demoSource *fragmentSourc return 0; } - free(sourceString); - sourceString = NULL; + FREE(sourceString); // Attach the vertex shader to our program glAttachShader(prgName, vertexShader); @@ -355,7 +354,7 @@ static GLuint _build_program(demoSource *vertexSource, demoSource *fragmentSourc ///////////////////////////////////////// // Allocate memory for the source string including the version preprocessor information - sourceString = malloc(fragmentSource->byteSize + versionStringSize); + sourceString = MALLOC(fragmentSource->byteSize + versionStringSize); // Prepend our fragment shader source string with the supported GLSL version so // the shader will work on ES, Legacy, and OpenGL 3.2 Core Profile contexts @@ -370,10 +369,10 @@ static GLuint _build_program(demoSource *vertexSource, demoSource *fragmentSourc glCompileShader(fragShader); glGetShaderiv(fragShader, GL_INFO_LOG_LENGTH, &logLength); if (logLength > 0) { - GLchar *log = (GLchar *)malloc(logLength); + GLchar *log = (GLchar *)MALLOC(logLength); glGetShaderInfoLog(fragShader, logLength, &logLength, log); LOG("Frag Shader compile log:\n%s\n", log); - free(log); + FREE(log); } glGetShaderiv(fragShader, GL_COMPILE_STATUS, &status); @@ -382,8 +381,7 @@ static GLuint _build_program(demoSource *vertexSource, demoSource *fragmentSourc return 0; } - free(sourceString); - sourceString = NULL; + FREE(sourceString); // Attach the fragment shader to our program glAttachShader(prgName, fragShader); @@ -395,10 +393,10 @@ static GLuint _build_program(demoSource *vertexSource, demoSource *fragmentSourc glLinkProgram(prgName); glGetProgramiv(prgName, GL_INFO_LOG_LENGTH, &logLength); if (logLength > 0) { - GLchar *log = (GLchar*)malloc(logLength); + GLchar *log = (GLchar*)MALLOC(logLength); glGetProgramInfoLog(prgName, logLength, &logLength, log); LOG("Program link log:\n%s\n", log); - free(log); + FREE(log); } glGetProgramiv(prgName, GL_LINK_STATUS, &status); @@ -410,10 +408,10 @@ static GLuint _build_program(demoSource *vertexSource, demoSource *fragmentSourc glValidateProgram(prgName); glGetProgramiv(prgName, GL_INFO_LOG_LENGTH, &logLength); if (logLength > 0) { - GLchar *log = (GLchar*)malloc(logLength); + GLchar *log = (GLchar*)MALLOC(logLength); glGetProgramInfoLog(prgName, logLength, &logLength, log); LOG("Program validate log:\n%s\n", log); - free(log); + FREE(log); } glGetProgramiv(prgName, GL_VALIDATE_STATUS, &status); @@ -476,7 +474,7 @@ static demoSource *_create_shader_source(const char *fileName) { asprintf(&filePath, "%s/shaders/%s", data_dir, fileName); if (filePath) { src = srcLoadSource(filePath); - free(filePath); + ASPRINTF_FREE(filePath); } else { ERRLOG("OOPS Could not load shader from %s (%s)", filePath, fileName); } diff --git a/src/video_util/imageUtil.m b/src/video_util/imageUtil.m index 9ac71f85..b09d7a44 100644 --- a/src/video_util/imageUtil.m +++ b/src/video_util/imageUtil.m @@ -38,11 +38,11 @@ demoImage *imgLoadImage(const char *filepathname, int flipVertical) { return NULL; } - demoImage *image = malloc(sizeof(demoImage)); + demoImage *image = MALLOC(sizeof(demoImage)); image->width = (GLuint)CGImageGetWidth(cgImage); image->height = (GLuint)CGImageGetHeight(cgImage); image->rowByteSize = image->width * 4; - image->data = malloc(image->height * image->rowByteSize); + image->data = MALLOC(image->height * image->rowByteSize); image->format = GL_RGBA; image->type = GL_UNSIGNED_BYTE; @@ -70,7 +70,7 @@ demoImage *imgLoadImage(const char *filepathname, int flipVertical) { } void imgDestroyImage(demoImage* image) { - free(image->data); - free(image); + FREE(image->data); + FREE(image); } diff --git a/src/video_util/modelUtil.c b/src/video_util/modelUtil.c index 014a8c9b..4c2efeaa 100644 --- a/src/video_util/modelUtil.c +++ b/src/video_util/modelUtil.c @@ -44,7 +44,7 @@ GLModel *mdlLoadModel(const char *filepathname) { if (!filepathname) { return NULL; } - GLModel *model = (GLModel *)calloc(sizeof(GLModel), 1); + GLModel *model = (GLModel *)CALLOC(sizeof(GLModel), 1); if (!model) { return NULL; } @@ -116,9 +116,9 @@ GLModel *mdlLoadModel(const char *filepathname) { if (GL_UNSIGNED_INT == model->elementType) { // ...load the UI elements and convert to UNSIGNED_SHORT - GLubyte *uiElements = (GLubyte *)malloc(model->elementArraySize); + GLubyte *uiElements = (GLubyte *)MALLOC(model->elementArraySize); size_t ushortElementArraySize = model->numElements * sizeof(GLushort); - model->elements = (GLubyte *)malloc(ushortElementArraySize); + model->elements = (GLubyte *)MALLOC(ushortElementArraySize); sizeRead = fread(uiElements, 1, model->elementArraySize, curFile); if (sizeRead != model->elementArraySize) { @@ -138,12 +138,12 @@ GLModel *mdlLoadModel(const char *filepathname) { ((GLushort *)model->elements)[elemNum] = ((GLuint *)uiElements)[elemNum]; } - free(uiElements); + FREE(uiElements); model->elementType = GL_UNSIGNED_SHORT; model->elementArraySize = model->numElements * sizeof(GLushort); } else { - model->elements = (GLubyte*)malloc(model->elementArraySize); + model->elements = (GLubyte*)MALLOC(model->elementArraySize); sizeRead = fread(model->elements, 1, model->elementArraySize, curFile); @@ -166,7 +166,7 @@ GLModel *mdlLoadModel(const char *filepathname) { model->positionType = attrib.datatype; model->positionSize = attrib.sizePerElement; model->numVertices = attrib.numElements; - model->positions = (GLubyte*)malloc(model->positionArraySize); + model->positions = (GLubyte*)MALLOC(model->positionArraySize); sizeRead = fread(model->positions, 1, model->positionArraySize, curFile); if (sizeRead != model->positionArraySize) { @@ -200,7 +200,7 @@ GLModel *mdlLoadModel(const char *filepathname) { return NULL; } - model->texCoords = (GLubyte*)malloc(model->texcoordArraySize); + model->texCoords = (GLubyte*)MALLOC(model->texcoordArraySize); sizeRead = fread(model->texCoords, 1, model->texcoordArraySize, curFile); if (sizeRead != model->texcoordArraySize) { @@ -235,7 +235,7 @@ GLModel *mdlLoadModel(const char *filepathname) { return NULL; } - model->normals = (GLubyte*)malloc(model->normalArraySize ); + model->normals = (GLubyte*)MALLOC(model->normalArraySize ); sizeRead = fread(model->normals, 1, model->normalArraySize , curFile); if (sizeRead != model->normalArraySize) { @@ -276,7 +276,7 @@ GLModel *mdlLoadQuadModel(void) { 0, 3, 2 }; - GLModel *model = (GLModel *)calloc(sizeof(GLModel), 1); + GLModel *model = (GLModel *)CALLOC(sizeof(GLModel), 1); if (!model) { return NULL; @@ -285,23 +285,23 @@ GLModel *mdlLoadQuadModel(void) { model->positionType = GL_FLOAT; model->positionSize = 3; model->positionArraySize = sizeof(posArray); - model->positions = (GLubyte*)malloc(model->positionArraySize); + model->positions = (GLubyte*)MALLOC(model->positionArraySize); memcpy(model->positions, posArray, model->positionArraySize); model->texcoordType = GL_FLOAT; model->texcoordSize = 2; model->texcoordArraySize = sizeof(texcoordArray); - model->texCoords = (GLubyte*)malloc(model->texcoordArraySize); + model->texCoords = (GLubyte*)MALLOC(model->texcoordArraySize); memcpy(model->texCoords, texcoordArray, model->texcoordArraySize ); model->normalType = GL_FLOAT; model->normalSize = 3; model->normalArraySize = sizeof(normalArray); - model->normals = (GLubyte*)malloc(model->normalArraySize); + model->normals = (GLubyte*)MALLOC(model->normalArraySize); memcpy(model->normals, normalArray, model->normalArraySize); model->elementArraySize = sizeof(elementArray); - model->elements = (GLubyte*)malloc(model->elementArraySize); + model->elements = (GLubyte*)MALLOC(model->elementArraySize); memcpy(model->elements, elementArray, model->elementArraySize); model->primType = GL_TRIANGLES; @@ -437,7 +437,7 @@ GLModel *mdlCreateQuad(GLfloat skew_x, GLfloat skew_y, GLfloat obj_w, GLfloat ob GLModel *model = NULL; do { - model = calloc(1, sizeof(GLModel)); + model = CALLOC(1, sizeof(GLModel)); if (!model) { break; } @@ -445,7 +445,7 @@ GLModel *mdlCreateQuad(GLfloat skew_x, GLfloat skew_y, GLfloat obj_w, GLfloat ob model->numElements = 6; model->primType = GL_TRIANGLES; - model->positions = malloc(sizeof(obj_positions)); + model->positions = MALLOC(sizeof(obj_positions)); if (!(model->positions)) { break; } @@ -455,7 +455,7 @@ GLModel *mdlCreateQuad(GLfloat skew_x, GLfloat skew_y, GLfloat obj_w, GLfloat ob model->positionArraySize = sizeof(obj_positions); if (tex_w > 0 && tex_h > 0) { - model->texCoords = malloc(sizeof(obj_texcoords)); + model->texCoords = MALLOC(sizeof(obj_texcoords)); if (!(model->texCoords)) { break; } @@ -473,7 +473,7 @@ GLModel *mdlCreateQuad(GLfloat skew_x, GLfloat skew_y, GLfloat obj_w, GLfloat ob model->normalArraySize = 0; } - model->elements = malloc(sizeof(indices)); + model->elements = MALLOC(sizeof(indices)); if (!(model->elements)) { break; } @@ -498,7 +498,7 @@ GLModel *mdlCreateQuad(GLfloat skew_x, GLfloat skew_y, GLfloat obj_w, GLfloat ob model->texWidth = tex_w; model->texHeight = tex_h; model->texFormat = TEX_FORMAT; - model->texPixels = (GLvoid *)calloc(tex_w * tex_h * sizeof(PIXEL_TYPE), 1); + model->texPixels = (GLvoid *)MALLOC(tex_w * tex_h * sizeof(PIXEL_TYPE)); if (model->texPixels == NULL) { break; } diff --git a/src/video_util/sourceUtil.c b/src/video_util/sourceUtil.c index 0c533bd9..ae5ead7b 100644 --- a/src/video_util/sourceUtil.c +++ b/src/video_util/sourceUtil.c @@ -12,13 +12,14 @@ // Based on sample code from https://developer.apple.com/library/mac/samplecode/GLEssentials/Introduction/Intro.html #include "sourceUtil.h" +#include "common.h" #include #include #include demoSource *srcLoadSource(const char *filepathname) { - demoSource *source = (demoSource *)calloc(sizeof(demoSource), 1); + demoSource *source = (demoSource *)CALLOC(sizeof(demoSource), 1); // Check the file name suffix to determine what type of shader this is const char *suffixBegin = filepathname + strlen(filepathname) - 4; @@ -42,7 +43,7 @@ demoSource *srcLoadSource(const char *filepathname) { source->byteSize = fileSize + 1; // Alloc memory for the string - source->string = malloc(source->byteSize); + source->string = MALLOC(source->byteSize); // Read entire file into the string from beginning of the file fseek(curFile, 0, SEEK_SET); @@ -57,7 +58,7 @@ demoSource *srcLoadSource(const char *filepathname) { } void srcDestroySource(demoSource *source) { - free(source->string); - free(source); + FREE(source->string); + FREE(source); }