LCOV - code coverage report
Current view: top level - include/Tools/System - memory.hpp (source / functions) Hit Total Coverage
Test: streampu_clean.info Lines: 10 10 100.0 %
Date: 2024-07-31 15:48:41 Functions: 5 5 100.0 %

          Line data    Source code
       1             : /*!
       2             :  * \file
       3             :  * \brief Memory allocation utilities.
       4             :  */
       5             : #ifndef SYSTEM_MEMORY_HPP__
       6             : #define SYSTEM_MEMORY_HPP__
       7             : 
       8             : #include <new>
       9             : 
      10             : namespace spu
      11             : {
      12             : namespace tools
      13             : {
      14             : void*
      15             : mem_alloc(std::size_t size);
      16             : void
      17             : mem_free(void* ptr);
      18             : 
      19             : template<typename T>
      20             : T*
      21      139690 : buffer_alloc(std::size_t nData)
      22             : {
      23      139690 :     T* const ptr = reinterpret_cast<T*>(mem_alloc(nData * sizeof(T)));
      24      139690 :     if (!ptr) throw std::bad_alloc();
      25      139690 :     return ptr;
      26             : }
      27             : 
      28             : template<typename T>
      29             : void
      30      139690 : buffer_free(T* ptr)
      31             : {
      32      139690 :     mem_free(ptr);
      33      139690 : }
      34             : 
      35             : template<class T>
      36             : struct aligned_allocator
      37             : {
      38             :     typedef T value_type;
      39        4821 :     aligned_allocator() {}
      40             :     template<class C>
      41             :     aligned_allocator(const aligned_allocator<C>& other)
      42             :     {
      43             :     }
      44      139690 :     T* allocate(std::size_t n) { return buffer_alloc<T>(n); }
      45      139690 :     void deallocate(T* p, std::size_t n) { buffer_free<T>(p); }
      46             : };
      47             : 
      48             : // Returns true if and only if storage allocated from ma1 can be
      49             : // deallocated from ma2, and vice versa. Always returns true for
      50             : // stateless allocators.
      51             : template<class C1, class C2>
      52             : bool
      53             : operator==(const aligned_allocator<C1>& ma1, const aligned_allocator<C2>& ma2)
      54             : {
      55             :     return true;
      56             : }
      57             : 
      58             : template<class C1, class C2>
      59             : bool
      60             : operator!=(const aligned_allocator<C1>& ma1, const aligned_allocator<C2>& ma2)
      61             : {
      62             :     return !(ma1 == ma2);
      63             : }
      64             : 
      65             : }
      66             : }
      67             : 
      68             : #endif // SYSTEM_MEMORY_HPP__

Generated by: LCOV version 1.14