forked from livecode/livecode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfoundation-core.cpp
More file actions
333 lines (268 loc) · 6.85 KB
/
Copy pathfoundation-core.cpp
File metadata and controls
333 lines (268 loc) · 6.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/* Copyright (C) 2003-2013 Runtime Revolution Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
#include <foundation.h>
#include <foundation-stdlib.h>
#include "foundation-private.h"
////////////////////////////////////////////////////////////////////////////////
#ifdef __LINUX__
const char *__MCSysCharset;
#endif
////////////////////////////////////////////////////////////////////////////////
bool MCInitialize(void)
{
if (!__MCUnicodeInitialize())
return false;
if (!__MCValueInitialize())
return false;
if (!__MCStringInitialize())
return false;
if (!__MCNameInitialize())
return false;
if (!__MCErrorInitialize())
return false;
if (!__MCTypeInfoInitialize())
return false;
if (!__MCForeignValueInitialize())
return false;
if (!__MCNumberInitialize())
return false;
if (!__MCArrayInitialize())
return false;
if (!__MCListInitialize())
return false;
if (!__MCSetInitialize())
return false;
if (!__MCDataInitialize())
return false;
if (!__MCRecordInitialize())
return false;
if (!__MCLocaleInitialize())
return false;
if (!__MCProperListInitialize())
return false;
if (!__MCStreamInitialize())
return false;
return true;
}
void MCFinalize(void)
{
__MCStreamFinalize();
__MCProperListFinalize();
__MCLocaleFinalize();
__MCRecordFinalize();
__MCDataFinalize();
__MCSetFinalize();
__MCListFinalize();
__MCArrayFinalize();
__MCNumberFinalize();
__MCForeignValueFinalize();
__MCTypeInfoFinalize();
__MCErrorFinalize();
__MCNameFinalize();
__MCStringFinalize();
__MCValueFinalize();
__MCUnicodeFinalize();
}
////////////////////////////////////////////////////////////////////////////////
MC_DLLEXPORT_DEF
bool MCMemoryAllocate(size_t p_size, void*& r_block)
{
void *t_block;
t_block = malloc(p_size != 0 ? p_size : 4);
if (t_block != nil)
{
r_block = t_block;
return true;
}
return MCErrorThrowOutOfMemory();
}
MC_DLLEXPORT_DEF
bool MCMemoryAllocateCopy(const void *p_block, size_t p_block_size, void*& r_block)
{
if (MCMemoryAllocate(p_block_size, r_block))
{
MCMemoryCopy(r_block, p_block, p_block_size);
return true;
}
return MCErrorThrowOutOfMemory();
}
MC_DLLEXPORT_DEF
bool MCMemoryReallocate(void *p_block, size_t p_new_size, void*& r_new_block)
{
void *t_new_block;
t_new_block = realloc(p_block, p_new_size != 0 ? p_new_size : 4);
if (t_new_block != nil)
{
r_new_block = t_new_block;
return true;
}
return MCErrorThrowOutOfMemory();
}
MC_DLLEXPORT_DEF
void MCMemoryDeallocate(void *p_block)
{
free(p_block);
}
//////////
MC_DLLEXPORT_DEF
bool MCMemoryNew(size_t p_size, void*& r_record)
{
if (MCMemoryAllocate(p_size, r_record))
{
MCMemoryClear(r_record, p_size);
return true;
}
return false;
}
MC_DLLEXPORT_DEF
void MCMemoryDelete(void *p_record)
{
MCMemoryDeallocate(p_record);
}
//////////
bool MCMemoryNewArray(uindex_t p_count, size_t p_size, void*& r_array)
{
if (MCMemoryAllocate(p_count * p_size, r_array))
{
MCMemoryClear(r_array, p_count * p_size);
return true;
}
return false;
}
bool MCMemoryNewArray(uindex_t p_count, size_t p_size, void*& r_array, uindex_t& r_count)
{
if (MCMemoryAllocate(p_count * p_size, r_array))
{
MCMemoryClear(r_array, p_count * p_size);
r_count = p_count;
return true;
}
return false;
}
bool MCMemoryResizeArray(uindex_t p_new_count, size_t p_size, void*& x_array, uindex_t& x_count)
{
if (MCMemoryReallocate(x_array, p_new_count * p_size, x_array))
{
if (p_new_count > x_count)
MCMemoryClear(static_cast<char *>(x_array) + x_count * p_size, (p_new_count - x_count) * p_size);
x_count = p_new_count;
return true;
}
return false;
}
void MCMemoryDeleteArray(void *p_array)
{
MCMemoryDeallocate(p_array);
}
////////////////////////////////////////////////////////////////////////////////
// These hash functions are taken from CoreFoundation - if they are good enough
// for Apple, they should be good enough for us :)
#define HASHFACTOR 2654435761U
/* Templates for hashing arbitrary-sized integers */
template <typename T>
static inline hash_t
MCHashUInt(T i)
{
hash_t h = 0;
for (unsigned int o = 0; o < sizeof(T); o += sizeof(hash_t))
h += HASHFACTOR * (hash_t) (i >> (o << 3));
return h;
}
template <typename T>
static inline hash_t
MCHashInt(T i)
{
return MCHashUInt((i >= 0) ? i : (-i));
}
MC_DLLEXPORT_DEF
hash_t MCHashInteger(integer_t i)
{
return MCHashInt (i);
}
MC_DLLEXPORT_DEF
hash_t
MCHashUInteger (uinteger_t i)
{
return MCHashUInt(i);
}
MC_DLLEXPORT_DEF
hash_t
MCHashSize (ssize_t i)
{
return MCHashInt (i);
}
hash_t
MCHashUSize (size_t i)
{
return MCHashUInt (i);
}
MC_DLLEXPORT_DEF
hash_t MCHashPointer(void *p)
{
return MCHashUInt((uintptr_t) p);
}
MC_DLLEXPORT_DEF
hash_t MCHashDouble(double d)
{
double i;
if (d < 0)
d = -d;
i = floor(d + 0.5);
hash_t t_integral_hash;
t_integral_hash = HASHFACTOR * (hash_t)fmod(i, (double)UINT32_MAX);
return (hash_t)(t_integral_hash + (hash_t)((d - i) * UINT32_MAX));
}
#define ELF_STEP(B) T1 = (H << 4) + B; T2 = T1 & 0xF0000000; if (T2) T1 ^= (T2 >> 24); T1 &= (~T2); H = T1;
MC_DLLEXPORT_DEF
hash_t MCHashBytes(const void *p_bytes, size_t length)
{
uint8_t *bytes = (uint8_t *)p_bytes;
/* The ELF hash algorithm, used in the ELF object file format */
uint32_t H = 0, T1, T2;
int32_t rem = length;
while (3 < rem)
{
ELF_STEP(bytes[length - rem]);
ELF_STEP(bytes[length - rem + 1]);
ELF_STEP(bytes[length - rem + 2]);
ELF_STEP(bytes[length - rem + 3]);
rem -= 4;
}
switch (rem)
{
case 3: ELF_STEP(bytes[length - 3]);
case 2: ELF_STEP(bytes[length - 2]);
case 1: ELF_STEP(bytes[length - 1]);
case 0: ;
}
return H;
}
MC_DLLEXPORT_DEF
hash_t MCHashBytesStream(hash_t p_start, const void *p_bytes, size_t length)
{
MCAssert((length % 4) == 0);
uint8_t *bytes = (uint8_t *)p_bytes;
/* The ELF hash algorithm, used in the ELF object file format */
uint32_t H = p_start, T1, T2;
int32_t rem = length;
while (3 < rem)
{
ELF_STEP(bytes[length - rem]);
ELF_STEP(bytes[length - rem + 1]);
ELF_STEP(bytes[length - rem + 2]);
ELF_STEP(bytes[length - rem + 3]);
rem -= 4;
}
return H;
}
#undef ELF_STEP
////////////////////////////////////////////////////////////////////////////////