MemoryPool#
-
class numpy::MemoryPool#
Memory management class for efficient array allocation.
Example#
#include <numpy/np_ndarray.h>
using namespace numpy;
// Use MemoryPool
MemoryPool obj;
// ... operations ...
Indexing / Selection#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
size_t |
NP_MEMORY_POOL.H:52 |
|
|
Statistics |
NP_MEMORY_POOL.H:150 |
Logical#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
T * |
NP_MEMORY_POOL.H:75 |
Other Methods#
Code Examples#
The following examples are extracted from the test suite.
get_statistics (np_test_2_all.cpp:4654)
4644 std::cout << " -> tests passed" << std::endl;
4645 }
4646
4647 void test_memory_statistics() {
4648 std::cout << "========= test_memory_statistics =======================";
4649
4650 MemoryPool<int> pool;
4651
4652 // Check initial statistics
4653 auto initial_stats = pool.get_statistics();
4654 if (!(initial_stats.total_allocated_bytes == 0)) {
4655 std::string description = std::string("test_memory_statistics():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(initial_stats.total_allocated_bytes == 0)";
4656 std::cout << std::string("[FAIL] ") + description << std::endl;
4657 throw std::runtime_error(description);
4658 }
4659 if (!(initial_stats.total_requested_bytes == 0)) {
4660 std::string description = std::string("test_memory_statistics():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(initial_stats.total_requested_bytes == 0)";
4661 std::cout << std::string("[FAIL] ") + description << std::endl;
4662 throw std::runtime_error(description);
4663 }
allocate (np_test_2_all.cpp:4418)
4408 using namespace numpy::memory;
4409
4410 namespace test_memory_pool {
4411
4412 void test_memory_pool_basic_allocate() {
4413 std::cout << "========= test_memory_pool_basic_allocate =======================";
4414
4415 MemoryPool<int> pool;
4416
4417 // Test basic allocation
4418 int* ptr1 = pool.allocate(100);
4419 if (!(ptr1 != nullptr)) {
4420 std::string description = std::string("test_memory_pool_basic_allocate():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(ptr1 != nullptr)";
4421 std::cout << std::string("[FAIL] ") + description << std::endl;
4422 throw std::runtime_error(description);
4423 }
4424
4425 // Test writing to allocated memory
4426 for (int i = 0; i < 100; ++i) {
4427 ptr1[i] = i;
4428 }
clear (np_test_2_all.cpp:4161)
4151 auto array = createFloat32Array({ 10, 10 }, static_cast<float>(i));
4152 temp_arrays.push_back(array);
4153
4154 auto view = array.view();
4155 temp_views.push_back(view);
4156 }
4157
4158 // std::cout << "Created 100 arrays and 100 views" << std::endl;
4159
4160 // Clear all arrays (views will keep memory alive)
4161 temp_arrays.clear();
4162 // std::cout << "Cleared original arrays" << std::endl;
4163
4164 // Verify views still work
4165 if (!(temp_views[50].getElementAt({ 5, 5 }) == 50.0f)) {
4166 std::string description = std::string("testMemoryLeakPrevention():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(temp_views[50].getElementAt({ 5, 5 }) == 50.0f)";
4167 std::cout << std::string("[FAIL] ") + description << std::endl;
4168 throw std::runtime_error(description);
4169 }
4170 // std::cout << "[OK] Views still valid after originals cleared";
deallocate (np_test_2_all.cpp:4439)
4429 // Verify data integrity
4430 for (int i = 0; i < 100; ++i) {
4431 if (!(ptr1[i] == i)) {
4432 std::string description = std::string("test_memory_pool_basic_allocate():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(ptr1[i] == i)";
4433 std::cout << std::string("[FAIL] ") + description << std::endl;
4434 throw std::runtime_error(description);
4435 }
4436 }
4437
4438 pool.deallocate(ptr1);
4439
4440 // std::cout << "Basic memory pool allocation test passed" << std::endl;
4441
4442 std::cout << " -> tests passed" << std::endl;
4443 }
4444
4445 void test_memory_pool_deallocate() {
4446 std::cout << "========= test_memory_pool_deallocate =======================";
4447
4448 MemoryPool<double> pool;