API Documentation
Loading...
Searching...
No Matches
MemoryManager.h
Go to the documentation of this file.
1/**--------------------------------------------------------------------------------------------
2Copyright (c) 2019, NDEVR LLC
3tyler.parke@ndevr.org
4 __ __ ____ _____ __ __ _______
5 | \ | | | __ \ | ___|\ \ / / | __ \
6 | \ | | | | \ \ | |___ \ \ / / | |__) |
7 | . \| | | |__/ / | |___ \ V / | _ /
8 | |\ |_|_____/__|_____|___\_/____| | \ \
9 |__| \__________________________________| \__\
10
11Subject to the terms of the Enterprise+ Agreement, NDEVR hereby grants
12Licensee a limited, non-exclusive, non-transferable, royalty-free license
13(without the right to sublicense) to use the API solely for the purpose of
14Licensee's internal development efforts to develop applications for which
15the API was provided.
16
17The above copyright notice and this permission notice shall be included in all
18copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
21INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
22PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
23FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25DEALINGS IN THE SOFTWARE.
26
27Library: Base
28File: MemoryManager
29Included in API: True
30Author(s): Tyler Parke
31 *-----------------------------------------------------------------------------------------**/
32#pragma once
33#include <NDEVR/BaseValues.h>
34#include <NDEVR/LibAssert.h>
35
36#include <algorithm>
37#include <typeinfo>
38#include <cstring>
39#ifdef _WIN32
40//#define NDEVR_LIB_SIZE_FUNCTION _msize
41#endif
42/**--------------------------------------------------------------------------------------------------
43Namespace: Parke
44Namespace that wraps all Logic created by Tyler Parke
45 *-----------------------------------------------------------------------------------------------**/
46namespace NDEVR
47{
48
49 /**--------------------------------------------------------------------------------------------------
50 Class: _ObjectAllocator
51
52 \brief The default object allocator for Buffer.
53
54 Author: Tyler Parke
55
56 Date: 2017-11-19
57 *-----------------------------------------------------------------------------------------------**/
58 template<class t_index_type, bool is_primitive>
60
61 /**--------------------------------------------------------------------------------------------------
62 Class: _ObjectAllocator
63
64 \brief The default object allocator for Buffers of primitive types such as doubles or ints
65
66 Author: Tyler Parke
67
68 Date: 2017-11-19
69 *-----------------------------------------------------------------------------------------------**/
70 template<class t_index_type>
71 class _ObjectAllocatorT<t_index_type, true>
72 {
73 public:
74 template<class t_buffer_type, class t_type>
75 static void _allocate(t_buffer_type&, t_index_type, t_index_type)
76 {}
77
78 /**--------------------------------------------------------------------------------------------------
79 Fn: template<bool t_primitive> static void _ObjectAllocator::_deallocate(t_buffer_type& buff,
80 t_index_type start, t_index_type size);
81
82 Deallocates.
83
84 Author: Tyler Parke
85
86 Date: 2017-11-19
87
88 Typeparams:
89 t_primitive - Type of the primitive.
90 Parameters:
91 buff - [in,out] The buffer.
92 start - The start.
93 size - The size.
94 *-----------------------------------------------------------------------------------------------**/
95 template<class t_buffer_type, class t_type>
96 static void _deallocate(t_buffer_type&, t_index_type, t_index_type)
97 {}
98
99 /**--------------------------------------------------------------------------------------------------
100 Fn: static void _ObjectAllocator::_allocateElement<true>(t_buffer_type& buff, t_index_type start,
101 t_index_type size, const t_type& object)
102
103 Allocate element.
104
105 Author: Tyler Parke
106
107 Date: 2017-11-19
108
109 Typeparams:
110 true - Type of the true.
111 Parameters:
112 buff - [in,out] The buffer.
113 start - The start.
114 size - The size.
115 object - The object.
116 *-----------------------------------------------------------------------------------------------**/
117 template<class t_buffer_type, class t_type>
118 static void _allocateElement(t_buffer_type& buff, t_index_type start, t_index_type size, const t_type& object)
119 {
120 buff.setAll(object, start, size);
121 }
122
123 /**--------------------------------------------------------------------------------------------------
124 Fn: static void _ObjectAllocator::_allocateElement<true>(t_buffer_type& buff, t_index_type index,
125 const t_type& object)
126
127 Allocate element.
128
129 Author: Tyler Parke
130
131 Date: 2017-11-19
132
133 Typeparams:
134 true - Type of the true.
135 Parameters:
136 buff - [in,out] The buffer.
137 index - Zero-based index of the.
138 object - The object.
139 *-----------------------------------------------------------------------------------------------**/
140 template<class t_buffer_type, class t_type>
141 static void _allocateElement(t_buffer_type& buff, t_index_type index, const t_type& object)
142 {
143 buff.get(index) = object;
144 }
145 template<class t_buffer_type, class t_type>
146 static void _allocateElement(t_buffer_type& buff, t_index_type index, t_type&& object)
147 {
148 buff.get(index) = std::forward<t_type>(object);
149 }
150 };
151
152 /**--------------------------------------------------------------------------------------------------
153 Class: _ObjectAllocator
154
155 \brief The default object allocator for Buffers of non-primitive types that have destructors or memory
156 on the heap.
157
158 Author: Tyler Parke
159
160 Date: 2017-11-19
161 *-----------------------------------------------------------------------------------------------**/
162 template<class t_index_type>
163 class _ObjectAllocatorT<t_index_type, false>
164 {
165 public:
166 /**--------------------------------------------------------------------------------------------------
167 Fn: static void _ObjectAllocator::_allocate<false>(t_buffer_type& buff, t_index_type start, t_index_type size)
168
169 Allocates.
170
171 Author: Tyler Parke
172
173 Date: 2017-11-19
174
175 Typeparams:
176 false - Type of the false.
177 Parameters:
178 buff - [in,out] The buffer.
179 start - The start.
180 size - The size.
181 *-----------------------------------------------------------------------------------------------**/
182 template<class t_buffer_type, class t_type>
183 static void _allocate(t_buffer_type& buff, t_index_type start, t_index_type size)
184 {
185 for (t_index_type i = 0; i < size; i++)
186 {
187 new (&buff.get(i + start)) t_type();
188 }
189 }
190 /**--------------------------------------------------------------------------------------------------
191 Fn: template<bool t_primitive> static void _ObjectAllocator::_deallocate(t_buffer_type& buff,
192 t_index_type start, t_index_type size);
193
194 Deallocates.
195
196 Author: Tyler Parke
197
198 Date: 2017-11-19
199
200 Typeparams:
201 t_primitive - Type of the primitive.
202 Parameters:
203 buff - [in,out] The buffer.
204 start - The start.
205 size - The size.
206 *-----------------------------------------------------------------------------------------------**/
207 template<class t_buffer_type, class t_type>
208 static void _deallocate(t_buffer_type& buff, t_index_type start, t_index_type size)
209 {
210 for (t_index_type i = 0; i < size; i++)
211 {
212 (&buff.get(i + start))->~t_type();
213 }
214 }
215
216 /**--------------------------------------------------------------------------------------------------
217 Fn: static void _ObjectAllocator::_allocateElement<false>(t_buffer_type& buff, t_index_type start,
218 t_index_type size, const t_type& object)
219
220 Allocate element.
221
222 Author: Tyler Parke
223
224 Date: 2017-11-19
225
226 Typeparams:
227 false - Type of the false.
228 Parameters:
229 buff - [in,out] The buffer.
230 start - The start.
231 size - The size.
232 object - The object.
233 *-----------------------------------------------------------------------------------------------**/
234 template<class t_buffer_type, class t_type>
235 static void _allocateElement(t_buffer_type& buff, t_index_type start, t_index_type size, const t_type& object)
236 {
237 for (t_index_type i = 0; i < size; i++)
238 {
239 new (&buff.get(i + start)) t_type(object);
240 }
241 }
242
243 /**--------------------------------------------------------------------------------------------------
244 Fn: static void _ObjectAllocator::_allocateElement<false>(t_buffer_type& buff, t_index_type index,
245 const t_type& object)
246
247 Allocate element.
248
249 Author: Tyler Parke
250
251 Date: 2017-11-19
252
253 Typeparams:
254 false - Type of the false.
255 Parameters:
256 buff - [in,out] The buffer.
257 index - Zero-based index of the.
258 object - The object.
259 *-----------------------------------------------------------------------------------------------**/
260 template<class t_buffer_type, class t_type>
261 static void _allocateElement(t_buffer_type& buff, t_index_type index, const t_type& object)
262 {
263 new (&buff.get(index)) t_type(object);
264 }
265 template<class t_buffer_type, class t_type>
266 static void _allocateElement(t_buffer_type& buff, t_index_type index, t_type&& object)
267 {
268 new (&buff.get(index)) t_type(std::move(object));
269 }
270 };
271
272 /**--------------------------------------------------------------------------------------------------
273 Class: ObjectAllocator
274
275 An object allocator.
276
277 Author: Tyler Parke
278
279 Date: 2017-11-19
280 *-----------------------------------------------------------------------------------------------**/
281 template<bool t_is_primitive, class t_index_type = uint04>
283 {
284 public:
285
286
287 /**--------------------------------------------------------------------------------------------------
288 Fn: static void ObjectAllocator::allocate(t_buffer_type& buffer, t_index_type start, t_index_type size)
289
290 Allocates.
291
292 Author: Tyler Parke
293
294 Date: 2017-11-19
295
296 Parameters:
297 buffer - [in,out] The buffer.
298 start - The start.
299 size - The size.
300 *-----------------------------------------------------------------------------------------------**/
301 template<class t_type, class t_buffer_type>
302 static void allocate(t_buffer_type& buffer, t_index_type start, t_index_type size)
303 {
304 _ObjectAllocatorT<t_index_type, t_is_primitive>::template _allocate<t_buffer_type, t_type>(buffer, start, size);
305 }
306
307 /**--------------------------------------------------------------------------------------------------
308 Fn: static void ObjectAllocator::deallocate(t_buffer_type& buffer, t_index_type start, t_index_type size)
309
310 Deallocates.
311
312 Author: Tyler Parke
313
314 Date: 2017-11-19
315
316 Parameters:
317 buffer - [in,out] The buffer.
318 start - The start.
319 size - The size.
320 *-----------------------------------------------------------------------------------------------**/
321 template<class t_type, class t_buffer_type>
322 static void deallocate(t_buffer_type& buffer, t_index_type start, t_index_type size)
323 {
324 _ObjectAllocatorT<t_index_type, t_is_primitive>::template _deallocate<t_buffer_type, t_type>(buffer, start, size);
325 }
326
327 /**--------------------------------------------------------------------------------------------------
328 Fn: static void ObjectAllocator::allocateElement(t_buffer_type& buffer, t_index_type start, t_index_type size,
329 const t_type& object)
330
331 Allocate element.
332
333 Author: Tyler Parke
334
335 Date: 2017-11-19
336
337 Parameters:
338 buffer - [in,out] The buffer.
339 start - The start.
340 size - The size.
341 object - The object.
342 *-----------------------------------------------------------------------------------------------**/
343 template<class t_type, class t_buffer_type>
344 static void allocateElement(t_buffer_type& buffer, t_index_type start, t_index_type size, const t_type& object)
345 {
346 _ObjectAllocatorT<t_index_type, t_is_primitive>::template _allocateElement<t_buffer_type, t_type>(buffer, start, size, object);
347 }
348 template<class t_type, class t_buffer_type>
349 static void allocateElement(t_buffer_type& buffer, t_index_type start, t_index_type size, t_type& object)
350 {
351 _ObjectAllocatorT<t_index_type, t_is_primitive>::template _allocateElement<t_buffer_type, t_type>(buffer, start, size, object);
352 }
353 /**--------------------------------------------------------------------------------------------------
354 Fn: static void ObjectAllocator::allocateElement(t_buffer_type& buffer, t_index_type index,
355 const t_type& object)
356
357 Allocate element.
358
359 Author: Tyler Parke
360
361 Date: 2017-11-19
362
363 Parameters:
364 buffer - [in,out] The buffer.
365 index - Zero-based index of the.
366 object - The object.
367 *-----------------------------------------------------------------------------------------------**/
368 template<class t_type, class t_buffer_type>
369 static void allocateElement(t_buffer_type& buffer, t_index_type index, const t_type& object)
370 {
371 _ObjectAllocatorT<t_index_type, t_is_primitive>::template _allocateElement<t_buffer_type, t_type>(buffer, index, object);
372 }
373 template<class t_type, class t_buffer_type>
374 static void allocateElement(t_buffer_type& buffer, t_index_type index, t_type&& object)
375 {
376 _ObjectAllocatorT<t_index_type, t_is_primitive>::template _allocateElement<t_buffer_type, t_type>(buffer, index, std::forward<t_type>(object));
377 }
378 /**--------------------------------------------------------------------------------------------------
379 Fn: static constexpr bool ObjectAllocator::isPrimitive()
380
381 Query if this object is primitive.
382
383 Author: Tyler Parke
384
385 Date: 2017-11-19
386
387 Returns: True if primitive, false if not.
388 *-----------------------------------------------------------------------------------------------**/
389
390 static constexpr bool isPrimitive()
391 {
392 return t_is_primitive;
393 }
394 };
395
396 /**--------------------------------------------------------------------------------------------------
397 Class: BufferAllocator
398
399 A buffer allocator.
400
401 Author: Tyler Parke
402
403 Date: 2017-11-19
404 *-----------------------------------------------------------------------------------------------**/
405 template<class t_type, class t_index_type, bool t_null_term = false>
406 class BufferAllocator
407 {
408 public:
409 [[nodiscard]] static constexpr bool IsNullTerm() { return t_null_term; }
410
411 constexpr explicit BufferAllocator()
412 : m_buffer(emptyPtr<t_null_term>())
413 , m_filled_size(0)
414#ifndef NDEVR_LIB_SIZE_FUNCTION
416#endif
417 {}
418 constexpr BufferAllocator(BufferAllocator&& allocator) noexcept
420 , m_filled_size(0)
421#ifndef NDEVR_LIB_SIZE_FUNCTION
423#endif
424 {
425 std::swap(allocator.m_buffer, m_buffer);
426 std::swap(allocator.m_filled_size, m_filled_size);
427#ifndef NDEVR_LIB_SIZE_FUNCTION
428 std::swap(allocator.m_allocated_size, m_allocated_size);
429#endif
430 }
431
432 /**--------------------------------------------------------------------------------------------------
433 Fn: BufferAllocator::~BufferAllocator()
434
435 Destructor.
436
437 Author: Tyler Parke
438
439 Date: 2017-11-19
440 *-----------------------------------------------------------------------------------------------**/
441
443 {
444 if (capacity() != 0)
445 free(m_buffer);
446 }
447
448 /**--------------------------------------------------------------------------------------------------
449 Fn: void BufferAllocator::createSpace(t_index_type size)
450
451 Creates a space.
452
453 Author: Tyler Parke
454
455 Date: 2017-11-19
456
457 Parameters:
458 size - The size.
459 *-----------------------------------------------------------------------------------------------**/
460 template<bool t_managed>
461 void createSpace(t_index_type size)
462 {
463 m_filled_size += size;
466 }
467
468 /**--------------------------------------------------------------------------------------------------
469 Fn: void BufferAllocator::createSpace(t_index_type location, t_index_type size)
470
471 Creates a space.
472
473 Author: Tyler Parke
474
475 Date: 2017-11-19
476
477 Parameters:
478 location - The location.
479 size - The size.
480 *-----------------------------------------------------------------------------------------------**/
481 template<bool t_managed>
482 void createSpace(t_index_type location, t_index_type size)
483 {
485 memmove(begin() + location + size, begin() + location, sizeof(t_type) * (m_filled_size - (size + location)));
486 }
487
488 /**--------------------------------------------------------------------------------------------------
489 Fn: void BufferAllocator::addLast()
490
491 Adds last.
492
493 Author: Tyler Parke
494
495 Date: 2017-11-19
496 *-----------------------------------------------------------------------------------------------**/
497
498 void addLast()
499 {
501 }
502
503 /**--------------------------------------------------------------------------------------------------
504 Fn: void BufferAllocator::removeLast()
505
506 Removes the last.
507
508 Author: Tyler Parke
509
510 Date: 2017-11-19
511 *-----------------------------------------------------------------------------------------------**/
512
518
519 /**--------------------------------------------------------------------------------------------------
520 Fn: t_index_type BufferAllocator::count(const t_type& value) const
521
522 Counts the given value.
523
524 Author: Tyler Parke
525
526 Date: 2017-11-19
527
528 Parameters:
529 value - The value.
530
531 Returns: An t_index_type.
532 *-----------------------------------------------------------------------------------------------**/
533
534 [[nodiscard]] t_index_type count(const t_type& value) const
535 {
536 t_index_type count = 0;
537 for (t_index_type i = 0; i < m_filled_size; i++)
538 {
539 if (m_buffer[i] == value)
540 ++count;
541 }
542 return count;
543 }
544
545 /**--------------------------------------------------------------------------------------------------
546 Fn: void BufferAllocator::addIndex()
547
548 Adds index.
549
550 Author: Tyler Parke
551
552 Date: 2017-11-19
553 *-----------------------------------------------------------------------------------------------**/
554
555 void addIndex()
556 {
558 }
559
560 /**--------------------------------------------------------------------------------------------------
561 Fn: void BufferAllocator::addIndex(t_index_type location)
562
563 Adds an index.
564
565 Author: Tyler Parke
566
567 Date: 2017-11-19
568
569 Parameters:
570 location - The location.
571 *-----------------------------------------------------------------------------------------------**/
572
573 void addIndex(t_index_type location)
574 {
575 lib_assert(location <= m_filled_size, "Out of bounds add");
577 memmove(m_buffer + location + 1, m_buffer + location, sizeof(t_type) * (m_filled_size - location - 1));
578
579 }
580
581 /**--------------------------------------------------------------------------------------------------
582 Fn: void BufferAllocator::removeIndex(t_index_type location)
583
584 Removes the index described by location.
585
586 Author: Tyler Parke
587
588 Date: 2017-11-19
589
590 Parameters:
591 location - The location.
592 *-----------------------------------------------------------------------------------------------**/
593#ifdef __clang__
594 #pragma clang diagnostic push
595 #pragma clang diagnostic ignored "-Wdynamic-class-memaccess"
596#endif
597 void removeIndex(t_index_type location)
598 {
599 lib_assert(location <= m_filled_size, "Out of bounds remove");
600 memmove(m_buffer + location, m_buffer + location + 1, (m_filled_size - location - 1) * sizeof(t_type));
603 }
604#ifdef __clang__
605 #pragma clang diagnostic pop
606#endif
607 /**--------------------------------------------------------------------------------------------------
608 Fn: sint04 BufferAllocator::compare(const BufferAllocator& allocator) const
609
610 Compares this const BufferAllocator&amp; object to another to determine their relative
611 ordering.
612
613 Author: Tyler Parke
614
615 Date: 2017-11-19
616
617 Parameters:
618 allocator - The constant buffer allocator&amp; to compare to this object.
619
620 Returns: Negative if 'allocator' is less than '', 0 if they are equal, or positive if it is
621 greater.
622 *-----------------------------------------------------------------------------------------------**/
623
624 [[nodiscard]] sint04 compare(const BufferAllocator& allocator) const
625 {
626 return cast<sint04>(memcmp(m_buffer, allocator.m_buffer, sizeof(t_type) * getMin(allocator.filledSize(), m_filled_size)));
627 }
628
629 /**--------------------------------------------------------------------------------------------------
630 Fn: sint04 BufferAllocator::compare(const BufferAllocator& allocator, t_index_type start,
631 t_index_type end) const
632
633 Compares objects.
634
635 Author: Tyler Parke
636
637 Date: 2017-11-19
638
639 Parameters:
640 allocator - Constant buffer allocator&amp; to be compared.
641 start - Uint 04 to be compared.
642 end - Uint 04 to be compared.
643
644 Returns: Negative if 'allocator' is less than 'start', 0 if they are equal, or positive if it is
645 greater.
646 *-----------------------------------------------------------------------------------------------**/
647
648 [[nodiscard]] sint04 compare(const BufferAllocator& allocator, t_index_type start, t_index_type end) const
649 {
650 return cast<sint04>(memcmp(&(m_buffer[start]), &(allocator.m_buffer[start]), sizeof(t_type) * getMin(allocator.filledSize(), m_filled_size, end)));
651 }
652
653 /**--------------------------------------------------------------------------------------------------
654 Fn: void BufferAllocator::swap(t_index_type index_a, t_index_type index_b)
655
656 Swaps.
657
658 Author: Tyler Parke
659
660 Date: 2017-11-19
661
662 Parameters:
663 index_a - The index a.
664 index_b - The index b.
665 *-----------------------------------------------------------------------------------------------**/
666
667 void swap(t_index_type index_a, t_index_type index_b)
668 {
669 std::swap(m_buffer[index_a], m_buffer[index_b]);
670 }
671
672 /**--------------------------------------------------------------------------------------------------
673 Fn: void BufferAllocator::clear()
674
675 Clears this object to its blank/initial state.
676
677 Author: Tyler Parke
678
679 Date: 2017-11-19
680 *-----------------------------------------------------------------------------------------------**/
681
682 void clear()
683 {
684 m_filled_size = 0;
686 }
687
688 /**--------------------------------------------------------------------------------------------------
689 Fn: void BufferAllocator::setSize(t_index_type size)
690
691 Sets a size.
692
693 Author: Tyler Parke
694
695 Date: 2017-11-19
696
697 Parameters:
698 size - The size.
699 *-----------------------------------------------------------------------------------------------**/
700
701 void setSize(t_index_type size)
702 {
703 m_filled_size = size;
706 }
707
708 /**--------------------------------------------------------------------------------------------------
709 Fn: t_type& BufferAllocator::get(t_index_type index)
710
711 Gets a t type&amp; using the given index.
712
713 Author: Tyler Parke
714
715 Date: 2017-11-19
716
717 Parameters:
718 index - The index to get.
719
720 Returns: A reference to a t_type.
721 *-----------------------------------------------------------------------------------------------**/
722
723 t_type& get(t_index_type index)
724 {
725 return m_buffer[index];
726 }
727
728 /**--------------------------------------------------------------------------------------------------
729 Fn: const t_type& BufferAllocator::get(t_index_type index) const
730
731 Gets a constant type&amp; using the given index.
732
733 Author: Tyler Parke
734
735 Date: 2017-11-19
736
737 Parameters:
738 index - The index to get.
739
740 Returns: A reference to a const t_type.
741 *-----------------------------------------------------------------------------------------------**/
742
743 [[nodiscard]] const t_type& get(t_index_type index) const
744 {
745 return m_buffer[index];
746 }
747
748 t_type* ptr()
749 {
750 return m_buffer;
751 }
752
753 [[nodiscard]] const t_type* ptr() const
754 {
755 return m_buffer;
756 }
757
758 [[nodiscard]] t_index_type memSize() const
759 {
760 return m_filled_size * sizeof(t_type);
761 }
762
763 /**--------------------------------------------------------------------------------------------------
764 Fn: t_type* BufferAllocator::begin()
765
766 Gets the begin.
767
768 Author: Tyler Parke
769
770 Date: 2017-11-19
771
772 Returns: Null if it fails, else a pointer to a t_type.
773 *-----------------------------------------------------------------------------------------------**/
774
775 [[nodiscard]] t_type* begin()
776 {
777 return m_buffer;
778 }
779
780 /**--------------------------------------------------------------------------------------------------
781 Fn: const t_type* BufferAllocator::begin() const
782
783 Gets the begin.
784
785 Author: Tyler Parke
786
787 Date: 2017-11-19
788
789 Returns: Null if it fails, else a pointer to a const t_type.
790 *-----------------------------------------------------------------------------------------------**/
791
792 [[nodiscard]] const t_type* begin() const
793 {
794 return m_buffer;
795 }
796
797 /**--------------------------------------------------------------------------------------------------
798 Fn: t_type* BufferAllocator::begin(t_index_type index) const
799
800 Begins the given index.
801
802 Author: Tyler Parke
803
804 Date: 2017-11-19
805
806 Parameters:
807 index - Zero-based index of the.
808
809 Returns: Null if it fails, else a pointer to a t_type.
810 *-----------------------------------------------------------------------------------------------**/
811
812 [[nodiscard]] t_type* begin(t_index_type index) const
813 {
814 return &(m_buffer[index]);
815 }
816
817 /**--------------------------------------------------------------------------------------------------
818 Fn: t_index_type BufferAllocator::capacity() const
819
820 Gets the capacity of the buffer or total space allocated to it.
821
822 Author: Tyler Parke
823
824 Date: 2017-04-15
825
826 Returns: The capacity of the current allocated buffer.
827 *-----------------------------------------------------------------------------------------------**/
828 [[nodiscard]] t_index_type capacity() const
829 {
830#ifdef NDEVR_LIB_SIZE_FUNCTION
832 if(t_null_term)
833 return cast<t_index_type>(NDEVR_LIB_SIZE_FUNCTION(m_buffer) / sizeof(t_type)) - 1;
834 else
835 return cast<t_index_type>(NDEVR_LIB_SIZE_FUNCTION(m_buffer) / sizeof(t_type));
836 else
837 return 0;
838#else
839 return m_allocated_size;
840#endif
841 }
842
843 /**--------------------------------------------------------------------------------------------------
844 Fn: t_type* BufferAllocator::end()
845
846 Gets the end.
847
848 Author: Tyler Parke
849
850 Date: 2017-11-19
851
852 Returns: Null if it fails, else a pointer to a t_type.
853 *-----------------------------------------------------------------------------------------------**/
854
855 [[nodiscard]] t_type* end()
856 {
857 if (capacity() == 0)
858 return m_buffer;
859 return &(get(m_filled_size));
860 }
861
862 /**--------------------------------------------------------------------------------------------------
863 Fn: const t_type* BufferAllocator::end() const
864
865 Gets the end.
866
867 Author: Tyler Parke
868
869 Date: 2017-11-19
870
871 Returns: Null if it fails, else a pointer to a const t_type.
872 *-----------------------------------------------------------------------------------------------**/
873
874 [[nodiscard]] const t_type* end() const
875 {
876 if (capacity() == 0)
877 return m_buffer;
878 return &(get(m_filled_size));
879 }
880
881 /**--------------------------------------------------------------------------------------------------
882 Fn: t_type* BufferAllocator::end(t_index_type index)
883
884 Ends the given index.
885
886 Author: Tyler Parke
887
888 Date: 2017-11-19
889
890 Parameters:
891 index - Zero-based index of the.
892
893 Returns: Null if it fails, else a pointer to a t_type.
894 *-----------------------------------------------------------------------------------------------**/
895
896 t_type* end(t_index_type index)
897 {
898 return &(get(m_filled_size - index));
899 }
900
901 /**--------------------------------------------------------------------------------------------------
902 Fn: inline void BufferAllocator::resizeSpace(t_index_type new_size)
903
904 Resize space.
905
906 Author: Tyler Parke
907
908 Date: 2017-11-19
909
910 Parameters:
911 new_size - Size of the new.
912 *-----------------------------------------------------------------------------------------------**/
913
914 inline void resizeSpace(t_index_type new_size)
915 {
916 if (new_size == 0)
917 {
918 if (capacity() != 0)
919 free(m_buffer);
920 m_buffer = emptyPtr<t_null_term>();//this will provide the needed null termination without a malloc
921 }
922 else
923 {
924 if (capacity() == 0)
925 m_buffer = (t_type*)malloc(sizeof(t_type) * (t_null_term ? new_size + 1 : new_size));
926 else
927 m_buffer = (t_type*)realloc(m_buffer, sizeof(t_type) * (t_null_term ? new_size + 1 : new_size));
928 if (m_buffer == nullptr)
929 {
931 lib_assert(false, "Unable to allocate buffer: Perhaps out of system memory!");
932 }
933 }
934#ifndef NDEVR_LIB_SIZE_FUNCTION
935 m_allocated_size = new_size;
936#endif
937 }
938
939 /**--------------------------------------------------------------------------------------------------
940 Fn: void BufferAllocator::setAll(const t_type& object, t_index_type offset, t_index_type size)
941
942 Sets all.
943
944 Author: Tyler Parke
945
946 Date: 2017-11-19
947
948 Parameters:
949 object - The object.
950 offset - The offset.
951 size - The size.
952 *-----------------------------------------------------------------------------------------------**/
953
954 void setAll(const t_type& object, t_index_type offset, t_index_type size)
955 {
956 //if (sizeof(int) == sizeof(t_type))
957 {
958 //memset(&m_buffer[offset], *((int*)(&object)), size);
959 }
960 //else
961 {
962 for (t_index_type i = offset; i < offset + size; i++)
963 {
964 m_buffer[i] = object;
965 }
966 }
967 }
968
969 /**--------------------------------------------------------------------------------------------------
970 Fn: template<bool is_primitive> void BufferAllocator::setAll(const t_type* src, t_index_type offset,
971 t_index_type size);
972
973 Sets all.
974
975 Author: Tyler Parke
976
977 Date: 2017-11-19
978
979 Typeparams:
980 is_primitive - Type of the is primitive.
981 Parameters:
982 src - Source for the.
983 offset - The offset.
984 size - The size.
985 *-----------------------------------------------------------------------------------------------**/
986
987 template<bool is_primitive>
988 void setAll(const t_type* src, t_index_type offset, t_index_type size)
989 {
990 if constexpr (is_primitive)
991 {
992 memmove(&m_buffer[offset], src, size * sizeof(t_type));
993 }
994 else
995 {
996 for (t_index_type i = 0; i < size; i++)
997 {
998 m_buffer[i + offset] = src[i];
999 }
1000 }
1001 }
1002
1003 /**--------------------------------------------------------------------------------------------------
1004 Fn: void BufferAllocator::setAll<true>(const t_type* src, t_index_type offset, t_index_type size)
1005
1006 Sets all.
1007
1008 Author: Tyler Parke
1009
1010 Date: 2017-11-19
1011
1012 Typeparams:
1013 true - Type of the true.
1014 Parameters:
1015 src - Source for the.
1016 offset - The offset.
1017 size - The size.
1018 *-----------------------------------------------------------------------------------------------**/
1019 /*template<>
1020 void setAll<true>(const t_type* src, t_index_type offset, t_index_type size)
1021 {
1022 memmove(&m_buffer[offset], src, size * sizeof(t_type));
1023 }*/
1024
1025 /**--------------------------------------------------------------------------------------------------
1026 Fn: void BufferAllocator::setAll<false>(const t_type* src, t_index_type offset, t_index_type size)
1027
1028 Sets all.
1029
1030 Author: Tyler Parke
1031
1032 Date: 2017-11-19
1033
1034 Typeparams:
1035 false - Type of the false.
1036 Parameters:
1037 src - Source for the.
1038 offset - The offset.
1039 size - The size.
1040 *-----------------------------------------------------------------------------------------------**/
1041 /*template<>
1042 void setAll<false>(const t_type* src, t_index_type offset, t_index_type size)
1043 {
1044 for (t_index_type i = 0; i < size; i++)
1045 {
1046 m_buffer[i + offset] = src[i];
1047 }
1048 }*/
1049
1050 /**--------------------------------------------------------------------------------------------------
1051 Fn: template<bool is_primitive> void BufferAllocator::setAll(const BufferAllocator& allocator,
1052 t_index_type offset, t_index_type other_offset, t_index_type size);
1053
1054 Sets all.
1055
1056 Author: Tyler Parke
1057
1058 Date: 2017-11-19
1059
1060 Typeparams:
1061 is_primitive - Type of the is primitive.
1062 Parameters:
1063 allocator - The allocator.
1064 offset - The offset.
1065 other_offset - The other offset.
1066 size - The size.
1067 *-----------------------------------------------------------------------------------------------**/
1068
1069 template<bool is_primitive, class t_other_index_type, bool t_other_null_term>
1070 void setAll(const BufferAllocator<t_type, t_other_index_type, t_other_null_term>& allocator, t_index_type offset, t_index_type other_offset, t_index_type size)
1071 {
1072 if constexpr (is_primitive)
1073 {
1074 memmove(&m_buffer[offset], &(allocator.m_buffer[other_offset]), size * sizeof(t_type));
1075 }
1076 else
1077 {
1078 for (t_index_type i = 0; i < size; i++)
1079 {
1080 m_buffer[i + offset] = allocator.get(i + other_offset);
1081 }
1082 }
1083 }
1084
1085 /**--------------------------------------------------------------------------------------------------
1086 Fn: void BufferAllocator::setAllFromSource(const t_other_allocator& allocator, t_index_type offset,
1087 t_index_type other_offset, t_index_type size)
1088
1089 Sets all from source.
1090
1091 Author: Tyler Parke
1092
1093 Date: 2017-11-19
1094
1095 Parameters:
1096 allocator - The allocator.
1097 offset - The offset.
1098 other_offset - The other offset.
1099 size - The size.
1100 *-----------------------------------------------------------------------------------------------**/
1101 template<class t_other_allocator>
1102 void setAllFromSource(const t_other_allocator& allocator, t_index_type offset, t_index_type other_offset, t_index_type size)
1103 {
1104 for (t_index_type i = 0; i < size; i++)
1105 {
1106 m_buffer[i + offset] = allocator.get(i + other_offset);
1107 }
1108 }
1109
1110 /**--------------------------------------------------------------------------------------------------
1111 Fn: inline void BufferAllocator::allocationSizeCheck()
1112
1113 Allocation size check.
1114
1115 Author: Tyler Parke
1116
1117 Date: 2017-11-19
1118 *-----------------------------------------------------------------------------------------------**/
1119 template<bool t_managed>
1121 {
1122 if constexpr (t_managed)//We only 1, assume more to come shortly and allocate more space than required
1123 {
1124 if(m_filled_size >= capacity())
1125 {
1128 }
1129 }
1130 else //We added more than 1, increase buffer only once for compaction
1131 {
1132 if (m_filled_size == capacity() + 1)
1133 {
1136 }
1137 else if (m_filled_size > capacity())
1138 {
1140 }
1141 }
1142 }
1143
1144 /**--------------------------------------------------------------------------------------------------
1145 Fn: void BufferAllocator::removeAllIndex(t_index_type start, t_index_type end)
1146
1147 Removes all index.
1148
1149 Author: Tyler Parke
1150
1151 Date: 2017-11-19
1152
1153 Parameters:
1154 start - The start.
1155 end - The end.
1156 *-----------------------------------------------------------------------------------------------**/
1157
1158 void removeAllIndex(t_index_type start, t_index_type end)
1159 {
1160 if (end == m_filled_size)
1161 {
1162 m_filled_size = start;
1163 }
1164 else
1165 {
1166 memmove(&m_buffer[start], &m_buffer[end], (m_filled_size - end) * sizeof(t_type));
1167 m_filled_size -= (end - start);
1168 }
1170 }
1171 template<class t_range_buffer>
1172 void removeAllIndices(const t_range_buffer& ranges)
1173 {
1174 uint04 offset = 0;
1175 for (uint04 i = 0; i < ranges.size(); i++)
1176 {
1177 auto range = ranges[i];
1178 t_index_type start = range.first;
1179 t_index_type end = range.second;
1180 uint04 next = i == ranges.size() - 1 ? m_filled_size : ranges[i + 1].start;
1181 memmove(&m_buffer[start], &m_buffer[end], (next - (end + offset)) * sizeof(t_type));
1182 offset += (end - start);
1183 }
1184 m_filled_size -= offset;
1186 }
1187
1188 /**--------------------------------------------------------------------------------------------------
1189 Fn: template<bool t_is_null> inline void BufferAllocator::nullTerminatorCheck();
1190
1191 Null terminator check.
1192
1193 Author: Tyler Parke
1194
1195 Date: 2017-11-19
1196
1197 Typeparams:
1198 t_is_null - Type of the is null.
1199 *-----------------------------------------------------------------------------------------------**/
1200
1201 template<bool t_is_null>
1203 {
1204 if constexpr (t_is_null)
1205 {
1206 if (capacity() != 0)
1207 memset(&(m_buffer[m_filled_size]), 0, sizeof(t_type));//Ensure null termination
1208 }
1209 }
1210
1211 /*template<>
1212 inline void nullTerminatorCheck<true>()
1213 {
1214 if (capacity() != 0)
1215 memset(&(m_buffer[m_filled_size]), 0, sizeof(t_type));//Ensure null termination
1216 }
1217 template<>
1218
1219 inline void nullTerminatorCheck<false>() {}*/
1220
1221
1222 /**--------------------------------------------------------------------------------------------------
1223 Fn: template<bool t_is_null> inline void BufferAllocator::nullTerminatorCheck();
1224
1225 Null terminator check.
1226
1227 Author: Tyler Parke
1228
1229 Date: 2017-11-19
1230
1231 Typeparams:
1232 t_is_null - Type of the is null.
1233 *-----------------------------------------------------------------------------------------------**/
1234
1235 template<bool t_is_null>
1236 [[nodiscard]] t_type* emptyPtr() const
1237 {
1238 if constexpr(t_is_null)
1239 {
1240 static constexpr t_type null_object = {};
1241 return const_cast<t_type*>(&null_object);
1242 }
1243 else
1244 {
1245 return nullptr;
1246 }
1247 }
1248
1249 /**--------------------------------------------------------------------------------------------------
1250 Fn: t_index_type BufferAllocator::filledSize() const
1251
1252 Filled size.
1253
1254 Author: Tyler Parke
1255
1256 Date: 2017-11-19
1257
1258 Returns: An t_index_type.
1259 *-----------------------------------------------------------------------------------------------**/
1260
1261 [[nodiscard]] constexpr t_index_type filledSize() const
1262 {
1263 return m_filled_size;
1264 }
1265 public:
1266
1267 /**--------------------------------------------------------------------------------------------------
1268 Fn: inline BufferAllocator& BufferAllocator::operator=(BufferAllocator&& value)
1269
1270 Move assignment operator.
1271
1272 Author: Tyler Parke
1273
1274 Date: 2017-11-19
1275
1276 Parameters:
1277 value - [in,out] The value.
1278
1279 Returns: A shallow copy of this object.
1280 *-----------------------------------------------------------------------------------------------**/
1281
1283 {
1284 std::swap(value.m_buffer, m_buffer);
1285 std::swap(value.m_filled_size, m_filled_size);
1286#ifndef NDEVR_LIB_SIZE_FUNCTION
1287 std::swap(value.m_allocated_size, m_allocated_size);
1288#endif
1289 return *this;
1290 }
1291 public:
1292 /** The buffer that stores the actual data. */
1293 t_type* m_buffer;
1294 t_index_type m_filled_size;
1295#ifndef NDEVR_LIB_SIZE_FUNCTION
1296 t_index_type m_allocated_size;
1297#endif
1298 };
1299}
#define lib_assert(expression, message)
Asserts some logic in the code. Disabled in non debug mode by default. Can be re-enabled in release u...
Definition LibAssert.h:70
static void _allocateElement(t_buffer_type &buff, t_index_type start, t_index_type size, const t_type &object)
Definition MemoryManager.h:235
static void _deallocate(t_buffer_type &buff, t_index_type start, t_index_type size)
Definition MemoryManager.h:208
static void _allocate(t_buffer_type &buff, t_index_type start, t_index_type size)
Definition MemoryManager.h:183
static void _allocateElement(t_buffer_type &buff, t_index_type index, t_type &&object)
Definition MemoryManager.h:266
static void _allocateElement(t_buffer_type &buff, t_index_type index, const t_type &object)
Definition MemoryManager.h:261
static void _allocate(t_buffer_type &, t_index_type, t_index_type)
Definition MemoryManager.h:75
static void _allocateElement(t_buffer_type &buff, t_index_type start, t_index_type size, const t_type &object)
Definition MemoryManager.h:118
static void _allocateElement(t_buffer_type &buff, t_index_type index, t_type &&object)
Definition MemoryManager.h:146
static void _deallocate(t_buffer_type &, t_index_type, t_index_type)
Definition MemoryManager.h:96
static void _allocateElement(t_buffer_type &buff, t_index_type index, const t_type &object)
Definition MemoryManager.h:141
The default object allocator for Buffer.
Definition MemoryManager.h:59
Definition Pointer.hpp:297
void createSpace(t_index_type location, t_index_type size)
Definition MemoryManager.h:482
const t_type & get(t_index_type index) const
Definition MemoryManager.h:743
sint04 compare(const BufferAllocator &allocator, t_index_type start, t_index_type end) const
Definition MemoryManager.h:648
void setAllFromSource(const t_other_allocator &allocator, t_index_type offset, t_index_type other_offset, t_index_type size)
Definition MemoryManager.h:1102
void createSpace(t_index_type size)
Definition MemoryManager.h:461
void setAll(const BufferAllocator< t_type, t_other_index_type, t_other_null_term > &allocator, t_index_type offset, t_index_type other_offset, t_index_type size)
Definition MemoryManager.h:1070
t_index_type m_filled_size
Definition MemoryManager.h:1294
const t_type * begin() const
Definition MemoryManager.h:792
void addIndex(t_index_type location)
Definition MemoryManager.h:573
sint04 compare(const BufferAllocator &allocator) const
Definition MemoryManager.h:624
static constexpr bool IsNullTerm()
Definition MemoryManager.h:409
t_type * end()
Definition MemoryManager.h:855
t_index_type capacity() const
Definition MemoryManager.h:828
void allocationSizeCheck()
Definition MemoryManager.h:1120
constexpr BufferAllocator(BufferAllocator &&allocator) noexcept
Definition MemoryManager.h:418
void removeAllIndex(t_index_type start, t_index_type end)
Definition MemoryManager.h:1158
void nullTerminatorCheck()
Definition MemoryManager.h:1202
void removeAllIndices(const t_range_buffer &ranges)
Definition MemoryManager.h:1172
constexpr t_index_type filledSize() const
Definition MemoryManager.h:1261
t_type * begin(t_index_type index) const
Definition MemoryManager.h:812
t_type * end(t_index_type index)
Definition MemoryManager.h:896
void swap(t_index_type index_a, t_index_type index_b)
Definition MemoryManager.h:667
const t_type * end() const
Definition MemoryManager.h:874
t_type & get(t_index_type index)
Definition MemoryManager.h:723
BufferAllocator & operator=(BufferAllocator &&value) noexcept
Definition MemoryManager.h:1282
t_type * emptyPtr() const
Definition MemoryManager.h:1236
void removeIndex(t_index_type location)
Definition MemoryManager.h:597
void setAll(const t_type *src, t_index_type offset, t_index_type size)
Definition MemoryManager.h:988
t_index_type m_allocated_size
Definition MemoryManager.h:1296
void addLast()
Definition MemoryManager.h:498
const t_type * ptr() const
Definition MemoryManager.h:753
t_index_type memSize() const
Definition MemoryManager.h:758
constexpr BufferAllocator()
Definition MemoryManager.h:411
t_type * begin()
Definition MemoryManager.h:775
t_type * ptr()
Definition MemoryManager.h:748
void clear()
Definition MemoryManager.h:682
~BufferAllocator()
Definition MemoryManager.h:442
void resizeSpace(t_index_type new_size)
Definition MemoryManager.h:914
void setAll(const t_type &object, t_index_type offset, t_index_type size)
Definition MemoryManager.h:954
t_type * m_buffer
Definition MemoryManager.h:1293
void setSize(t_index_type size)
Definition MemoryManager.h:701
t_index_type count(const t_type &value) const
Definition MemoryManager.h:534
void removeLast()
Definition MemoryManager.h:513
void addIndex()
Definition MemoryManager.h:555
Definition MemoryManager.h:283
static constexpr bool isPrimitive()
Definition MemoryManager.h:390
static void deallocate(t_buffer_type &buffer, t_index_type start, t_index_type size)
Definition MemoryManager.h:322
static void allocate(t_buffer_type &buffer, t_index_type start, t_index_type size)
Definition MemoryManager.h:302
static void allocateElement(t_buffer_type &buffer, t_index_type index, t_type &&object)
Definition MemoryManager.h:374
static void allocateElement(t_buffer_type &buffer, t_index_type index, const t_type &object)
Definition MemoryManager.h:369
static void allocateElement(t_buffer_type &buffer, t_index_type start, t_index_type size, t_type &object)
Definition MemoryManager.h:349
static void allocateElement(t_buffer_type &buffer, t_index_type start, t_index_type size, const t_type &object)
Definition MemoryManager.h:344
Definition ACIColor.h:37
int32_t sint04
-Defines an alias representing a 4 byte, signed integer. -Can represent exact integer values -2147483...
Definition BaseValues.hpp:76
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:120
constexpr t_to cast(const Angle< t_from > &value)
Definition Angle.h:514
constexpr t_type getMin(const t_type &left, const t_type &right)
Finds the minimum of the given arguments based on the < operator.
Definition BaseFunctions.hpp:67
Definition BaseValues.hpp:272