MemoryPool ========== .. cpp:class:: numpy::MemoryPool Memory management class for efficient array allocation. Example ------- .. code-block:: cpp #include using namespace numpy; // Use MemoryPool MemoryPool obj; // ... operations ... Indexing / Selection -------------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``size_t get_allocation_size(size_trequested_size)`` - size_t - NP_MEMORY_POOL.H:52 - * - ``Statistics get_statistics()`` - Statistics - NP_MEMORY_POOL.H:150 - :ref:`View ` Logical ------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``T \* allocate(size_tcount)`` - T \* - NP_MEMORY_POOL.H:75 - :ref:`View ` Other Methods ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``void clear()`` - void - NP_MEMORY_POOL.H:115 - :ref:`View ` * - ``void compact()`` - void - NP_MEMORY_POOL.H:123 - * - ``void deallocate(T \*ptr)`` - void - NP_MEMORY_POOL.H:100 - :ref:`View ` Code Examples ------------- The following examples are extracted from the test suite. .. _example-memorypool-get_statistics-0: .. dropdown:: get_statistics (np_test_2_all.cpp:4654) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4644 :emphasize-lines: 11 std::cout << " -> tests passed" << std::endl; } void test_memory_statistics() { std::cout << "========= test_memory_statistics ======================="; MemoryPool pool; // Check initial statistics auto initial_stats = pool.get_statistics(); if (!(initial_stats.total_allocated_bytes == 0)) { std::string description = std::string("test_memory_statistics():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(initial_stats.total_allocated_bytes == 0)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(initial_stats.total_requested_bytes == 0)) { std::string description = std::string("test_memory_statistics():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(initial_stats.total_requested_bytes == 0)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } .. _example-memorypool-allocate-1: .. dropdown:: allocate (np_test_2_all.cpp:4418) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4408 :emphasize-lines: 11 using namespace numpy::memory; namespace test_memory_pool { void test_memory_pool_basic_allocate() { std::cout << "========= test_memory_pool_basic_allocate ======================="; MemoryPool pool; // Test basic allocation int* ptr1 = pool.allocate(100); if (!(ptr1 != nullptr)) { std::string description = std::string("test_memory_pool_basic_allocate():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(ptr1 != nullptr)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // Test writing to allocated memory for (int i = 0; i < 100; ++i) { ptr1[i] = i; } .. _example-memorypool-clear-2: .. dropdown:: clear (np_test_2_all.cpp:4161) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4151 :emphasize-lines: 11 auto array = createFloat32Array({ 10, 10 }, static_cast(i)); temp_arrays.push_back(array); auto view = array.view(); temp_views.push_back(view); } // std::cout << "Created 100 arrays and 100 views" << std::endl; // Clear all arrays (views will keep memory alive) temp_arrays.clear(); // std::cout << "Cleared original arrays" << std::endl; // Verify views still work if (!(temp_views[50].getElementAt({ 5, 5 }) == 50.0f)) { std::string description = std::string("testMemoryLeakPrevention():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(temp_views[50].getElementAt({ 5, 5 }) == 50.0f)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // std::cout << "[OK] Views still valid after originals cleared"; .. _example-memorypool-deallocate-3: .. dropdown:: deallocate (np_test_2_all.cpp:4439) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4429 :emphasize-lines: 11 // Verify data integrity for (int i = 0; i < 100; ++i) { if (!(ptr1[i] == i)) { std::string description = std::string("test_memory_pool_basic_allocate():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(ptr1[i] == i)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } } pool.deallocate(ptr1); // std::cout << "Basic memory pool allocation test passed" << std::endl; std::cout << " -> tests passed" << std::endl; } void test_memory_pool_deallocate() { std::cout << "========= test_memory_pool_deallocate ======================="; MemoryPool pool;