//内存池对象初始化,首次分配8KB内存作为内存池 BOOL CMemPool::Initialize() { if( NULL == handle ) { HANDLE procHeap = GetProcessHeap(); // 分配内存池的头节点. handle = HeapAlloc(procHeap, 0, sizeof(HEADER)); if (handle) { LPHEADER header = (LPHEADER)handle; // 分配头节点成功,现在初始化内存池. header->lpHead = NULL; header->hHeap = procHeap; //初次实际分配8KB内存到内存池. BOOL ableToAddMemory = AddMemory(0); if (!ableToAddMemory) { //分配内存失败,系统资源瓶颈! HeapFree(header->hHeap, 0, handle); handle = NULL; } } } return (handle != NULL); }
void Tribe::Memorize(NPC* npc, Perception* perception) { // Retriv date from the perception csString name = perception->GetType(); float radius = perception->GetRadius(); csVector3 pos; iSector* sector = NULL; if(!perception->GetLocation(pos,sector)) { NPCDebug(npc, 5, "Failed to memorize '%s' perception without valid position",name.GetDataSafe()); return; } // Store the perception if not known from before Memory* memory = FindPrivMemory(name,pos,sector,radius,npc); if(memory) { NPCDebug(npc, 5, "Has this in private knowledge -> do nothing"); return; } memory = FindMemory(name,pos,sector,radius); if(memory) { NPCDebug(npc, 5, "Has this in tribe knowledge -> do nothing"); return; } NPCDebug(npc, 5, "Store in private memory: '%s' %.2f %.2f %2f %.2f '%s'",name.GetDataSafe(),pos.x,pos.y,pos.z,radius,npc->GetName()); AddMemory(name,pos,sector,radius,npc); }
void c_entry() { unsigned int *p; ClearTextBuffer(); AddMemory(); puts("Checking memory...\n"); if((p=(unsigned int *)malloc(0x400000))) { unsigned int lfsr=12467; while(1) { int i; unsigned int lfsrtemp=lfsr; for(i=0;i<262144;++i) { unsigned int w=lfsr&0xfffff; unsigned int j=lfsr&0xfffff; CYCLE_LFSR; unsigned int x=lfsr&0xfffff; unsigned int k=lfsr&0xfffff; p[j]=w; p[k]=x; CYCLE_LFSR; } lfsr=lfsrtemp; for(i=0;i<262144;++i) { unsigned int w=lfsr&0xfffff; unsigned int j=lfsr&0xfffff; CYCLE_LFSR; unsigned int x=lfsr&0xfffff; unsigned int k=lfsr&0xfffff; if(p[j]!=w) { printf("Error at %x\n",w); printf("expected %x, got %x\n",w,p[j]); } if(p[k]!=x) { printf("Error at %x\n",w); printf("expected %x, got %x\n",w,p[j]); } CYCLE_LFSR; } } } }
void Tribe::ProspectMine(NPC* npc, csString resource, csString nick) { if(!npc->GetActor()) { return; } csVector3 pos; iSector* sector; psGameObject::GetPosition(npc->GetActor(), pos, sector); // Add this mine to the npcs memory AddMemory(resource,pos,sector,5,npc); // Update the resource nick AddResource(resource, 0, nick); }
LPVOID CMemPool::GetAlignedMemory(DWORD dwSize, DWORD dwAlignSize) { assert(handle != NULL); BOOL haveEnoughMemory = TRUE; LPVOID lpMemory = NULL; LPHEADER poolHeader = (LPHEADER)handle; LPMEMORY_BLOCK currentBlock; DWORD sizeNeeded; DWORD padLength; currentBlock = poolHeader->lpHead; // 判断是否需要更多的内存,如果是,则分配之. sizeNeeded = dwSize; if (currentBlock->dwSize - currentBlock->dwIndex < sizeNeeded + dwAlignSize) { haveEnoughMemory = AddMemory(sizeNeeded + dwAlignSize); currentBlock = poolHeader->lpHead; } // 现在有了足够的内存,返回它! if (haveEnoughMemory) { if (dwAlignSize) { padLength = (DWORD)currentBlock + sizeof(MEMORY_BLOCK) + currentBlock->dwIndex; currentBlock->dwIndex += (dwAlignSize - (padLength % dwAlignSize)) % dwAlignSize; } //这里得到了内存地址,返回它! lpMemory = (LPVOID)&(currentBlock->lpMemory[currentBlock->dwIndex]); currentBlock->dwIndex += sizeNeeded; } return lpMemory; }