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
42namespace NDEVR
43{
44
45 /**--------------------------------------------------------------------------------------------------
46 \brief The default object allocator for Buffer.
47
48 Author: Tyler Parke
49
50 Date: 2017-11-19
51 **/
52 template<class t_index_type, bool is_primitive>
54
55 /**--------------------------------------------------------------------------------------------------
56 \brief The default object allocator for Buffers of primitive types such as doubles or ints
57
58 Author: Tyler Parke
59
60 Date: 2017-11-19
61 **/
62 template<class t_index_type>
63 class ObjectAllocatorT<t_index_type, true>
64 {
65 public:
66 template<class t_buffer_type, class t_type>
67 static void Allocate(t_buffer_type&, t_index_type, t_index_type)
68 {}
69
70 /**--------------------------------------------------------------------------------------------------
71
72 Deallocates.
73
74 Author: Tyler Parke
75
76 Date: 2017-11-19
77
78 Typeparams:
79 t_primitive - Type of the primitive.
80 Parameters:
81 buff - [in,out] The buffer.
82 start - The start.
83 size - The size.
84 **/
85 template<class t_buffer_type, class t_type>
86 static void Deallocate(t_buffer_type&, t_index_type, t_index_type)
87 {}
88
89 /**--------------------------------------------------------------------------------------------------
90
91 Allocate element.
92
93 Author: Tyler Parke
94
95 Date: 2017-11-19
96
97 Typeparams:
98 true - Type of the true.
99 Parameters:
100 buff - [in,out] The buffer.
101 start - The start.
102 size - The size.
103 object - The object.
104 **/
105 template<class t_buffer_type, class t_type>
106 static void AllocateElement(t_buffer_type& buff, t_index_type start, t_index_type size, const t_type& object)
107 {
108 buff.setAll(object, start, size);
109 }
110
111 /**--------------------------------------------------------------------------------------------------
112
113 Allocate element.
114
115 Author: Tyler Parke
116
117 Date: 2017-11-19
118
119 Typeparams:
120 true - Type of the true.
121 Parameters:
122 buff - [in,out] The buffer.
123 index - Zero-based index of the.
124 object - The object.
125 **/
126 template<class t_buffer_type, class t_type>
127 static void AllocateElement(t_buffer_type& buff, t_index_type index, const t_type& object)
128 {
129 buff.get(index) = object;
130 }
131 template<class t_buffer_type, class t_type>
132 static void AllocateElement(t_buffer_type& buff, t_index_type index, t_type&& object)
133 {
134 buff.get(index) = std::forward<t_type>(object);
135 }
136 };
137
138 /**--------------------------------------------------------------------------------------------------
139 Class: ObjectAllocator
140
141 \brief The default object allocator for Buffers of non-primitive types that have destructors or memory
142 on the heap.
143
144 Author: Tyler Parke
145
146 Date: 2017-11-19
147 **/
148 template<class t_index_type>
149 class ObjectAllocatorT<t_index_type, false>
150 {
151 public:
152 /**--------------------------------------------------------------------------------------------------
153 Allocates.
154
155 Author: Tyler Parke
156
157 Date: 2017-11-19
158
159 Typeparams:
160 false - Type of the false.
161 Parameters:
162 buff - [in,out] The buffer.
163 start - The start.
164 size - The size.
165 **/
166 template<class t_buffer_type, class t_type>
167 static void Allocate(t_buffer_type& buff, t_index_type start, t_index_type size)
168 {
169 for (t_index_type i = 0; i < size; i++)
170 {
171 new (&buff.get(i + start)) t_type();
172 }
173 }
174 /**--------------------------------------------------------------------------------------------------
175
176 Deallocates.
177
178 Author: Tyler Parke
179
180 Date: 2017-11-19
181
182 Typeparams:
183 t_primitive - Type of the primitive.
184 Parameters:
185 buff - [in,out] The buffer.
186 start - The start.
187 size - The size.
188 **/
189 template<class t_buffer_type, class t_type>
190 static void Deallocate(t_buffer_type& buff, t_index_type start, t_index_type size)
191 {
192 for (t_index_type i = 0; i < size; i++)
193 {
194 (&buff.get(i + start))->~t_type();
195 }
196 }
197
198 /**--------------------------------------------------------------------------------------------------
199
200 Allocate element.
201
202 Author: Tyler Parke
203
204 Date: 2017-11-19
205
206 Typeparams:
207 false - Type of the false.
208 Parameters:
209 buff - [in,out] The buffer.
210 start - The start.
211 size - The size.
212 object - The object.
213 **/
214 template<class t_buffer_type, class t_type>
215 static void AllocateElement(t_buffer_type& buff, t_index_type start, t_index_type size, const t_type& object)
216 {
217 for (t_index_type i = 0; i < size; i++)
218 {
219 new (&buff.get(i + start)) t_type(object);
220 }
221 }
222
223 /**--------------------------------------------------------------------------------------------------
224
225 Allocate element.
226
227 Author: Tyler Parke
228
229 Date: 2017-11-19
230
231 Typeparams:
232 false - Type of the false.
233 Parameters:
234 buff - [in,out] The buffer.
235 index - Zero-based index of the.
236 object - The object.
237 **/
238 template<class t_buffer_type, class t_type>
239 static void AllocateElement(t_buffer_type& buff, t_index_type index, const t_type& object)
240 {
241 new (&buff.get(index)) t_type(object);
242 }
243 template<class t_buffer_type, class t_type>
244 static void AllocateElement(t_buffer_type& buff, t_index_type index, t_type&& object)
245 {
246 new (&buff.get(index)) t_type(std::move(object));
247 }
248 };
249
250 /**--------------------------------------------------------------------------------------------------
251 Class: ObjectAllocator
252
253 An object allocator.
254
255 Author: Tyler Parke
256
257 Date: 2017-11-19
258 **/
259 template<bool t_is_primitive, class t_index_type = uint04>
261 {
262 public:
263
264
265 /**--------------------------------------------------------------------------------------------------
266 Allocates.
267
268 Author: Tyler Parke
269
270 Date: 2017-11-19
271
272 Parameters:
273 buffer - [in,out] The buffer.
274 start - The start.
275 size - The size.
276 **/
277 template<class t_type, class t_buffer_type>
278 static void Allocate(t_buffer_type& buffer, t_index_type start, t_index_type size)
279 {
281 }
282
283 /**--------------------------------------------------------------------------------------------------
284 Deallocates.
285
286 Author: Tyler Parke
287
288 Date: 2017-11-19
289
290 Parameters:
291 buffer - [in,out] The buffer.
292 start - The start.
293 size - The size.
294 **/
295 template<class t_type, class t_buffer_type>
296 static void Deallocate(t_buffer_type& buffer, t_index_type start, t_index_type size)
297 {
299 }
300
301 /**--------------------------------------------------------------------------------------------------
302
303 Allocate element.
304
305 Author: Tyler Parke
306
307 Date: 2017-11-19
308
309 Parameters:
310 buffer - [in,out] The buffer.
311 start - The start.
312 size - The size.
313 object - The object.
314 **/
315 template<class t_type, class t_buffer_type>
316 static void AllocateElement(t_buffer_type& buffer, t_index_type start, t_index_type size, const t_type& object)
317 {
319 }
320 template<class t_type, class t_buffer_type>
321 static void AllocateElement(t_buffer_type& buffer, t_index_type start, t_index_type size, t_type& object)
322 {
324 }
325 /**--------------------------------------------------------------------------------------------------
326
327 Allocate element.
328
329 Author: Tyler Parke
330
331 Date: 2017-11-19
332
333 Parameters:
334 buffer - [in,out] The buffer.
335 index - Zero-based index of the.
336 object - The object.
337 **/
338 template<class t_type, class t_buffer_type>
339 static void AllocateElement(t_buffer_type& buffer, t_index_type index, const t_type& object)
340 {
342 }
343 template<class t_type, class t_buffer_type>
344 static void AllocateElement(t_buffer_type& buffer, t_index_type index, t_type&& object)
345 {
347 }
348 /**--------------------------------------------------------------------------------------------------
349 Query if this object is primitive.
350
351 Author: Tyler Parke
352
353 Date: 2017-11-19
354
355 \returns True if primitive, false if not.
356 **/
357
358 static constexpr bool isPrimitive()
359 {
360 return t_is_primitive;
361 }
362 };
363
364 /**--------------------------------------------------------------------------------------------------
365 \brief Specific logic for reserving memory for a Buffer. When managed, and more memory is needed
366 memory is reserved using the ([Current Capacity] * 3) / 2) + 1
367 **/
368 template<class t_type, class t_index_type, bool t_null_term = false>
369 class BufferAllocator
370 {
371 public:
372 [[nodiscard]] static constexpr bool IsNullTerm() { return t_null_term; }
373
374 constexpr explicit BufferAllocator()
375 : m_buffer(emptyPtr<t_null_term>())
376 , m_filled_size(0)
377#ifndef NDEVR_LIB_SIZE_FUNCTION
379#endif
380 {}
381 constexpr BufferAllocator(BufferAllocator&& allocator) noexcept
383 , m_filled_size(0)
384#ifndef NDEVR_LIB_SIZE_FUNCTION
386#endif
387 {
388 std::swap(allocator.m_buffer, m_buffer);
389 std::swap(allocator.m_filled_size, m_filled_size);
390#ifndef NDEVR_LIB_SIZE_FUNCTION
391 std::swap(allocator.m_allocated_size, m_allocated_size);
392#endif
393 }
395 {
396 if (capacity() != 0)
397 free(m_buffer);
398 }
399 template<bool t_managed>
400 void createSpace(t_index_type size)
401 {
402 m_filled_size += size;
405 }
406 template<bool t_managed>
407 void createSpace(t_index_type location, t_index_type size)
408 {
410 memmove(begin() + location + size, begin() + location, sizeof(t_type) * (m_filled_size - (size + location)));
411 }
412 void addLast()
413 {
415 }
421
422 [[nodiscard]] t_index_type count(const t_type& value) const
423 {
424 t_index_type count = 0;
425 for (t_index_type i = 0; i < m_filled_size; i++)
426 {
427 if (m_buffer[i] == value)
428 ++count;
429 }
430 return count;
431 }
432
433 /**--------------------------------------------------------------------------------------------------
434 Adds index.
435
436 Author: Tyler Parke
437
438 Date: 2017-11-19
439 **/
440
441 void addIndex()
442 {
444 }
445
446 /**--------------------------------------------------------------------------------------------------
447 Adds an index.
448
449 Author: Tyler Parke
450
451 Date: 2017-11-19
452
453 Parameters:
454 location - The location.
455 **/
456
457 void addIndex(t_index_type location)
458 {
459 lib_assert(location <= m_filled_size, "Out of bounds add");
461 memmove(m_buffer + location + 1, m_buffer + location, sizeof(t_type) * (m_filled_size - location - 1));
462
463 }
464
465 /**--------------------------------------------------------------------------------------------------
466 Removes the index described by location.
467
468 Author: Tyler Parke
469
470 Date: 2017-11-19
471
472 Parameters:
473 location - The location.
474 **/
475#ifdef __clang__
476 #pragma clang diagnostic push
477 #pragma clang diagnostic ignored "-Wdynamic-class-memaccess"
478#endif
479 void removeIndex(t_index_type location)
480 {
481 lib_assert(location <= m_filled_size, "Out of bounds remove");
482 memmove(m_buffer + location, m_buffer + location + 1, (m_filled_size - location - 1) * sizeof(t_type));
485 }
486#ifdef __clang__
487 #pragma clang diagnostic pop
488#endif
489 /**--------------------------------------------------------------------------------------------------
490 Compares this const BufferAllocator&amp; object to another to determine their relative
491 ordering.
492
493 Author: Tyler Parke
494
495 Date: 2017-11-19
496
497 Parameters:
498 allocator - The constant buffer allocator&amp; to compare to this object.
499
500 \returns Negative if 'allocator' is less than '', 0 if they are equal, or positive if it is
501 greater.
502 **/
503
504 [[nodiscard]] sint04 compare(const BufferAllocator& allocator) const
505 {
506 return cast<sint04>(memcmp(m_buffer, allocator.m_buffer, sizeof(t_type) * getMin(allocator.filledSize(), m_filled_size)));
507 }
508
509 /**--------------------------------------------------------------------------------------------------
510
511 Compares objects.
512
513 Author: Tyler Parke
514
515 Date: 2017-11-19
516
517 Parameters:
518 allocator - Constant buffer allocator&amp; to be compared.
519 start - Uint 04 to be compared.
520 end - Uint 04 to be compared.
521
522 \returns Negative if 'allocator' is less than 'start', 0 if they are equal, or positive if it is
523 greater.
524 **/
525
526 [[nodiscard]] sint04 compare(const BufferAllocator& allocator, t_index_type start, t_index_type end) const
527 {
528 return cast<sint04>(memcmp(&(m_buffer[start]), &(allocator.m_buffer[start]), sizeof(t_type) * getMin(allocator.filledSize(), m_filled_size, end)));
529 }
530
531 /**--------------------------------------------------------------------------------------------------
532 Swaps.
533
534 Author: Tyler Parke
535
536 Date: 2017-11-19
537
538 Parameters:
539 index_a - The index a.
540 index_b - The index b.
541 **/
542
543 void swap(t_index_type index_a, t_index_type index_b)
544 {
545 std::swap(m_buffer[index_a], m_buffer[index_b]);
546 }
547
548 /**--------------------------------------------------------------------------------------------------
549 Clears this object to its blank/initial state.
550
551 Author: Tyler Parke
552
553 Date: 2017-11-19
554 **/
555
556 void clear()
557 {
558 m_filled_size = 0;
560 }
561
562 /**--------------------------------------------------------------------------------------------------
563 Sets a size.
564
565 Author: Tyler Parke
566
567 Date: 2017-11-19
568
569 Parameters:
570 size - The size.
571 **/
572
573 void setSize(t_index_type size)
574 {
575 m_filled_size = size;
578 }
579
580 /**--------------------------------------------------------------------------------------------------
581 Gets a t type&amp; using the given index.
582
583 Author: Tyler Parke
584
585 Date: 2017-11-19
586
587 Parameters:
588 index - The index to get.
589
590 \returns A reference to a t_type.
591 **/
592
593 t_type& get(t_index_type index)
594 {
595 return m_buffer[index];
596 }
597
598 /**--------------------------------------------------------------------------------------------------
599 Gets a constant type&amp; using the given index.
600
601 Author: Tyler Parke
602
603 Date: 2017-11-19
604
605 Parameters:
606 index - The index to get.
607
608 \returns A reference to a const t_type.
609 **/
610
611 [[nodiscard]] const t_type& get(t_index_type index) const
612 {
613 return m_buffer[index];
614 }
615
616 t_type* ptr()
617 {
618 return m_buffer;
619 }
620
621 [[nodiscard]] const t_type* ptr() const
622 {
623 return m_buffer;
624 }
625
626 [[nodiscard]] t_index_type memSize() const
627 {
628 return m_filled_size * sizeof(t_type);
629 }
630
631 /**--------------------------------------------------------------------------------------------------
632 Gets the begin.
633
634 Author: Tyler Parke
635
636 Date: 2017-11-19
637
638 \returns Null if it fails, else a pointer to a t_type.
639 **/
640
641 [[nodiscard]] t_type* begin()
642 {
643 return m_buffer;
644 }
645
646 /**--------------------------------------------------------------------------------------------------
647 Gets the begin.
648
649 Author: Tyler Parke
650
651 Date: 2017-11-19
652
653 \returns Null if it fails, else a pointer to a const t_type.
654 **/
655
656 [[nodiscard]] const t_type* begin() const
657 {
658 return m_buffer;
659 }
660
661 /**--------------------------------------------------------------------------------------------------
662 Begins the given index.
663
664 Author: Tyler Parke
665
666 Date: 2017-11-19
667
668 Parameters:
669 index - Zero-based index of the.
670
671 \returns Null if it fails, else a pointer to a t_type.
672 **/
673
674 [[nodiscard]] t_type* begin(t_index_type index) const
675 {
676 return &(m_buffer[index]);
677 }
678
679 /**--------------------------------------------------------------------------------------------------
680 Gets the capacity of the buffer or total space allocated to it.
681
682 Author: Tyler Parke
683
684 Date: 2017-04-15
685
686 \returns The capacity of the current allocated buffer.
687 **/
688 [[nodiscard]] t_index_type capacity() const
689 {
690#ifdef NDEVR_LIB_SIZE_FUNCTION
692 if(t_null_term)
693 return cast<t_index_type>(NDEVR_LIB_SIZE_FUNCTION(m_buffer) / sizeof(t_type)) - 1;
694 else
695 return cast<t_index_type>(NDEVR_LIB_SIZE_FUNCTION(m_buffer) / sizeof(t_type));
696 else
697 return 0;
698#else
699 return m_allocated_size;
700#endif
701 }
702
703 /**--------------------------------------------------------------------------------------------------
704 Gets the end.
705
706 Author: Tyler Parke
707
708 Date: 2017-11-19
709
710 \returns Null if it fails, else a pointer to a t_type.
711 **/
712
713 [[nodiscard]] t_type* end()
714 {
715 if (capacity() == 0)
716 return m_buffer;
717 return &(get(m_filled_size));
718 }
719
720 /**--------------------------------------------------------------------------------------------------
721 Gets the end.
722
723 Author: Tyler Parke
724
725 Date: 2017-11-19
726
727 \returns Null if it fails, else a pointer to a const t_type.
728 **/
729
730 [[nodiscard]] const t_type* end() const
731 {
732 if (capacity() == 0)
733 return m_buffer;
734 return &(get(m_filled_size));
735 }
736
737 /**--------------------------------------------------------------------------------------------------
738 Ends the given index.
739
740 Author: Tyler Parke
741
742 Date: 2017-11-19
743
744 Parameters:
745 index - Zero-based index of the.
746
747 \returns Null if it fails, else a pointer to a t_type.
748 **/
749
750 t_type* end(t_index_type index)
751 {
752 return &(get(m_filled_size - index));
753 }
754
755 /**--------------------------------------------------------------------------------------------------
756 Resize space.
757
758 Author: Tyler Parke
759
760 Date: 2017-11-19
761
762 Parameters:
763 new_size - Size of the new.
764 **/
765
766 inline void resizeSpace(t_index_type new_size)
767 {
768 if (new_size == 0)
769 {
770 if (capacity() != 0)
771 free(m_buffer);
772 m_buffer = emptyPtr<t_null_term>();//this will provide the needed null termination without a malloc
773 }
774 else
775 {
776 if (capacity() == 0)
777 m_buffer = (t_type*)malloc(sizeof(t_type) * (t_null_term ? new_size + 1 : new_size));
778 else
779 m_buffer = (t_type*)realloc(m_buffer, sizeof(t_type) * (t_null_term ? new_size + 1 : new_size));
780 if (m_buffer == nullptr)
781 {
783 lib_assert(false, "Unable to allocate buffer: Perhaps out of system memory!");
784 }
785 }
786#ifndef NDEVR_LIB_SIZE_FUNCTION
787 m_allocated_size = new_size;
788#endif
789 }
790
791 /**--------------------------------------------------------------------------------------------------
792 Sets all.
793
794 Author: Tyler Parke
795
796 Date: 2017-11-19
797
798 Parameters:
799 object - The object.
800 offset - The offset.
801 size - The size.
802 **/
803
804 void setAll(const t_type& object, t_index_type offset, t_index_type size)
805 {
806 //if (sizeof(int) == sizeof(t_type))
807 {
808 //memset(&m_buffer[offset], *((int*)(&object)), size);
809 }
810 //else
811 {
812 for (t_index_type i = offset; i < offset + size; i++)
813 {
814 m_buffer[i] = object;
815 }
816 }
817 }
818
819 /**--------------------------------------------------------------------------------------------------
820
821 Sets all.
822
823 Author: Tyler Parke
824
825 Date: 2017-11-19
826
827 Typeparams:
828 is_primitive - Type of the is primitive.
829 Parameters:
830 src - Source for the.
831 offset - The offset.
832 size - The size.
833 **/
834
835 template<bool is_primitive>
836 void setAll(const t_type* src, t_index_type offset, t_index_type size)
837 {
838 if constexpr (is_primitive)
839 {
840 memmove(&m_buffer[offset], src, size * sizeof(t_type));
841 }
842 else
843 {
844 for (t_index_type i = 0; i < size; i++)
845 {
846 m_buffer[i + offset] = src[i];
847 }
848 }
849 }
850
851 /**--------------------------------------------------------------------------------------------------
852 Sets all.
853
854 Author: Tyler Parke
855
856 Date: 2017-11-19
857
858 Typeparams:
859 true - Type of the true.
860 Parameters:
861 src - Source for the.
862 offset - The offset.
863 size - The size.
864 **/
865 /*template<>
866 void setAll<true>(const t_type* src, t_index_type offset, t_index_type size)
867 {
868 memmove(&m_buffer[offset], src, size * sizeof(t_type));
869 }*/
870
871 /**--------------------------------------------------------------------------------------------------
872 Sets all.
873
874 Author: Tyler Parke
875
876 Date: 2017-11-19
877
878 Typeparams:
879 false - Type of the false.
880 Parameters:
881 src - Source for the.
882 offset - The offset.
883 size - The size.
884 **/
885 /*template<>
886 void setAll<false>(const t_type* src, t_index_type offset, t_index_type size)
887 {
888 for (t_index_type i = 0; i < size; i++)
889 {
890 m_buffer[i + offset] = src[i];
891 }
892 }*/
893
894 /**--------------------------------------------------------------------------------------------------
895
896 Sets all.
897
898 Author: Tyler Parke
899
900 Date: 2017-11-19
901
902 Typeparams:
903 is_primitive - Type of the is primitive.
904 Parameters:
905 allocator - The allocator.
906 offset - The offset.
907 other_offset - The other offset.
908 size - The size.
909 **/
910
911 template<bool is_primitive, class t_other_index_type, bool t_other_null_term>
912 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)
913 {
914 if constexpr (is_primitive)
915 {
916 memmove(&m_buffer[offset], &(allocator.m_buffer[other_offset]), size * sizeof(t_type));
917 }
918 else
919 {
920 for (t_index_type i = 0; i < size; i++)
921 {
922 m_buffer[i + offset] = allocator.get(i + other_offset);
923 }
924 }
925 }
926
927 /**--------------------------------------------------------------------------------------------------
928
929 Sets all from source.
930
931 Author: Tyler Parke
932
933 Date: 2017-11-19
934
935 Parameters:
936 allocator - The allocator.
937 offset - The offset.
938 other_offset - The other offset.
939 size - The size.
940 **/
941 template<class t_other_allocator>
942 void setAllFromSource(const t_other_allocator& allocator, t_index_type offset, t_index_type other_offset, t_index_type size)
943 {
944 for (t_index_type i = 0; i < size; i++)
945 {
946 m_buffer[i + offset] = allocator.get(i + other_offset);
947 }
948 }
949
950 /**--------------------------------------------------------------------------------------------------
951 Allocation size check.
952
953 Author: Tyler Parke
954
955 Date: 2017-11-19
956 **/
957 template<bool t_managed>
959 {
960 if constexpr (t_managed)//We only 1, assume more to come shortly and allocate more space than required
961 {
962 if(m_filled_size >= capacity())
963 {
966 }
967 }
968 else //We added more than 1, increase buffer only once for compaction
969 {
970 if (m_filled_size == capacity() + 1)
971 {
974 }
975 else if (m_filled_size > capacity())
976 {
978 }
979 }
980 }
981
982 /**--------------------------------------------------------------------------------------------------
983 Removes all index.
984
985 Author: Tyler Parke
986
987 Date: 2017-11-19
988
989 Parameters:
990 start - The start.
991 end - The end.
992 **/
993
994 void removeAllIndex(t_index_type start, t_index_type end)
995 {
996 if (end == m_filled_size)
997 {
998 m_filled_size = start;
999 }
1000 else
1001 {
1002 memmove(&m_buffer[start], &m_buffer[end], (m_filled_size - end) * sizeof(t_type));
1003 m_filled_size -= (end - start);
1004 }
1006 }
1007 template<class t_range_buffer>
1008 void removeAllIndices(const t_range_buffer& ranges)
1009 {
1010 uint04 offset = 0;
1011 for (uint04 i = 0; i < ranges.size(); i++)
1012 {
1013 auto range = ranges[i];
1014 t_index_type start = range.first;
1015 t_index_type end = range.second;
1016 uint04 next = i == ranges.size() - 1 ? m_filled_size : ranges[i + 1].start;
1017 memmove(&m_buffer[start], &m_buffer[end], (next - (end + offset)) * sizeof(t_type));
1018 offset += (end - start);
1019 }
1020 m_filled_size -= offset;
1022 }
1023
1024 /**--------------------------------------------------------------------------------------------------
1025 Null terminator check.
1026
1027 Author: Tyler Parke
1028
1029 Date: 2017-11-19
1030
1031 Typeparams:
1032 t_is_null - Type of the is null.
1033 **/
1034
1035 template<bool t_is_null>
1037 {
1038 if constexpr (t_is_null)
1039 {
1040 if (capacity() != 0)
1041 memset(&(m_buffer[m_filled_size]), 0, sizeof(t_type));//Ensure null termination
1042 }
1043 }
1044
1045 /*template<>
1046 inline void nullTerminatorCheck<true>()
1047 {
1048 if (capacity() != 0)
1049 memset(&(m_buffer[m_filled_size]), 0, sizeof(t_type));//Ensure null termination
1050 }
1051 template<>
1052
1053 inline void nullTerminatorCheck<false>() {}*/
1054
1055
1056 /**--------------------------------------------------------------------------------------------------
1057 Null terminator check.
1058
1059 Author: Tyler Parke
1060
1061 Date: 2017-11-19
1062
1063 Typeparams:
1064 t_is_null - Type of the is null.
1065 **/
1066
1067 template<bool t_is_null>
1068 [[nodiscard]] t_type* emptyPtr() const
1069 {
1070 if constexpr(t_is_null)
1071 {
1072 static constexpr t_type null_object = {};
1073 return const_cast<t_type*>(&null_object);
1074 }
1075 else
1076 {
1077 return nullptr;
1078 }
1079 }
1080
1081 /**--------------------------------------------------------------------------------------------------
1082 Filled size.
1083
1084 Author: Tyler Parke
1085
1086 Date: 2017-11-19
1087
1088 \returns An t_index_type.
1089 **/
1090
1091 [[nodiscard]] constexpr t_index_type filledSize() const
1092 {
1093 return m_filled_size;
1094 }
1095 public:
1096
1097 /**--------------------------------------------------------------------------------------------------
1098 Move assignment operator.
1099
1100 Author: Tyler Parke
1101
1102 Date: 2017-11-19
1103
1104 Parameters:
1105 value - [in,out] The value.
1106
1107 \returns A shallow copy of this object.
1108 **/
1109
1111 {
1112 std::swap(value.m_buffer, m_buffer);
1113 std::swap(value.m_filled_size, m_filled_size);
1114#ifndef NDEVR_LIB_SIZE_FUNCTION
1115 std::swap(value.m_allocated_size, m_allocated_size);
1116#endif
1117 return *this;
1118 }
1119 public:
1120 /** The buffer that stores the actual data. */
1121 t_type* m_buffer;
1122 t_index_type m_filled_size;
1123#ifndef NDEVR_LIB_SIZE_FUNCTION
1124 t_index_type m_allocated_size;
1125#endif
1126 };
1127}
#define lib_assert(expression, message)
Definition LibAssert.h:61
Specific logic for reserving memory for a Buffer. When managed, and more memory is needed memory is r...
Definition Pointer.hpp:311
void createSpace(t_index_type location, t_index_type size)
Definition MemoryManager.h:407
const t_type & get(t_index_type index) const
Definition MemoryManager.h:611
sint04 compare(const BufferAllocator &allocator, t_index_type start, t_index_type end) const
Definition MemoryManager.h:526
void setAllFromSource(const t_other_allocator &allocator, t_index_type offset, t_index_type other_offset, t_index_type size)
Definition MemoryManager.h:942
void createSpace(t_index_type size)
Definition MemoryManager.h:400
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:912
t_index_type m_filled_size
Definition MemoryManager.h:1122
const t_type * begin() const
Definition MemoryManager.h:656
void addIndex(t_index_type location)
Definition MemoryManager.h:457
sint04 compare(const BufferAllocator &allocator) const
Definition MemoryManager.h:504
static constexpr bool IsNullTerm()
Definition MemoryManager.h:372
t_type * end()
Definition MemoryManager.h:713
t_index_type capacity() const
Definition MemoryManager.h:688
void allocationSizeCheck()
Definition MemoryManager.h:958
constexpr BufferAllocator(BufferAllocator &&allocator) noexcept
Definition MemoryManager.h:381
void removeAllIndex(t_index_type start, t_index_type end)
Definition MemoryManager.h:994
void nullTerminatorCheck()
Definition MemoryManager.h:1036
void removeAllIndices(const t_range_buffer &ranges)
Definition MemoryManager.h:1008
constexpr t_index_type filledSize() const
Definition MemoryManager.h:1091
t_type * begin(t_index_type index) const
Definition MemoryManager.h:674
t_type * end(t_index_type index)
Definition MemoryManager.h:750
void swap(t_index_type index_a, t_index_type index_b)
Definition MemoryManager.h:543
const t_type * end() const
Definition MemoryManager.h:730
t_type & get(t_index_type index)
Definition MemoryManager.h:593
BufferAllocator & operator=(BufferAllocator &&value) noexcept
Definition MemoryManager.h:1110
t_type * emptyPtr() const
Definition MemoryManager.h:1068
void removeIndex(t_index_type location)
Definition MemoryManager.h:479
void setAll(const t_type *src, t_index_type offset, t_index_type size)
Definition MemoryManager.h:836
t_index_type m_allocated_size
Definition MemoryManager.h:1124
void addLast()
Definition MemoryManager.h:412
const t_type * ptr() const
Definition MemoryManager.h:621
t_index_type memSize() const
Definition MemoryManager.h:626
constexpr BufferAllocator()
Definition MemoryManager.h:374
t_type * begin()
Definition MemoryManager.h:641
t_type * ptr()
Definition MemoryManager.h:616
void clear()
Definition MemoryManager.h:556
~BufferAllocator()
Definition MemoryManager.h:394
void resizeSpace(t_index_type new_size)
Definition MemoryManager.h:766
void setAll(const t_type &object, t_index_type offset, t_index_type size)
Definition MemoryManager.h:804
t_type * m_buffer
Definition MemoryManager.h:1121
void setSize(t_index_type size)
Definition MemoryManager.h:573
t_index_type count(const t_type &value) const
Definition MemoryManager.h:422
void removeLast()
Definition MemoryManager.h:416
void addIndex()
Definition MemoryManager.h:441
Definition MemoryManager.h:261
static void AllocateElement(t_buffer_type &buffer, t_index_type start, t_index_type size, t_type &object)
Definition MemoryManager.h:321
static constexpr bool isPrimitive()
Definition MemoryManager.h:358
static void Deallocate(t_buffer_type &buffer, t_index_type start, t_index_type size)
Definition MemoryManager.h:296
static void AllocateElement(t_buffer_type &buffer, t_index_type index, t_type &&object)
Definition MemoryManager.h:344
static void AllocateElement(t_buffer_type &buffer, t_index_type index, const t_type &object)
Definition MemoryManager.h:339
static void AllocateElement(t_buffer_type &buffer, t_index_type start, t_index_type size, const t_type &object)
Definition MemoryManager.h:316
static void Allocate(t_buffer_type &buffer, t_index_type start, t_index_type size)
Definition MemoryManager.h:278
static void AllocateElement(t_buffer_type &buff, t_index_type index, const t_type &object)
Definition MemoryManager.h:239
static void AllocateElement(t_buffer_type &buff, t_index_type index, t_type &&object)
Definition MemoryManager.h:244
static void Allocate(t_buffer_type &buff, t_index_type start, t_index_type size)
Definition MemoryManager.h:167
static void Deallocate(t_buffer_type &buff, t_index_type start, t_index_type size)
Definition MemoryManager.h:190
static void AllocateElement(t_buffer_type &buff, t_index_type start, t_index_type size, const t_type &object)
Definition MemoryManager.h:215
static void AllocateElement(t_buffer_type &buff, t_index_type index, const t_type &object)
Definition MemoryManager.h:127
static void AllocateElement(t_buffer_type &buff, t_index_type index, t_type &&object)
Definition MemoryManager.h:132
static void Allocate(t_buffer_type &, t_index_type, t_index_type)
Definition MemoryManager.h:67
static void AllocateElement(t_buffer_type &buff, t_index_type start, t_index_type size, const t_type &object)
Definition MemoryManager.h:106
static void Deallocate(t_buffer_type &, t_index_type, t_index_type)
Definition MemoryManager.h:86
The default object allocator for Buffer.
Definition MemoryManager.h:53
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:64
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:96
constexpr t_to cast(const Angle< t_from > &value)
Definition Angle.h:375
constexpr t_type getMin(const t_type &left, const t_type &right)
Finds the minimum of the given arguments based on the < operator Author: Tyler Parke Date: 2017-11-05...
Definition BaseFunctions.hpp:56
Defines for a given type (such as sint04, fltp08, UUID, etc) a maximum, minimum, and reserved 'invali...
Definition BaseValues.hpp:233