1 /*
2 Copyright (C) 2015-2021, Dirk Krause
3 SPDX-License-Identifier: BSD-3-Clause
4 */
5 
6 /*
7 	WARNING: This file was generated by the dkct program (see
8 	http://dktools.sourceforge.net/ for details).
9 	Changes you make here will be lost if dkct is run again!
10 	You should modify the original source and run dkct on it.
11 	Original source: dk4mem.ctr
12 */
13 
14 #ifndef DK4MEM_H_INCLUDED
15 /** Avoid multiple inclusions. */
16 #define DK4MEM_H_INCLUDED 1
17 
18 
19 /**	@file	dk4mem.h Memory operations.
20 
21 	Memory allocation and deallocation;
22 	copy, reset and compare memory regions.
23 
24 	The functions provided by the C runtime libraries are used by
25 	default. On Windows you can decide to use Windows API functions
26 	instead, define DK4_WIN_AVOID_CRT or DK4_WIN_DENY_CRT to 1.
27 
28 	When using Windows API functions dynamic memory allocations use
29 	the global heap. Define DK4_USE_WINDOWS_LOCAL_ALLOC to 1 to
30 	use the local heap instead.
31 */
32 
33 #ifndef DK4CONF_H_INCLUDED
34 #if DK4_BUILDING_DKTOOLS4
35 #include "dk4conf.h"
36 #else
37 #include <dktools-4/dk4conf.h>
38 #endif
39 #endif
40 
41 #ifndef DK4TYPES_H_INCLUDED
42 #if DK4_BUILDING_DKTOOLS4
43 #include <libdk4base/dk4types.h>
44 #else
45 #include <dktools-4/dk4types.h>
46 #endif
47 #endif
48 
49 #ifndef DK4ERROR_H_INCLUDED
50 #if DK4_BUILDING_DKTOOLS4
51 #include <libdk4base/dk4error.h>
52 #else
53 #include <dktools-4/dk4error.h>
54 #endif
55 #endif
56 
57 #if	DK4_HAVE_STRING_H
58 #ifndef	STRING_H_INCLUDED
59 #include <string.h>
60 #define	STRING_H_INCLUDED 1
61 #endif
62 #endif
63 
64 #if	DK4_HAVE_STRINGS_H
65 #ifndef	STRINGS_H_INCLUDED
66 #include <strings.h>
67 #define	STRINGS_H_INCLUDED 1
68 #endif
69 #endif
70 
71 
72 #ifdef __cplusplus
73 extern "C" {
74 #endif
75 
76 #if	((!(DK4_ON_WINDOWS)) && (!(DK4_HAVE_MEMCPY)) && (!(DK4_HAVE_BCOPY)))
77 
78 /**	Fallback function to use if no suitable memory copy function is found.
79 	This function does not do any error checking.
80 	The caller is responsible to provide valid arguments.
81 	If dst and src overlap, src must be right from dst.
82 
83 	CRT on Windows: Not used.
84 	@param	dst	Destination buffer address.
85 	@param	src	Source buffer address.
86 	@param	sz	Number of bytes to copy.
87 */
88 void
89 dk4mem_fallback_cpy(
90   void       *dst,
91   const void *src,
92   size_t      sz
93 );
94 
95 #endif
96 
97 
98 
99 #if	((!(DK4_ON_WINDOWS)) && (!(DK4_HAVE_MEMSET)) && (!(DK4_HAVE_BZERO)))
100 
101 /**	Fallback function to use if no suitable memory reseet function is found.
102 	This function does not do any error checking.
103 	The caller is responsible to provide valid arguments.
104 
105 	CRT on Windows: Not used.
106 	@param	bptr	Address of buffer to reset.
107 	@param	sz	Number of bytes to reset.
108 */
109 void
110 dk4mem_fallback_reset(
111   void       *bptr,
112   size_t      sz
113 );
114 
115 #endif
116 
117 #if	(DK4_ON_WINDOWS && (DK4_WIN_AVOID_CRT || DK4_WIN_DENY_CRT)) \
118 || ((!(DK4_ON_WINDOWS)) && (!(DK4_HAVE_MEMCMP)) && (!(DK4_HAVE_BCMP)))
119 
120 /**	Fallback function to use if no suitable memory comparison function
121 	is found.
122 	This function does not do any error checking.
123 	The caller is responsible to provide valid arguments.
124 
125 	CRT on Windows: Not used.
126 	@param	s1	Address of left buffer in comparison.
127 	@param	s2	Address of right buffer in comparison.
128 	@param	sz	Buffer size (number of bytes).
129 	@return	Positive value if s1>s2, 0 for equal buffers, -1 if s1<s2.
130 */
131 int
132 
133 dk4mem_fallback_cmp(
134   const void *s1,
135   const void *s2,
136   size_t      sz
137 );
138 
139 #endif
140 
141 /**	Reset memory range.
142 	Do not use dk4mem_reset() to sanitize memory (overwrite sensitive
143 	data before memory is freed or the program exits),
144 	use dk4mem_reset_secure() from dk4memrs.h instead.
145 
146 	CRT on Windows: Optional.
147 	@param	bptr	Pointer to start of buffer.
148 	@param	sz	Buffer size (number of bytes).
149 	@param	erp	Error report for diagnostics, may be NULL.
150 
151 	Error codes:
152 	- DK4_E_INVALID_ARGUMENTS<br>
153 	  if bptr is NULL or sz is 0.
154 */
155 void
156 dk4mem_reset(
157   void       *bptr,
158   size_t      sz,
159   dk4_er_t   *erp
160 );
161 
162 /**	Set memory range to specified value.
163 
164 	This function uses self-made fallback code if memset() is not
165 	available or use of the CRT is disabled on Windows.
166 	To reset memory, the recommended function is dk4mem_reset().
167 
168 	CRT on Windows:	Optional, disabling CRT degrades performance.
169 	@param	bptr	Pointer to start of buffer.
170 	@param	sz	Buffer size (number of bytes).
171 	@param	uc	Byte value to write.
172 	@param	erp	Error report for diagnostics, may be NULL.
173 
174 	Error codes:
175 	- DK4_E_INVALID_ARGUMENTS<br>
176 	  if bptr is NULL or sz is 0.
177 */
178 void
179 dk4mem_set(
180   void          *bptr,
181   size_t         sz,
182   unsigned char  uc,
183   dk4_er_t      *erp
184 );
185 
186 /**	Copy memory range, source and destination must not overlap.
187 
188 	CRT on Windows: Optional.
189 	@param	dst	Destination address.
190 	@param	src	Source address.
191 	@param	sz	Range size (number of bytes).
192 	@param	erp	Error report, may be NULL.
193 
194 	Error codes:
195 	- DK4_E_INVALID_ARGUMENTS<br>
196 	  if dst or src is NULL or sz is 0.
197 */
198 void
199 dk4mem_cpy(
200   void       *dst,
201   void const *src,
202   size_t      sz,
203   dk4_er_t   *erp
204 );
205 
206 /**	Compare two memory ranges.
207 
208 	CRT on Windows: Optional, disabling CRT degrades performance.
209 	@param	s1	Left memory range.
210 	@param	s2	Right memory range.
211 	@param	sz	Number of bytes to compare.
212 	@param	erp	Error report, may be NULL.
213 	@return	1 if s1>s2, -1 if s1<s2, 0 if s1==s2.
214 
215 	Error codes:
216 	- DK4_E_INVALID_ARGUMENTS<br>
217 	  if s1 or s2 is NULL or sz is 0.
218 */
219 int
220 dk4mem_cmp(
221   const void *s1,
222   const void *s2,
223   size_t      sz,
224   dk4_er_t   *erp
225 );
226 
227 #if (DK4_ON_WINDOWS) || (DK4_HAVE_MALLOC && DK4_HAVE_FREE)
228 
229 /**	Dynamically allocate memory. The memory range is not initialized.
230 
231 	CRT on Windows: Optional.
232 	@param	bytes	Number of bytes to allocate.
233 	@param	erp	Error report, may be NULL.
234 	@return	Pointer to memory on success, NULL on error.
235 	Error codes:
236 	- DK4_E_INVALID_ARGUMENTS<br>
237 	  if bytes is 0,
238 	- DK4_E_MATH_OVERFLOW<br>
239 	  if the specified size is too large,
240 	- DK4_E_MEMORY_ALLOCATION_FAILED<br>
241 	  with mem.elsize and mem.nelem
242 	  set if there is not enough memory
243 	  available.
244 */
245 void *
246 dk4mem_malloc_bytes(
247   size_t      bytes,
248   dk4_er_t   *erp
249 );
250 
251 /**	Dynamically allocate memory. The memory range is not initialized.
252 
253 	CRT on Windows: Optional.
254 	@param	elsize	Size of each element.
255 	@param	nelem	Number of elements.
256 	@param	erp	Error report, may be NULL.
257 	@return	Pointer to memory on success, NULL on error.
258 
259 	Error codes:
260 	- DK4_E_INVALID_ARGUMENTS<br>
261 	  if elsize or nelem is 0,
262 	- DK4_E_MATH_OVERFLOW<br>
263 	  on numeric overflow when calculating the
264 	  product of elsize and nelem,
265 	- DK4_E_MEMORY_ALLOCATION_FAILED<br>
266 	  with mem.elsize and mem.nelem
267 	  set if there is not enough memory available.
268 */
269 void *
270 dk4mem_malloc(
271   size_t      elsize,
272   size_t      nelem,
273   dk4_er_t   *erp
274 );
275 
276 /**	Dynamically allocate memory and initialize memory range.
277 
278 	CRT on Windows: Optional.
279 	@param	elsize	Size of each element.
280 	@param	nelem	Number of elements.
281 	@param	erp	Error report, may be NULL.
282 	@return	Pointer to memory on success, NULL on error.
283 
284 	Error codes:
285 	- DK4_E_INVALID_ARGUMENTS<br>
286 	  if elsize or nelem is 0,
287 	- DK4_E_MATH_OVERFLOW<br>
288 	  on numeric overflow when calculating the
289 	  product of elsize and nelem,
290 	- DK4_E_MEMORY_ALLOCATION_FAILED<br>
291 	  with mem.elsize and mem.nelem
292 	  set if there is not enough memory available.
293 */
294 void *
295 dk4mem_calloc(
296   size_t      elsize,
297   size_t      nelem,
298   dk4_er_t   *erp
299 );
300 
301 /**	Release memory allocated by dk4mem_malloc() or dk4mem_calloc().
302 	The function checks whether the pointer is NULL and frees
303 	the memory only for non-NULL pointers.
304 
305 	CRT on Windows: Optional.
306 	@param	ptr	Pointer to start address of memory to release.
307 */
308 void
309 
310 dk4mem_free(
311   void       *ptr
312 );
313 
314 #endif
315 
316 #ifdef __cplusplus
317 }
318 #endif
319 
320 
321 /**	Allocate and initialize memory.
322 
323 	CRT on Windows: Optional.
324 	@param	tp	Type to allocate.
325 	@param	ne	Number of elements to allocate.
326 	@param	er	Error report, may be NULL.
327 	@return	Pointer to a new tp on success, NULL on error.
328 
329 	Error codes:
330 	- DK4_E_INVALID_ARGUMENTS<br>
331 	  if elsize or nelem is 0,
332 	- DK4_E_MATH_OVERFLOW<br>
333 	  on numeric overflow when calculating the product of elsize and nelem,
334 	- DK4_E_MEMORY_ALLOCATION_FAILED<br>
335 	  with mem.elsize and mem.nelem set if there is not enough memory
336 	  available.
337 */
338 #define	dk4mem_new(tp,ne,er) 	(tp *)dk4mem_calloc(sizeof(tp),ne,er)
339 
340 
341 
342 /**	Free memory previously allocated.
343 
344 	CRT on Windows: Optional.
345 	@param	ptr	Start address of memory range to free.
346 */
347 #define	dk4mem_release(ptr) \
348 do { if (NULL != ptr) { dk4mem_free(ptr); }  ptr = NULL; } while (0)
349 
350 
351 /**	Find number of elements in array.
352 
353 	CRT on Windows: Not used.
354 	@param	v	Variable.
355 	@param	t	Type.
356 	@return	The size of the variable divided by the size of the type.
357 */
358 #define	DK4_SIZEOF(v,t)	(sizeof(v)/sizeof(t))
359 
360 
361 
362 /**	Increase byte number to alignment size.
363 
364 	CRT on Windows: Not used.
365 	@param	sz	Original byte number.
366 	@param	al	Alignment size, typically 16.
367 	@return	Size corrected for alignment.
368 */
369 #define	DK4_MEM_ALIGN(sz,al) ((0 == (sz % al)) ? (sz) : (al * (1 + (sz/al))))
370 
371 
372 
373 
374 
375 
376 #if DK4_HAVE_INLINE && DK4_USE_INLINE
377 /* +++ DK4_HAVE_INLINE */
378 #if	DK4_ON_WINDOWS
379 /* +++ DK4_HAVE_INLINE, Windows */
380 #if		DK4_WIN_AVOID_CRT || DK4_WIN_DENY_CRT
381 /* +++ DK4_HAVE_INLINE, Windows, ! crt */
382 
383 /**	Pure memory copy operation. All arguments must be checked
384 	by the caller before calling this function.
385 
386 	CRT on Windows: Optional.
387 	@param	dst	Destination buffer address.
388 	@param	src	Source buffer address.
389 	@param	sz	Number of bytes to copy.
390 */
391 static
392 inline
393 void
DK4_MEMCPY(void * dst,const void * src,size_t sz)394 DK4_MEMCPY(void *dst, const void *src, size_t sz)
395 {
396   CopyMemory(dst, src, sz);
397 }
398 
399 /**	Pure memory reset. All arguments must be checked by the caller
400 	before calling this function.
401 	Do not use DK4_MEMRES() to sanitize memory (overwrite sensitive
402 	data before memory is freed or the program exits),
403 	use dk4mem_reset_secure() from dk4memrs.h instead.
404 
405 	CRT on Windows: Optional.
406 	@param	dst	Start address of buffer to reset.
407 	@param	sz	Number of bytes to reset.
408 */
409 static
410 inline
411 void
DK4_MEMRES(void * dst,size_t sz)412 DK4_MEMRES(void *dst, size_t sz)
413 {
414   ZeroMemory(dst, sz);
415 }
416 
417 /**	Pure memory comparison.
418 	All arguments must be checked by the caller before calling this
419 	function.
420 
421 	CRT on Windows: Optional, disabling CRT probably degrades performance.
422 	@param	s1	Address of left buffer in comparison.
423 	@param	s2	Address of right buffer in comparison.
424 	@param	sz	Number of bytes to compare.
425 	@return	1 if s1>s2, 0 for equal buffers, -1 if s1<s2.
426 */
427 static
428 inline
429 int
DK4_MEMCMP(const void * s1,const void * s2,size_t sz)430 DK4_MEMCMP(const void *s1, const void *s2, size_t sz)
431 {
432   return ( dk4mem_fallback_cmp(s1, s2, sz) );
433 }
434 
435 /* --- DK4_HAVE_INLINE, Windows, ! crt */
436 #else
437 /* +++ DK4_HAVE_INLINE, Windows, crt */
438 
439 /**	Pure memory copy operation. All arguments must be checked
440 	by the caller before calling this function.
441 
442 	CRT on Windows: Optional.
443 	@param	dst	Destination buffer address.
444 	@param	src	Source buffer address.
445 	@param	sz	Number of bytes to copy.
446 */
447 static
448 inline
449 void
DK4_MEMCPY(void * dst,const void * src,size_t sz)450 DK4_MEMCPY(void *dst, const void *src, size_t sz)
451 {
452   memcpy(dst, src, sz);
453 }
454 
455 /**	Pure memory reset. All arguments must be checked by the caller
456 	before calling this function.
457 	Do not use DK4_MEMRES() to sanitize memory (overwrite sensitive
458 	data before memory is freed or the program exits),
459 	use dk4mem_reset_secure() from dk4memrs.h instead.
460 
461 	CRT on Windows: Optional.
462 	@param	dst	Start address of buffer to reset.
463 	@param	sz	Number of bytes to reset.
464 */
465 static
466 inline
467 void
DK4_MEMRES(void * dst,size_t sz)468 DK4_MEMRES(void *dst, size_t sz)
469 {
470   memset(dst, 0, sz);
471 }
472 
473 /**	Pure memory comparison.
474 	All arguments must be checked by the caller before calling this
475 	function.
476 
477 	CRT on Windows: Optional, disabling CRT degrades performance.
478 	@param	s1	Address of left buffer in comparison.
479 	@param	s2	Address of right buffer in comparison.
480 	@param	sz	Number of bytes to compare.
481 	@return	1 if s1>s2, 0 for equal buffers, -1 if s1<s2.
482 */
483 static
484 inline
485 int
DK4_MEMCMP(const void * s1,const void * s2,size_t sz)486 DK4_MEMCMP(const void *s1, const void *s2, size_t sz)
487 {
488   return (memcmp(s1, s2, sz));
489 }
490 
491 /* --- DK4_HAVE_INLINE, Windows, crt */
492 #endif
493 /* --- DK4_HAVE_INLINE, Windows */
494 #else
495 /* +++ DK4_HAVE_INLINE, ! Windows */
496 #if		DK4_HAVE_MEMSET
497 /* +++ DK4_HAVE_INLINE, ! Windows, memset */
498 
499 /**	Pure memory reset. All arguments must be checked by the caller
500 	before calling this function.
501 	Do not use DK4_MEMRES() to sanitize memory (overwrite sensitive
502 	data before memory is freed or the program exits),
503 	use dk4mem_reset_secure() from dk4memrs.h instead.
504 
505 	CRT on Windows: Optional.
506 	@param	dst	Start address of buffer to reset.
507 	@param	sz	Number of bytes to reset.
508 */
509 static
510 inline
511 void
DK4_MEMRES(void * dst,size_t sz)512 DK4_MEMRES(void *dst, size_t sz)
513 {
514   memset(dst, 0, sz);
515 }
516 
517 /* --- DK4_HAVE_INLINE, ! Windows, memset */
518 #else
519 /* +++ DK4_HAVE_INLINE, ! Windows, ! memset */
520 #if			DK4_HAVE_BZERO
521 /* +++ DK4_HAVE_INLINE, ! Windows, ! memset, bzero */
522 
523 /**	Pure memory reset. All arguments must be checked by the caller
524 	before calling this function.
525 	Do not use DK4_MEMRES() to sanitize memory (overwrite sensitive
526 	data before memory is freed or the program exits),
527 	use dk4mem_reset_secure() from dk4memrs.h instead.
528 
529 	CRT on Windows: Optional.
530 	@param	dst	Start address of buffer to reset.
531 	@param	sz	Number of bytes to reset.
532 */
533 static
534 inline
535 void
DK4_MEMRES(void * dst,size_t sz)536 DK4_MEMRES(void *dst, size_t sz)
537 {
538   bzero(dst, sz);
539 }
540 
541 /* --- DK4_HAVE_INLINE, ! Windows, ! memset, bzero */
542 #else
543 /* +++ DK4_HAVE_INLINE, ! Windows, ! memset, ! bzero */
544 
545 /**	Pure memory reset. All arguments must be checked by the caller
546 	before calling this function.
547 	Do not use DK4_MEMRES() to sanitize memory (overwrite sensitive
548 	data before memory is freed or the program exits),
549 	use dk4mem_reset_secure() from dk4memrs.h instead.
550 
551 	CRT on Windows: Optional.
552 	@param	dst	Start address of buffer to reset.
553 	@param	sz	Number of bytes to reset.
554 */
555 static
556 inline
557 void
DK4_MEMRES(void * dst,size_t sz)558 DK4_MEMRES(void *dst, size_t sz)
559 {
560   dk4mem_fallback_reset(dst, sz);
561 }
562 
563 /* --- DK4_HAVE_INLINE, ! Windows, ! memset, ! bzero */
564 #endif
565 /* --- DK4_HAVE_INLINE, ! Windows, ! memset */
566 #endif
567 #if		DK4_HAVE_MEMCPY
568 /* +++ DK4_HAVE_INLINE, ! Windows, memcpy */
569 
570 /**	Pure memory copy operation. All arguments must be checked
571 	by the caller before calling this function.
572 
573 	CRT on Windows: Optional.
574 	@param	dst	Destination buffer address.
575 	@param	src	Source buffer address.
576 	@param	sz	Number of bytes to copy.
577 */
578 static
579 inline
580 void
DK4_MEMCPY(void * dst,const void * src,size_t sz)581 DK4_MEMCPY(void *dst, const void *src, size_t sz)
582 {
583   memcpy(dst, src, sz);
584 }
585 
586 /* --- DK4_HAVE_INLINE, ! Windows, memcpy */
587 #else
588 /* +++ DK4_HAVE_INLINE, ! Windows, ! memcpy */
589 #if			DK4_HAVE_BCOPY
590 /* +++ DK4_HAVE_INLINE, ! Windows, ! memcpy, bcopy */
591 
592 /**	Pure memory copy operation. All arguments must be checked
593 	by the caller before calling this function.
594 
595 	CRT on Windows: Optional.
596 	@param	dst	Destination buffer address.
597 	@param	src	Source buffer address.
598 	@param	sz	Number of bytes to copy.
599 */
600 static
601 inline
602 void
DK4_MEMCPY(void * dst,const void * src,size_t sz)603 DK4_MEMCPY(void *dst, const void *src, size_t sz)
604 {
605   bcopy(src, dst, sz);
606 }
607 
608 /* --- DK4_HAVE_INLINE, ! Windows, ! memcpy, bcopy */
609 #else
610 /* +++ DK4_HAVE_INLINE, ! Windows, ! memcpy, ! bcopy */
611 
612 /**	Pure memory copy operation. All arguments must be checked
613 	by the caller before calling this function.
614 
615 	CRT on Windows: Optional.
616 	@param	dst	Destination buffer address.
617 	@param	src	Source buffer address.
618 	@param	sz	Number of bytes to copy.
619 */
620 static
621 inline
622 void
DK4_MEMCPY(void * dst,const void * src,size_t sz)623 DK4_MEMCPY(void *dst, const void *src, size_t sz)
624 {
625   dk4mem_fallback_cpy(dst, src, sz);
626 }
627 
628 /* --- DK4_HAVE_INLINE, ! Windows, ! memcpy, ! bcopy */
629 #endif
630 /* --- DK4_HAVE_INLINE, ! Windows, ! memcpy */
631 #endif
632 #if		DK4_HAVE_MEMCMP
633 /* +++ DK4_HAVE_INLINE, ! Windows, memcmp */
634 
635 /**	Pure memory comparison.
636 	All arguments must be checked by the caller before calling this
637 	function.
638 
639 	CRT on Windows: Optional, disabling CRT degrades performance.
640 	@param	s1	Address of left buffer in comparison.
641 	@param	s2	Address of right buffer in comparison.
642 	@param	sz	Number of bytes to compare.
643 	@return	1 if s1>s2, 0 for equal buffers, -1 if s1<s2.
644 */
645 static
646 inline
647 int
DK4_MEMCMP(const void * s1,const void * s2,size_t sz)648 DK4_MEMCMP(const void *s1, const void *s2, size_t sz)
649 {
650   return (memcmp(s1, s2, sz));
651 }
652 
653 /* --- DK4_HAVE_INLINE, ! Windows, memcmp */
654 #else
655 /* +++ DK4_HAVE_INLINE, ! Windows, ! memcmp */
656 #if			DK4_HAVE_BCMP
657 /* +++ DK4_HAVE_INLINE, ! Windows, ! memcmp, bcmp */
658 
659 /**	Pure memory comparison.
660 	All arguments must be checked by the caller before calling this
661 	function.
662 
663 	CRT on Windows: Optional, disabling CRT degrades performance.
664 	@param	s1	Address of left buffer in comparison.
665 	@param	s2	Address of right buffer in comparison.
666 	@param	sz	Number of bytes to compare.
667 	@return	1 if s1>s2, 0 for equal buffers, -1 if s1<s2.
668 */
669 static
670 inline
671 int
DK4_MEMCMP(const void * s1,const void * s2,size_t sz)672 DK4_MEMCMP(const void *s1, const void *s2, size_t sz)
673 {
674   return (bcmp(s1, s2, sz));
675 }
676 
677 /* --- DK4_HAVE_INLINE, ! Windows, ! memcmp, bcmp */
678 #else
679 /* +++ DK4_HAVE_INLINE, ! Windows, ! memcmp, ! bcmp */
680 
681 /**	Pure memory comparison.
682 	All arguments must be checked by the caller before calling this
683 	function.
684 
685 	CRT on Windows: Optional, disabling CRT degrades performance.
686 	@param	s1	Address of left buffer in comparison.
687 	@param	s2	Address of right buffer in comparison.
688 	@param	sz	Number of bytes to compare.
689 	@return	1 if s1>s2, 0 for equal buffers, -1 if s1<s2.
690 */
691 static
692 inline
693 int
DK4_MEMCMP(const void * s1,const void * s2,size_t sz)694 DK4_MEMCMP(const void *s1, const void *s2, size_t sz)
695 {
696   return ( dk4mem_fallback_cmp(s1, s2, sz) );
697 }
698 
699 /* --- DK4_HAVE_INLINE, ! Windows, ! memcmp, ! bcmp */
700 #endif
701 /* --- DK4_HAVE_INLINE, ! Windows, ! memcmp */
702 #endif
703 /* --- DK4_HAVE_INLINE, ! Windows */
704 #endif
705 /* --- DK4_HAVE_INLINE */
706 #else
707 /* +++ ! DK4_HAVE_INLINE */
708 #if	DK4_ON_WINDOWS
709 /* +++ ! DK4_HAVE_INLINE, Windows */
710 #if		DK4_WIN_AVOID_CRT || DK4_WIN_DENY_CRT
711 /* +++ ! DK4_HAVE_INLINE, Windows, ! crt */
712 
713 /**	Pure memory copy operation. All arguments must be checked
714 	by the caller before calling this function.
715 
716 	CRT on Windows: Optional.
717 	@param	dst	Destination buffer address.
718 	@param	src	Source buffer address.
719 	@param	sz	Number of bytes to copy.
720 */
721 #define	DK4_MEMCPY(dst,src,sz)	CopyMemory(dst, src, sz)
722 
723 /**	Pure memory reset. All arguments must be checked by the caller
724 	before calling this function.
725 	Do not use DK4_MEMRES() to sanitize memory (overwrite sensitive
726 	data before memory is freed or the program exits),
727 	use dk4mem_reset_secure() from dk4memrs.h instead.
728 
729 	CRT on Windows: Optional.
730 	@param	dst	Start address of buffer to reset.
731 	@param	sz	Number of bytes to reset.
732 */
733 #define	DK4_MEMRES(dst,sz)	ZeroMemory(dst, sz)
734 
735 /**	Pure memory comparison.
736 	All arguments must be checked by the caller before calling this
737 	function.
738 
739 	CRT on Windows: Optional, disabling CRT degrades performance.
740 	@param	s1	Address of left buffer in comparison.
741 	@param	s2	Address of right buffer in comparison.
742 	@param	sz	Number of bytes to compare.
743 	@return	1 if s1>s2, 0 for equal buffers, -1 if s1<s2.
744 */
745 #define	DK4_MEMCMP(s1,s2,sz) dk4mem_fallback_cmp(s1, s2, sz)
746 
747 /* --- ! DK4_HAVE_INLINE, Windows, ! crt */
748 #else
749 /* +++ ! DK4_HAVE_INLINE, Windows, crt */
750 
751 /**	Pure memory copy operation. All arguments must be checked
752 	by the caller before calling this function.
753 
754 	CRT on Windows: Optional.
755 	@param	dst	Destination buffer address.
756 	@param	src	Source buffer address.
757 	@param	sz	Number of bytes to copy.
758 */
759 #define	DK4_MEMCPY(dst,src,sz)	memcpy(dst, src, sz)
760 
761 /**	Pure memory reset. All arguments must be checked by the caller
762 	before calling this function.
763 	Do not use DK4_MEMRES() to sanitize memory (overwrite sensitive
764 	data before memory is freed or the program exits),
765 	use dk4mem_reset_secure() from dk4memrs.h instead.
766 
767 	CRT on Windows: Optional.
768 	@param	dst	Start address of buffer to reset.
769 	@param	sz	Number of bytes to reset.
770 */
771 #define	DK4_MEMRES(dst,sz)	memset(dst, 0, sz)
772 
773 /**	Pure memory comparison.
774 	All arguments must be checked by the caller before calling this
775 	function.
776 
777 	CRT on Windows: Optional, disabling CRT degrades performance.
778 	@param	s1	Address of left buffer in comparison.
779 	@param	s2	Address of right buffer in comparison.
780 	@param	sz	Number of bytes to compare.
781 	@return	1 if s1>s2, 0 for equal buffers, -1 if s1<s2.
782 */
783 #define	DK4_MEMCMP(s1,s2,sz)	memcmp(s1, s2, sz)
784 
785 /* --- ! DK4_HAVE_INLINE, Windows, crt */
786 #endif
787 /* --- ! DK4_HAVE_INLINE, Windows */
788 #else
789 /* +++ ! DK4_HAVE_INLINE, ! Windows */
790 #if		DK4_HAVE_MEMSET
791 /* +++ ! DK4_HAVE_INLINE, ! Windows, memset */
792 
793 /**	Pure memory reset. All arguments must be checked by the caller
794 	before calling this function.
795 	Do not use DK4_MEMRES() to sanitize memory (overwrite sensitive
796 	data before memory is freed or the program exits),
797 	use dk4mem_reset_secure() from dk4memrs.h instead.
798 
799 	CRT on Windows: Optional.
800 	@param	dst	Start address of buffer to reset.
801 	@param	sz	Number of bytes to reset.
802 */
803 #define	DK4_MEMRES(dst,sz)	memset(dst, 0, sz)
804 
805 /* --- ! DK4_HAVE_INLINE, ! Windows, memset */
806 #else
807 /* +++ ! DK4_HAVE_INLINE, ! Windows, ! memset */
808 #if			DK4_HAVE_BZERO
809 /* +++ ! DK4_HAVE_INLINE, ! Windows, ! memset, bzero */
810 
811 /**	Pure memory reset. All arguments must be checked by the caller
812 	before calling this function.
813 	Do not use DK4_MEMRES() to sanitize memory (overwrite sensitive
814 	data before memory is freed or the program exits),
815 	use dk4mem_reset_secure() from dk4memrs.h instead.
816 
817 	CRT on Windows: Optional.
818 	@param	dst	Start address of buffer to reset.
819 	@param	sz	Number of bytes to reset.
820 */
821 #define	DK4_MEMRES(dst,sz)	bzero(dst, sz)
822 
823 /* --- ! DK4_HAVE_INLINE, ! Windows, ! memset, bzero */
824 #else
825 /* +++ ! DK4_HAVE_INLINE, ! Windows, ! memset, ! bzero */
826 
827 /**	Pure memory reset. All arguments must be checked by the caller
828 	before calling this function.
829 	Do not use DK4_MEMRES() to sanitize memory (overwrite sensitive
830 	data before memory is freed or the program exits),
831 	use dk4mem_reset_secure() from dk4memrs.h instead.
832 
833 	CRT on Windows: Optional.
834 	@param	dst	Start address of buffer to reset.
835 	@param	sz	Number of bytes to reset.
836 */
837 #define	DK4_MEMRES(dst,sz) dk4mem_fallback_reset(dst, sz)
838 
839 /* --- ! DK4_HAVE_INLINE, ! Windows, ! memset, ! bzero */
840 #endif
841 /* --- ! DK4_HAVE_INLINE, ! Windows, ! memset */
842 #endif
843 #if		DK4_HAVE_MEMCPY
844 /* +++ ! DK4_HAVE_INLINE, ! Windows, memcpy */
845 
846 /**	Pure memory copy operation. All arguments must be checked
847 	by the caller before calling this function.
848 
849 	CRT on Windows: Optional.
850 	@param	dst	Destination buffer address.
851 	@param	src	Source buffer address.
852 	@param	sz	Number of bytes to copy.
853 */
854 #define	DK4_MEMCPY(dst,src,sz)	memcpy(dst, src, sz)
855 
856 /* --- ! DK4_HAVE_INLINE, ! Windows, memcpy */
857 #else
858 /* +++ ! DK4_HAVE_INLINE, ! Windows, ! memcpy */
859 #if			DK4_HAVE_BCOPY
860 /* +++ ! DK4_HAVE_INLINE, ! Windows, ! memcpy, bcopy */
861 
862 /**	Pure memory copy operation. All arguments must be checked
863 	by the caller before calling this function.
864 
865 	CRT on Windows: Optional.
866 	@param	dst	Destination buffer address.
867 	@param	src	Source buffer address.
868 	@param	sz	Number of bytes to copy.
869 */
870 #define	DK4_MEMCPY(dst,src,sz)	bcopy(src, dst, sz)
871 
872 /* --- ! DK4_HAVE_INLINE, ! Windows, ! memcpy, bcopy */
873 #else
874 /* +++ ! DK4_HAVE_INLINE, ! Windows, ! memcpy, ! bcopy */
875 
876 /**	Pure memory copy operation. All arguments must be checked
877 	by the caller before calling this function.
878 
879 	CRT on Windows: Optional.
880 	@param	dst	Destination buffer address.
881 	@param	src	Source buffer address.
882 	@param	sz	Number of bytes to copy.
883 */
884 #define	DK4_MEMCPY(dst,src,sz) dk4mem_fallback_cpy(dst, src, sz)
885 
886 /* --- ! DK4_HAVE_INLINE, ! Windows, ! memcpy, ! bcopy */
887 #endif
888 /* --- ! DK4_HAVE_INLINE, ! Windows, ! memcpy */
889 #endif
890 #if		DK4_HAVE_MEMCMP
891 /* +++ ! DK4_HAVE_INLINE, ! Windows, memcmp */
892 
893 /**	Pure memory comparison.
894 	All arguments must be checked by the caller before calling this
895 	function.
896 
897 	CRT on Windows: Optional, disabling CRT degrades performance.
898 	@param	s1	Address of left buffer in comparison.
899 	@param	s2	Address of right buffer in comparison.
900 	@param	sz	Number of bytes to compare.
901 	@return	1 if s1>s2, 0 for equal buffers, -1 if s1<s2.
902 */
903 #define	DK4_MEMCMP(s1,s2,sz)	memcmp(s1, s2, sz)
904 
905 /* --- ! DK4_HAVE_INLINE, ! Windows, memcmp */
906 #else
907 /* +++ ! DK4_HAVE_INLINE, ! Windows, ! memcmp */
908 #if			DK4_HAVE_BCMP
909 /* +++ ! DK4_HAVE_INLINE, ! Windows, ! memcmp, bcmp */
910 
911 /**	Pure memory comparison.
912 	All arguments must be checked by the caller before calling this
913 	function.
914 
915 	CRT on Windows: Optional, disabling CRT degrades performance.
916 	@param	s1	Address of left buffer in comparison.
917 	@param	s2	Address of right buffer in comparison.
918 	@param	sz	Number of bytes to compare.
919 	@return	1 if s1>s2, 0 for equal buffers, -1 if s1<s2.
920 */
921 #define	DK4_MEMCMP(s1,s2,sz)	bcmp(s1, s2, sz)
922 
923 /* --- ! DK4_HAVE_INLINE, ! Windows, ! memcmp, bcmp */
924 #else
925 /* +++ ! DK4_HAVE_INLINE, ! Windows, ! memcmp, ! bcmp */
926 
927 /**	Pure memory comparison.
928 	All arguments must be checked by the caller before calling this
929 	function.
930 
931 	CRT on Windows: Optional, disabling CRT degrades performance.
932 	@param	s1	Address of left buffer in comparison.
933 	@param	s2	Address of right buffer in comparison.
934 	@param	sz	Number of bytes to compare.
935 	@return	1 if s1>s2, 0 for equal buffers, -1 if s1<s2.
936 */
937 #define	DK4_MEMCMP(s1,s2,sz) dk4mem_fallback_cmp(s1, s2, sz)
938 
939 /* --- ! DK4_HAVE_INLINE, ! Windows, ! memcmp, ! bcmp */
940 #endif
941 /* --- ! DK4_HAVE_INLINE, ! Windows, ! memcmp */
942 #endif
943 /* --- ! DK4_HAVE_INLINE, ! Windows */
944 #endif
945 /* --- ! DK4_HAVE_INLINE */
946 #endif
947 
948 
949 
950 #endif
951