comparison foosdk/sdk/foobar2000/SDK/mem_block_container.h @ 1:20d02a178406 default tip

*: check in everything else yay
author Paper <paper@tflc.us>
date Mon, 05 Jan 2026 02:15:46 -0500
parents
children
comparison
equal deleted inserted replaced
0:e9bb126753e7 1:20d02a178406
1 #pragma once
2
3 #include "filesystem.h"
4
5 //! Generic interface for a memory block; used by various other interfaces to return memory blocks while allowing caller to allocate.
6 class NOVTABLE mem_block_container {
7 public:
8 virtual const void * get_ptr() const = 0;
9 virtual void * get_ptr() = 0;
10 virtual t_size get_size() const = 0;
11 virtual void set_size(t_size p_size) = 0;
12
13 void from_stream(stream_reader * p_stream,t_size p_bytes,abort_callback & p_abort);
14
15 void set(const void * p_buffer,t_size p_size);
16 void set(const mem_block_container & source) {copy(source);}
17 template<typename t_source> void set(const t_source & source) {
18 PFC_STATIC_ASSERT( sizeof(source[0]) == 1 );
19 set(source.get_ptr(), source.get_size());
20 }
21
22 inline void copy(const mem_block_container & p_source) {set(p_source.get_ptr(),p_source.get_size());}
23 inline void reset() {set_size(0);}
24
25 const mem_block_container & operator=(const mem_block_container & p_source) {copy(p_source);return *this;}
26
27 void resize(size_t v) { set_size(v); }
28 size_t size() const { return get_size(); }
29 protected:
30 mem_block_container() {}
31 ~mem_block_container() {}
32 };
33
34 //! mem_block_container implementation.
35 template<template<typename> class t_alloc = pfc::alloc_standard>
36 class mem_block_container_impl_t : public mem_block_container {
37 public:
38 const void* get_ptr() const override { return m_data.get_ptr(); }
39 void* get_ptr() override { return m_data.get_ptr(); }
40 t_size get_size() const override { return m_data.get_size(); }
41 void set_size(t_size p_size) override { m_data.set_size(p_size); }
42 private:
43 pfc::array_t<t_uint8, t_alloc> m_data;
44 };
45
46 class mem_block_container_impl : public mem_block_container {
47 public:
48 const void * get_ptr() const override {return m_data.ptr();}
49 void * get_ptr() override {return m_data.ptr();}
50 t_size get_size() const override {return m_data.size();}
51 void set_size(t_size p_size) override {m_data.resize(p_size);}
52
53 pfc::mem_block m_data;
54 };
55
56
57 template<unsigned alignBytes = 16> class mem_block_container_aligned_impl : public mem_block_container {
58 public:
59 const void * get_ptr() const {return m_data.get_ptr();}
60 void * get_ptr() {return m_data.get_ptr();}
61 t_size get_size() const {return m_data.get_size();}
62 void set_size(t_size p_size) {m_data.set_size(p_size);}
63 private:
64 pfc::mem_block_aligned<16> m_data;
65 };
66
67 template<unsigned alignBytes = 16> class mem_block_container_aligned_incremental_impl : public mem_block_container {
68 public:
69 mem_block_container_aligned_incremental_impl() : m_size() {}
70 const void * get_ptr() const {return m_data.get_ptr();}
71 void * get_ptr() {return m_data.get_ptr();}
72 t_size get_size() const {return m_size;}
73 void set_size(t_size p_size) {
74 if (m_data.size() < p_size) {
75 m_data.resize( pfc::multiply_guarded<size_t>(p_size, 3) / 2 );
76 }
77 m_size = p_size;
78 }
79 private:
80 pfc::mem_block_aligned<16> m_data;
81 size_t m_size;
82 };
83
84 class mem_block_container_temp_impl : public mem_block_container {
85 public:
86 mem_block_container_temp_impl(void * p_buffer,t_size p_size) : m_size(0), m_buffer_size(p_size), m_buffer(p_buffer) {}
87 const void * get_ptr() const {return m_buffer;}
88 void * get_ptr() {return m_buffer;}
89 t_size get_size() const {return m_size;}
90 void set_size(t_size p_size) {if (p_size > m_buffer_size) throw pfc::exception_overflow(); m_size = p_size;}
91 private:
92 t_size m_size,m_buffer_size;
93 void * m_buffer;
94 };
95
96 template<typename t_ref>
97 class mem_block_container_ref_impl : public mem_block_container {
98 public:
99 mem_block_container_ref_impl(t_ref & ref) : m_ref(ref) {
100 PFC_STATIC_ASSERT( sizeof(ref[0]) == 1 );
101 }
102 const void * get_ptr() const {return m_ref.get_ptr();}
103 void * get_ptr() {return m_ref.get_ptr();}
104 t_size get_size() const {return m_ref.get_size();}
105 void set_size(t_size p_size) {m_ref.set_size(p_size);}
106 private:
107 t_ref & m_ref;
108 };