r13658: More moving around of files:
[samba.git] / source4 / lib / util / util.h
1 /* 
2    Unix SMB/CIFS implementation.
3    Utility functions for Samba
4    Copyright (C) Andrew Tridgell 1992-1999
5    Copyright (C) John H Terpstra 1996-1999
6    Copyright (C) Luke Kenneth Casson Leighton 1996-1999
7    Copyright (C) Paul Ashton 1998 - 1999
8     
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #ifndef _SAMBA_UTIL_H_
25 #define _SAMBA_UTIL_H_
26
27 struct substitute_context;
28
29 #include "util/xfile.h"
30 #include "util/debug.h"
31 #include "util/mutex.h"
32 #include "util/byteorder.h"
33 #include "util/util_proto.h"
34
35 /* zero a structure */
36 #define ZERO_STRUCT(x) memset((char *)&(x), 0, sizeof(x))
37
38 /* zero a structure given a pointer to the structure */
39 #define ZERO_STRUCTP(x) do { if ((x) != NULL) memset((char *)(x), 0, sizeof(*(x))); } while(0)
40
41 /* zero a structure given a pointer to the structure - no zero check */
42 #define ZERO_STRUCTPN(x) memset((char *)(x), 0, sizeof(*(x)))
43
44 /* pointer difference macro */
45 #define PTR_DIFF(p1,p2) ((ptrdiff_t)(((const char *)(p1)) - (const char *)(p2)))
46
47 /* work out how many elements there are in a static array */
48 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
49
50 /* assert macros */
51 #define SMB_ASSERT(b) do { if (!(b)) { \
52         DEBUG(0,("PANIC: assert failed at %s(%d)\n", __FILE__, __LINE__)); \
53         smb_panic("assert failed"); }} while (0)
54
55 #ifndef MIN
56 #define MIN(a,b) ((a)<(b)?(a):(b))
57 #endif
58
59 #ifndef MAX
60 #define MAX(a,b) ((a)>(b)?(a):(b))
61 #endif
62
63 #ifndef ABS
64 #define ABS(a) ((a)>0?(a):(-(a)))
65 #endif
66
67 #ifndef SAFE_FREE /* Oh no this is also defined in tdb.h */
68 /**
69  * Free memory if the pointer and zero the pointer.
70  *
71  * @note You are explicitly allowed to pass NULL pointers -- they will
72  * always be ignored.
73  **/
74 #define SAFE_FREE(x) do { if ((x) != NULL) {free(discard_const_p(void *, (x))); (x)=NULL;} } while(0)
75 #endif
76
77 #define malloc_p(type) (type *)malloc(sizeof(type))
78 #define malloc_array_p(type, count) (type *)realloc_array(NULL, sizeof(type), count)
79 #define realloc_p(p, type, count) (type *)realloc_array(p, sizeof(type), count)
80
81 #endif /* _SAMBA_UTIL_H_ */