Dynalib Utils
DynaAllocImpl.h
Go to the documentation of this file.
1 //
2 // Created by Ken Kopelson on 18/10/17.
3 //
4 
5 #ifndef DYNAALLOCIMPL_H
6 #define DYNAALLOCIMPL_H
7 
8 #include <iostream>
9 #include <type_traits>
10 using namespace std;
11 #include "BitManip.h"
12 #include "DynaAlloc.h"
13 #include "CheckForError.h"
14 
15 template <class T> T* DynaAllocArray<T>::newArray(uint count) {
16  return reallocArray(nullptr, 0, count);
17 }
18 
19 template <class T> T* DynaAllocArray<T>::reallocArray(T* array, uint oldCount, uint newCount) {
20  if (newCount != oldCount || array == nullptr) {
21  try {
22  if (array == nullptr) {
23  array = new T[newCount];
24  memset((void*)array, 0, newCount * sizeof(T));
25  }
26  else if (newCount == 0) {
27  array = deleteArray(array);
28  }
29  else {
30  T* newArray = new T[newCount];
31  uint copyCount = newCount > oldCount ? oldCount : newCount;
32  memcpy(newArray, array, copyCount * sizeof(T));
33 
34  delete[] array;
35  array = newArray;
36  if (newCount > oldCount)
37  memset((array + oldCount), 0, (newCount - oldCount) * sizeof(T));
38  }
39  }
40  catch (std::exception& e) {
41  }
42  }
43  return array;
44 }
45 
46 template <class T> T* DynaAllocArray<T>::deleteArray(T* array) {
47  delete[] array;
48  array = nullptr;
49  return array;
50 }
51 
52 template <class T> T* DynaAllocArray<T>::clearArray(T* array, uint count) {
53  memset((void*)array, 0, count * sizeof(T));
54  return array;
55 }
56 
57 template <class T> T** DynaAllocVect<T>::newVect(uint count) {
58  return reallocVect(nullptr, 0, count, true);
59 }
60 
61 template <class T> T** DynaAllocVect<T>::reallocVect(T** array, uint oldCount, uint newCount, bool isOwner) {
62  if (newCount != oldCount || array == nullptr) {
63  try {
64  if (array == nullptr) {
65  array = new T*[newCount];
66  memset(array, 0, newCount * sizeof(T*));
67  }
68  else if (newCount == 0) {
69  array = deleteVect(array, oldCount, isOwner);
70  }
71  else {
72  T** newArray = new T*[newCount];
73  uint copyCount = newCount > oldCount ? oldCount : newCount;
74  memcpy(newArray, array, copyCount * sizeof(T*));
75  if (isOwner && (newCount < oldCount)) {
79  for (uint idx = newCount; idx < oldCount; ++idx) {
80  delete array[idx];
81  }
82  }
88  memset(array, 0, oldCount * sizeof(T*));
89  delete[] array;
90 
91  if (newCount > oldCount) {
92  memset((newArray + oldCount), 0, (newCount - oldCount) * sizeof(T*));
93  }
94  array = newArray;
95  }
96  }
97  catch (std::exception& e) {
98  }
99  }
100  return array;
101 }
102 
103 template <class T> T** DynaAllocVect<T>::deleteVect(T** array, uint arrayCount, bool isOwner) {
104  if (isOwner) {
105  for (uint idx = 0; idx < arrayCount; ++idx) {
106  delete array[idx];
107  }
108  }
109  memset(array, 0, arrayCount * sizeof(T*));
110  delete[] array;
111  array = nullptr;
112  return array;
113 }
114 
115 template <class T> uint DynaAllocArray<T>::ALLOC_UNITS = 10;
116 
117 template <class T> uint DynaAllocVect<T>::ALLOC_UNITS = 10;
118 
119 #endif //DYNAALLOCIMPL_H
Definition: DynaAlloc.h:13
static T * newArray(uint count)
Definition: DynaAllocImpl.h:15
static T ** deleteVect(T **array, uint arrayCount, bool isOwner)
Definition: DynaAllocImpl.h:103
static T * reallocArray(T *array, uint oldCount, uint newCount)
Definition: DynaAllocImpl.h:19
static T * deleteArray(T *array)
Definition: DynaAllocImpl.h:46
static T ** newVect(uint count)
Definition: DynaAllocImpl.h:57
static T ** reallocVect(T **array, uint oldCount, uint newCount, bool isOwner)
Definition: DynaAllocImpl.h:61
static T * clearArray(T *array, uint count)
Definition: DynaAllocImpl.h:52
Definition: DynaAlloc.h:26