Contents
|
|
If neither weak handles nor strong handles reference a memory block, then it is deleted immediately. To access a cached memory block again, the weak handle is simply assigned to a strong handle. This causes the removal of the memory block from the cache list automatically.
If the accumulated sizes of the memory blocks in the cache list exceed the "cache and locked memory size limit", then memory blocks are removed from the cache list and deleted, according to the least-recently-used principle.
The image below shows that the cache list contains only those memory blocks that are exclusively referenced by weak handles. The memory blocks, which exist outside the cache list, are always referenced by at least one strong handle and may also be referenced by weak handles.
Relations between cache list, locked memory, memory blocks and handles
MLMemoryBlockHandle handle = MLMemoryManager::singleton()->allocate(id, size);
MLMemoryBlockHandle handle; // ... int* data = static_cast<int*>(handle.data()); for (size_t i=0; i<handle.size(); ++i) { int readValue = data[i]; data[i] = readValue+1; }
MLWeakMemoryBlockHandle weakHandle; { MLMemoryBlockHandle handle; // ... // Assign the handle to the weak handle to release it, // i.e. the memory block is appended to the cache list. weakHandle = handle; } // Now only the weak memory handle exists
MLWeakMemoryBlockHandle weakHandle; // ... MLMemoryBlockHandle handle = weakHandle; if (!handle.isNull()) { // The data still exists and can be used } else { // The data was deleted while it was cached, so allocate a new memory block handle = MLMemoryManager::singleton()->allocate(id, size); }
void deleteMemoryCallBack(void* data, size_t sizeIsNotUsed, void* userDataIsNotUsed) { delete [] static_cast<MyCustomObject*>(data); } int main(int argc, char** argv) { // ... MyCustomObject* data = new MyCustomObject[1000]; MLMemoryManager::singleton()->addAllocatedMemory(id, data, size, deleteMemoryCallBack, NULL); // ... }
MLMemoryManager::initialize(); // ... MLMemoryManager::deinitialize();
MLMemoryManager::singleton()->setCacheAndLockedMemorySizeLimit(newLimit);
1.5.8