21 #ifndef SELDON_FILE_ALLOCATOR_CXX
23 #include "Allocator.hxx"
35 typename MallocObject<T>::pointer
36 MallocObject<T>::allocate(
size_t num,
void* h)
42 void* memory_block = malloc(
sizeof(
size_t) +
sizeof(
char*) +
43 (num + 2) *
sizeof(T));
44 memcpy(memory_block, &num,
sizeof(
size_t));
45 char* data =
static_cast<char*
>(memory_block)
46 +
sizeof(
size_t) +
sizeof(
char*) +
sizeof(T);
49 pointer data_P =
reinterpret_cast<pointer
>(
new(data) T[num]);
50 size_t taille =
sizeof(
char*);
51 memcpy(
reinterpret_cast<char *
>(data_P) -
sizeof(
char*),
52 &memory_block, taille);
59 void MallocObject<T>::deallocate(pointer data,
size_t num,
void* h)
62 char* ptr =
reinterpret_cast<char *
>(data) -
sizeof(
char*);
63 size_t taille =
sizeof(
char*);
64 memcpy(&memory_block, ptr, taille);
65 for (
size_t i = 0; i < num; i++)
73 void* MallocObject<T>::reallocate(pointer data,
size_t num,
void* h)
76 return allocate(num, h);
78 void * memory_block;
size_t taille =
sizeof(
char*);
80 reinterpret_cast<char *
>(data) -
sizeof(
char*), taille);
82 size_t initial_num = *
reinterpret_cast<long*
>(memory_block);
84 if (initial_num < num)
86 memory_block = realloc(memory_block,
sizeof(
size_t) +
sizeof(
char*) +
87 (num + 2) *
sizeof(T));
89 new(
static_cast<char *
>(memory_block) +
sizeof(
size_t) +
sizeof(T) +
90 sizeof(
char*) + initial_num *
sizeof(T)) T[num - initial_num];
92 else if (initial_num > num)
94 for (
size_t i = num; i < initial_num; i++)
97 memory_block = realloc(memory_block,
sizeof(
size_t) +
sizeof(
char*) +
98 (num + 2) *
sizeof(T));
104 memcpy(memory_block, &num,
sizeof(
size_t));
107 reinterpret_cast<pointer
>(
static_cast<char*
>(memory_block) +
108 sizeof(
size_t) +
sizeof(
char*) +
sizeof(T));
109 memcpy(
reinterpret_cast<char *
>(data_P) -
sizeof(
char*),
110 &memory_block, taille);
117 void MallocObject<T>::memoryset(pointer data,
char c,
size_t num)
119 memset(
reinterpret_cast<void*
>(data), c, num);
125 MallocObject<T>::memorycpy(pointer datat, pointer datas,
size_t num)
127 for (
size_t i = 0; i < num; i++)
138 typename NaNAlloc<T>::pointer
139 NaNAlloc<T>::allocate(
size_t num,
void* h)
141 pointer data =
static_cast<pointer
>( malloc(num *
sizeof(T)) );
142 if (numeric_limits<value_type>::has_signaling_NaN)
143 for (
size_t i = 0; i < num; i++)
144 data[i] = numeric_limits<value_type>::signaling_NaN();
145 else if (numeric_limits<value_type>::has_quiet_NaN)
146 for (
size_t i = 0; i < num; i++)
147 data[i] = numeric_limits<value_type>::quiet_NaN();
148 else if (numeric_limits<value_type>::has_infinity)
149 for (
size_t i = 0; i < num; i++)
150 data[i] = numeric_limits<value_type>::infinity();
155 void* NaNAlloc<T>::reallocate(pointer data,
size_t num,
void* h)
157 void* datav = realloc(
reinterpret_cast<void*
>(data), num *
sizeof(T));
158 pointer datap =
reinterpret_cast<pointer
>(datav);
159 if (numeric_limits<value_type>::has_signaling_NaN)
160 for (
size_t i = 0; i < num; i++)
161 datap[i] = numeric_limits<value_type>::signaling_NaN();
162 else if (numeric_limits<value_type>::has_quiet_NaN)
163 for (
size_t i = 0; i < num; i++)
164 datap[i] = numeric_limits<value_type>::quiet_NaN();
165 else if (numeric_limits<value_type>::has_infinity)
166 for (
size_t i = 0; i < num; i++)
167 datap[i] = numeric_limits<value_type>::infinity();
173 #define SELDON_FILE_ALLOCATOR_CXX