Address PoolAllocator::allocate(Size *size) { Size index, nPools = 1; MemoryPool *pool = ZERO; /* Find the correct pool size. */ for (index = POOL_MIN_POWER; index < POOL_MAX_POWER; index++) if (*size <= (Size) 1 << (index + 1)) break; /* Do we need to allocate an initial pool? */ if (!pools[index] && parent) pool = pools[index] = newPool(index, POOL_MIN_COUNT(*size)); /* Search for pool with enough memory. */ else { /* Loop current pools. */ for (pool = pools[index]; pool; pool = pool->next, nPools++) { /* At least one block still free? */ if (pool->free) break; /* If no pool has free space anymore, allocate another. */ if (!pool->next) { pool = newPool(index, POOL_MIN_COUNT(*size) * nPools); break; } } } /* Attempt to allocate. */ return pool ? pool->allocate() : ZERO; }
void GCMemoryManager::gc_begin(bool recordGCBeginTime, bool recordPreGCUsage, bool recordAccumulatedGCTime) { assert(_last_gc_stat != NULL && _current_gc_stat != NULL, "Just checking"); if (recordAccumulatedGCTime) { _accumulated_timer.start(); } // _num_collections now increases in gc_end, to count completed collections if (recordGCBeginTime) { _current_gc_stat->set_index(_num_collections+1); _current_gc_stat->set_start_time(Management::timestamp()); } if (recordPreGCUsage) { // Keep memory usage of all memory pools for (int i = 0; i < MemoryService::num_memory_pools(); i++) { MemoryPool* pool = MemoryService::get_memory_pool(i); MemoryUsage usage = pool->get_memory_usage(); _current_gc_stat->set_before_gc_usage(i, usage); HS_DTRACE_PROBE8(hotspot, mem__pool__gc__begin, name(), strlen(name()), pool->name(), strlen(pool->name()), usage.init_size(), usage.used(), usage.committed(), usage.max_size()); } } }
int Path::randomWalk(const Scene *scene, Sampler *sampler, int nSteps, int rrStart, ETransportMode mode, MemoryPool &pool) { /* Determine the relevant edge and vertex to start the random walk */ PathVertex *curVertex = m_vertices[m_vertices.size()-1], *predVertex = m_vertices.size() < 2 ? NULL : m_vertices[m_vertices.size()-2]; PathEdge *predEdge = m_edges.empty() ? NULL : m_edges[m_edges.size()-1]; Spectrum throughput(1.0f); for (int i=0; i<nSteps || nSteps == -1; ++i) { PathVertex *succVertex = pool.allocVertex(); PathEdge *succEdge = pool.allocEdge(); if (!curVertex->sampleNext(scene, sampler, predVertex, predEdge, succEdge, succVertex, mode, rrStart != -1 && i >= rrStart, &throughput)) { pool.release(succVertex); pool.release(succEdge); return i; } append(succEdge, succVertex); predVertex = curVertex; curVertex = succVertex; predEdge = succEdge; } return nSteps; }
TVMStatus VMMemoryPoolAllocate(TVMMemoryPoolID memory, TVMMemorySize size, void **pointer){ TMachineSignalState sigState; MachineSuspendSignals(&sigState); ThreadStore* tStore = ThreadStore::getInstance(); if((size == 0) || (pointer == NULL)){ printf("VMMemoryPoolAllocate(): size was zero or pointer was null.\n"); MachineResumeSignals(&sigState); return VM_STATUS_ERROR_INVALID_PARAMETER; } // printf("VMMemoryPoolAllocate(): looking for pool: %d\n", memory); MemoryPool *pool = tStore->findMemoryPoolByID(memory); if(pool == NULL){ printf("VMMemoryPoolAllocate(): pool was null.\n"); MachineResumeSignals(&sigState); return VM_STATUS_ERROR_INVALID_PARAMETER; } uint8_t* newMemory = pool->allocateMemory(size); // printf("VMMemoryPoolAllocate(): allocated chunk %d\n", newMemory); if(newMemory == NULL){ printf("VMMemoryPoolAllocate(): new memory allocated was null.\n"); return VM_STATUS_ERROR_INSUFFICIENT_RESOURCES; } // printf("VMMemoryPoolAllocate(): Memory allocated successfully!\n"); *pointer = newMemory; //if execution gets here, everything is valid and MachineResumeSignals(&sigState); //the memory should be allocated return VM_STATUS_SUCCESS; }
TVMStatus VMMemoryPoolDeallocate(TVMMemoryPoolID memory, void *pointer){ TMachineSignalState sigState; MachineSuspendSignals(&sigState); ThreadStore* tStore = ThreadStore::getInstance(); if(pointer == NULL){ printf("VMMemoryPoolDeallocate(): pointer was null.\n"); return VM_STATUS_ERROR_INVALID_PARAMETER; } MemoryPool *pool = tStore->findMemoryPoolByID(memory); if(pool == NULL){ printf("VMMemoryPoolDeallocate(): pool was null.\n"); return VM_STATUS_ERROR_INVALID_PARAMETER; } // printf("VMMemoryPoolDeallocate(): attempting to deallocate chunk %d\n", pointer); if(pool->deallocate((uint8_t*)pointer) == false){ //attempts to deallocate the memory specified by pointer return VM_STATUS_ERROR_INVALID_PARAMETER; //returns true on successful deallocation, and false if the }//memory pointed to by pointer was not previously allocated in the memory pool MachineResumeSignals(&sigState); return VM_STATUS_SUCCESS; }
void Path::release(size_t start, size_t end, MemoryPool &pool) { for (size_t i=start; i<end; ++i) { pool.release(m_vertices[i]); if (i+1 < end) pool.release(m_edges[i]); } }
void Path::release(MemoryPool &pool) { for (size_t i=0; i<m_vertices.size(); ++i) pool.release(m_vertices[i]); for (size_t i=0; i<m_edges.size(); ++i) pool.release(m_edges[i]); m_vertices.clear(); m_edges.clear(); }
MemoryPool* MemoryService::get_memory_pool(instanceHandle ph) { for (int i = 0; i < _pools_list->length(); i++) { MemoryPool* pool = _pools_list->at(i); if (pool->is_pool(ph)) { return pool; } } return NULL; }
void MemoryService::track_memory_usage() { // Track the peak memory usage for (int i = 0; i < _pools_list->length(); i++) { MemoryPool* pool = _pools_list->at(i); pool->record_peak_memory_usage(); } // Detect low memory LowMemoryDetector::detect_low_memory(); }
// recompute enabled flag void LowMemoryDetector::recompute_enabled_for_collected_pools() { bool enabled = false; int num_memory_pools = MemoryService::num_memory_pools(); for (int i=0; i<num_memory_pools; i++) { MemoryPool* pool = MemoryService::get_memory_pool(i); if (pool->is_collected_pool() && is_enabled(pool)) { enabled = true; break; } } _enabled_for_collected_pools = enabled; }
//----------------------------------------------------------------------------- static void testAppendChar() { MemoryPool pool = newMemoryPool(); StringBuffer s = newStringBuffer(&pool, "A test string"); s.appendChar(&s, '!'); assertEquals(string, "A test string!", s.str); s.appendChar(&s, '-'); assertEquals(string, "A test string!-", s.str); pool.drain(&pool); }
void MemoryService::oops_do(OopClosure* f) { int i; for (i = 0; i < _pools_list->length(); i++) { MemoryPool* pool = _pools_list->at(i); pool->oops_do(f); } for (i = 0; i < _managers_list->length(); i++) { MemoryManager* mgr = _managers_list->at(i); mgr->oops_do(f); } }
void PoolAllocator::release(Address addr) { for (Size i = POOL_MIN_POWER - 1; i < POOL_MAX_POWER; i++) { for (MemoryPool *p = pools[i]; p; p = p->next) { if (addr < p->addr + (p->count * p->size) && addr >= p->addr) { p->release(addr); return; } } } }
//----------------------------------------------------------------------------- static void testAppendString() { MemoryPool pool = newMemoryPool(); StringBuffer s = newStringBuffer(&pool, "A test string"); s.append(&s, " - appended value"); assertEquals(string, "A test string - appended value", s.str); s.append(&s, " & another value"); assertEquals(string, "A test string - appended value & another value", s.str); assertTrue(s.size > strlen("A test string - appended value & another value")); pool.drain(&pool); }
int Path::randomWalkFromPixel(const Scene *scene, Sampler *sampler, int nSteps, const Point2i &pixelPosition, int rrStart, MemoryPool &pool) { PathVertex *v1 = pool.allocVertex(), *v2 = pool.allocVertex(); PathEdge *e0 = pool.allocEdge(), *e1 = pool.allocEdge(); /* Use a special sampling routine for the first two sensor vertices so that the resulting subpath passes through the specified pixel position */ int t = vertex(0)->sampleSensor(scene, sampler, pixelPosition, e0, v1, e1, v2); if (t < 1) { pool.release(e0); pool.release(v1); return 0; } append(e0, v1); if (t < 2) { pool.release(e1); pool.release(v2); return 1; } append(e1, v2); PathVertex *predVertex = v1, *curVertex = v2; PathEdge *predEdge = e1; Spectrum throughput(1.0f); for (; t<nSteps || nSteps == -1; ++t) { PathVertex *succVertex = pool.allocVertex(); PathEdge *succEdge = pool.allocEdge(); if (!curVertex->sampleNext(scene, sampler, predVertex, predEdge, succEdge, succVertex, ERadiance, rrStart != -1 && t >= rrStart, &throughput)) { pool.release(succVertex); pool.release(succEdge); return t; } append(succEdge, succVertex); predVertex = curVertex; curVertex = succVertex; predEdge = succEdge; } return nSteps; }
//----------------------------------------------------------------------------- static void testToString() { MemoryPool pool = newMemoryPool(); StringBuffer sb = newStringBuffer(&pool, "A test strin"); sb.appendChar(&sb, 'g'); assertEquals(string, "A test string", sb.str); string s = sb.toString(&sb, &pool); assertEquals(string, "A test string", s); assertEquals(unsigned_int, 13, strlen(s)); pool.drain(&pool); }
int main() { MemoryPool<A, 32> m; cout << endl; for (int i = 0; i < 5; ++i) { cout << "***** " << i << " *****" << endl << endl; A *temp = m.allocate(); cout << "return pointer = " << static_cast<const void *>(temp) << endl; m.deallocate(temp); cout << endl; } return 0; }
// // Method that tells the pool owned by the calling thread that // it is free to reuse all of its memory // void MemoryManager::CPP_FreeAllTempMemory() { DWORD dwThreadID; MemoryPool* pmp; dwThreadID = GetCurrentThreadId(); //the id of the calling thread pmp = GetMemoryPool(dwThreadID); if (!pmp) //no more room for pools { return; } pmp->FreeAllTempMemory(); }
// // Destructor. Because of the way memory pools get copied, // this function needs to call an additional function to clear // up the MemoryPool memory - the deconstructor on MemoryPool // does not actually delete its memory // MemoryManager::~MemoryManager(void) { MemoryPool* pmp = m_rgmp; int i; for (i = 0; i < m_impMax; i++) { if (pmp->m_rgchMemBlock) { pmp->ClearPool(); } pmp++; } delete [] m_rgmp; }
// // Method that will query the correct memory pool of the calling // thread for a set number of bytes. Returns 0 if there was a // failure in getting the memory. // LPSTR MemoryManager::CPP_GetTempMemory(int cByte) { DWORD dwThreadID; MemoryPool* pmp; dwThreadID = GetCurrentThreadId(); //the id of the calling thread pmp = GetMemoryPool(dwThreadID); if (!pmp) //no more room for pools { return 0; } return pmp->GetTempMemory(cByte); }
int main (void) { Thread thread(send_thread); bool result = true; int result_counter = 0; while (true) { osEvent evt = queue.get(); if (evt.status == osEventMessage) { message_t *message = (message_t*)evt.value.p; const float expected_voltage = CREATE_VOLTAGE(message->counter); const float expected_current = CREATE_CURRENT(message->counter); // Check using macros if received values correspond to values sent via queue bool expected_values = (expected_voltage == message->voltage) && (expected_current == message->current); result = result && expected_values; const char *result_msg = expected_values ? "OK" : "FAIL"; printf("%3d %.2fV %.2fA ... [%s]\r\n", message->counter, message->voltage, message->current, result_msg); mpool.free(message); if (result == false || ++result_counter == QUEUE_SIZE) { break; } } } notify_completion(result); return 0; }
void MemoryService::gc_begin(bool fullGC) { GCMemoryManager* mgr; if (fullGC) { mgr = _major_gc_manager; } else { mgr = _minor_gc_manager; } assert(mgr->is_gc_memory_manager(), "Sanity check"); mgr->gc_begin(); // Track the peak memory usage when GC begins for (int i = 0; i < _pools_list->length(); i++) { MemoryPool* pool = _pools_list->at(i); pool->record_peak_memory_usage(); } }
TVMStatus VMMemoryPoolCreate(void *base, TVMMemorySize size, TVMMemoryPoolIDRef memory){ TMachineSignalState sigState; MachineSuspendSignals(&sigState); ThreadStore* tStore = ThreadStore::getInstance(); if((base == NULL) || (memory == NULL)){ MachineResumeSignals(&sigState); return VM_STATUS_ERROR_INVALID_PARAMETER; } MemoryPool *pool = new MemoryPool((uint8_t*)base, size); *memory = pool->getID(); tStore->insert(pool); MachineResumeSignals(&sigState); return VM_STATUS_SUCCESS; }
ConstPointer duplicate_uint_if_needed(const std::uint64_t *uint, int uint64_count, int new_uint64_count, bool force, MemoryPool &pool) { #ifdef _DEBUG if (uint == nullptr && uint64_count > 0) { throw invalid_argument("uint"); } if (uint64_count < 0) { throw invalid_argument("uint64_count"); } if (new_uint64_count < 0) { throw invalid_argument("new_uint64_count"); } #endif if (!force && uint64_count >= new_uint64_count) { return ConstPointer::Aliasing(uint); } Pointer allocation = pool.get_for_uint64_count(new_uint64_count); set_uint_uint(uint, uint64_count, new_uint64_count, allocation.get()); ConstPointer const_allocation; const_allocation.acquire(allocation); return const_allocation; }
Field create_dynamic_field( MemoryPool pool, const char* name, size_t name_length, Field::type_e type, void* cbdata_get, void* cbdata_set ) { ib_field_t* f = NULL; ib_status_t rc = ib_field_create_dynamic( &f, pool.ib(), name, name_length, static_cast<ib_ftype_t>(type), Hooks::field_dynamic_get, cbdata_get, Hooks::field_dynamic_set, cbdata_set ); throw_if_error(rc); return Field(f); }
int main (void) { GREENTEA_SETUP(20, "default_auto"); Thread thread(send_thread, NULL, osPriorityNormal, STACK_SIZE); bool result = true; int result_counter = 0; while (true) { osEvent evt = queue.get(); if (evt.status == osEventMessage) { message_t *message = (message_t*)evt.value.p; const float expected_voltage = CREATE_VOLTAGE(message->counter); const float expected_current = CREATE_CURRENT(message->counter); // Check using macros if received values correspond to values sent via queue bool expected_values = (expected_voltage == message->voltage) && (expected_current == message->current); result = result && expected_values; const char *result_msg = expected_values ? "OK" : "FAIL"; printf("%3d %.2fV %.2fA ... [%s]\r\n", message->counter, message->voltage, message->current, result_msg); mpool.free(message); if (result == false || ++result_counter == QUEUE_SIZE) { break; } } } GREENTEA_TESTSUITE_RESULT(result); return 0; }
ConstPointer duplicate_poly_if_needed(const std::uint64_t *poly, int coeff_count, int coeff_uint64_count, int new_coeff_count, int new_coeff_uint64_count, bool force, MemoryPool &pool) { #ifdef _DEBUG if (poly == nullptr && coeff_count > 0 && coeff_uint64_count > 0) { throw invalid_argument("poly"); } if (coeff_count < 0) { throw invalid_argument("coeff_count"); } if (coeff_uint64_count < 0) { throw invalid_argument("coeff_uint64_count"); } if (new_coeff_count < 0) { throw invalid_argument("new_coeff_count"); } if (new_coeff_uint64_count < 0) { throw invalid_argument("new_coeff_uint64_count"); } #endif if (!force && coeff_count >= new_coeff_count && coeff_uint64_count == new_coeff_uint64_count) { return ConstPointer::Aliasing(poly); } Pointer allocation = pool.get_for_uint64_count(new_coeff_count * new_coeff_uint64_count); set_poly_poly(poly, coeff_count, coeff_uint64_count, new_coeff_count, new_coeff_uint64_count, allocation.get()); ConstPointer const_allocation; const_allocation.acquire(allocation); return const_allocation; }
int main() { MemoryPool *m = new MemoryPool(4); int *h; int *p=(int*)m->Alloc(); *p = 1; h = p; for (int i = 2; i < 256; i++) { p = (int*)m->Alloc(); *p = i; } for (; h != p; h++) cout << *h << " " << endl; delete m; }
void GCMemoryManager::gc_end() { _accumulated_timer.stop(); _last_gc_stat->set_end_time(Management::timestamp()); int i; // keep the last gc statistics for all memory pools for (i = 0; i < MemoryService::num_memory_pools(); i++) { MemoryPool* pool = MemoryService::get_memory_pool(i); MemoryUsage usage = pool->get_memory_usage(); HS_DTRACE_PROBE8(hotspot, mem__pool__gc__end, name(), strlen(name()), pool->name(), strlen(pool->name()), usage.init_size(), usage.used(), usage.committed(), usage.max_size()); _last_gc_stat->set_after_gc_usage(i, usage); } // Set last collection usage of the memory pools managed by this collector for (i = 0; i < num_memory_pools(); i++) { MemoryPool* pool = get_memory_pool(i); MemoryUsage usage = pool->get_memory_usage(); // Compare with GC usage threshold pool->set_last_collection_usage(usage); LowMemoryDetector::detect_after_gc_memory(pool); } }
void LowMemoryDetector::process_sensor_changes(TRAPS) { ResourceMark rm(THREAD); HandleMark hm(THREAD); // No need to hold Service_lock to call out to Java int num_memory_pools = MemoryService::num_memory_pools(); for (int i = 0; i < num_memory_pools; i++) { MemoryPool* pool = MemoryService::get_memory_pool(i); SensorInfo* sensor = pool->usage_sensor(); SensorInfo* gc_sensor = pool->gc_usage_sensor(); if (sensor != NULL && sensor->has_pending_requests()) { sensor->process_pending_requests(CHECK); } if (gc_sensor != NULL && gc_sensor->has_pending_requests()) { gc_sensor->process_pending_requests(CHECK); } } }