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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
|
%% options
copyright owner = Dirk Krause
copyright year = 2011-xxxx
license = bsd
%% header
#include "dk3conf.h"
#include "dk3types.h"
#ifdef __cplusplus
extern "C" {
#endif
/** Reset memory range.
* @param mp Memory pointer.
* @param sz Size of memory range in bytes.
*/
void
dk3mem_res(void *mp, size_t sz) ;
/** Reset memory range.
* @param mp Memory pointer.
* @param sz Size of memory range in bytes.
* @return 1 on success, 0 on error.
*/
int
dk3mem_reset_secure(void *mp, size_t sz) ;
/** Copy memory range. The buffers must not overlap!
* @param dp Destination buffer pointer.
* @param sp Source pointer.
* @param sz Number of bytes to copy.
*/
void
dk3mem_cpy(void *dp, void const *sp, size_t sz);
/** Compare two memory ranges byte by byte.
* @param s1 Left pointer.
* @param s2 Right pointer.
* @param sz Maximum number of bytes to use in comparison.
* @return 1 for s1>s2, 0 for s1==s2, -1 for s1<s2.
*/
int
dk3mem_cmp(void const *s1, void const *s2, size_t sz);
/** Allocate memory dynamically.
* @param sz Number of bytes to allocate.
* @return Pointer to new memory on success, NULL on error.
*/
void *
dk3mem_malloc(size_t sz);
/** Allocate memory dynamically.
* @param sz Number of bytes to allocate.
* @param app Application structure for diagnostics, may be NULL.
* @return Pointer to new memory on success, NULL on error.
*/
void *
dk3mem_malloc_app(size_t sz, dk3_app_t *app);
/** Allocate memory dynamically.
* @param sz Element size.
* @param ne Number of elements.
* @return Pointer to new memory on success, NULL on error.
*/
void *
dk3mem_alloc(size_t sz, size_t ne);
/** Allocate memory dynamically.
* @param sz Element size.
* @param ne Number of elements.
* @param app Application structure for diagnostics, may be NULL.
* @return Pointer to new memory on success, NULL on error.
*/
void *
dk3mem_alloc_app(size_t sz, size_t ne, dk3_app_t *app);
/** Release dynamically allocated memory.
* @param po Pointer to memory.
*/
void
dk3mem_free(void *po);
/** Calculate number of bytes (product element size, number of elements).
@param sz Element size.
@param ne Number of elements.
@param ec Pointer to error code variable, may be NULL.
Set to DK3_ERROR_MATH_OUT_OF_RANGE if the element size sz is 0,
DK3_ERROR_MATH_OVERFLOW on numeric overflow.
@return Product on success, 0 on error (mathematical overflow,
one of the sizes 0).
*/
size_t
dk3mem_mul_size_t(size_t sz, size_t ne, int *ec);
/** Summary of two sizes.
@param a One size.
@param b Other size.
@param ec Pointer to error code variable, may be NULL.
Set to DK3_ERROR_MATH_OVERFLOW if a numeric overflow occurs.
@return Positive number on success, 0 on error (mathematical overflow).
*/
size_t
dk3mem_add_size_t(size_t a, size_t b, int *ec);
#ifdef __cplusplus
}
#endif
/** Get number of elements in an array.
* @param v Variable.
* @param t Type.
* @return Number of elememnts of type \a t in \a v.
*/
#define DK3_SIZEOF(v,t) (sizeof(v)/sizeof(t))
/** Calculate byte alignment for a given size, round upwards if necessary.
@param a Original size.
@param b Alignment size.
@return a, corrected upwards if necessary.
*/
#define DK3_MEM_ALIGN(a,b) \
((0 == b) ? (a) : ((0 == (a) % (b)) ? (a) : ((b) * (((a) / (b)) + 1))))
/** Allocate memory dynamically.
* @param t Type.
* @param s Number of elements.
* @return Type-casted pointer to new memory or NULL.
*/
#define dk3_new(t,s) ((t *)dk3mem_alloc_app(sizeof(t),(size_t)(s),NULL))
/** Allocate memory dynamically.
* @param t Type.
* @param s Number of elements.
* @param a Application structure for diagnostics, may be NULL.
* @return Type-casted pointer to new memory or NULL.
*/
#define dk3_new_app(t,s,a) ((t *)dk3mem_alloc_app(sizeof(t),(size_t)(s),(a)))
/** Release memory.
* @param po Pointer to memory to release.
*/
#define dk3_delete(po) \
do { if(po) { dk3mem_free((void *)(po)); } } while(0)
/** Release memory.
* @param po Pointer to memory to release.
*/
#define dk3_release(po) \
do { if(po) { dk3mem_free((void *)(po)); } po = NULL; } while(0)
/** Release C++ object.
* @param po Object pointer.
*/
#define dk3_cpp_release(po) \
do { if(po) { delete(po); } po = NULL; } while(0)
%% module
#include "dk3all.h"
$(trace-include)
void
dk3mem_res(void *mp, size_t sz)
/* {{{ */
{
if((NULL != mp) && (0 != sz)) {
#if DK3_HAVE_MEMSET
memset(mp, (int)'\0', sz);
#else
#if DK3_HAVE_BZERO
bzero(mp, sz);
#else
register char *ptr;
register size_t i;
ptr = (char *)mp; i = sz;
while(i--) { *(ptr++) = '\0'; }
#endif
#endif
}
}
/* }}} */
void
dk3mem_cpy(void *dp, void const *sp, size_t sz)
/* {{{ */
{
if((NULL != dp) && (NULL != sp) && (0 != sz)) {
#if DK3_HAVE_MEMCPY
memcpy(dp, sp, sz);
#else
#if DK3_HAVE_BCOPY
bcopy(sp, dp, sz);
#else
register char *mydp;
register char const *mysp;
register size_t i;
mydp = (char *)dp;
mysp = (char const *)sp;
i = sz;
while(i--) { *(mydp++) = *(mysp++); }
#endif
#endif
}
}
/* }}} */
int
dk3mem_cmp(void const *s1, void const *s2, size_t sz)
/* {{{ */
{
int back = 0;
if(NULL != s1) {
if(NULL != s2) {
#if DK3_HAVE_MEMCMP
back = memcmp(s1, s2, sz);
#else
#if DK3_HAVE_BCMP
back = bcmp(s1, s2, sz);
#else
register unsigned char const *mys1;
register unsigned char const *mys2;
register size_t i;
register int myback = 0;
mys1 = (unsigned char const *)s1;
mys2 = (unsigned char const *)s2;
for(i = 0; ((i < sz) && (0 == myback)); i++) {
if(*mys1 > *mys2) {
myback = 1;
} else {
if(*mys1 < *mys2) {
myback = -1;
} else {
mys1++; mys2++;
}
}
}
back = myback;
#endif
#endif
} else { back = 1; }
} else {
if(s2) { back = -1; }
}
return back;
}
/* }}} */
void *
dk3mem_malloc_app(size_t sz, dk3_app_t *app)
/* {{{ */
{
void *back = NULL;
char c8buffer[64]; /* Buffer to show number of bytes. */
dkChar dkbuffer[64]; /* Buffer to show number of bytes. */
$? "+ dk3mem_malloc %u", (unsigned)sz
if(0 != sz) {
#if DK3_ON_WINDOWS && (_MSC_VER > 1100)
$? ". use LocalAlloc"
back = (void *)LocalAlloc((LMEM_FIXED | LMEM_ZEROINIT), sz);
#else
#if DK3_HAVE_MALLOC && DK3_HAVE_FREE
$? ". use malloc %u", (unsigned)sz
back = malloc(sz);
$? ". malloc used %!8s", TR_8PTR(back)
#else
#error "The malloc and free functions are not available here!"
#endif
#endif
if(NULL != back) { $? ". attempt to reset"
dk3mem_res(back, sz);
} else { $? "! allocation failed"
if(NULL != app) { $? ". writing error message"
/* ERROR: Failed to allocate n bytes! */
if(0 != dk3ma_um_to_c8_string(c8buffer,sizeof(c8buffer),(dk3_um_t)sz)) {
(void)dk3str_cnv_c8_to_str_app(
dkbuffer, DK3_SIZEOF(dkbuffer,dkChar), c8buffer, NULL
);
dk3app_log_i3(app, DK3_LL_ERROR, 12, 13, dkbuffer);
} else {
dk3app_log_i1(app, DK3_LL_ERROR, 14);
}
}
}
} $? "- dk3mem_malloc %!8s", TR_8PTR(back)
return back;
}
/* }}} */
void *
dk3mem_malloc(size_t sz)
/* {{{ */
{
void *back;
back = dk3mem_malloc_app(sz, NULL);
return back;
}
/* }}} */
size_t
dk3mem_mul_size_t(size_t sz, size_t ne, int *ec)
/* {{{ */
{
size_t back = 0;
if (0 < sz) {
if ((DK3_SIZE_T_MAX / sz) < ne) {
if (NULL != ec) { *ec = DK3_ERROR_MATH_OVERFLOW; }
} else {
back = (sz * ne);
}
} else {
if (NULL != ec) { *ec = DK3_ERROR_MATH_OUT_OF_RANGE; }
}
return back;
}
/* }}} */
size_t
dk3mem_add_size_t(size_t a, size_t b, int *ec)
{
size_t back = 0;
if((DK3_SIZE_T_MAX - a) >= b) { back = a + b; }
else { if(NULL != ec) { *ec = DK3_ERROR_MATH_OVERFLOW; } }
return back;
}
void *
dk3mem_alloc_app(size_t sz, size_t ne, dk3_app_t *app)
/* {{{ */
{
void *back = NULL;
size_t as = 0; /* Allocation size. */
char c8b1[64]; /* Buffer for element size. */
char c8b2[64]; /* Buffer for number of elements. */
dkChar dkb1[64]; /* Buffer for element size. */
dkChar dkb2[64]; /* Buffer for number of elements. */
int ok = 0; /* Flag: Success. */
$? "+ dk3mem_alloc %u %u", (unsigned)sz, (unsigned)ne
if((0 != sz) && (0 != ne)) {
as = dk3mem_mul_size_t(sz, ne, NULL);
if(0 != as) {
back = dk3mem_malloc_app(as, app);
} else {
if(NULL != app) {
/* ERROR: Numeric overflow! */
ok = 0;
dkb1[0] = dkb2[0] = dkT('\0');
if(0 != dk3ma_um_to_c8_string(c8b1, sizeof(c8b1),(dk3_um_t)sz)) {
if(0 != dk3ma_um_to_c8_string(c8b2, sizeof(c8b2),(dk3_um_t)ne)) {
(void)dk3str_cnv_c8_to_str_app(
dkb1, DK3_SIZEOF(dkb1,dkChar), c8b1, NULL
);
(void)dk3str_cnv_c8_to_str_app(
dkb2, DK3_SIZEOF(dkb2,dkChar), c8b2, NULL
);
ok = 1;
}
}
if(0 != ok) {
dk3app_log_i5(app, DK3_LL_ERROR, 16, 17, 18, dkb1, dkb2);
} else {
dk3app_log_i1(app, DK3_LL_ERROR, 15);
}
}
}
} $? "- dk3mem_alloc %!8s", TR_8PTR(back)
return back;
}
/* }}} */
void *
dk3mem_alloc(size_t sz, size_t ne)
/* {{{ */
{
void *back;
back = dk3mem_alloc_app(sz, ne, NULL);
return back;
}
/* }}} */
void
dk3mem_free (void *po)
/* {{{ */
{
if(NULL != po) {
#if DK3_ON_WINDOWS && (_MSC_VER > 1100)
LocalFree((HLOCAL)po);
#else
#if DK3_HAVE_MALLOC && DK3_HAVE_FREE
free(po);
#endif
#endif
}
}
/* }}} */
int
dk3mem_reset_secure(void *mp, size_t sz)
{
char *ptr;
int back = 0;
if ((NULL != mp) && (0 < sz)) {
#if DK3_ON_WINDOWS
SecureZeroMemory(mp, sz);
back = 1;
#else
dk3mem_res(mp, sz);
back = 1;
ptr = (char *)mp;
while (sz--) {
if ('\0' != *(ptr++)) {
back = 0;
}
}
#endif
}
return back;
}
/* vim: set ai sw=2 filetype=c foldmethod=marker foldopen=all : */
|