acme
Foundational cross-platform layer of the ca2 framework
Loading...
Searching...
No Matches
memory_base.h
1#pragma once
2
3
4//#include "acme/filesystem/file/serializable.h"
5//#include "acme/memory/memory.h"
6
7class bstring_manager;
9class memory;
10class shared_memory;
11class paged_memory;
13
14
15enum enum_memory
16{
17
18 e_memory_none,
19 e_memory_primitive,
20 e_memory_shared,
21 e_memory_virtual,
22
23};
24
25
26class CLASS_DECL_ACME MEMORY :
27 public block
28{
29public:
30
31
32 bool m_bOwner;
33 bool m_bReadOnly;
34 ::u8 * m_beginStorage;
35 memsize m_sizeStorage;
36 ::f64 m_dAllocationRateUp;
37 ::u32 m_dwAllocationAddUp;
38
39 memory_container * m_pcontainer;
40
41 enum_memory m_ememory;
42
43 read_only_memory * m_preadonlymemory;
44 memory * m_pprimitivememory;
45 shared_memory * m_psharedmemory;
46 paged_memory * m_pvirtualmemory;
47
48 MEMORY();
49 MEMORY(const MEMORY & memory) { _assign(memory); }
50 MEMORY(MEMORY && memory) { _assign(memory); zero(memory); }
51
52 MEMORY & operator = (const MEMORY & memory) { assign(memory); return *this; }
53
54 void _assign(const MEMORY & memory)
55 {
56
57 memory_copy(this, &memory, sizeof(MEMORY));
58
59 }
60
61
62 void assign(const MEMORY & memory)
63 {
64
65 if (this != &memory)
66 {
67
68 _assign(memory);
69
70 }
71
72 }
73
74 ::u8 * storage_begin() { return m_beginStorage; }
75 const ::u8 * storage_begin() const { return m_beginStorage; }
76 memsize storage_size() const { return maximum(0,m_sizeStorage); }
77 ::u8 * storage_end() { return storage_begin() + storage_size(); }
78 const ::u8 * storage_end() const { return storage_begin() + storage_size(); }
79 memsize offset() const { return m_begin - m_beginStorage; }
80 memsize max_offset() const { return storage_size(); }
81
82};
83
84
85template < typename MUTABLE >
86MUTABLE * __mutable(const MUTABLE * pmutable) { return (MUTABLE *)pmutable; }
87
88
89class CLASS_DECL_ACME memory_base :
90 public MEMORY,
91 virtual public ::subparticle
92{
93public:
94
95 typedef bstring_manager manager;
96
97 typedef ::u8 value_type;
98
99
100 //MEMORY m_memory;
101
102#if MEMDLEAK
103
104 string m_strTag;
105
106 ::i32 m_iLine;
107
108#endif
109
110
111 memory_base() { }
112 memory_base(const memory_base & base) : MEMORY(base) {}
113 memory_base(memory_base && base) : MEMORY(::transfer(base)) {}
114 ~memory_base() override;
115
116
117 using ::subparticle::clear_flag;
118 using MEMORY::operator();
119
120
121 virtual string as_utf8() const;
122
123 //virtual char_pointer c_str();
124
125 //using MEMORY::begins;
126 //virtual bool begins(const ::scoped_string & scopedstr) const { return this->begins((const BLOCK &) scopedstr); }
127 //virtual bool begins(const ::scoped_string & scopedstr, character_count iCount = -1) const;
128
129 virtual bool begins_eat(const BLOCK & block);
130
131 //virtual bool case_insensitive_begins(const ::scoped_string & scopedstr, character_count iCount = -1) const{ return ((::const_ansi_range *)this)->case_insensitive_begins(scopedstr); }
132 //virtual bool begins(const ::scoped_string & scopedstr, character_count iCount = -1) const;
133 //virtual bool case_insensitive_begins(const ::scoped_string & scopedstr, character_count iCount = -1) const;
134
135 //using MEMORY::ends;
136 //virtual bool ends(const ::scoped_string & scopedstr) const { return this->ends((const BLOCK &) scopedstr); }
137 //virtual bool ends(const ::scoped_string & scopedstr, character_count iCount = -1) const;
138 //virtual bool case_insensitive_ends(const ::scoped_string & scopedstr) const { return ((::const_ansi_range *)this)->case_insensitive_ends(scopedstr); }
139
140 virtual bool ends_eat(const BLOCK & block);
141 //virtual bool ends(const ::scoped_string & scopedstr, character_count iCount = -1) const;
142 //virtual bool case_insensitive_ends(const ::scoped_string & scopedstr, character_count iCount = -1) const;
143
144 virtual memory detach_as_primitive_memory();
145 //virtual ::u8 * detach_virtual_memory();
146 //virtual HGLOBAL detach_shared_memory();
147
148 //bool is_enabled() const;
149 //bool is_locked() const;
150
151 inline void defer_set_size(memsize sizeLarger) { if (sizeLarger > size()) return set_size(sizeLarger); }
152 virtual void set_size(memsize dwNewLength);
153 virtual void allocate_internal(memsize dwNewLength);
154
155 inline void truncate(memsize sizeSmaller) { if (sizeSmaller < size()) return set_size(sizeSmaller); }
156
157 virtual ::u8 * impl_alloc(memsize dwAllocation);
158 virtual ::u8 * impl_realloc(void * pdata,memsize dwAllocation);
159 virtual void impl_free(::u8 * pdata);
160
161 virtual void reserve(memsize dwNewLength);
162
163
164 virtual void erase_offset();
165
166 //virtual void random_bytes(::collection::count c = -1);
167
168
169 //virtual ::stream & write(::stream & stream) const override;
170 //virtual ::stream & read(::stream & stream) override;
171
172
173 virtual memsize calc_allocation(memsize size);
174
175
176 //virtual void transfer_to(::file::file * pfileOut, memsize uiBufferSize = 1024 * 1024) const;
177 virtual void assign_file(::file::file * pfileIn, memsize uiBufferSize = 1024 * 1024);
178 virtual void assign_entire_file(::file::file * pfileIn, memsize uiBufferSize = 1024 * 1024);
179
180 virtual void append_file(::file::file * pfileIn, memsize uiBufferSize = 1024 * 1024);
181 virtual void append_entire_file(::file::file * pfileIn, memsize uiBufferSize = 1024 * 1024);
182
183
184 void allocate_add_up(memsize iAddUp);
185
186
187 ::subparticle_pointer clone() override;
188
189
190// inline ::u8 * storage_begin() { return m_memory.storage_begin(); }
191// inline const ::u8 * storage_begin() const { return m_memory.storage_begin(); }
192// inline ::u8 * storage_end() { return m_memory.storage_end(); }
193// inline const ::u8 * storage_end() const { return m_memory.storage_end(); }
194// inline memsize storage_size() const { return m_memory.storage_size(); }
195
196
197 //inline memsize size() const { return m_memory.size(); }
198 //inline const ::u8 * data() const { return m_memory.data(); }
199 //inline ::u8 * data() { return m_memory.data(); }
200
201 //inline memsize size() const;
202 //inline ::u8 * data() const;
203 //inline ::u8 * data();
204
205//
206// inline const ::u8 * begin() const { return data(); }
207// inline ::u8 * begin() { return data(); }
208// inline const ::u8 * end() const { return m_memory.end(); }
209// inline ::u8 * end() { return m_memory.end(); }
210
211 //inline const_char_pointer c_str() const { return (const_char_pointer )data(); }
212 //inline char_pointer sz() { return (char_pointer )data(); }
213
214 ::block & block() { return *this; }
215 const ::block & block() const { return *this; }
216
217 inline bool is_set() const { return MEMORY::is_set(); }
218 inline bool is_empty() const { return MEMORY::is_empty(); }
219 inline bool has_data() const { return this->size() > 0; }
220
221 inline ::u8 operator [] (::collection::index i) const { return data()[i]; }
222 inline ::u8 & operator [] (::collection::index i) { return data()[i]; }
223
224 inline operator const ::u8 * () const { return data(); }
225 inline operator ::u8 * () { return data(); }
226
227 inline operator const void * () const { return data(); }
228 inline operator void * () { return data(); }
229
230
231 template < prototype_character CHARACTER >
232 range < CHARACTER * > get_buffer(character_count len)
233 {
234
235 set_size((len + 1) * sizeof(CHARACTER));
236
237 auto p = (CHARACTER *)begin();
238
239 p[len] = 0;
240
241 return { p, len, true };
242
243 }
244
245
246 memory_base & operator = (const ::memory_base & memory) { assign(memory.data(), memory.size()); return *this; }
247 memory_base & operator = (const ::block & block) { assign(block.data(), block.size()); return *this; }
248
249
250 memory_base & operator += (const ::memory_base & memory) { append(memory.data(), memory.size()); return *this; }
251 memory_base & operator += (const ::block & block) { append(block.data(), block.size()); return *this; }
252
253
254 void from_string(const ::wide_character * pwsz);
255 void from_string(const ::scoped_string & scopedstr);
256 void from_string(const ::string & str);
257 void from_string(const ::payload & payload);
258 void append_from_string(const ::wide_character * pwsz);
259 void append_from_string(const ::scoped_string & scopedstr);
260 void append_from_string(const ::string & str);
261 void append_from_string(const ::payload & payload);
262 void append_byte(::u8 b){ append(&b, 1);}
263 //::string as_string(memsize iStart = 0, memsize uiSize = -1) const;
264 virtual string get_string() const;
265 virtual string get_string(memsize iStart, memsize uiSize = -1) const;
266
267 void delete_begin(memsize iSize);
268 void eat_begin(const void * pdata, memsize iSize);
269 void set_data(const void * pdata, memsize uiSize);
270 void set_at(::collection::index i, const void * pdata, memsize uiSize);
271 void copy_from(const memory_base * pstorage);
272 void copy_from(const void * pdata, memsize s);
273 void copy_to(void * pdata, memsize s) const;
274 void set(::u8 b,memsize iStart = 0,memsize uiSize = -1);
275 void zero(memsize iStart = 0, memsize uiSize = -1);
276
277
278 void assign(const ::block & block);
279 void assign(const void * pdata, memsize iCount);
280 void assign(const void * pdata, memsize iStart, memsize iCount);
281 void assign(memsize iCount, uchar uch);
282
283
284 void append(const ::block & block);
285 void append(const void * pdata, memsize iCount);
286 void append(memsize iCount, uchar uch);
287 void append(const memory_base & memory, memsize iStart = 0, memsize iCount = -1);
288 void append(::u8 b);
289
290 void clear();
291
292 void splice(const ::u8 * pbMemory, memsize iCountSrc, memsize iStartDst = 0, memsize iCountDst = 0);
293 void splice(const memory_base & memory, memsize iStartDst = 0, memsize iCountDst = 0, memsize iStartSrc = 0, memsize iCountSrc = -1);
294 ::memory slice(memsize start, memsize count);
295 ::memory left(memsize count);
296 ::memory right(memsize count);
297
298 void move_and_grow(memsize offset);
299 void transfer(memsize offset, bool bGrow = false);
300
301 //void assign(const ::scoped_string & scopedstr);
302 //void append(const ::scoped_string & scopedstr);
303
304 void to_hex(string & str, memsize iStart = 0, memsize size = -1);
305 string to_hex(memsize iStart = 0, memsize size = -1);
306 character_count from_hex(const ::block & block);
307
308 void to_base64(string & str, memsize iStart = 0, memsize size = -1);
309 string to_base64(memsize iStart = 0, memsize size = -1);
310 void from_base64(const ::scoped_string & scopedstr);
311
312 inline void to_asc(string & str) const { str.assign((const_char_pointer )data(), size()); }
313 inline string to_asc() const { string str; to_asc(str); return str; }
314 inline void from_asc(const ::scoped_string & scopedstr) { assign(scopedstr.data(), scopedstr.size()); }
315
316 bool operator == (const memory_base & s) const;
317 bool operator == (const struct block & block) const ;
318
319
320 memory_base & reverse();
321
322 memory_base & prefix_der_uint_content();
323 memory_base & prefix_der_length();
324 memory_base & prefix_der_type(::i32 iType);
325
326 memory_base & prefix_der_uint();
327 memory_base & prefix_der_sequence();
328
329 memsize get_length() const;
330 memsize length() const;
331
332 inline void set_char_at_grow(character_count iChar, ::i8 ch);
333
334
335 ::u8 * find(const ::block& block, ::collection::index iStart = 0) const;
336 ::collection::index find_index(const ::block& block, ::collection::index iStart = 0) const;
337 ::collection::index find_index(::i8 ch, ::collection::index iStart = 0) const;
338 ::u8* rear_find(const ::block& block, ::collection::index iStart = 0) const;
339 ::collection::index reverse_find_index(const ::block& block, ::collection::index iStart = 0) const;
340 ::u8* reverse_find_byte_not_in_block(const ::block& block, ::collection::index iStart = 0) const;
341 ::collection::index reverse_find_index_of_byte_not_in_block(const ::block& block, ::collection::index iStart = 0) const;
342
343 ::u8 * find_line_prefix(const ::block& blockPrefix, ::collection::index iStart = 0);
344 ::collection::index find_line_prefix_index(const ::block& blockPrefix, ::collection::index iStart = 0);
345 void patch_line_suffix(const ::block & blockPrefix, const ::block& blockSuffix, ::collection::index iStart = 0);
346
347
348#if defined(UNIVERSAL_WINDOWS) && defined(__cplusplus_winrt)
349
350 inline Array < uchar, 1U > ^ get_os_bytes(memsize pos = 0, memsize size = -1) const;
351 inline ::winrt::Windows::Storage::Streams::IBuffer ^ get_os_crypt_buffer(memsize pos = 0, memsize size = -1) const;
352 inline ::winrt::Windows::Storage::Streams::IBuffer ^ get_os_buffer(memsize pos = 0, memsize size = -1) const;
353 inline void set_os_bytes(Array < uchar, 1U > ^ a, memsize pos = 0, memsize size = -1);
354 inline void set_os_crypt_buffer(::winrt::Windows::Storage::Streams::IBuffer ^ ibuf, memsize pos = 0, memsize size = -1);
355 inline void set_os_buffer(::winrt::Windows::Storage::Streams::IBuffer ^ ibuf, memsize pos = 0, memsize size = -1);
356
357#endif
358
359//#if defined(__APPLE__)
360//
361// CFDataRef get_os_cf_data(memsize pos = 0, memsize size = -1) const;
362// void set_os_cf_data(CFDataRef data, memsize pos = 0, memsize size = -1);
363//
364//#endif
365
366
367
368 memory_base & erase(memsize pos = 0,memsize len = -1);
369
370 //void fread(FILE * pfile);
371
372
373};
374
375
376//inline ::u8 * memory_base::internal_get_data() const
377//{
378// return (::u8 *) m_memory.begin();
379//}
380//inline memsize memory_base::size() const
381//{
382// return size();
383//}
384//inline ::u8 * memory_base::data() const
385//{
386// return (::u8*)m_memory.m_pbegin;
387//}
388//inline ::u8 * memory_base::data()
389//{
390// return (::u8*)m_memory.m_pbegin;
391//}
392//inline ::u8 * memory_base::data() const
393//{
394// return data();
395//}
396//inline ::u8 * memory_base::data()
397//{
398// return data();
399//}
400//inline bool memory_base::has_data() const
401//{
402// return size() > 0;
403//}
404//inline bool memory_base::is_empty() const
405//{
406// return !has_data();
407//}
408
409
410//inline uchar memory_base::operator [] (::collection::index i) const
411//{
412// return this->data()[i];
413//}
414//inline uchar & memory_base::operator [] (::collection::index i)
415//{
416// return this->data()[i];
417//}
418
419
420//inline memory_base::operator const ::u8 *() const
421//{
422// return this->data();
423//}
424//inline memory_base::operator ::u8 *()
425//{
426// return this->data();
427//}
428//inline memory_base::operator const void *() const
429//{
430// return this->data();
431//}
432//inline memory_base::operator void *()
433//{
434// return this->data();
435//}
436
437inline void memory_base::allocate_add_up(memsize iAddUp)
438{
439
440 if (iAddUp != 0)
441 {
442
443 set_size(size() + iAddUp);
444
445 }
446
447}
448
449inline void memory_base::splice(const memory_base & memory, memsize iStartDst, memsize iCountDst, memsize iStartSrc, memsize iCountSrc)
450{
451
452 splice(
453 &memory.data()[iStartSrc],
454 minimum(memory.size(), iCountSrc < 0 ? memory.size() - iCountSrc + 1 : iCountSrc),
455 iStartDst,
456 iCountDst);
457
458}
459
Definition file.h:33
Definition memory_base.h:92
Definition memory_container.h:15
Definition memory.h:33
Definition paged_memory.h:9
Definition payload.h:54
Definition read_only_memory.h:9
Definition shared_memory.h:15