unsigned int SHMIntHashIndex_AX::malloc()
{

    if(m_iLastTotal != (*m_piTotal)) {
        SHMAccess::reopen();
        m_iLastTotal = (*m_piTotal);
    }
    if( ( getCount()*100/getTotal() ) > 98 ){
        expandMem();
    }

    unsigned int j=0;    
    if (*m_piUsed <= *m_piTotal) {
        (*m_piUsed)++;
        j = *m_piHead;
        
        if( *m_piUsed == 1+(*m_piTotal) ){
            *m_piHead = 0;
            *m_piTail = 0;
        }else{
            *m_piHead = m_poList[j].m_iNext;
        }
        


    } else {
        //##空间不够
        THROW(MBC_SHMIntHashIndex+1);
    }

    #ifdef  DEBUG_TEST
    printf("分配到偏移为%u。     ", j);
    #endif    
    return j;
}
Exemple #2
0
int main(int argc, const char **argv) {
	unsigned char *instructions;
	
	if(argc==1) {
		printf("The RNA Programming Language -- Interpreter v%s\nUsage: %s filename\n", RNA_INTERP_VERSION, argv[0]);
		
		return EXIT_FAILURE;
	}
	
	{
		BOOL failed;
		char *tempStrg;
		FILE *stream;
		Memory *instructionHolder;
		
		if(createMem(&instructionHolder)==NO) {
			printf("Interpreter error: Cannot allocate memory to hold RNA instructions.\nTerminated.\n");
			
			return EXIT_FAILURE;
		}
		
		if((stream=fopen(argv[1], "r"))==NULL) {
			printf("Interpreter error: Cannot open RNA instruction file: %s.\nTerminated.\n", argv[1]);
			destroyMem(&instructionHolder, YES);
			
			return EXIT_FAILURE;
		}
		
		failed=NO;
		if((tempStrg=(char *)malloc(sizeof(char)*512))==NULL) {
			printf("Interpreter error: Cannot allocate memory to hold RNA instructions.\nTerminated.\n");
			fclose(stream);
			destroyMem(&instructionHolder, YES);
			
			return EXIT_FAILURE;
		}
		
		while(feof(stream)==0) {
			fread(tempStrg, sizeof(char), 512, stream);
			if(appendMem(instructionHolder, tempStrg)==NO) {
				failed=YES;
				
				break;
			}
		}
		free(tempStrg);
		fclose(stream);
		
		if(failed==YES) {
			printf("Interpreter error: Cannot resize memory space used to hold RNA instructions.\nTerminated.\n");
			destroyMem(&instructionHolder, YES);
			
			return EXIT_FAILURE;
		}
		
		instructions=instructionHolder->mem;
		destroyMem(&instructionHolder, NO);
	}
	
	{
		// A=1 U=2 G=3 C=4
		unsigned char *ptr;
		Memory *memory;
		if(createMem(&memory)==NO) {
			printf("Interpreter error: Cannot allocate memory to hold on-execution data.\nTerminated.\n");
			free(instructions);
			
			return EXIT_FAILURE;
		}
		
		unsigned int codon=0, codonSize=100, loopActivatorIndex=0;
		unsigned long loop, loopActivators[2][64], loopy, strg;
		BOOL loopActivated=NO, skipUntilLoopTerminator=NO, started=NO;
		for(loop=ustrlen(instructions), loopy=0; loopy<loop; ++loopy) {
			{
				unsigned int inst=0;
				switch(instructions[loopy]) {
					case 'A':
					case 'a':
						inst=1;
						break;
					case 'U':
					case 'u':
						inst=2;
						break;
					case 'G':
					case 'g':
						inst=3;
						break;
					case 'C':
					case 'c':
						inst=4;
						break;
				}
				
				if(inst==0)
					continue;
				codon+=inst*codonSize;
			}
			
			if(codonSize==1) {
				if(started==NO) {
					if(codon==123) { // Metionine
						started=YES;
						strg=0;
						ptr=memory->mem;
					}
				} else {
					if(skipUntilLoopTerminator==YES && (codon==212 || codon==214))
						skipUntilLoopTerminator=NO;
					else {
						switch(codon) {
							case 211: // Termination
							case 213:
							case 231:
								started=NO;
								break;
							case 233: // Tryptophan
								strg=0;
								break;
							case 111: // Lysine
							case 113:
								++strg;
								break;
							case 112: // Asparagin
							case 114:
								--strg;
								break;
							case 341: // Alanine
							case 342:
							case 343:
							case 344:
								strg=(unsigned long)*ptr;
								break;
							case 141: // Threonine
							case 142:
							case 143:
							case 144:
								expandMem(memory, strg+1);
								ptr=&(memory->mem[strg]);
								break;
							case 441: // Proline
							case 442:
							case 443:
							case 444:
								{
									int temp;
									scanf("%d", &temp);
									*ptr=(unsigned char)(temp&0xF);
								}
								break;
							case 421: // Leucine
							case 422:
							case 423:
							case 424:
								printf("%c", (char)(*ptr&0x7F));
								break;
							case 131: // Arginine
							case 133:
							case 431:
							case 432:
							case 433:
							case 434:
								expandMem(memory, strg+1);
								*ptr+=memory->mem[strg];
								break;
							case 132: // Serine
							case 134:
							case 241:
							case 242:
							case 243:
							case 244:
								expandMem(memory, strg+1);
								*ptr*=memory->mem[strg];
								break;
							case 411: // Glutamine
							case 413:
								expandMem(memory, strg+1);
								*ptr-=memory->mem[strg];
								break;
							case 412: // Histidine
							case 414:
								expandMem(memory, strg+1);
								*ptr/=memory->mem[strg];
								break;
							case 311: // Glutamic acid
							case 313:
								expandMem(memory, strg+1);
								*ptr=(*ptr==memory->mem[strg]?1:0);
								break;
							case 312: // Aspartic acid
							case 314:
								if(*ptr==0) {
									if(loopActivated==NO)
										skipUntilLoopTerminator=YES;
									else {
										loopy=loopActivators[1][loopActivatorIndex++]-1;
										loopActivated=NO;
									}
								} else {
									loopActivated=YES;
									loopActivators[0][loopActivatorIndex++]=loopy-2;
								}
								break;
							case 212: // Tyrosine
							case 214:
								if(loopActivated==YES) {
									loopActivators[1][--loopActivatorIndex]=loopy+1;
									loopy=loopActivators[0][loopActivatorIndex]-1;
								}
								break;
							// Other codons are no-op yet. :)
						}
					}
				}
				
				codon=0;
				codonSize=100;
			} else
				codonSize/=10;
		}
	}
	
	free(instructions);
	
	return EXIT_SUCCESS;
}