Enable simple heap memory fencing/scribbling in debug builds

This commit is contained in:
Aaron Culliney 2015-12-30 22:16:57 -08:00
parent fce1f010b0
commit abae59767c
28 changed files with 265 additions and 155 deletions

View File

@ -131,7 +131,7 @@ void Java_org_deadc0de_apple2ix_Apple2CrashHandler_nativeProcessCrash(JNIEnv *en
} }
if (symbolsPath) { if (symbolsPath) {
FREE(symbolsPath); ASPRINTF_FREE(symbolsPath);
} }
(*env)->ReleaseStringUTFChars(env, jCrashPath, crashPath); (*env)->ReleaseStringUTFChars(env, jCrashPath, crashPath);

View File

@ -342,7 +342,7 @@ void Java_org_deadc0de_apple2ix_Apple2Activity_nativeChooseDisk(JNIEnv *env, jcl
} else { } else {
video_backend->animation_showDiskChosen(drive); video_backend->animation_showDiskChosen(drive);
} }
FREE(gzPath); ASPRINTF_FREE(gzPath);
} else { } else {
video_backend->animation_showDiskChosen(drive); 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"); asprintf(&str, "{ disk1 = \"%s\"; readOnly1 = %s; disk2 = \"%s\"; readOnly2 = %s }", (disk1 ?: ""), readOnly1 ? "true" : "false", (disk2 ?: ""), readOnly2 ? "true" : "false");
if (str) { if (str) {
jstr = (*env)->NewStringUTF(env, str); jstr = (*env)->NewStringUTF(env, str);
FREE(str); ASPRINTF_FREE(str);
} }
return jstr; return jstr;

View File

@ -243,14 +243,14 @@ static void sound_init( CAY8910 *_this, const char *device, unsigned long nSampl
sound_generator_framesiz = sound_generator_freq / (int)hz; sound_generator_framesiz = sound_generator_freq / (int)hz;
#if 0 #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 ) ) == sound_generator_framesiz * sound_channels ) ) ==
NULL NULL
|| ( tape_buf = || ( tape_buf =
malloc( sizeof( libspectrum_signed_word ) * MALLOC( sizeof( libspectrum_signed_word ) *
sound_generator_framesiz ) ) == NULL ) { sound_generator_framesiz ) ) == NULL ) {
if( sound_buf ) { if( sound_buf ) {
free( sound_buf ); FREE( sound_buf );
sound_buf = NULL; sound_buf = NULL;
} }
sound_end(_this); sound_end(_this);
@ -263,14 +263,14 @@ static void sound_init( CAY8910 *_this, const char *device, unsigned long nSampl
#ifdef HAVE_SAMPLERATE #ifdef HAVE_SAMPLERATE
if( settings_current.sound_hifi ) { if( settings_current.sound_hifi ) {
if( ( convert_input_buffer = malloc( sizeof( float ) * if( ( convert_input_buffer = MALLOC( sizeof( float ) *
sound_generator_framesiz * sound_generator_framesiz *
sound_channels ) ) == NULL sound_channels ) ) == NULL
|| ( convert_output_buffer = || ( convert_output_buffer =
malloc( sizeof( float ) * sound_framesiz * sound_channels ) ) == MALLOC( sizeof( float ) * sound_framesiz * sound_channels ) ) ==
NULL ) { NULL ) {
if( convert_input_buffer ) { if( convert_input_buffer ) {
free( convert_input_buffer ); FREE( convert_input_buffer );
convert_input_buffer = NULL; convert_input_buffer = NULL;
} }
sound_end(_this); sound_end(_this);
@ -373,17 +373,17 @@ static void sound_end( CAY8910 *_this )
#if 0 #if 0
if( sound_enabled ) { if( sound_enabled ) {
if( sound_buf ) { if( sound_buf ) {
free( sound_buf ); FREE( sound_buf );
sound_buf = NULL; sound_buf = NULL;
free( tape_buf ); FREE( tape_buf );
tape_buf = NULL; tape_buf = NULL;
} }
if( convert_input_buffer ) { if( convert_input_buffer ) {
free( convert_input_buffer ); FREE( convert_input_buffer );
convert_input_buffer = NULL; convert_input_buffer = NULL;
} }
if( convert_output_buffer ) { if( convert_output_buffer ) {
free( convert_output_buffer ); FREE( convert_output_buffer );
convert_output_buffer = NULL; convert_output_buffer = NULL;
} }
#ifdef HAVE_SAMPLERATE #ifdef HAVE_SAMPLERATE
@ -397,7 +397,7 @@ static void sound_end( CAY8910 *_this )
#if 0 #if 0
if( sound_buf ) { if( sound_buf ) {
free( sound_buf ); FREE( sound_buf );
sound_buf = NULL; sound_buf = NULL;
} }
#endif #endif

View File

@ -1413,7 +1413,7 @@ static bool MB_DSInit()
SAMPLE_RATE = audio_backend->systemSettings.sampleRateHz; SAMPLE_RATE = audio_backend->systemSettings.sampleRateHz;
MB_BUF_SIZE = audio_backend->systemSettings.stereoBufferSizeSamples * audio_backend->systemSettings.bytesPerSample * MB_CHANNELS; MB_BUF_SIZE = audio_backend->systemSettings.stereoBufferSizeSamples * audio_backend->systemSettings.bytesPerSample * MB_CHANNELS;
g_dwDSBufferSize = MB_BUF_SIZE; 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 #ifndef APPLE2IX
bool bRes = DSZeroVoiceBuffer(&MockingboardVoice, (char*)"MB", g_dwDSBufferSize); bool bRes = DSZeroVoiceBuffer(&MockingboardVoice, (char*)"MB", g_dwDSBufferSize);
@ -1722,7 +1722,7 @@ void MB_Initialize()
for(i=0; i<NUM_VOICES; i++) for(i=0; i<NUM_VOICES; i++)
{ {
#ifdef APPLE2IX #ifdef APPLE2IX
ppAYVoiceBuffer[i] = malloc(sizeof(short) * SAMPLE_RATE); // Buffer can hold a max of 1 seconds worth of samples ppAYVoiceBuffer[i] = MALLOC(sizeof(short) * SAMPLE_RATE); // Buffer can hold a max of 1 seconds worth of samples
#else #else
ppAYVoiceBuffer[i] = new short [SAMPLE_RATE]; // Buffer can hold a max of 1 seconds worth of samples ppAYVoiceBuffer[i] = new short [SAMPLE_RATE]; // Buffer can hold a max of 1 seconds worth of samples
#endif #endif

View File

@ -212,7 +212,7 @@ void playq_destroyPlayQueue(INOUT PlayQueue_s **queue) {
while (node) { while (node) {
PQListNode_s *p = node; PQListNode_s *p = node;
node = node->next; node = node->next;
free(p); FREE(p);
} }
} }
@ -226,13 +226,13 @@ PlayQueue_s *playq_createPlayQueue(const long *nodeIdPtr, unsigned long numBuffe
assert(numBuffers <= MAX_PLAYQ_BUFFERS); assert(numBuffers <= MAX_PLAYQ_BUFFERS);
do { do {
playq = calloc(1, sizeof(PlayQueue_s)); playq = CALLOC(1, sizeof(PlayQueue_s));
if (!playq) { if (!playq) {
ERRLOG("no memory"); ERRLOG("no memory");
break; break;
} }
PQList_s *list = calloc(1, sizeof(PQList_s)); PQList_s *list = CALLOC(1, sizeof(PQList_s));
playq->_internal = list; playq->_internal = list;
if (!list) { if (!list) {
ERRLOG("no memory"); ERRLOG("no memory");
@ -241,7 +241,7 @@ PlayQueue_s *playq_createPlayQueue(const long *nodeIdPtr, unsigned long numBuffe
bool allocSuccess = true; bool allocSuccess = true;
for (unsigned long i=0; i<numBuffers; i++) { for (unsigned long i=0; i<numBuffers; i++) {
PQListNode_s *listNode = calloc(1, sizeof(PQListNode_s)); PQListNode_s *listNode = CALLOC(1, sizeof(PQListNode_s));
LOG("CREATING PlayNode_s node ID: %ld", nodeIdPtr[i]); LOG("CREATING PlayNode_s node ID: %ld", nodeIdPtr[i]);
listNode->node.nodeId = nodeIdPtr[i]; listNode->node.nodeId = nodeIdPtr[i];
if (!listNode) { if (!listNode) {

View File

@ -357,7 +357,7 @@ static ALVoice *_openal_createVoice(unsigned long numChannels) {
ALVoice *voice = NULL; ALVoice *voice = NULL;
do { do {
voice = calloc(1, sizeof(*voice)); voice = CALLOC(1, sizeof(*voice));
if (voice == NULL) { if (voice == NULL) {
ERRLOG("OOPS, Out of memory!"); ERRLOG("OOPS, Out of memory!");
break; break;
@ -424,7 +424,7 @@ static ALVoice *_openal_createVoice(unsigned long numChannels) {
unsigned long maxSamples = openal_audio_backend.systemSettings.monoBufferSizeSamples * numChannels; unsigned long maxSamples = openal_audio_backend.systemSettings.monoBufferSizeSamples * numChannels;
voice->buffersize = maxSamples * openal_audio_backend.systemSettings.bytesPerSample; voice->buffersize = maxSamples * openal_audio_backend.systemSettings.bytesPerSample;
voice->data = calloc(1, voice->buffersize); voice->data = CALLOC(1, voice->buffersize);
if (voice->data == NULL) { if (voice->data == NULL) {
ERRLOG("OOPS, Error allocating %d bytes", voice->buffersize); ERRLOG("OOPS, Error allocating %d bytes", voice->buffersize);
break; break;
@ -488,7 +488,7 @@ static long openal_createSoundBuffer(const AudioContext_s *audio_context, INOUT
} }
ALVoices immutableNode = { /*const*/.source = voice->source }; ALVoices immutableNode = { /*const*/.source = voice->source };
ALVoices *vnode = calloc(1, sizeof(ALVoices)); ALVoices *vnode = CALLOC(1, sizeof(ALVoices));
if (!vnode) { if (!vnode) {
ERRLOG("OOPS, Not enough memory"); ERRLOG("OOPS, Not enough memory");
break; break;
@ -497,7 +497,7 @@ static long openal_createSoundBuffer(const AudioContext_s *audio_context, INOUT
vnode->voice = voice; vnode->voice = voice;
HASH_ADD_INT(voices, source, vnode); 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"); ERRLOG("OOPS, Not enough memory");
break; 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..."); 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"); ERRLOG("OOPS, Not enough memory");
break; break;
} }
@ -580,7 +580,7 @@ static long openal_systemSetup(INOUT AudioContext_s **audio_context) {
if (result) { if (result) {
if (ctx) { if (ctx) {
AudioContext_s *ctxPtr = calloc(1, sizeof(AudioContext_s)); AudioContext_s *ctxPtr = CALLOC(1, sizeof(AudioContext_s));
ctxPtr->_internal = ctx; ctxPtr->_internal = ctx;
openal_systemShutdown(&ctxPtr); openal_systemShutdown(&ctxPtr);
} }

View File

@ -426,14 +426,14 @@ static long opensl_createSoundBuffer(const AudioContext_s *audio_context, INOUT
voice->ringBuffer = prevBuffer; voice->ringBuffer = prevBuffer;
} else { } else {
LOG("Creating new SLVoice ..."); LOG("Creating new SLVoice ...");
voice = calloc(1, sizeof(*voice)); voice = CALLOC(1, sizeof(*voice));
if (voice == NULL) { if (voice == NULL) {
ERRLOG("OOPS, Out of memory!"); ERRLOG("OOPS, Out of memory!");
break; break;
} }
voice->bufferSize = bufferSize; voice->bufferSize = bufferSize;
// Allocate enough space for the temp buffer (including a maximum allowed overflow) // 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) { if (voice->ringBuffer == NULL) {
ERRLOG("OOPS, Error allocating %lu bytes", (unsigned long)voice->bufferSize+ctx->submitSize); ERRLOG("OOPS, Error allocating %lu bytes", (unsigned long)voice->bufferSize+ctx->submitSize);
break; break;
@ -444,7 +444,7 @@ static long opensl_createSoundBuffer(const AudioContext_s *audio_context, INOUT
voice->ctx = ctx; 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"); ERRLOG("OOPS, Not enough memory");
break; break;
} }
@ -556,14 +556,14 @@ static long opensles_systemSetup(INOUT AudioContext_s **audio_context) {
// //
// Engine creation ... // Engine creation ...
// //
ctx = calloc(1, sizeof(EngineContext_s)); ctx = CALLOC(1, sizeof(EngineContext_s));
if (!ctx) { if (!ctx) {
result = -1; result = -1;
break; break;
} }
ctx->submitSize = android_stereoBufferSubmitSizeSamples * opensles_audio_backend.systemSettings.bytesPerSample * NUM_CHANNELS; 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) { if (ctx->mixBuf == NULL) {
ERRLOG("OOPS, Error allocating %lu bytes", (unsigned long)ctx->submitSize); ERRLOG("OOPS, Error allocating %lu bytes", (unsigned long)ctx->submitSize);
break; break;
@ -608,7 +608,7 @@ static long opensles_systemSetup(INOUT AudioContext_s **audio_context) {
} }
// create soundcore API wrapper // create soundcore API wrapper
if ((*audio_context = calloc(1, sizeof(AudioContext_s))) == NULL) { if ((*audio_context = CALLOC(1, sizeof(AudioContext_s))) == NULL) {
result = -1; result = -1;
ERRLOG("OOPS, Not enough memory"); ERRLOG("OOPS, Not enough memory");
break; break;
@ -707,7 +707,7 @@ static long opensles_systemSetup(INOUT AudioContext_s **audio_context) {
if (result != SL_RESULT_SUCCESS) { if (result != SL_RESULT_SUCCESS) {
if (ctx) { if (ctx) {
AudioContext_s *ctxPtr = calloc(1, sizeof(AudioContext_s)); AudioContext_s *ctxPtr = CALLOC(1, sizeof(AudioContext_s));
ctxPtr->_internal = ctx; ctxPtr->_internal = ctx;
opensles_systemShutdown(&ctxPtr); opensles_systemShutdown(&ctxPtr);
} }

View File

@ -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; 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) { if (!samples_buffer) {
err = -1; err = -1;
break; break;
} }
samples_buffer_idx = bufferSizeIdealMax; 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) { if (!remainder_buffer) {
err = -1; err = -1;
break; break;

View File

@ -319,19 +319,8 @@ static const char *log_end = "\n";
*ptr = '\0'; \ *ptr = '\0'; \
} while (0); } while (0);
#define FREE(x) \ // memory management
do { \ #include "memmngt.h"
free((x)); \
(x) = NULL; \
} while (0)
#ifdef __APPLE__
#define CFRELEASE(x) \
do { \
CFRelease((x)); \
(x) = NULL; \
} while (0)
#endif
// branch prediction // branch prediction
#define LIKELY(x) __builtin_expect((x), true) #define LIKELY(x) __builtin_expect((x), true)

View File

@ -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])); memset(&disk6.disk[drive], 0x0, sizeof(disk6.disk[drive]));
disk6.disk[drive].fd = -1; disk6.disk[drive].fd = -1;
@ -1076,7 +1076,7 @@ bool disk6_loadState(StateHelper_s *helper) {
if (namelen) { if (namelen) {
unsigned long gzlen = (_GZLEN+1); unsigned long gzlen = (_GZLEN+1);
char *namebuf = malloc(namelen+gzlen+1); char *namebuf = MALLOC(namelen+gzlen+1);
if (!helper->load(fd, namebuf, namelen)) { if (!helper->load(fd, namebuf, namelen)) {
FREE(namebuf); FREE(namebuf);
break; break;
@ -1088,6 +1088,7 @@ bool disk6_loadState(StateHelper_s *helper) {
namebuf[namelen+gzlen] = '\0'; namebuf[namelen+gzlen] = '\0';
LOG("LOAD disk[%lu] : (%u) %s", i, namelen, namebuf); LOG("LOAD disk[%lu] : (%u) %s", i, namelen, namebuf);
if (disk6_insert(i, namebuf, disk6.disk[i].is_protected)) { if (disk6_insert(i, namebuf, disk6.disk[i].is_protected)) {
FREE(namebuf);
break; break;
} }
} }

View File

@ -38,7 +38,7 @@ int json_createFromFile(const char *filePath, INOUT JSON_s *parsedData) {
} }
jsonLen = JSON_LENGTH*2; jsonLen = JSON_LENGTH*2;
jsonString = malloc(jsonLen); jsonString = MALLOC(jsonLen);
if (jsonString == NULL) { if (jsonString == NULL) {
ERRLOG("WHOA : %s", strerror(errno)); ERRLOG("WHOA : %s", strerror(errno));
break; break;
@ -56,7 +56,7 @@ int json_createFromFile(const char *filePath, INOUT JSON_s *parsedData) {
if (jsonLen - jsonIdx < JSON_LENGTH) { if (jsonLen - jsonIdx < JSON_LENGTH) {
//LOG("reallocating json string ..."); //LOG("reallocating json string ...");
jsonLen <<= 1; jsonLen <<= 1;
char *newString = realloc(jsonString, jsonLen); char *newString = REALLOC(jsonString, jsonLen);
if (!newString) { if (!newString) {
ERRLOG("WHOA2 : %s", strerror(errno)); ERRLOG("WHOA2 : %s", strerror(errno));
bytesRead = -1; bytesRead = -1;
@ -83,7 +83,7 @@ int json_createFromFile(const char *filePath, INOUT JSON_s *parsedData) {
unsigned int numTokens = DEFAULT_NUMTOK; unsigned int numTokens = DEFAULT_NUMTOK;
do { do {
if (!jsonTokens) { if (!jsonTokens) {
jsonTokens = calloc(numTokens, sizeof(jsmntok_t)); jsonTokens = CALLOC(numTokens, sizeof(jsmntok_t));
if (!jsonTokens) { if (!jsonTokens) {
ERRLOG("WHOA3 : %s", strerror(errno)); ERRLOG("WHOA3 : %s", strerror(errno));
break; break;
@ -91,12 +91,12 @@ int json_createFromFile(const char *filePath, INOUT JSON_s *parsedData) {
} else { } else {
//LOG("reallocating json tokens ..."); //LOG("reallocating json tokens ...");
numTokens <<= 1; numTokens <<= 1;
jsmntok_t *newTokens = realloc(jsonTokens, numTokens * sizeof(jsmntok_t)); jsmntok_t *newTokens = REALLOC(jsonTokens, numTokens * sizeof(jsmntok_t));
memset(newTokens, '\0', numTokens * sizeof(jsmntok_t));
if (!newTokens) { if (!newTokens) {
ERRLOG("WHOA4 : %s", strerror(errno)); ERRLOG("WHOA4 : %s", strerror(errno));
break; break;
} }
memset(newTokens, '\0', numTokens * sizeof(jsmntok_t));
jsonTokens = newTokens; jsonTokens = newTokens;
} }
jsmn_init(&parser); jsmn_init(&parser);

124
src/memmngt.h Normal file
View File

@ -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

View File

@ -2367,7 +2367,7 @@ YY_RULE_SETUP
char *buf = NULL; char *buf = NULL;
asprintf(&buf, "%s/%s", getenv("HOME"), "cputrace.txt"); asprintf(&buf, "%s/%s", getenv("HOME"), "cputrace.txt");
cpu65_trace_toggle(buf); cpu65_trace_toggle(buf);
free(buf); ASPRINTF_FREE(buf);
#else #else
LOG("CPU tracing not enabled..."); LOG("CPU tracing not enabled...");
#endif #endif
@ -2382,7 +2382,7 @@ YY_RULE_SETUP
char *buf = NULL; char *buf = NULL;
asprintf(&buf, "%s/%s", getenv("HOME"), "disktrace.txt"); asprintf(&buf, "%s/%s", getenv("HOME"), "disktrace.txt");
c_toggle_disk_trace_6(buf, NULL); c_toggle_disk_trace_6(buf, NULL);
free(buf); ASPRINTF_FREE(buf);
#else #else
LOG("Disk tracing not enabled..."); LOG("Disk tracing not enabled...");
#endif #endif

View File

@ -854,7 +854,7 @@ ADDRS [0-9a-fA-F]+
char *buf = NULL; char *buf = NULL;
asprintf(&buf, "%s/%s", getenv("HOME"), "cputrace.txt"); asprintf(&buf, "%s/%s", getenv("HOME"), "cputrace.txt");
cpu65_trace_toggle(buf); cpu65_trace_toggle(buf);
free(buf); ASPRINTF_FREE(buf);
#else #else
LOG("CPU tracing not enabled..."); LOG("CPU tracing not enabled...");
#endif #endif
@ -865,7 +865,7 @@ ADDRS [0-9a-fA-F]+
char *buf = NULL; char *buf = NULL;
asprintf(&buf, "%s/%s", getenv("HOME"), "disktrace.txt"); asprintf(&buf, "%s/%s", getenv("HOME"), "disktrace.txt");
c_toggle_disk_trace_6(buf, NULL); c_toggle_disk_trace_6(buf, NULL);
free(buf); ASPRINTF_FREE(buf);
#else #else
LOG("Disk tracing not enabled..."); LOG("Disk tracing not enabled...");
#endif #endif

View File

@ -1195,8 +1195,7 @@ bool c_debugger_should_break() {
if (pthread_self() != cpu_thread_id) { if (pthread_self() != cpu_thread_id) {
// OOPS ... // OOPS ...
ERRLOG("should only call this from cpu thread, bailing..."); ERRLOG("should only call this from cpu thread, bailing...");
void *ptr = NULL; RELEASE_BREAK();
free(ptr);
} }
if (at_haltpt()) { if (at_haltpt()) {

View File

@ -140,7 +140,7 @@ int test_setup_boot_disk(const char *fileName, int readonly) {
CFRELEASE(fileURL); CFRELEASE(fileURL);
CFIndex length = CFStringGetLength(filePath); CFIndex length = CFStringGetLength(filePath);
CFIndex maxSize = CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8); CFIndex maxSize = CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8);
disk = (char *)malloc(maxSize); disk = (char *)MALLOC(maxSize);
if (!CFStringGetCString(filePath, disk, maxSize, kCFStringEncodingUTF8)) { if (!CFStringGetCString(filePath, disk, maxSize, kCFStringEncodingUTF8)) {
FREE(disk); FREE(disk);
} }
@ -184,7 +184,7 @@ int test_setup_boot_disk(const char *fileName, int readonly) {
while (*path) { while (*path) {
char *disk = *path; char *disk = *path;
++path; ++path;
FREE(disk); ASPRINTF_FREE(disk);
} }
return err; return err;

View File

@ -72,7 +72,7 @@ TEST test_boot_disk_bytes() {
ASSERT(expectedSize == EXPECTED_DISK_TRACE_FILE_SIZE); ASSERT(expectedSize == EXPECTED_DISK_TRACE_FILE_SIZE);
fseek(fp, 0, SEEK_SET); 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) { if (fread(buf, 1, EXPECTED_DISK_TRACE_FILE_SIZE, fp) != EXPECTED_DISK_TRACE_FILE_SIZE) {
ASSERT(false); ASSERT(false);
} }
@ -120,7 +120,7 @@ TEST test_boot_disk_bytes_nib() {
ASSERT(expectedSize == EXPECTED_DISK_TRACE_NIB_FILE_SIZE); ASSERT(expectedSize == EXPECTED_DISK_TRACE_NIB_FILE_SIZE);
fseek(fp, 0, SEEK_SET); 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) { if (fread(buf, 1, EXPECTED_DISK_TRACE_NIB_FILE_SIZE, fp) != EXPECTED_DISK_TRACE_NIB_FILE_SIZE) {
ASSERT(false); ASSERT(false);
} }
@ -173,7 +173,7 @@ TEST test_boot_disk_bytes_po() {
ASSERT(expectedSize == EXPECTED_DISK_TRACE_PO_FILE_SIZE); ASSERT(expectedSize == EXPECTED_DISK_TRACE_PO_FILE_SIZE);
fseek(fp, 0, SEEK_SET); 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) { if (fread(buf, 1, EXPECTED_DISK_TRACE_PO_FILE_SIZE, fp) != EXPECTED_DISK_TRACE_PO_FILE_SIZE) {
ASSERT(false); ASSERT(false);
} }
@ -368,7 +368,7 @@ TEST test_disk_bytes_savehello_dsk() {
ASSERT(expectedSize == EXPECTED_DISKWRITE_TRACE_DSK_FILE_SIZE); ASSERT(expectedSize == EXPECTED_DISKWRITE_TRACE_DSK_FILE_SIZE);
fseek(fp, 0, SEEK_SET); 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) { if (fread(buf, 1, EXPECTED_DISKWRITE_TRACE_DSK_FILE_SIZE, fp) != EXPECTED_DISKWRITE_TRACE_DSK_FILE_SIZE) {
ASSERT(false); ASSERT(false);
} }
@ -404,7 +404,7 @@ TEST test_disk_bytes_savehello_dsk() {
ASSERT(expectedSize == DSK_SIZE); ASSERT(expectedSize == DSK_SIZE);
fseek(fp, 0, SEEK_SET); 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) { if (fread(buf, 1, DSK_SIZE, fp) != DSK_SIZE) {
ASSERT(false); ASSERT(false);
} }
@ -462,7 +462,7 @@ TEST test_disk_bytes_savehello_nib() {
ASSERT(expectedSize == EXPECTED_DISKWRITE_TRACE_NIB_FILE_SIZE); ASSERT(expectedSize == EXPECTED_DISKWRITE_TRACE_NIB_FILE_SIZE);
fseek(fp, 0, SEEK_SET); 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) { if (fread(buf, 1, EXPECTED_DISKWRITE_TRACE_NIB_FILE_SIZE, fp) != EXPECTED_DISKWRITE_TRACE_NIB_FILE_SIZE) {
ASSERT(false); ASSERT(false);
} }
@ -498,7 +498,7 @@ TEST test_disk_bytes_savehello_nib() {
ASSERT(expectedSize == NIB_SIZE); ASSERT(expectedSize == NIB_SIZE);
fseek(fp, 0, SEEK_SET); 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) { if (fread(buf, 1, NIB_SIZE, fp) != NIB_SIZE) {
ASSERT(false); ASSERT(false);
} }
@ -556,7 +556,7 @@ TEST test_disk_bytes_savehello_po() {
ASSERT(expectedSize == EXPECTED_DISKWRITE_TRACE_PO_FILE_SIZE); ASSERT(expectedSize == EXPECTED_DISKWRITE_TRACE_PO_FILE_SIZE);
fseek(fp, 0, SEEK_SET); 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) { if (fread(buf, 1, EXPECTED_DISKWRITE_TRACE_PO_FILE_SIZE, fp) != EXPECTED_DISKWRITE_TRACE_PO_FILE_SIZE) {
ASSERT(false); ASSERT(false);
} }
@ -592,7 +592,7 @@ TEST test_disk_bytes_savehello_po() {
ASSERT(expectedSize == DSK_SIZE); ASSERT(expectedSize == DSK_SIZE);
fseek(fp, 0, SEEK_SET); 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) { if (fread(buf, 1, DSK_SIZE, fp) != DSK_SIZE) {
ASSERT(false); ASSERT(false);
} }
@ -669,7 +669,7 @@ TEST test_outofspace_dsk() {
ASSERT(expectedSize == EXPECTED_OOS_DSK_TRACE_FILE_SIZE); ASSERT(expectedSize == EXPECTED_OOS_DSK_TRACE_FILE_SIZE);
fseek(fp, 0, SEEK_SET); 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) { if (fread(buf, 1, EXPECTED_OOS_DSK_TRACE_FILE_SIZE, fp) != EXPECTED_OOS_DSK_TRACE_FILE_SIZE) {
ASSERT(false); ASSERT(false);
} }
@ -705,7 +705,7 @@ TEST test_outofspace_dsk() {
ASSERT(expectedSize == DSK_SIZE); ASSERT(expectedSize == DSK_SIZE);
fseek(fp, 0, SEEK_SET); 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) { if (fread(buf, 1, DSK_SIZE, fp) != DSK_SIZE) {
ASSERT(false); ASSERT(false);
} }
@ -760,7 +760,7 @@ TEST test_outofspace_nib() {
ASSERT(expectedSize == NIB_SIZE); ASSERT(expectedSize == NIB_SIZE);
fseek(fp, 0, SEEK_SET); 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) { if (fread(buf, 1, NIB_SIZE, fp) != NIB_SIZE) {
ASSERT(false); ASSERT(false);
} }
@ -815,7 +815,7 @@ TEST test_outofspace_po() {
ASSERT(expectedSize == DSK_SIZE); ASSERT(expectedSize == DSK_SIZE);
fseek(fp, 0, SEEK_SET); 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) { if (fread(buf, 1, DSK_SIZE, fp) != DSK_SIZE) {
ASSERT(false); ASSERT(false);
} }
@ -937,7 +937,7 @@ TEST test_bload_trace_dsk() {
ASSERT(expectedSize == EXPECTED_BLOAD_TRACE_DSK_FILE_SIZE); ASSERT(expectedSize == EXPECTED_BLOAD_TRACE_DSK_FILE_SIZE);
fseek(fp, 0, SEEK_SET); 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) { if (fread(buf, 1, EXPECTED_BLOAD_TRACE_DSK_FILE_SIZE, fp) != EXPECTED_BLOAD_TRACE_DSK_FILE_SIZE) {
ASSERT(false); ASSERT(false);
} }
@ -1055,7 +1055,7 @@ TEST test_bload_trace_nib() {
ASSERT(expectedSize == EXPECTED_BLOAD_TRACE_NIB_FILE_SIZE); ASSERT(expectedSize == EXPECTED_BLOAD_TRACE_NIB_FILE_SIZE);
fseek(fp, 0, SEEK_SET); 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) { if (fread(buf, 1, EXPECTED_BLOAD_TRACE_NIB_FILE_SIZE, fp) != EXPECTED_BLOAD_TRACE_NIB_FILE_SIZE) {
ASSERT(false); ASSERT(false);
} }
@ -1173,7 +1173,7 @@ TEST test_bload_trace_po() {
ASSERT(expectedSize == EXPECTED_BLOAD_TRACE_PO_FILE_SIZE); ASSERT(expectedSize == EXPECTED_BLOAD_TRACE_PO_FILE_SIZE);
fseek(fp, 0, SEEK_SET); 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) { if (fread(buf, 1, EXPECTED_BLOAD_TRACE_PO_FILE_SIZE, fp) != EXPECTED_BLOAD_TRACE_PO_FILE_SIZE) {
ASSERT(false); ASSERT(false);
} }
@ -1288,7 +1288,7 @@ TEST test_data_stability_dsk() {
ASSERT(expectedSize == EXPECTED_STABILITY_DSK_FILE_SIZE); ASSERT(expectedSize == EXPECTED_STABILITY_DSK_FILE_SIZE);
fseek(fp, 0, SEEK_SET); 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) { if (fread(buf, 1, EXPECTED_STABILITY_DSK_FILE_SIZE, fp) != EXPECTED_STABILITY_DSK_FILE_SIZE) {
ASSERT(false); ASSERT(false);
} }
@ -1323,7 +1323,7 @@ TEST test_data_stability_nib() {
ASSERT(expectedSize == EXPECTED_STABILITY_NIB_FILE_SIZE); ASSERT(expectedSize == EXPECTED_STABILITY_NIB_FILE_SIZE);
fseek(fp, 0, SEEK_SET); 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) { if (fread(buf, 1, EXPECTED_STABILITY_NIB_FILE_SIZE, fp) != EXPECTED_STABILITY_NIB_FILE_SIZE) {
ASSERT(false); ASSERT(false);
} }
@ -1358,7 +1358,7 @@ TEST test_data_stability_po() {
ASSERT(expectedSize == EXPECTED_STABILITY_PO_FILE_SIZE); ASSERT(expectedSize == EXPECTED_STABILITY_PO_FILE_SIZE);
fseek(fp, 0, SEEK_SET); 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) { if (fread(buf, 1, EXPECTED_STABILITY_PO_FILE_SIZE, fp) != EXPECTED_STABILITY_PO_FILE_SIZE) {
ASSERT(false); ASSERT(false);
} }

View File

@ -70,7 +70,7 @@ TEST test_boot_disk_cputrace() {
long expectedSize = ftell(fp); long expectedSize = ftell(fp);
ASSERT(expectedSize == EXPECTED_CPU_TRACE_FILE_SIZE); ASSERT(expectedSize == EXPECTED_CPU_TRACE_FILE_SIZE);
fseek(fp, 0, SEEK_SET); 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) { if (fread(buf, 1, EXPECTED_CPU_TRACE_FILE_SIZE, fp) != EXPECTED_CPU_TRACE_FILE_SIZE) {
ASSERT(false); ASSERT(false);
} }
@ -121,7 +121,7 @@ TEST test_cputrace_hello_dsk() {
long expectedSize = ftell(fp); long expectedSize = ftell(fp);
ASSERT(expectedSize == EXPECTED_CPUTRACE_HELLO_FILE_SIZE); ASSERT(expectedSize == EXPECTED_CPUTRACE_HELLO_FILE_SIZE);
fseek(fp, 0, SEEK_SET); 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) { if (fread(buf, 1, EXPECTED_CPUTRACE_HELLO_FILE_SIZE, fp) != EXPECTED_CPUTRACE_HELLO_FILE_SIZE) {
ASSERT(false); ASSERT(false);
} }
@ -171,7 +171,7 @@ TEST test_cputrace_hello_nib() {
long expectedSize = ftell(fp); long expectedSize = ftell(fp);
ASSERT(expectedSize == EXPECTED_CPUTRACE_HELLO_NIB_FILE_SIZE); ASSERT(expectedSize == EXPECTED_CPUTRACE_HELLO_NIB_FILE_SIZE);
fseek(fp, 0, SEEK_SET); 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) { if (fread(buf, 1, EXPECTED_CPUTRACE_HELLO_NIB_FILE_SIZE, fp) != EXPECTED_CPUTRACE_HELLO_NIB_FILE_SIZE) {
ASSERT(false); ASSERT(false);
} }
@ -221,7 +221,7 @@ TEST test_cputrace_hello_po() {
long expectedSize = ftell(fp); long expectedSize = ftell(fp);
ASSERT(expectedSize == EXPECTED_CPUTRACE_HELLO_PO_FILE_SIZE); ASSERT(expectedSize == EXPECTED_CPUTRACE_HELLO_PO_FILE_SIZE);
fseek(fp, 0, SEEK_SET); 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) { if (fread(buf, 1, EXPECTED_CPUTRACE_HELLO_PO_FILE_SIZE, fp) != EXPECTED_CPUTRACE_HELLO_PO_FILE_SIZE) {
ASSERT(false); ASSERT(false);
} }
@ -267,7 +267,7 @@ TEST test_boot_disk_vmtrace() {
ASSERT(expectedSize == EXPECTED_VM_TRACE_FILE_SIZE); ASSERT(expectedSize == EXPECTED_VM_TRACE_FILE_SIZE);
fseek(fp, 0, SEEK_SET); 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) { if (fread(buf, 1, EXPECTED_VM_TRACE_FILE_SIZE, fp) != EXPECTED_VM_TRACE_FILE_SIZE) {
ASSERT(false); ASSERT(false);
} }
@ -315,7 +315,7 @@ TEST test_boot_disk_vmtrace_nib() {
ASSERT(expectedSize == EXPECTED_VM_TRACE_NIB_FILE_SIZE); ASSERT(expectedSize == EXPECTED_VM_TRACE_NIB_FILE_SIZE);
fseek(fp, 0, SEEK_SET); 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) { if (fread(buf, 1, EXPECTED_VM_TRACE_NIB_FILE_SIZE, fp) != EXPECTED_VM_TRACE_NIB_FILE_SIZE) {
ASSERT(false); ASSERT(false);
} }
@ -363,7 +363,7 @@ TEST test_boot_disk_vmtrace_po() {
ASSERT(expectedSize == EXPECTED_VM_TRACE_PO_FILE_SIZE); ASSERT(expectedSize == EXPECTED_VM_TRACE_PO_FILE_SIZE);
fseek(fp, 0, SEEK_SET); 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) { if (fread(buf, 1, EXPECTED_VM_TRACE_PO_FILE_SIZE, fp) != EXPECTED_VM_TRACE_PO_FILE_SIZE) {
ASSERT(false); ASSERT(false);
} }

View File

@ -27,7 +27,7 @@ static GLModel *messageModel = NULL;
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
static void *_create_alert(void) { static void *_create_alert(void) {
GLModelHUDElement *hudElement = (GLModelHUDElement *)calloc(sizeof(GLModelHUDElement), 1); GLModelHUDElement *hudElement = (GLModelHUDElement *)CALLOC(sizeof(GLModelHUDElement), 1);
if (hudElement) { if (hudElement) {
hudElement->colorScheme = RED_ON_BLACK; hudElement->colorScheme = RED_ON_BLACK;
hudElement->blackIsTransparent = false; 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) { static void _alertToModel(char *message, unsigned int messageCols, unsigned int messageRows) {
if (!message) { assert(message);
return;
}
isEnabled = false; isEnabled = false;
@ -78,7 +76,7 @@ static void _alertToModel(char *message, unsigned int messageCols, unsigned int
hudElement->tpl = message; hudElement->tpl = message;
hudElement->pixWidth = fbWidth; hudElement->pixWidth = fbWidth;
hudElement->pixHeight = fbHeight; hudElement->pixHeight = fbHeight;
hudElement->pixels = calloc(fbWidth * fbHeight, 1); hudElement->pixels = MALLOC(fbWidth * fbHeight);
if (!hudElement->pixels) { if (!hudElement->pixels) {
LOG("OOPS cannot create animation message intermediate framebuffer!"); LOG("OOPS cannot create animation message intermediate framebuffer!");
break; 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 framedStride = framedCols+1/*\0*/;
const unsigned int sourceStride = cols+1/*\0*/; const unsigned int sourceStride = cols+1/*\0*/;
char *message = calloc(framedStride*framedRows, 1); char *message = CALLOC(framedStride*framedRows, 1);
if (!message) { if (!message) {
LOG("OOPS cannot create memory for animation message!"); LOG("OOPS cannot create memory for animation message!");
return; return;

View File

@ -13,7 +13,7 @@
#include "glvideo.h" #include "glvideo.h"
void *glhud_createDefault(void) { void *glhud_createDefault(void) {
GLModelHUDElement *hudElement = (GLModelHUDElement *)calloc(sizeof(GLModelHUDElement), 1); GLModelHUDElement *hudElement = (GLModelHUDElement *)CALLOC(sizeof(GLModelHUDElement), 1);
if (hudElement) { if (hudElement) {
hudElement->colorScheme = RED_ON_BLACK; hudElement->colorScheme = RED_ON_BLACK;
} }

View File

@ -32,7 +32,7 @@ static glnode_array_node_s *tail = NULL;
void glnode_registerNode(glnode_render_order_t order, GLNode node) { void glnode_registerNode(glnode_render_order_t order, GLNode node) {
pthread_mutex_lock(&mutex); 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); assert(arrayNode);
arrayNode->next = NULL; arrayNode->next = NULL;
arrayNode->last = NULL; arrayNode->last = NULL;

View File

@ -118,12 +118,12 @@ static void _setup_axis_object(GLModel *parent) {
}; };
const unsigned int size = sizeof(axisTemplate); const unsigned int size = sizeof(axisTemplate);
hudElement->tpl = calloc(size, 1); hudElement->tpl = CALLOC(size, 1);
hudElement->tplWidth = AXIS_TEMPLATE_COLS; hudElement->tplWidth = AXIS_TEMPLATE_COLS;
hudElement->tplHeight = AXIS_TEMPLATE_ROWS; hudElement->tplHeight = AXIS_TEMPLATE_ROWS;
memcpy(hudElement->tpl, axisTemplate, size); 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->pixWidth = AXIS_FB_WIDTH;
hudElement->pixHeight = AXIS_FB_HEIGHT; hudElement->pixHeight = AXIS_FB_HEIGHT;
} }
@ -166,12 +166,12 @@ static void _setup_button_object(GLModel *parent) {
}; };
const unsigned int size = sizeof(buttonTemplate); const unsigned int size = sizeof(buttonTemplate);
hudElement->tpl = calloc(size, 1); hudElement->tpl = CALLOC(size, 1);
hudElement->tplWidth = BUTTON_TEMPLATE_COLS; hudElement->tplWidth = BUTTON_TEMPLATE_COLS;
hudElement->tplHeight = BUTTON_TEMPLATE_ROWS; hudElement->tplHeight = BUTTON_TEMPLATE_ROWS;
memcpy(hudElement->tpl, buttonTemplate, size); 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->pixWidth = BUTTON_FB_WIDTH;
hudElement->pixHeight = BUTTON_FB_HEIGHT; hudElement->pixHeight = BUTTON_FB_HEIGHT;
} }

View File

@ -448,11 +448,11 @@ static void _setup_touchkbd_hud(GLModel *parent) {
hudKeyboard->pixWidth = KBD_FB_WIDTH; hudKeyboard->pixWidth = KBD_FB_WIDTH;
hudKeyboard->pixHeight = KBD_FB_HEIGHT; hudKeyboard->pixHeight = KBD_FB_HEIGHT;
const unsigned int size = sizeof(kbdTemplateUCase/* assuming all the same */); const unsigned int size = sizeof(kbdTemplateUCase/* assuming all the same dimensions */);
hudKeyboard->tpl = calloc(size, 1); hudKeyboard->tpl = MALLOC(size);
hudKeyboard->pixels = calloc(KBD_FB_WIDTH * KBD_FB_HEIGHT, 1); 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 // setup normal color pixels
hudKeyboard->colorScheme = RED_ON_BLACK; hudKeyboard->colorScheme = RED_ON_BLACK;
@ -460,7 +460,7 @@ static void _setup_touchkbd_hud(GLModel *parent) {
} }
static void *_create_touchkbd_hud(void) { static void *_create_touchkbd_hud(void) {
GLModelHUDKeyboard *hudKeyboard = (GLModelHUDKeyboard *)calloc(sizeof(GLModelHUDKeyboard), 1); GLModelHUDKeyboard *hudKeyboard = (GLModelHUDKeyboard *)CALLOC(sizeof(GLModelHUDKeyboard), 1);
if (hudKeyboard) { if (hudKeyboard) {
hudKeyboard->blackIsTransparent = true; hudKeyboard->blackIsTransparent = true;
hudKeyboard->opaquePixelHalo = true; hudKeyboard->opaquePixelHalo = true;

View File

@ -344,15 +344,15 @@ static void _setup_touchmenu(GLModel *parent) {
} }
const unsigned int size = sizeof(topMenuTemplate); const unsigned int size = sizeof(topMenuTemplate);
hudMenu->tpl = calloc(size, 1); hudMenu->tpl = CALLOC(size, 1);
hudMenu->pixels = calloc(MENU_FB_WIDTH * MENU_FB_HEIGHT, 1); hudMenu->pixels = CALLOC(MENU_FB_WIDTH * MENU_FB_HEIGHT, 1);
hudMenu->pixelsAlt = calloc(MENU_FB_WIDTH * MENU_FB_HEIGHT, 1); hudMenu->pixelsAlt = CALLOC(MENU_FB_WIDTH * MENU_FB_HEIGHT, 1);
_present_menu(parent); _present_menu(parent);
} }
static void *_create_touchmenu(void) { static void *_create_touchmenu(void) {
GLModelHUDMenu *hudMenu = (GLModelHUDMenu *)calloc(sizeof(GLModelHUDMenu), 1); GLModelHUDMenu *hudMenu = (GLModelHUDMenu *)CALLOC(sizeof(GLModelHUDMenu), 1);
if (hudMenu) { if (hudMenu) {
hudMenu->blackIsTransparent = true; hudMenu->blackIsTransparent = true;
hudMenu->opaquePixelHalo = true; hudMenu->opaquePixelHalo = true;

View File

@ -90,17 +90,17 @@ static void _create_CRT_model(void) {
#endif #endif
}; };
GLModel *crt = calloc(1, sizeof(GLModel)); GLModel *crt = CALLOC(1, sizeof(GLModel));
crt->numVertices = 4; crt->numVertices = 4;
crt->numElements = 6; crt->numElements = 6;
crt->positions = malloc(sizeof(crt_positions)); crt->positions = MALLOC(sizeof(crt_positions));
memcpy(crt->positions, &crt_positions[0], sizeof(crt_positions)); memcpy(crt->positions, &crt_positions[0], sizeof(crt_positions));
crt->positionType = GL_FLOAT; crt->positionType = GL_FLOAT;
crt->positionSize = 4; // x,y,z coordinates crt->positionSize = 4; // x,y,z coordinates
crt->positionArraySize = sizeof(crt_positions); 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)); memcpy(crt->texCoords, &crt_texcoords[0], sizeof(crt_texcoords));
crt->texcoordType = GL_FLOAT; crt->texcoordType = GL_FLOAT;
crt->texcoordSize = 2; // s,t coordinates crt->texcoordSize = 2; // s,t coordinates
@ -111,7 +111,7 @@ static void _create_CRT_model(void) {
crt->normalSize = GL_NONE; crt->normalSize = GL_NONE;
crt->normalArraySize = 0; crt->normalArraySize = 0;
crt->elements = malloc(sizeof(indices)); crt->elements = MALLOC(sizeof(indices));
memcpy(crt->elements, &indices[0], sizeof(indices)); memcpy(crt->elements, &indices[0], sizeof(indices));
crt->elementType = GL_UNSIGNED_SHORT; crt->elementType = GL_UNSIGNED_SHORT;
crt->elementArraySize = sizeof(indices); 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 // 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 // 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 // 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); glGetShaderiv(vertexShader, GL_INFO_LOG_LENGTH, &logLength);
if (logLength > 0) { if (logLength > 0) {
GLchar *log = (GLchar *)malloc(logLength); GLchar *log = (GLchar *)MALLOC(logLength);
glGetShaderInfoLog(vertexShader, logLength, &logLength, log); glGetShaderInfoLog(vertexShader, logLength, &logLength, log);
LOG("Vtx Shader compile log:%s\n", log); LOG("Vtx Shader compile log:%s\n", log);
free(log); FREE(log);
} }
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &status); glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &status);
@ -344,8 +344,7 @@ static GLuint _build_program(demoSource *vertexSource, demoSource *fragmentSourc
return 0; return 0;
} }
free(sourceString); FREE(sourceString);
sourceString = NULL;
// Attach the vertex shader to our program // Attach the vertex shader to our program
glAttachShader(prgName, vertexShader); 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 // 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 // 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 // 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); glCompileShader(fragShader);
glGetShaderiv(fragShader, GL_INFO_LOG_LENGTH, &logLength); glGetShaderiv(fragShader, GL_INFO_LOG_LENGTH, &logLength);
if (logLength > 0) { if (logLength > 0) {
GLchar *log = (GLchar *)malloc(logLength); GLchar *log = (GLchar *)MALLOC(logLength);
glGetShaderInfoLog(fragShader, logLength, &logLength, log); glGetShaderInfoLog(fragShader, logLength, &logLength, log);
LOG("Frag Shader compile log:\n%s\n", log); LOG("Frag Shader compile log:\n%s\n", log);
free(log); FREE(log);
} }
glGetShaderiv(fragShader, GL_COMPILE_STATUS, &status); glGetShaderiv(fragShader, GL_COMPILE_STATUS, &status);
@ -382,8 +381,7 @@ static GLuint _build_program(demoSource *vertexSource, demoSource *fragmentSourc
return 0; return 0;
} }
free(sourceString); FREE(sourceString);
sourceString = NULL;
// Attach the fragment shader to our program // Attach the fragment shader to our program
glAttachShader(prgName, fragShader); glAttachShader(prgName, fragShader);
@ -395,10 +393,10 @@ static GLuint _build_program(demoSource *vertexSource, demoSource *fragmentSourc
glLinkProgram(prgName); glLinkProgram(prgName);
glGetProgramiv(prgName, GL_INFO_LOG_LENGTH, &logLength); glGetProgramiv(prgName, GL_INFO_LOG_LENGTH, &logLength);
if (logLength > 0) { if (logLength > 0) {
GLchar *log = (GLchar*)malloc(logLength); GLchar *log = (GLchar*)MALLOC(logLength);
glGetProgramInfoLog(prgName, logLength, &logLength, log); glGetProgramInfoLog(prgName, logLength, &logLength, log);
LOG("Program link log:\n%s\n", log); LOG("Program link log:\n%s\n", log);
free(log); FREE(log);
} }
glGetProgramiv(prgName, GL_LINK_STATUS, &status); glGetProgramiv(prgName, GL_LINK_STATUS, &status);
@ -410,10 +408,10 @@ static GLuint _build_program(demoSource *vertexSource, demoSource *fragmentSourc
glValidateProgram(prgName); glValidateProgram(prgName);
glGetProgramiv(prgName, GL_INFO_LOG_LENGTH, &logLength); glGetProgramiv(prgName, GL_INFO_LOG_LENGTH, &logLength);
if (logLength > 0) { if (logLength > 0) {
GLchar *log = (GLchar*)malloc(logLength); GLchar *log = (GLchar*)MALLOC(logLength);
glGetProgramInfoLog(prgName, logLength, &logLength, log); glGetProgramInfoLog(prgName, logLength, &logLength, log);
LOG("Program validate log:\n%s\n", log); LOG("Program validate log:\n%s\n", log);
free(log); FREE(log);
} }
glGetProgramiv(prgName, GL_VALIDATE_STATUS, &status); 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); asprintf(&filePath, "%s/shaders/%s", data_dir, fileName);
if (filePath) { if (filePath) {
src = srcLoadSource(filePath); src = srcLoadSource(filePath);
free(filePath); ASPRINTF_FREE(filePath);
} else { } else {
ERRLOG("OOPS Could not load shader from %s (%s)", filePath, fileName); ERRLOG("OOPS Could not load shader from %s (%s)", filePath, fileName);
} }

View File

@ -38,11 +38,11 @@ demoImage *imgLoadImage(const char *filepathname, int flipVertical) {
return NULL; return NULL;
} }
demoImage *image = malloc(sizeof(demoImage)); demoImage *image = MALLOC(sizeof(demoImage));
image->width = (GLuint)CGImageGetWidth(cgImage); image->width = (GLuint)CGImageGetWidth(cgImage);
image->height = (GLuint)CGImageGetHeight(cgImage); image->height = (GLuint)CGImageGetHeight(cgImage);
image->rowByteSize = image->width * 4; image->rowByteSize = image->width * 4;
image->data = malloc(image->height * image->rowByteSize); image->data = MALLOC(image->height * image->rowByteSize);
image->format = GL_RGBA; image->format = GL_RGBA;
image->type = GL_UNSIGNED_BYTE; image->type = GL_UNSIGNED_BYTE;
@ -70,7 +70,7 @@ demoImage *imgLoadImage(const char *filepathname, int flipVertical) {
} }
void imgDestroyImage(demoImage* image) { void imgDestroyImage(demoImage* image) {
free(image->data); FREE(image->data);
free(image); FREE(image);
} }

View File

@ -44,7 +44,7 @@ GLModel *mdlLoadModel(const char *filepathname) {
if (!filepathname) { if (!filepathname) {
return NULL; return NULL;
} }
GLModel *model = (GLModel *)calloc(sizeof(GLModel), 1); GLModel *model = (GLModel *)CALLOC(sizeof(GLModel), 1);
if (!model) { if (!model) {
return NULL; return NULL;
} }
@ -116,9 +116,9 @@ GLModel *mdlLoadModel(const char *filepathname) {
if (GL_UNSIGNED_INT == model->elementType) { if (GL_UNSIGNED_INT == model->elementType) {
// ...load the UI elements and convert to UNSIGNED_SHORT // ...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); size_t ushortElementArraySize = model->numElements * sizeof(GLushort);
model->elements = (GLubyte *)malloc(ushortElementArraySize); model->elements = (GLubyte *)MALLOC(ushortElementArraySize);
sizeRead = fread(uiElements, 1, model->elementArraySize, curFile); sizeRead = fread(uiElements, 1, model->elementArraySize, curFile);
if (sizeRead != model->elementArraySize) { if (sizeRead != model->elementArraySize) {
@ -138,12 +138,12 @@ GLModel *mdlLoadModel(const char *filepathname) {
((GLushort *)model->elements)[elemNum] = ((GLuint *)uiElements)[elemNum]; ((GLushort *)model->elements)[elemNum] = ((GLuint *)uiElements)[elemNum];
} }
free(uiElements); FREE(uiElements);
model->elementType = GL_UNSIGNED_SHORT; model->elementType = GL_UNSIGNED_SHORT;
model->elementArraySize = model->numElements * sizeof(GLushort); model->elementArraySize = model->numElements * sizeof(GLushort);
} else { } else {
model->elements = (GLubyte*)malloc(model->elementArraySize); model->elements = (GLubyte*)MALLOC(model->elementArraySize);
sizeRead = fread(model->elements, 1, model->elementArraySize, curFile); sizeRead = fread(model->elements, 1, model->elementArraySize, curFile);
@ -166,7 +166,7 @@ GLModel *mdlLoadModel(const char *filepathname) {
model->positionType = attrib.datatype; model->positionType = attrib.datatype;
model->positionSize = attrib.sizePerElement; model->positionSize = attrib.sizePerElement;
model->numVertices = attrib.numElements; model->numVertices = attrib.numElements;
model->positions = (GLubyte*)malloc(model->positionArraySize); model->positions = (GLubyte*)MALLOC(model->positionArraySize);
sizeRead = fread(model->positions, 1, model->positionArraySize, curFile); sizeRead = fread(model->positions, 1, model->positionArraySize, curFile);
if (sizeRead != model->positionArraySize) { if (sizeRead != model->positionArraySize) {
@ -200,7 +200,7 @@ GLModel *mdlLoadModel(const char *filepathname) {
return NULL; return NULL;
} }
model->texCoords = (GLubyte*)malloc(model->texcoordArraySize); model->texCoords = (GLubyte*)MALLOC(model->texcoordArraySize);
sizeRead = fread(model->texCoords, 1, model->texcoordArraySize, curFile); sizeRead = fread(model->texCoords, 1, model->texcoordArraySize, curFile);
if (sizeRead != model->texcoordArraySize) { if (sizeRead != model->texcoordArraySize) {
@ -235,7 +235,7 @@ GLModel *mdlLoadModel(const char *filepathname) {
return NULL; return NULL;
} }
model->normals = (GLubyte*)malloc(model->normalArraySize ); model->normals = (GLubyte*)MALLOC(model->normalArraySize );
sizeRead = fread(model->normals, 1, model->normalArraySize , curFile); sizeRead = fread(model->normals, 1, model->normalArraySize , curFile);
if (sizeRead != model->normalArraySize) { if (sizeRead != model->normalArraySize) {
@ -276,7 +276,7 @@ GLModel *mdlLoadQuadModel(void) {
0, 3, 2 0, 3, 2
}; };
GLModel *model = (GLModel *)calloc(sizeof(GLModel), 1); GLModel *model = (GLModel *)CALLOC(sizeof(GLModel), 1);
if (!model) { if (!model) {
return NULL; return NULL;
@ -285,23 +285,23 @@ GLModel *mdlLoadQuadModel(void) {
model->positionType = GL_FLOAT; model->positionType = GL_FLOAT;
model->positionSize = 3; model->positionSize = 3;
model->positionArraySize = sizeof(posArray); model->positionArraySize = sizeof(posArray);
model->positions = (GLubyte*)malloc(model->positionArraySize); model->positions = (GLubyte*)MALLOC(model->positionArraySize);
memcpy(model->positions, posArray, model->positionArraySize); memcpy(model->positions, posArray, model->positionArraySize);
model->texcoordType = GL_FLOAT; model->texcoordType = GL_FLOAT;
model->texcoordSize = 2; model->texcoordSize = 2;
model->texcoordArraySize = sizeof(texcoordArray); model->texcoordArraySize = sizeof(texcoordArray);
model->texCoords = (GLubyte*)malloc(model->texcoordArraySize); model->texCoords = (GLubyte*)MALLOC(model->texcoordArraySize);
memcpy(model->texCoords, texcoordArray, model->texcoordArraySize ); memcpy(model->texCoords, texcoordArray, model->texcoordArraySize );
model->normalType = GL_FLOAT; model->normalType = GL_FLOAT;
model->normalSize = 3; model->normalSize = 3;
model->normalArraySize = sizeof(normalArray); model->normalArraySize = sizeof(normalArray);
model->normals = (GLubyte*)malloc(model->normalArraySize); model->normals = (GLubyte*)MALLOC(model->normalArraySize);
memcpy(model->normals, normalArray, model->normalArraySize); memcpy(model->normals, normalArray, model->normalArraySize);
model->elementArraySize = sizeof(elementArray); model->elementArraySize = sizeof(elementArray);
model->elements = (GLubyte*)malloc(model->elementArraySize); model->elements = (GLubyte*)MALLOC(model->elementArraySize);
memcpy(model->elements, elementArray, model->elementArraySize); memcpy(model->elements, elementArray, model->elementArraySize);
model->primType = GL_TRIANGLES; model->primType = GL_TRIANGLES;
@ -437,7 +437,7 @@ GLModel *mdlCreateQuad(GLfloat skew_x, GLfloat skew_y, GLfloat obj_w, GLfloat ob
GLModel *model = NULL; GLModel *model = NULL;
do { do {
model = calloc(1, sizeof(GLModel)); model = CALLOC(1, sizeof(GLModel));
if (!model) { if (!model) {
break; break;
} }
@ -445,7 +445,7 @@ GLModel *mdlCreateQuad(GLfloat skew_x, GLfloat skew_y, GLfloat obj_w, GLfloat ob
model->numElements = 6; model->numElements = 6;
model->primType = GL_TRIANGLES; model->primType = GL_TRIANGLES;
model->positions = malloc(sizeof(obj_positions)); model->positions = MALLOC(sizeof(obj_positions));
if (!(model->positions)) { if (!(model->positions)) {
break; break;
} }
@ -455,7 +455,7 @@ GLModel *mdlCreateQuad(GLfloat skew_x, GLfloat skew_y, GLfloat obj_w, GLfloat ob
model->positionArraySize = sizeof(obj_positions); model->positionArraySize = sizeof(obj_positions);
if (tex_w > 0 && tex_h > 0) { if (tex_w > 0 && tex_h > 0) {
model->texCoords = malloc(sizeof(obj_texcoords)); model->texCoords = MALLOC(sizeof(obj_texcoords));
if (!(model->texCoords)) { if (!(model->texCoords)) {
break; break;
} }
@ -473,7 +473,7 @@ GLModel *mdlCreateQuad(GLfloat skew_x, GLfloat skew_y, GLfloat obj_w, GLfloat ob
model->normalArraySize = 0; model->normalArraySize = 0;
} }
model->elements = malloc(sizeof(indices)); model->elements = MALLOC(sizeof(indices));
if (!(model->elements)) { if (!(model->elements)) {
break; break;
} }
@ -498,7 +498,7 @@ GLModel *mdlCreateQuad(GLfloat skew_x, GLfloat skew_y, GLfloat obj_w, GLfloat ob
model->texWidth = tex_w; model->texWidth = tex_w;
model->texHeight = tex_h; model->texHeight = tex_h;
model->texFormat = TEX_FORMAT; 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) { if (model->texPixels == NULL) {
break; break;
} }

View File

@ -12,13 +12,14 @@
// Based on sample code from https://developer.apple.com/library/mac/samplecode/GLEssentials/Introduction/Intro.html // Based on sample code from https://developer.apple.com/library/mac/samplecode/GLEssentials/Introduction/Intro.html
#include "sourceUtil.h" #include "sourceUtil.h"
#include "common.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
demoSource *srcLoadSource(const char *filepathname) { 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 // Check the file name suffix to determine what type of shader this is
const char *suffixBegin = filepathname + strlen(filepathname) - 4; const char *suffixBegin = filepathname + strlen(filepathname) - 4;
@ -42,7 +43,7 @@ demoSource *srcLoadSource(const char *filepathname) {
source->byteSize = fileSize + 1; source->byteSize = fileSize + 1;
// Alloc memory for the string // 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 // Read entire file into the string from beginning of the file
fseek(curFile, 0, SEEK_SET); fseek(curFile, 0, SEEK_SET);
@ -57,7 +58,7 @@ demoSource *srcLoadSource(const char *filepathname) {
} }
void srcDestroySource(demoSource *source) { void srcDestroySource(demoSource *source) {
free(source->string); FREE(source->string);
free(source); FREE(source);
} }