void* InsertToFreeList(unsigned int size,void * pData) //使用空闲空间链表的一小段空间 { unsigned int n = size / SEGMENT_SIZE; unsigned int i = 0; while(n) { n >>= 1; i++; } void *p = GetFree(i); //获取空间 memset(p,i,1); //空间第一个字节写入空间大小,为了垃圾回收 memcpy(p+1,pData,size); //其余空间写入数据 return p; }
int main(){ int start,end,size; int m; GetFree(); getchar(); m=window(); if(m==2) find=L; while(m){ switch(m){ case 1: SortList(); printf("\n空闲链表情况:\n"); PrintList(); printf("请输入进程需要的空闲区大小:"); scanf("%d",&size); Assign(size); printf("\n按任意键继续"); break; case 2: SortList(); printf("\n空闲链表情况:\n"); PrintList(); printf("请输入进程需要的空闲区大小:"); scanf("%d",&size); NF_Assign(size);printf("\n按任意键继续"); break; case 3: BFSortList(); printf("\n空闲链表情况:\n"); PrintList(); printf("请输入进程需要的空闲区大小:"); scanf("%d",&size); Assign(size);printf("\n按任意键继续"); break; case 4: printf("请输入回收区的首地址和中止地址:"); scanf("%d %d",&start,&end); Recover(start,end); break; case 0: exit(0); default : printf("\n\t\t输入错误,请重新输入"); getchar(); } getchar(); m=window(); } return 0; }
//------------------------------------------------------------------------------ bool stVPNode::AddEntry(stSize size, const stByte * object) { stSize totalsize; stSize offs; totalsize = size + sizeof(stSize); if (totalsize <= GetFree()) { offs = Page->GetPageSize() - size; // Update entry offset *Entry = offs; // Write object memcpy( (void *) (Page->GetData() + *Entry), (void *) object, size); return true; } else { // there is no room for the object return false; }//end if }//end stVPNode::AddEntry
void* Splice(unsigned int n) //分裂节点并返回分裂出的一个节点的指向内存,另一个节点插入链表相应位置 { if(n > MAX_FREE_PART) //请求大于最大支持的内存块 { LinkNode* p = &freeList.partner[MAX_FREE_PART - 1]; //是否是最大的块内存不足 if(p->next) //最大的块还有剩余~说明请求的内存太大了,不支持 { fprintf(stderr,"Max support memory part is 2 ^ %d!\n",MAX_FREE_PART); exit(-1); } //分裂超出最大块出现在最大块已经不足的情况下,那么返回新申请的最大块给他即可 return malloc(SEGMENT_SIZE << MAX_FREE_PART); } else { void* pBig = GetFree(n); //索取n的空间并分裂 void* pHalf = pBig + (SEGMENT_SIZE << (n-1)); InsertFree(n-1,pHalf); return pBig; } }
int CInterpreter::GetID( char *id_name ) { int id; id = FindSymbol( id_name, m_IDKeywords ); if ( id == -1 ) return Error("'%s' : unknown identifier", id_name); //FIXME: Function pointers would be awfully nice.. but not inside a class! Weee!! switch (id) { //Affect takes control of an entity case ID_AFFECT: return GetAffect(); break; //Wait for a specified amount of time case ID_WAIT: return GetWait(); break; //Generic set call case ID_SET: return GetSet(); break; case ID_LOOP: return GetLoop(); break; case ID_PRINT: return GetPrint(); break; case ID_USE: return GetUse(); break; case ID_FLUSH: return GetFlush(); break; case ID_RUN: return GetRun(); break; case ID_KILL: return GetKill(); break; case ID_REMOVE: return GetRemove(); break; case ID_CAMERA: return GetCamera(); break; case ID_SOUND: return GetSound(); break; case ID_MOVE: return GetMove(); break; case ID_ROTATE: return GetRotate(); break; case ID_IF: return GetIf(); break; case ID_ELSE: //return Error("syntax error : else without matching if"); return GetElse(); //FIXME: Protect this call so that floating else's aren't allowed break; case ID_GET: return Error("syntax error : illegal use of \"get\""); break; case ID_TAG: return Error("syntax error : illegal use of \"tag\""); break; case ID_TASK: return GetTask(); break; case ID_DO: return GetDo(); break; case ID_DECLARE: return GetDeclare(); break; case ID_FREE: return GetFree(); break; case ID_REM: GetRem(); break; case ID_DOWAIT: GetDoWait(); break; case ID_SIGNAL: GetSignal(); break; case ID_WAITSIGNAL: GetWaitSignal(); break; case ID_PLAY: GetPlay(); //Bad eighties slang joke... yeah, it's not really funny, I know... break; //Local variable types case TK_FLOAT: case TK_INT: case TK_STRING: case TK_VECTOR: GetVariable( id ); break; //Unknown ID default: case -1: return Error("'%s' : unknown identifier", id_name); break; } return true; }