kfr

Fast, modern C++ DSP framework, FFT, Sample Rate Conversion, FIR/IIR/Biquad Filters (SSE, AVX, AVX-512, ARM NEON)
Log | Files | Refs | README

commit 843aa4a0b8db0381618f5b6bc75eabd4a1c76a4a
parent be64c6dae04e7bbd7de11435e1c4f196f0aff5c9
Author: [email protected] <[email protected]>
Date:   Fri, 31 Mar 2017 23:27:19 +0300

Memory allocated using aligned_malloc is now shared

Diffstat:
Minclude/kfr/base/memory.hpp | 21++++++++++++++++-----
1 file changed, 16 insertions(+), 5 deletions(-)

diff --git a/include/kfr/base/memory.hpp b/include/kfr/base/memory.hpp @@ -56,6 +56,7 @@ struct mem_header u8 alignment; u8 reserved1; u8 reserved2; + std::atomic_uint references; size_t size; } #ifdef CMT_GNU_ATTRIBUTES @@ -74,19 +75,29 @@ inline void* aligned_malloc(size_t size, size_t alignment) void* ptr = malloc(size + (alignment - 1) + sizeof(mem_header)); if (ptr == nullptr) return nullptr; - void* aligned_ptr = advance(ptr, sizeof(mem_header)); - aligned_ptr = align_up(aligned_ptr, alignment); - aligned_header(aligned_ptr)->alignment = static_cast<u8>(alignment > 255 ? 255 : alignment); - aligned_header(aligned_ptr)->offset = static_cast<u8>(distance(aligned_ptr, ptr)); - aligned_header(aligned_ptr)->size = size; + void* aligned_ptr = advance(ptr, sizeof(mem_header)); + aligned_ptr = align_up(aligned_ptr, alignment); + aligned_header(aligned_ptr)->alignment = static_cast<u8>(alignment > 255 ? 255 : alignment); + aligned_header(aligned_ptr)->offset = static_cast<u8>(distance(aligned_ptr, ptr)); + aligned_header(aligned_ptr)->references = 1; + aligned_header(aligned_ptr)->size = size; return aligned_ptr; } + inline void aligned_free(void* ptr) { get_memory_statistics().deallocation_count++; get_memory_statistics().deallocation_size += aligned_size(ptr); free(advance(ptr, -static_cast<ptrdiff_t>(aligned_header(ptr)->offset))); } + +inline void aligned_add_ref(void* ptr) { aligned_header(ptr)->references++; } + +inline void aligned_release(void* ptr) +{ + if (--aligned_header(ptr)->references == 0) + aligned_free(ptr); +} } template <typename T = void, size_t alignment = platform<>::native_cache_alignment>