void *SafeRecurseCpp( func_sr rtn, void *arg ) /********************************************/ /* This code assumes NO parameters on the stack! */ { #define SAVE_SIZE 512 /* this must be smaller than the stack */ void *savearea; void *retval; if( stackavail() < 0x2000 ) { /* stack getting low! */ savearea = CMemAlloc( SAVE_SIZE ); if( savearea != NULL ) { memcpy( bp(), savearea, SAVE_SIZE ); memcpy( sp(), sp() + SAVE_SIZE, bp() - sp() ); setbp( bp() + SAVE_SIZE ); setsp( sp() + SAVE_SIZE ); retval = rtn( arg ); setsp( sp() - SAVE_SIZE ); memcpy( sp() + SAVE_SIZE, sp(), bp() - sp() - SAVE_SIZE ); setbp( bp() - SAVE_SIZE ); memcpy( savearea, bp(), SAVE_SIZE ); CMemFree( savearea ); return( retval ); } } return( rtn( arg ) ); }
void *SafeRecurseCG( func_sr rtn, void *arg ) /**********************************************/ /* This code assumes NO parameters on the stack! */ { #define SAVE_SIZE 512 /* this must be smaller than the stack */ void *savearea; void *retval; mem_out_action old_action; if( stackavail() < 0x2000 ) { /* stack getting low! */ old_action = SetMemOut( MO_OK ); savearea = CGAlloc( SAVE_SIZE ); if( savearea == NULL ) { FatalError( "No memory to save stack" ); } SetMemOut( old_action ); CypCopy( bp(), savearea, SAVE_SIZE ); CypCopy( sp(), sp() + SAVE_SIZE, bp() - sp() ); setbp( bp() + SAVE_SIZE ); setsp( sp() + SAVE_SIZE ); retval = rtn( arg ); setsp( sp() - SAVE_SIZE ); CypCopy( sp() + SAVE_SIZE, sp(), bp() - sp() - SAVE_SIZE ); setbp( bp() - SAVE_SIZE ); CypCopy( savearea, bp(), SAVE_SIZE ); CGFree( savearea ); return( retval ); } else { return( rtn( arg ) ); } }
static int CheckBounds(long start, long length, U16 handle) { if (handletable[handle].Nowhere.size - start - length < 0) { stopmsg(STOPMSG_INFO_ONLY | STOPMSG_NO_BUZZER, "Memory reference out of bounds."); DisplayHandle(handle); return (1); } if (length > (long)USHRT_MAX) { stopmsg(STOPMSG_INFO_ONLY | STOPMSG_NO_BUZZER, "Tried to move > 65,535 bytes."); DisplayHandle(handle); return (1); } if (handletable[handle].Nowhere.stored_at == DISK && (stackavail() <= DISKWRITELEN)) { stopmsg(STOPMSG_INFO_ONLY | STOPMSG_NO_BUZZER, "Stack space insufficient for disk memory."); DisplayHandle(handle); return (1); } if (length <= 0) { stopmsg(STOPMSG_INFO_ONLY | STOPMSG_NO_BUZZER, "Zero or negative length."); DisplayHandle(handle); return (1); } if (start < 0) { stopmsg(STOPMSG_INFO_ONLY | STOPMSG_NO_BUZZER, "Negative offset."); DisplayHandle(handle); return (1); } return (0); }
static char *ReceiveNextPacket(int *destSize, IPX_Address *node, int currentPacketIndex) { static int packetProcessingDepth; char *packet; IPX_Listen((ECB *)m_packets[currentPacketIndex]); /* repost the ECB */ /* prevent stack overflow in case of flooding, also limit depth because it affects the performance (menus become unresponsive) (bah, seems that IPX itself causes it... or it's just VMWare) */ if (stackavail() < 128 || packetProcessingDepth > MAX_RECV_PACKETS * 2) { WriteToLog("Stack overflow prevention triggered."); *destSize = 0; return nullptr; } packetProcessingDepth++; packet = ReceivePacket(destSize, node); packetProcessingDepth--; return packet; }
_WCRTLINK int write( int handle, const void *buffer, unsigned len ) #endif /**********************************************************************/ { unsigned iomode_flags; char *buf; unsigned buf_size; unsigned len_written, i, j; #if defined(__NT__) HANDLE h; LONG cur_ptr_low; LONG cur_ptr_high; DWORD rc1; #else tiny_ret_t rc1; #endif int rc2; __handle_check( handle, -1 ); iomode_flags = __GetIOMode( handle ); if( iomode_flags == 0 ) { #if defined(__WINDOWS__) || defined(__WINDOWS_386__) // How can we write to the handle if we never opened it? JBS return( _lwrite( handle, buffer, len ) ); #else __set_errno( EBADF ); return( -1 ); #endif } if( !(iomode_flags & _WRITE) ) { __set_errno( EACCES ); /* changed from EBADF to EACCES 23-feb-89 */ return( -1 ); } #if defined(__NT__) h = __getOSHandle( handle ); #endif // put a semaphore around our writes _AccessFileH( handle ); if( (iomode_flags & _APPEND) && !(iomode_flags & _ISTTY) ) { #if defined(__NT__) if( GetFileType( h ) == FILE_TYPE_DISK ) { cur_ptr_low = 0; cur_ptr_high = 0; rc1 = SetFilePointer( h, cur_ptr_low, &cur_ptr_high, FILE_END ); if( rc1 == INVALID_SET_FILE_POINTER ) { // this might be OK so if( GetLastError() != NO_ERROR ) { _ReleaseFileH( handle ); return( __set_errno_nt() ); } } } #elif defined(__OS2__) { unsigned long dummy; rc1 = DosChgFilePtr( handle, 0L, SEEK_END, &dummy ); // should we explicitly ignore ERROR_SEEK_ON_DEVICE here? } #else rc1 = TinySeek( handle, 0L, SEEK_END ); #endif #if !defined(__NT__) if( TINY_ERROR( rc1 ) ) { _ReleaseFileH( handle ); return( __set_errno_dos( TINY_INFO( rc1 ) ) ); } #endif } len_written = 0; rc2 = 0; // Pad the file with zeros if necessary if( iomode_flags & _FILEEXT ) { // turn off file extended flag __SetIOMode_nogrow( handle, iomode_flags&(~_FILEEXT) ); // It is not required to pad a file with zeroes on an NTFS file system; // unfortunately it is required on FAT (and probably FAT32). (JBS) rc2 = zero_pad( handle ); } if( rc2 == 0 ) { if( iomode_flags & _BINARY ) { /* if binary mode */ rc2 = os_write( handle, buffer, len, &len_written ); /* end of binary mode part */ } else { /* text mode */ i = stackavail(); if( i < 0x00b0 ) { __STKOVERFLOW(); /* not enough stack space */ } buf_size = 512; if( i < (512 + 48) ) { buf_size = 128; } #if defined(__AXP__) || defined(__PPC__) buf = alloca( buf_size ); #else buf = __alloca( buf_size ); #endif j = 0; for( i = 0; i < len; ) { if( ((const char*)buffer)[i] == '\n' ) { buf[j] = '\r'; ++j; if( j == buf_size ) { rc2 = os_write( handle, buf, buf_size, &j ); if( rc2 == -1 ) break; len_written += j; if( rc2 == ENOSPC ) break; len_written = i; j = 0; } } buf[j] = ((const char*)buffer)[i]; ++i; ++j; if( j == buf_size ) { rc2 = os_write( handle, buf, buf_size, &j ); if( rc2 == -1 ) break; len_written += j; if( rc2 == ENOSPC ) break; len_written = i; j = 0; } } if( j ) { rc2 = os_write( handle, buf, j, &i ); if( rc2 == ENOSPC ) { len_written += i; } else { len_written = len; } } /* end of text mode part */ } } _ReleaseFileH( handle ); if( rc2 == -1 ) { return( rc2 ); } else { return( len_written ); } }
int main(int argc, char **argv){ fflush(stdout); fprintf(stdout, "\nProgram that generates the precomputed distance file for the OPF classifier\n"); fprintf(stdout, "\nIf you have any problem, please contact: "); fprintf(stdout, "\n- [email protected]"); fprintf(stdout, "\n- [email protected]\n"); fprintf(stdout, "\nLibOPF version 2.0 (2009)\n"); fprintf(stdout, "\n"); fflush(stdout); if(argc != 4){ fprintf(stderr, "\nusage opf_distance <P1> <P2> <P3>"); fprintf(stderr, "\nP1: Dataset in the OPF file format"); fprintf(stderr, "\nP2: Distance ID\n"); fprintf(stderr, "\n 1 - Euclidean"); fprintf(stderr, "\n 2 - Chi-Square"); fprintf(stderr, "\n 3 - Manhattan (L1)"); fprintf(stderr, "\n 4 - Canberra"); fprintf(stderr, "\n 5 - Squared Chord"); fprintf(stderr,"\n 6 - Squared Chi-Squared"); fprintf(stderr,"\n 7 - BrayCurtis"); fprintf(stderr,"\n 8 - dLog"); fprintf(stderr, "\nP3: Distance normalization? 1- yes 0 - no"); exit(-1); } Subgraph *sg = ReadSubgraph(argv[1]); FILE *fp = fopen("distances.dat", "wb"); int i, j, distance = atoi(argv[2]), normalize = atoi(argv[3]); float **Distances = NULL, max = FLT_MIN; size_t result; result = fwrite(&sg->nnodes, sizeof(int), 1, fp); fprintf(stdout, "\n Stack %u...", stackavail()); Distances = (float **)malloc(sg->nnodes*sizeof(float *)); for (i = 0; i < sg->nnodes; i++) Distances[i] = (float *)calloc(sizeof(float),sg->nnodes); fprintf(stdout, "\n Stack after %u...", stackavail()); switch(distance){ case 1: fprintf(stdout, "\n Computing euclidean distance ..."); for (i = 0; i < sg->nnodes; i++){ for (j = 0; j < sg->nnodes; j++){ if(i == j) Distances[i][j] = 0.0; else Distances[sg->node[i].position][sg->node[j].position] = opf_EuclDist(sg->node[i].feat, sg->node[j].feat, sg->nfeats); if(Distances[sg->node[i].position][sg->node[j].position] > max) max = Distances[sg->node[i].position][sg->node[j].position]; } } break; case 2: fprintf(stdout, "\n Computing chi-square distance ...\n"); for (i = 0; i < sg->nnodes; i++){ for (j = 0; j < sg->nnodes; j++){ if(i == j) Distances[i][j] = 0.0; else Distances[sg->node[i].position][sg->node[j].position] = opf_ChiSquaredDist(sg->node[i].feat, sg->node[j].feat, sg->nfeats); if(Distances[sg->node[i].position][sg->node[j].position] > max) max = Distances[sg->node[i].position][sg->node[j].position]; } } break; case 3: fprintf(stdout, "\n Computing Manhattan distance ...\n"); for (i = 0; i < sg->nnodes; i++){ for (j = 0; j < sg->nnodes; j++){ if(i == j) Distances[i][j] = 0.0; else Distances[sg->node[i].position][sg->node[j].position] = opf_ManhattanDist(sg->node[i].feat, sg->node[j].feat, sg->nfeats); if(Distances[sg->node[i].position][sg->node[j].position] > max) max = Distances[sg->node[i].position][sg->node[j].position]; } } break; case 4: fprintf(stdout, "\n Computing Canberra distance ...\n"); for (i = 0; i < sg->nnodes; i++){ for (j = 0; j < sg->nnodes; j++){ if(i == j) Distances[i][j] = 0.0; else Distances[sg->node[i].position][sg->node[j].position] = opf_CanberraDist(sg->node[i].feat, sg->node[j].feat, sg->nfeats); if(Distances[sg->node[i].position][sg->node[j].position] > max) max = Distances[sg->node[i].position][sg->node[j].position]; } } break; case 5: fprintf(stdout, "\n Computing Squared Chord distance ...\n"); for (i = 0; i < sg->nnodes; i++){ for (j = 0; j < sg->nnodes; j++){ if(i == j) Distances[i][j] = 0.0; else Distances[sg->node[i].position][sg->node[j].position] = opf_SquaredChordDist(sg->node[i].feat, sg->node[j].feat, sg->nfeats); if(Distances[sg->node[i].position][sg->node[j].position] > max) max = Distances[sg->node[i].position][sg->node[j].position]; } } break; case 6: fprintf(stdout, "\n Computing Squared Chi-squared distance ...\n"); for (i = 0; i < sg->nnodes; i++){ for (j = 0; j < sg->nnodes; j++){ if(i == j) Distances[i][j] = 0.0; else Distances[sg->node[i].position][sg->node[j].position] = opf_SquaredChiSquaredDist(sg->node[i].feat, sg->node[j].feat, sg->nfeats); if(Distances[sg->node[i].position][sg->node[j].position] > max) max = Distances[sg->node[i].position][sg->node[j].position]; } } break; case 7: fprintf(stdout, "\n Computing Bray Curtis distance ...\n"); for (i = 0; i < sg->nnodes; i++){ for (j = 0; j < sg->nnodes; j++){ if(i == j) Distances[i][j] = 0.0; else Distances[sg->node[i].position][sg->node[j].position] = opf_BrayCurtisDist(sg->node[i].feat, sg->node[j].feat, sg->nfeats); if(Distances[sg->node[i].position][sg->node[j].position] > max) max = Distances[sg->node[i].position][sg->node[j].position]; } } case 8: fprintf(stdout, "\n Computing dLog ...\n"); for (i = 0; i < sg->nnodes; i++){ for (j = 0; j < sg->nnodes; j++){ if(i == j) Distances[i][j] = 0.0; else Distances[sg->node[i].position][sg->node[j].position] = opf_dLog(sg->node[i].feat, sg->node[j].feat, sg->nfeats); if(Distances[sg->node[i].position][sg->node[j].position] > max) max = Distances[sg->node[i].position][sg->node[j].position]; } //fprintf(stdout, "\t[%d] %.2f", i, Distances[i][1]); } break; default: fprintf(stderr, "\nInvalid distance ID ...\n"); } fprintf(stdout, "\n Writing matrix file ...\n"); fflush(stdout); getchar(); if (!normalize) max = 1.0; for (i = 0; i < sg->nnodes; i++){ for (j = 0; j < sg->nnodes; j++){ Distances[i][j]/=max; result = fwrite(&Distances[i][j], sizeof(float), 1, fp); } } fprintf(stdout, "\n\nDistances generated ...\n"); fflush(stdout); fprintf(stdout, "\n\nDeallocating memory ...\n"); for (i = 0; i < sg->nnodes; i++) free(Distances[i]); free(Distances); DestroySubgraph(&sg); fclose(fp); return 0; }