void test3() { HeapAllocator<void> allocator; for (size_t i = 0; i < 1000000; ++i) { auto p = allocator.allocate(1234); allocator.deallocate(p); } std::cout << "test3: SUCCESS.\n"; }
void testa() { HeapAllocator<uint32_t> a; auto p0 = a.allocate(1); auto p1 = a.allocate(1); auto p2 = a.allocate(2); a.deallocate(p1); auto p3 = a.allocate(3); }
void test2() { HeapAllocator<size_t> allocator; std::list<std::pair<HeapPointer<size_t>, size_t>> l; std::random_device rd; std::default_random_engine rnd(rd()); try { for (size_t i = 0; i < 10024*1024; ++i) { size_t v = rnd(); // std::cout << v << "\n"; auto p0 = allocator.allocate(1); //std::cout << reinterpret_cast<void*>(p0.nativePtr()) << ": p0.size = " << p0.size() << ".\n"; if (p0.size() < sizeof(size_t)) { std::cout << "test2: FAILED (allocation to small).\n"; exit(1); } *p0 = v; l.push_back(std::make_pair(p0, v)); } } catch (...) { for (auto &p : l) { if (*(p.first) != p.second) { std::cout << "test2: FAILED (values do not correspond).\n"; exit(1); } // std::cout << "+"; allocator.deallocate(p.first); } std::cout << "test2: SUCCESS.\n"; return; } std::cout << "test2: FAILED (allocation to small).\n"; exit(1); }
void test4() { HeapAllocator<void> allocator; std::random_device rd; std::default_random_engine rnd(rd()); std::uniform_int_distribution<int> uniform_dist(1, 1234); for (size_t i = 0; i < 1000000; ++i) { auto p = allocator.allocate(uniform_dist(rnd)); allocator.deallocate(p); } std::cout << "test4: SUCCESS.\n"; }
void test1() { HeapAllocator<uint8_t> allocator; std::list<HeapPointerBase> l; if (!MemoryManager::instance().checkHeap()) { exit(2); } for (size_t i = 0; i < 5000; ++i) { HeapPointerBase p0; try { p0 = allocator.allocate(10); } catch (std::bad_alloc &e) { std::cerr << "Out of memory!\n"; MemoryManager::instance().printHeapChunks(); MemoryManager::instance().checkHeap(); break; } // std::cout << reinterpret_cast<void*>(p0.nativePtr()) << ": p0.size = " << p0.size() << ".\n"; if (p0.size() < 10) { std::cout << "test1: FAILED (allocation to small).\n"; exit(1); } l.push_back(p0); } for (auto &p : l) { allocator.deallocate(p); } if (!MemoryManager::instance().checkHeap()) { exit(2); } std::cout << "test1: SUCCESS.\n"; }
Error operator()(AsyncLoaderTaskContext& ctx) { void* mem = m_alloc.allocate(10); if(!mem) return ErrorCode::FUNCTION_FAILED; HighRezTimer::sleep(0.1); m_alloc.deallocate(mem, 10); if(m_barrier) { m_barrier->wait(); } return ErrorCode::NONE; }
void GarbageCollector::collectPartition (int partitionNum) { int i; HeapAllocator* heapAllocator; vector<AllocatedObject*>* reachableObjects; /* Get all reachable objects of partition*/ heapAllocator = ptrVM->getHeapAllocator (); reachableObjects = heapAllocator->reachableObjectsForPartition (partitionNum); /* Insert them one by one in the partition */ for (i = 0; i < reachableObjects->size (); i++) { MemoryBlock *memBlock; int size; AllocatedObject* allocObj; size = reachableObjects->at (i)->getClassInfo ()->getSize (); memBlock = heapAllocator->allocateInPartition (partitionNum+1, size); if (memBlock) { MemoryBlock *prevMemBlock; byte* prevMem; byte *mem; int j = 0; allocObj = reachableObjects->at(i); prevMemBlock = allocObj->getMemBlock (); prevMem = prevMemBlock->getMemory (); mem = memBlock->getMemory (); ptrVM->updateAddressForAllocatedObject (prevMemBlock->getStartPos (), memBlock->getStartPos (), allocObj); memBlock->setAllocatedVariable (allocObj); allocObj->setMemBlock (memBlock); /* Copy data from previous block to the new block*/ for (j = 0; j < memBlock->getSize (); j++) { mem[j] = prevMem[j]; } heapAllocator->freeAddress (prevMemBlock->getStartPos ()); /* Update the value of pointer in the reachable objects */ j = allocObj->totalAddresses () - 1; while (j >= 0) { *allocObj->popAddress () = memBlock->getStartPos (); j--; } } else { break; } } if (i < reachableObjects->size ()) { /* Not all objects have been allocated * collect this partition also */ collectPartition (partitionNum + 1); /* Insert remaining reachable objects in this partition */ for (; i < reachableObjects->size (); i++) { MemoryBlock *memBlock; int size; AllocatedObject* allocObj; size = reachableObjects->at (i)->getClassInfo ()->getSize (); memBlock = heapAllocator->allocateInPartition (partitionNum+1, size); if (memBlock) { MemoryBlock *prevMemBlock; int j = 0; allocObj = reachableObjects->at(i); prevMemBlock = allocObj->getMemBlock (); ptrVM->updateAddressForAllocatedObject (prevMemBlock->getStartPos (), memBlock->getStartPos (), allocObj); memBlock->setAllocatedVariable (reachableObjects->at (i)); allocObj->setMemBlock (memBlock); /* Copy data from previous block to the new block*/ for (j = 0; j < memBlock->getSize (); j++) { memBlock->getMemory ()[i] = prevMemBlock->getMemory ()[i]; } heapAllocator->freeAddress (prevMemBlock->getStartPos ()); /* Update the value of pointer in the reachable objects */ j = allocObj->totalAddresses () - 1; while (j >= 0) { *allocObj->popAddress () = memBlock->getStartPos (); j--; } } else { break; } } } delete reachableObjects; }