Debian Codegraph @

modules/lsp-runtime-lib/src/main/io/OutMemoryStream.cpp

raw
1 /*
2 * Copyright (C) 2024 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2024 Vladimir Sadovnikov <[email protected]>
4 *
5 * This file is part of lsp-runtime-lib
6 * Created on: 19 авг. 2019 г.
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 #include <lsp-plug.in/io/OutMemoryStream.h>
23 #include <lsp-plug.in/stdlib/string.h>
24 #include <stdlib.h>
25
26 namespace lsp
27 {
28 namespace io
29 {
30
31 OutMemoryStream::OutMemoryStream()
32 {
33 pData = NULL;
34 nSize = 0;
35 nCapacity = 0;
36 nQuantity = 0x1000;
37 nPosition = 0;
38 }
39
40 OutMemoryStream::OutMemoryStream(size_t quantity)
41 {
42 pData = NULL;
43 nSize = 0;
44 nCapacity = 0;
45 nQuantity = quantity;
46 nPosition = 0;
47 }
48
49 OutMemoryStream::~OutMemoryStream()
50 {
51 drop();
52 }
53
54 wssize_t OutMemoryStream::position()
55 {
56 return nPosition;
57 }
58
59 ssize_t OutMemoryStream::write(const void *buf, size_t count)
60 {
61 size_t sz = nPosition + count;
62 status_t res = reserve(sz);
63 if (res != STATUS_OK)
64 return -res;
65
66 // Append data
67 ::memcpy(&pData[nPosition], buf, count);
68 nPosition = sz;
69 if (nSize < sz)
70 nSize = sz;
71
72 set_error(STATUS_OK);
73 return count;
74 }
75
76 ssize_t OutMemoryStream::writeb(int v)
77 {
78 status_t res = reserve(nPosition + 1);
79 if (res != STATUS_OK)
80 return -res;
81
82 pData[nPosition++] = v;
83 if (nSize < nPosition)
84 nSize = nPosition;
85 return 1;
86 }
87
88 status_t OutMemoryStream::write_byte(int v)
89 {
90 status_t res = reserve(nPosition + 1);
91 if (res != STATUS_OK)
92 return STATUS_NO_MEM;
93
94 pData[nPosition++] = v;
95 if (nSize < nPosition)
96 nSize = nPosition;
97 return STATUS_OK;
98 }
99
100 wssize_t OutMemoryStream::seek(wsize_t position)
101 {
102 if (position > nSize)
103 position = nSize;
104 set_error(STATUS_OK);
105 return nPosition = position;
106 }
107
108 status_t OutMemoryStream::flush()
109 {
110 return set_error(STATUS_OK);
111 }
112
113 uint8_t *OutMemoryStream::release()
114 {
115 uint8_t *data = pData;
116 pData = NULL;
117 nSize = 0;
118 nCapacity = 0;
119 nPosition = 0;
120
121 return data;
122 }
123
124 void OutMemoryStream::drop()
125 {
126 if (pData != NULL)
127 ::free(pData);
128 pData = NULL;
129 nSize = 0;
130 nCapacity = 0;
131 nPosition = 0;
132 }
133
134 void OutMemoryStream::clear()
135 {
136 nSize = 0;
137 nPosition = 0;
138 }
139
140 bool OutMemoryStream::reduce(size_t size)
141 {
142 if (nSize <= size)
143 return false;
144
145 nSize = size;
146 if (nPosition > size)
147 nPosition = size;
148
149 return true;
150 }
151
152 status_t OutMemoryStream::reserve(size_t amount)
153 {
154 if (amount <= nCapacity)
155 return set_error(STATUS_OK);
156
157 size_t ncap = ((amount + nQuantity - 1) / nQuantity) * nQuantity; // Quantify capacity
158 uint8_t *p = reinterpret_cast<uint8_t *>(::realloc(pData, ncap));
159 if (p == NULL)
160 return set_error(STATUS_NO_MEM);
161 pData = p;
162 nCapacity = ncap;
163 return set_error(STATUS_OK);
164 }
165
166 status_t OutMemoryStream::close()
167 {
168 return set_error(STATUS_OK);
169 }
170
171 } /* namespace io */
172 } /* namespace lsp */