/* WARNING: This file was generated by dkct. Changes you make here will be lost if dkct is run again! You should modify the original source and run dkct on it. Original source: dk4mem.ctr */ /* Copyright (C) 2015-2017, Dirk Krause Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above opyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the author nor the names of contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifndef DK4MEM_H_INCLUDED /** Avoid multiple inclusions. */ #define DK4MEM_H_INCLUDED 1 #line 9 "dk4mem.ctr" /** @file Memory allocation and deallocation; copy, reset and compare memory regions. The functions provided by the C runtime libraries are used by default. On Windows you can decide to use Windows API functions instead, define DK4_WIN_AVOID_CRT or DK4_WIN_DENY_CRT to 1. When using Windows API functions dynamic memory allocations use the global heap. Define DK4_USE_WINDOWS_LOCAL_ALLOC to 1 to use the local heap instead. */ #ifndef DK4CONF_H_INCLUDED #include "dk4conf.h" #endif #ifndef DK4TYPES_H_INCLUDED #include "dk4types.h" #endif #ifndef DK4ERROR_H_INCLUDED #include "dk4error.h" #endif #if DK4_HAVE_STRING_H #ifndef STRING_H_INCLUDED #include #define STRING_H_INCLUDED 1 #endif #endif #if DK4_HAVE_STRINGS_H #ifndef STRINGS_H_INCLUDED #include #define STRINGS_H_INCLUDED 1 #endif #endif #ifdef __cplusplus extern "C" { #endif /** Fallback function to use if no suitable memory copy function is found. This function does not do any error checking. The caller is responsible to provide valid arguments. @param dst Destination buffer address. @param src Source buffer address. @param sz Number of bytes to copy. */ void dk4mem_fallback_cpy(void *dst, const void *src, size_t sz); /** Fallback function to use if no suitable memory reseet function is found. This function does not do any error checking. The caller is responsible to provide valid arguments. @param bptr Address of buffer to reset. @param sz Number of bytes to reset. */ void dk4mem_fallback_reset(void *bptr, size_t sz); /** Fallback function to use if no suitable memory comparison function is found. This function does not do any error checking. The caller is responsible to provide valid arguments. @param s1 Address of left buffer in comparison. @param s2 Address of right buffer in comparison. @param sz Buffer size (number of bytes). @return Positive value if s1>s2, 0 for equal buffers, -1 if s1 if bptr is NULL or sz is 0. */ void dk4mem_reset(void *bptr, size_t sz, dk4_er_t *erp); /** Set memory range to specified value. CRT on Windows: Optional. @param bptr Pointer to start of buffer. @param sz Buffer size (number of bytes). @param uc Byte value to write. @param erp Error report for diagnostics, may be NULL. Error codes: - DK4_E_INVALID_ARGUMENTS
if bptr is NULL or sz is 0. */ void dk4mem_set(void *bptr, size_t sz, unsigned char uc, dk4_er_t *erp); /** Copy memory range. CRT on Windows: Optional. @param dst Destination address. @param src Source address. @param sz Range size (number of bytes). @param erp Error report, may be NULL. Error codes: - DK4_E_INVALID_ARGUMENTS
if dst or src is NULL or sz is 0. */ void dk4mem_cpy(void *dst, void const *src, size_t sz, dk4_er_t *erp); /** Compare two memory ranges. CRT on Windows: Optional, disabling CRT degrades performance. @param s1 Left memory range. @param s2 Right memory range. @param sz Number of bytes to compare. @param erp Error report, may be NULL. @return 1 if s1>s2, -1 if s1 if s1 or s2 is NULL or sz is 0. */ int dk4mem_cmp(const void *s1, const void *s2, size_t sz, dk4_er_t *erp); #if (DK4_ON_WINDOWS) || (DK4_HAVE_MALLOC && DK4_HAVE_FREE) /** Dynamically allocate memory. The memory range is not initialized. CRT on Windows: Optional. @param bytes Number of bytes to allocate. @param erp Error report, may be NULL. @return Pointer to memory on success, NULL on error. Error codes: - DK4_E_INVALID_ARGUMENTS
if bytes is 0, - DK4_E_MATH_OVERFLOW
if the specified size is too large, - DK4_E_MEMORY_ALLOCATION_FAILED
with mem.elsize and mem.nelem set if there is not enough memory available. */ void * dk4mem_malloc_bytes(size_t bytes, dk4_er_t *erp); /** Dynamically allocate memory. The memory range is not initialized. CRT on Windows: Optional. @param elsize Size of each element. @param nelem Number of elements. @param erp Error report, may be NULL. @return Pointer to memory on success, NULL on error. Error codes: - DK4_E_INVALID_ARGUMENTS
if elsize or nelem is 0, - DK4_E_MATH_OVERFLOW
on numeric overflow when calculating the product of elsize and nelem, - DK4_E_MEMORY_ALLOCATION_FAILED
with mem.elsize and mem.nelem set if there is not enough memory available. */ void * dk4mem_malloc(size_t elsize, size_t nelem, dk4_er_t *erp); /** Dynamically allocate memory and initialize memory range. CRT on Windows: Optional. @param elsize Size of each element. @param nelem Number of elements. @param erp Error report, may be NULL. @return Pointer to memory on success, NULL on error. Error codes: - DK4_E_INVALID_ARGUMENTS
if elsize or nelem is 0, - DK4_E_MATH_OVERFLOW
on numeric overflow when calculating the product of elsize and nelem, - DK4_E_MEMORY_ALLOCATION_FAILED
with mem.elsize and mem.nelem set if there is not enough memory available. */ void * dk4mem_calloc(size_t elsize, size_t nelem, dk4_er_t *erp); /** Release memory allocated by dk4mem_malloc() or dk4mem_calloc(). The function checks whether the pointer is NULL and frees the memory only for non-NULL pointers. CRT on Windows: Optional. @param ptr Pointer to start address of memory to release. */ void dk4mem_free(void *ptr); #endif /* if defined(_WIN32) ... */ #ifdef __cplusplus } #endif /** Allocate and initialize memory. @param tp Type to allocate. @param ne Number of elements to allocate. @param er Error report, may be NULL. @return Pointer to a new tp on success, NULL on error. Error codes: - DK4_E_INVALID_ARGUMENTS
if elsize or nelem is 0, - DK4_E_MATH_OVERFLOW
on numeric overflow when calculating the product of elsize and nelem, - DK4_E_MEMORY_ALLOCATION_FAILED
with mem.elsize and mem.nelem set if there is not enough memory available. */ #define dk4mem_new(tp,ne,er) (tp *)dk4mem_calloc(sizeof(tp),ne,er) /** Free memory previously allocated. @param ptr Start address of memory range to free. */ #define dk4mem_release(ptr) \ do { if (NULL != ptr) { dk4mem_free(ptr); } ptr = NULL; } while (0) /** Find number of elements in array. @param v Variable. @param t Type. @return The size of the variable divided by the size of the type. */ #define DK4_SIZEOF(v,t) (sizeof(v)/sizeof(t)) /** Increase byte number to alignment size. @param sz Original byte number. @param al Alignment size, typically 16. @return Size corrected for alignment. */ #define DK4_MEM_ALIGN(sz,al) ((0 == (sz % al)) ? (sz) : (al * (1 + (sz/al)))) #if DK4_HAVE_INLINE /* +++ DK4_HAVE_INLINE */ #if DK4_ON_WINDOWS /* +++ DK4_HAVE_INLINE, Windows */ #if DK4_WIN_AVOID_CRT || DK4_WIN_DENY_CRT /* +++ DK4_HAVE_INLINE, Windows, ! crt */ /** Pure memory copy operation. All arguments must be checked by the caller before calling this function. @param dst Destination buffer address. @param src Source buffer address. @param sz Number of bytes to copy. */ static inline void DK4_MEMCPY(void *dst, const void *src, size_t sz) { CopyMemory(dst, src, sz); } /** Pure memory reset. All arguments must be checked by the caller before calling this function. @param dst Start address of buffer to reset. @param sz Number of bytes to reset. */ static inline void DK4_MEMRES(void *dst, size_t sz) { ZeroMemory(dst, sz); } /** Pure memory comparison. All arguments must be checked by the caller before calling this function. @param s1 Address of left buffer in comparison. @param s2 Address of right buffer in comparison. @param sz Number of bytes to compare. @return 1 if s1>s2, 0 for equal buffers, -1 if s1s2, 0 for equal buffers, -1 if s1s2, 0 for equal buffers, -1 if s1s2, 0 for equal buffers, -1 if s1s2, 0 for equal buffers, -1 if s1s2, 0 for equal buffers, -1 if s1s2, 0 for equal buffers, -1 if s1s2, 0 for equal buffers, -1 if s1s2, 0 for equal buffers, -1 if s1s2, 0 for equal buffers, -1 if s1