| 1 |
/* |
| 2 |
* Copyright (C) 2025 Linux Studio Plugins Project <https://lsp-plug.in/> |
| 3 |
* (C) 2025 Vladimir Sadovnikov <[email protected]> |
| 4 |
* |
| 5 |
* This file is part of lsp-runtime-lib |
| 6 |
* Created on: 21 февр. 2023 г. |
| 7 |
* |
| 8 |
* lsp-runtime-lib is free software: you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU Lesser General Public License as published by |
| 10 |
* the Free Software Foundation, either version 3 of the License, or |
| 11 |
* any later version. |
| 12 |
* |
| 13 |
* lsp-runtime-lib is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU Lesser General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Lesser General Public License |
| 19 |
* along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
|
| 22 |
#ifndef LSP_PLUG_IN_IO_INSHAREDMEMORYSTREAM_H_ |
| 23 |
#define LSP_PLUG_IN_IO_INSHAREDMEMORYSTREAM_H_ |
| 24 |
|
| 25 |
#include <lsp-plug.in/runtime/version.h> |
| 26 |
#include <lsp-plug.in/io/IInStream.h> |
| 27 |
#include <lsp-plug.in/io/OutMemoryStream.h> |
| 28 |
|
| 29 |
namespace lsp |
| 30 |
{ |
| 31 |
namespace io |
| 32 |
{ |
| 33 |
/** |
| 34 |
* Input memory stream with shared memory chunk among all owners. |
| 35 |
* The read position is individual for each owner. |
| 36 |
*/ |
| 37 |
class InSharedMemoryStream: public IInStream |
| 38 |
{ |
| 39 |
protected: |
| 40 |
typedef struct shared_data_t |
| 41 |
{ |
| 42 |
uint8_t *pData; |
| 43 |
size_t nSize; |
| 44 |
mutable size_t nRefs; |
| 45 |
lsp_memdrop_t enDrop; |
| 46 |
} shared_data_t; |
| 47 |
|
| 48 |
protected: |
| 49 |
size_t nOffset; |
| 50 |
shared_data_t *pShared; |
| 51 |
|
| 52 |
protected: |
| 53 |
void release_shared(); |
| 54 |
|
| 55 |
public: |
| 56 |
explicit InSharedMemoryStream(); |
| 57 |
|
| 58 |
/** Copy constructor, the current read position becomes the same to the origin |
| 59 |
* @param src source stream to copy |
| 60 |
*/ |
| 61 |
InSharedMemoryStream(const InSharedMemoryStream & src); |
| 62 |
explicit InSharedMemoryStream(const InSharedMemoryStream * src); |
| 63 |
|
| 64 |
/** Move constructor, the current read position becomes the same to the origin |
| 65 |
* @param src source stream to copy |
| 66 |
*/ |
| 67 |
InSharedMemoryStream(InSharedMemoryStream && src); |
| 68 |
|
| 69 |
/** |
| 70 |
* Create memory input stream |
| 71 |
* @param data associated buffer |
| 72 |
* @param size size of buffer |
| 73 |
*/ |
| 74 |
explicit InSharedMemoryStream(const void *data, size_t size); |
| 75 |
|
| 76 |
/** |
| 77 |
* Create memory input stream |
| 78 |
* @param data associated buffer |
| 79 |
* @param size size of buffer |
| 80 |
* @param drop the method how to drop data on close() |
| 81 |
*/ |
| 82 |
explicit InSharedMemoryStream(void *data, size_t size, lsp_memdrop_t drop); |
| 83 |
|
| 84 |
InSharedMemoryStream & operator = (const InSharedMemoryStream &); |
| 85 |
InSharedMemoryStream & operator = (InSharedMemoryStream &&); |
| 86 |
|
| 87 |
virtual ~InSharedMemoryStream() override; |
| 88 |
|
| 89 |
public: |
| 90 |
/** |
| 91 |
* Wrap the memory buffer, drop previous buffer using specified mechanism |
| 92 |
* @param data associated buffer |
| 93 |
* @param size size of buffer |
| 94 |
* @param drop the method how to drop data on close() |
| 95 |
*/ |
| 96 |
status_t wrap(void *data, size_t size, lsp_memdrop_t drop); |
| 97 |
|
| 98 |
/** |
| 99 |
* Wrap the memory buffer, drop previous buffer using specified mechanism |
| 100 |
* @param data associated buffer |
| 101 |
* @param size size of buffer |
| 102 |
*/ |
| 103 |
status_t wrap(const void *data, size_t size); |
| 104 |
|
| 105 |
/** |
| 106 |
* Wrap the data contained inside of another shared memory stream |
| 107 |
* @param src source stream to wrap |
| 108 |
*/ |
| 109 |
void wrap(const InSharedMemoryStream &src); |
| 110 |
void wrap(const InSharedMemoryStream *src); |
| 111 |
|
| 112 |
/** |
| 113 |
* Wrap the data contained inside of another shared memory stream |
| 114 |
* @param src source stream to wrap |
| 115 |
* @param offset initial read offset |
| 116 |
*/ |
| 117 |
void wrap(const InSharedMemoryStream &src, wsize_t offset); |
| 118 |
void wrap(const InSharedMemoryStream *src, wsize_t offset); |
| 119 |
|
| 120 |
/** |
| 121 |
* Get the memory contents |
| 122 |
* @return memory contents |
| 123 |
*/ |
| 124 |
inline const uint8_t *data() const { return (pShared != NULL) ? pShared->pData : NULL; } |
| 125 |
|
| 126 |
/** |
| 127 |
* Obtain the size of memory buffer wrapped by the memory stream |
| 128 |
* @return the size of memory buffer in bytes |
| 129 |
*/ |
| 130 |
inline size_t size() const { return (pShared != NULL) ? pShared->nSize : 0; } |
| 131 |
|
| 132 |
/** |
| 133 |
* Obtain the overall reference count to the wrapped data |
| 134 |
* @return overall reference count to the wrapped data |
| 135 |
*/ |
| 136 |
inline size_t references() const { return (pShared != NULL) ? pShared->nRefs : 0; } |
| 137 |
|
| 138 |
/** |
| 139 |
* Swap contents |
| 140 |
* @param src the shared memory stream to perform swap |
| 141 |
*/ |
| 142 |
void swap(InSharedMemoryStream &src); |
| 143 |
void swap(InSharedMemoryStream *src); |
| 144 |
|
| 145 |
void take(InSharedMemoryStream &src); |
| 146 |
void take(InSharedMemoryStream *src); |
| 147 |
|
| 148 |
status_t take(OutMemoryStream &src); |
| 149 |
status_t take(OutMemoryStream *src); |
| 150 |
|
| 151 |
public: // IInStream |
| 152 |
virtual wssize_t avail() override; |
| 153 |
virtual wssize_t position() override; |
| 154 |
virtual ssize_t read(void *dst, size_t count) override; |
| 155 |
virtual ssize_t read_byte() override; |
| 156 |
virtual wssize_t seek(wsize_t position) override; |
| 157 |
virtual wssize_t skip(wsize_t amount) override; |
| 158 |
virtual status_t close() override; |
| 159 |
}; |
| 160 |
|
| 161 |
} /* namespace io */ |
| 162 |
} /* namespace lsp */ |
| 163 |
|
| 164 |
|
| 165 |
#endif /* LSP_PLUG_IN_IO_INSHAREDMEMORYSTREAM_H_ */ |