Mercurial > repos > rhope
view number.c @ 121:1af91ceaaf49
Fixed memory leak when an output is only attached to a single null input
author | Mike Pavone <pavone@retrodev.com> |
---|---|
date | Thu, 28 Oct 2010 21:04:12 -0400 |
parents | 429b5f441381 |
children |
line wrap: on
line source
#include <math.h> #include <stdlib.h> #include "datum.h" #include "structs.h" #ifdef SEGA long atol(const char * string) { int i = 0; BOOL neg_flag = FALSE; long value = 0.0; while(string[i] != '\0') { if(string[i] >= '0' && string[i] <= '9') { value *= 10; value += string[i] - '0'; } else if(string[i] == '-') neg_flag = TRUE; else if(string[i] != ' ' && string[i] != '\n' && string[i] != '\r' && string[i] != '\t') { break; } ++i; } if(neg_flag) value = 0-value; return value; } #endif int vis_stringtoint(datum ** inputlist, queue_entry * worker_entry) { datum * output; int result = atol(inputlist[0]->c.generic.data); VIS_EnterCriticalSection(inputlist[0]->lock); if(inputlist[0]->ref_count == 1) { VIS_LeaveCriticalSection(inputlist[0]->lock); VIS_FREE(inputlist[0]->c.generic.data, "<String@Whole Number, Freeing string for reuse as integer object"); inputlist[0]->company = worker_entry->instance->def->program->companylist + BUILTIN_TYPE_WHOLE; inputlist[0]->union_type = 2; } else { VIS_LeaveCriticalSection(inputlist[0]->lock); release_ref(inputlist[0]); inputlist[0] = new_datum(BUILTIN_TYPE_WHOLE, 2, 0, worker_entry->instance->def->program); } inputlist[0]->c.integers.num_a = result; return 0; } int vis_whole_fromhex(datum ** inputlist, queue_entry * worker_entry) { int i; int result = 0; char * data = inputlist[0]->c.generic.data; for(i = 0; i < inputlist[0]->c.generic.len-1; ++i) { result <<= 4; if(data[i] >= '0' && data[i] <= '9') result |= data[i] - '0'; else if(data[i] >= 'A' && data[i] <= 'F') result |= data[i] - 'A' + 0xA; else if(data[i] >= 'a' && data[i] <= 'f') result |= data[i] - 'a' + 0xA; } release_ref(inputlist[0]); inputlist[0] = new_datum(BUILTIN_TYPE_WHOLE, 2, 0, worker_entry->instance->def->program); inputlist[0]->c.integers.num_a = result; return 0; } int vis_whole_add(datum ** inputlist, queue_entry * worker_entry) { datum * output; int ref_count; //VIS_PROFILE_START(PROF_ADDWHOLE); VIS_EnterCriticalSection(inputlist[0]->lock); ref_count = inputlist[0]->ref_count; VIS_LeaveCriticalSection(inputlist[0]->lock); if(ref_count == 1) output = inputlist[0]; else { VIS_EnterCriticalSection(inputlist[1]->lock); ref_count = inputlist[1]->ref_count; VIS_LeaveCriticalSection(inputlist[1]->lock); if(ref_count == 1) output = inputlist[1]; else output = new_datum(BUILTIN_TYPE_WHOLE, 2, 0, worker_entry->instance->def->program); } output->c.integers.num_a = inputlist[0]->c.integers.num_a + inputlist[1]->c.integers.num_a; if(output == inputlist[0]) release_ref(inputlist[1]); else if(output == inputlist[1]) release_ref(inputlist[0]); else { release_ref(inputlist[0]); release_ref(inputlist[1]); } inputlist[0] = output; //VIS_PROFILE_END(PROF_ADDWHOLE); return 0; } int vis_whole_subtract(datum ** inputlist, queue_entry * worker_entry) { datum * output; output = new_datum(BUILTIN_TYPE_WHOLE, 2, 0, worker_entry->instance->def->program); output->c.integers.num_a = inputlist[0]->c.integers.num_a - inputlist[1]->c.integers.num_a; release_ref(inputlist[0]); release_ref(inputlist[1]); inputlist[0] = output; return 0; } int vis_whole_mult(datum ** inputlist, queue_entry * worker_entry) { inputlist[0] = copy_datum(inputlist[0], 0); inputlist[0]->c.integers.num_a *= inputlist[1]->c.integers.num_a; release_ref(inputlist[1]); return 0; } int vis_whole_div(datum ** inputlist, queue_entry * worker_entry) { inputlist[0] = copy_datum(inputlist[0], 0); inputlist[0]->c.integers.num_a /= inputlist[1]->c.integers.num_a; release_ref(inputlist[1]); return 0; } int vis_wholeequal(datum ** inputlist, queue_entry * worker_entry) { datum * output; //VIS_PROFILE_START(PROF_EQUALWHOLE); output = new_datum(BUILTIN_TYPE_YESNO, 2, 0, worker_entry->instance->def->program); datum_set_yesno(output, inputlist[0]->c.integers.num_a == inputlist[1]->c.integers.num_a); release_ref(inputlist[0]); release_ref(inputlist[1]); //VIS_PROFILE_END(PROF_EQUALWHOLE); inputlist[0] = output; return 0; } int vis_whole_or(datum ** inputlist, queue_entry * worker_entry) { inputlist[0] = copy_datum(inputlist[0], 0); inputlist[0]->c.integers.num_a |= inputlist[1]->c.integers.num_a; release_ref(inputlist[1]); return 0; } int vis_whole_and(datum ** inputlist, queue_entry * worker_entry) { inputlist[0] = copy_datum(inputlist[0], 0); inputlist[0]->c.integers.num_a &= inputlist[1]->c.integers.num_a; release_ref(inputlist[1]); return 0; } int vis_whole_lsh(datum ** inputlist, queue_entry * worker_entry) { inputlist[0] = copy_datum(inputlist[0], 0); inputlist[0]->c.integers.num_a <<= inputlist[1]->c.integers.num_a; release_ref(inputlist[1]); return 0; } int vis_whole_rsh(datum ** inputlist, queue_entry * worker_entry) { inputlist[0] = copy_datum(inputlist[0], 0); inputlist[0]->c.integers.num_a >>= inputlist[1]->c.integers.num_a; release_ref(inputlist[1]); return 0; } int vis_whole_modulus(datum ** inputlist, queue_entry * worker_entry) { inputlist[0] = copy_datum(inputlist[0], 0); inputlist[0]->c.integers.num_a %= inputlist[1]->c.integers.num_a; release_ref(inputlist[1]); return 0; } int vis_greaterint(datum ** inputlist, queue_entry * worker_entry) { int result; if(inputlist[0]->c.integers.num_a > inputlist[1]->c.integers.num_a) result = 1; else result = 0; release_ref(inputlist[0]); release_ref(inputlist[1]); inputlist[0] = new_datum(BUILTIN_TYPE_YESNO, 2, 0, worker_entry->instance->def->program); datum_set_yesno(inputlist[0], result); return 0; } int vis_lesserint(datum ** inputlist, queue_entry * worker_entry) { int result, ref_count; //VIS_PROFILE_START(PROF_LESSERWHOLE); if(inputlist[0]->c.integers.num_a < inputlist[1]->c.integers.num_a) result = 1; else result = 0; VIS_EnterCriticalSection(inputlist[0]->lock); ref_count = inputlist[0]->ref_count; VIS_LeaveCriticalSection(inputlist[0]->lock); if(ref_count == 1) { release_ref(inputlist[1]); inputlist[0]->company = worker_entry->instance->def->program->companylist + BUILTIN_TYPE_YESNO; inputlist[0]->union_type = 2; } else { VIS_EnterCriticalSection(inputlist[1]->lock); ref_count = inputlist[1]->ref_count; VIS_LeaveCriticalSection(inputlist[1]->lock); if(ref_count == 1) { release_ref(inputlist[0]); inputlist[0] = inputlist[1]; inputlist[0]->company = worker_entry->instance->def->program->companylist + BUILTIN_TYPE_YESNO; inputlist[0]->union_type = 2; } else { release_ref(inputlist[0]); release_ref(inputlist[1]); inputlist[0] = new_datum(BUILTIN_TYPE_YESNO, 2, 0, worker_entry->instance->def->program); } } //datum_set_yesno(inputlist[0], result); inputlist[0]->c.integers.num_a = result; //VIS_PROFILE_END(PROF_LESSERWHOLE); return 0; } #ifndef WIN32 double atof(const char * string) { int i = 0; BOOL neg_flag = FALSE; double value = 0.0; double divisor = 10; while(string[i] != '\0' && string[i] != '.') { if(string[i] >= '0' && string[i] <= '9') { value *= 10.0; value += string[i] - '0'; } else if(string[i] == '-') neg_flag = TRUE; else if(string[i] != ' ' && string[i] != '\n' && string[i] != '\r' && string[i] != '\t') return value; ++i; } if(string[i] != '\0') { ++i; while(string[i] != '\0' && string[i] >= '0' && string[i] <= '9') { value += ((double)(string[i] - '0'))/divisor; divisor *= 10; ++i; } } if(neg_flag) value = 0.0-value; return value; } #endif int vis_stringtoreal(datum ** inputlist, queue_entry * worker_entry) { datum * output; output = new_datum(BUILTIN_TYPE_REAL, 3, 0, worker_entry->instance->def->program); output->c.real = atof(inputlist[0]->c.generic.data); release_ref(inputlist[0]); inputlist[0] = output; return 0; } int vis_real_add(datum ** inputlist, queue_entry * worker_entry) { datum * output; output = new_datum(BUILTIN_TYPE_REAL, 3, 0, worker_entry->instance->def->program); output->c.real = inputlist[0]->c.real + inputlist[1]->c.real; release_ref(inputlist[0]); release_ref(inputlist[1]); inputlist[0] = output; return 0; } int vis_real_subtract(datum ** inputlist, queue_entry * worker_entry) { datum * output; output = new_datum(BUILTIN_TYPE_REAL, 3, 0, worker_entry->instance->def->program); output->c.real = inputlist[0]->c.real - inputlist[1]->c.real; release_ref(inputlist[0]); release_ref(inputlist[1]); inputlist[0] = output; return 0; } int vis_realequal(datum ** inputlist, queue_entry * worker_entry) { datum * output = new_datum(BUILTIN_TYPE_YESNO, 2, 0, worker_entry->instance->def->program); datum_set_yesno(output, inputlist[0]->c.real == inputlist[1]->c.real); release_ref(inputlist[0]); release_ref(inputlist[1]); inputlist[0] = output; return 0; } int vis_greaterreal(datum ** inputlist, queue_entry * worker_entry) { int result; if(inputlist[0]->c.real > inputlist[1]->c.real) result = 1; else result = 0; release_ref(inputlist[0]); release_ref(inputlist[1]); inputlist[0] = new_datum(BUILTIN_TYPE_YESNO, 2, 0, worker_entry->instance->def->program); datum_set_yesno(inputlist[0], result); return 0; } int vis_lesserreal(datum ** inputlist, queue_entry * worker_entry) { int result; if(inputlist[0]->c.real < inputlist[1]->c.real) result = 1; else result = 0; release_ref(inputlist[0]); release_ref(inputlist[1]); inputlist[0] = new_datum(BUILTIN_TYPE_YESNO, 2, 0, worker_entry->instance->def->program); datum_set_yesno(inputlist[0], result); return 0; } int vis_real_div(datum ** inputlist, queue_entry * worker_entry) { inputlist[0] = copy_datum(inputlist[0], 0); inputlist[0]->c.real /= inputlist[1]->c.real; release_ref(inputlist[1]); return 0; } int vis_real_mult(datum ** inputlist, queue_entry * worker_entry) { inputlist[0] = copy_datum(inputlist[0], 0); inputlist[0]->c.real *= inputlist[1]->c.real; release_ref(inputlist[1]); return 0; } #define PI 3.14159265 int vis_real_cos(datum ** inputlist, queue_entry * worker_entry) { inputlist[0] = copy_datum(inputlist[0], 0); inputlist[0]->c.real = cos(inputlist[0]->c.real * PI/180.0); return 0; } int vis_real_sin(datum ** inputlist, queue_entry * worker_entry) { inputlist[0] = copy_datum(inputlist[0], 0); inputlist[0]->c.real = sin(inputlist[0]->c.real * PI/180.0); return 0; } int vis_real_tan(datum ** inputlist, queue_entry * worker_entry) { inputlist[0] = copy_datum(inputlist[0], 0); inputlist[0]->c.real = tan(inputlist[0]->c.real * PI/180.0); return 0; } int vis_real_arccos(datum ** inputlist, queue_entry * worker_entry) { inputlist[0] = copy_datum(inputlist[0], 0); inputlist[0]->c.real = acos(inputlist[0]->c.real)*180.0/PI; return 0; } int vis_real_arcsin(datum ** inputlist, queue_entry * worker_entry) { inputlist[0] = copy_datum(inputlist[0], 0); inputlist[0]->c.real = asin(inputlist[0]->c.real)*180.0/PI; return 0; } int vis_real_arctan(datum ** inputlist, queue_entry * worker_entry) { inputlist[0] = copy_datum(inputlist[0], 0); inputlist[0]->c.real = atan(inputlist[0]->c.real)*180.0/PI; return 0; } int vis_real_exp(datum ** inputlist, queue_entry * worker_entry) { inputlist[0] = copy_datum(inputlist[0], 0); inputlist[0]->c.real = pow(inputlist[0]->c.real, inputlist[1]->c.real); release_ref(inputlist[1]); return 0; } int vis_real_sqrt(datum ** inputlist, queue_entry * worker_entry) { inputlist[0] = copy_datum(inputlist[0], 0); inputlist[0]->c.real = sqrt(inputlist[0]->c.real); return 0; } int vis_whole_fromreal(datum ** inputlist, queue_entry * worker_entry) { datum * output = new_datum(BUILTIN_TYPE_WHOLE, 2, 0, worker_entry->instance->def->program); output->c.integers.num_a = inputlist[0]->c.real; release_ref(inputlist[0]); inputlist[0] = output; return 0; } int vis_real_fromwhole(datum ** inputlist, queue_entry * worker_entry) { datum * output = new_datum(BUILTIN_TYPE_REAL, 3, 0, worker_entry->instance->def->program); output->c.real = inputlist[0]->c.integers.num_a; release_ref(inputlist[0]); inputlist[0] = output; return 0; }