2018-12-03 17:35:58 +03:00
/*
2008-10-18 15:59:21 +04:00
Unix SMB / CIFS implementation .
Samba utility functions
Copyright ( C ) Andrew Tridgell 1992 - 1999
Copyright ( C ) Jelmer Vernooij < jelmer @ samba . org > 2008
2018-12-03 17:35:58 +03:00
2008-10-18 15:59:21 +04:00
This program is free software ; you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation ; either version 3 of the License , or
( at your option ) any later version .
2018-12-03 17:35:58 +03:00
2008-10-18 15:59:21 +04:00
This program 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 .
2018-12-03 17:35:58 +03:00
2008-10-18 15:59:21 +04:00
You should have received a copy of the GNU General Public License
along with this program . If not , see < http : //www.gnu.org/licenses/>.
*/
# ifndef _SAMBA_MEMORY_H_
# define _SAMBA_MEMORY_H_
# ifndef SAFE_FREE /* Oh no this is also defined in tdb.h */
/**
* Free memory if the pointer and zero the pointer .
*
* @ note You are explicitly allowed to pass NULL pointers - - they will
* always be ignored .
* */
2010-08-28 10:38:48 +04:00
# define SAFE_FREE(x) do { if ((x) != NULL) {free(x); (x)=NULL;} } while(0)
2008-10-18 15:59:21 +04:00
# endif
2022-07-27 18:40:03 +03:00
/**
* Zero string and free memory if the pointer and zero the pointer .
*
* @ note You are explicitly allowed to pass NULL pointers - - they will
* always be ignored .
* */
# define BURN_FREE_STR(x) do { \
if ( ( x ) ! = NULL ) { \
size_t s = strlen ( x ) ; \
memset_s ( ( x ) , s , 0 , s ) ; \
free ( x ) ; ( x ) = NULL ; \
} \
} while ( 0 )
/**
* Zero and free memory if the pointer and zero the pointer .
*
* @ note You are explicitly allowed to pass NULL pointers - - they will
* always be ignored .
* */
# define BURN_FREE(x, s) do { \
if ( ( x ) ! = NULL ) { \
memset_s ( ( x ) , ( s ) , 0 , ( s ) ) ; \
free ( x ) ; ( x ) = NULL ; \
} \
} while ( 0 )
2018-12-03 17:35:58 +03:00
/**
* Type - safe version of malloc . Allocated one copy of the
2008-10-18 15:59:21 +04:00
* specified data type .
*/
# define malloc_p(type) (type *)malloc(sizeof(type))
/**
* Allocate an array of elements of one data type . Does type - checking .
*/
2008-10-18 16:07:51 +04:00
# define malloc_array_p(type, count) (type *)realloc_array(NULL, sizeof(type), count, false)
2008-10-18 15:59:21 +04:00
2018-12-03 17:35:58 +03:00
/**
2008-10-18 15:59:21 +04:00
* Resize an array of elements of one data type . Does type - checking .
*/
2008-10-18 16:07:51 +04:00
# define realloc_p(p, type, count) (type *)realloc_array(p, sizeof(type), count, false)
2008-10-18 15:59:21 +04:00
2018-12-03 17:35:58 +03:00
/**
* Zero a structure .
2008-10-18 15:59:21 +04:00
*/
# ifndef ZERO_STRUCT
2018-12-03 17:37:03 +03:00
# define ZERO_STRUCT(x) memset_s((char *)&(x), sizeof(x), 0, sizeof(x))
2008-10-18 15:59:21 +04:00
# endif
2018-12-03 17:35:58 +03:00
/**
* Zero a structure given a pointer to the structure .
2008-10-18 15:59:21 +04:00
*/
# ifndef ZERO_STRUCTP
2018-12-03 17:37:03 +03:00
# define ZERO_STRUCTP(x) do { \
if ( ( x ) ! = NULL ) { \
memset_s ( ( char * ) ( x ) , sizeof ( * ( x ) ) , 0 , sizeof ( * ( x ) ) ) ; \
} \
} while ( 0 )
2008-10-18 15:59:21 +04:00
# endif
2018-12-03 17:35:58 +03:00
/**
* Zero a structure given a pointer to the structure - no zero check .
2008-10-18 15:59:21 +04:00
*/
# ifndef ZERO_STRUCTPN
2018-12-03 17:37:03 +03:00
# define ZERO_STRUCTPN(x) memset_s((char *)(x), sizeof(*(x)), 0, sizeof(*(x)))
2008-10-18 15:59:21 +04:00
# endif
2018-12-03 17:35:58 +03:00
/**
* Zero an array - note that sizeof ( array ) must work - ie . it must not be a
* pointer .
*/
2008-10-18 15:59:21 +04:00
# ifndef ZERO_ARRAY
2018-12-03 17:37:03 +03:00
# define ZERO_ARRAY(x) memset_s((char *)(x), sizeof(x), 0, sizeof(x))
2008-10-18 15:59:21 +04:00
# endif
2018-12-03 17:37:03 +03:00
/**
* Zero a given len of an array
*/
# define ZERO_ARRAY_LEN(x, l) memset_s((char *)(x), (l), 0, (l))
2008-10-18 15:59:21 +04:00
/**
2018-12-03 17:35:58 +03:00
* Work out how many elements there are in a static array
2008-10-18 15:59:21 +04:00
*/
# ifndef ARRAY_SIZE
# define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
# endif
2018-12-03 17:35:58 +03:00
/**
* Pointer difference macro .
2008-10-18 15:59:21 +04:00
*/
# ifndef PTR_DIFF
# define PTR_DIFF(p1,p2) ((ptrdiff_t)(((const char *)(p1)) - (const char *)(p2)))
# endif
# endif /* _SAMBA_MEMORY_H_ */