aee2adf38fe1c8cc7dc1842c4744f6b6c97b338c
[amitay/samba.git] / source4 / include / smb_macros.h
1 /* 
2    Unix SMB/CIFS implementation.
3    SMB parameters and setup
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 _SMB_MACROS_H
25 #define _SMB_MACROS_H
26
27 /* zero a structure */
28 #define ZERO_STRUCT(x) memset((char *)&(x), 0, sizeof(x))
29
30 /* zero a structure given a pointer to the structure */
31 #define ZERO_STRUCTP(x) do { if ((x) != NULL) memset((char *)(x), 0, sizeof(*(x))); } while(0)
32
33 /* zero a structure given a pointer to the structure - no zero check */
34 #define ZERO_STRUCTPN(x) memset((char *)(x), 0, sizeof(*(x)))
35
36 /* pointer difference macro */
37 #define PTR_DIFF(p1,p2) ((ptrdiff_t)(((const char *)(p1)) - (const char *)(p2)))
38
39 /* work out how many elements there are in a static array */
40 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
41
42 /* assert macros */
43 #define SMB_ASSERT(b) do { if (!(b)) { \
44         DEBUG(0,("PANIC: assert failed at %s(%d)\n", __FILE__, __LINE__)); \
45         smb_panic("assert failed"); }} while (0)
46
47 /* REWRITE TODO: remove these smb_xxx macros */
48 #define smb_buf(buf) (((char *)(buf)) + MIN_SMB_SIZE + CVAL(buf,HDR_WCT+4)*2)
49
50 #define smb_len(buf) (PVAL(buf,3)|(PVAL(buf,2)<<8)|(PVAL(buf,1)<<16))
51 #define _smb_setlen(buf,len) do {(buf)[0] = 0; (buf)[1] = ((len)&0x10000)>>16; \
52         (buf)[2] = ((len)&0xFF00)>>8; (buf)[3] = (len)&0xFF;} while (0)
53
54 #ifndef MIN
55 #define MIN(a,b) ((a)<(b)?(a):(b))
56 #endif
57
58 #ifndef MAX
59 #define MAX(a,b) ((a)>(b)?(a):(b))
60 #endif
61
62 #ifndef ABS
63 #define ABS(a) ((a)>0?(a):(-(a)))
64 #endif
65
66 #ifndef SAFE_FREE /* Oh no this is also defined in tdb.h */
67 /**
68  * Free memory if the pointer and zero the pointer.
69  *
70  * @note You are explicitly allowed to pass NULL pointers -- they will
71  * always be ignored.
72  **/
73 #define SAFE_FREE(x) do { if ((x) != NULL) {free(discard_const_p(void *, (x))); (x)=NULL;} } while(0)
74 #endif
75
76 #endif /* _SMB_MACROS_H */