suricata
util-mem.h
Go to the documentation of this file.
1 /* Copyright (C) 2007-2020 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 /**
19  * \file
20  *
21  * \author Pablo Rincon Crespo <pablo.rincon.crespo@gmail.com>
22  * \author Bill Meeks <billmeeks8@gmail.com>
23  *
24  * Utility Macros for memory management
25  *
26  * \todo Add wrappers for functions that allocate/free memory here.
27  * Currently we have malloc, calloc, realloc, strdup, strndup and
28  * free, but there are more.
29  */
30 
31 #ifndef SURICATA_UTIL_MEM_H
32 #define SURICATA_UTIL_MEM_H
33 
34 #if CPPCHECK==1 || defined(__clang_analyzer__)
35 #define SCMalloc malloc
36 #define SCCalloc calloc
37 #define SCRealloc realloc
38 #define SCFree free
39 #define SCStrdup strdup
40 #define SCStrndup strndup
41 #define SCMallocAligned _mm_malloc
42 #define SCFreeAligned _mm_free
43 #else /* CPPCHECK */
44 
45 
46 void *SCMallocFunc(const size_t sz);
47 #define SCMalloc(sz) SCMallocFunc((sz))
48 
49 void *SCReallocFunc(void *ptr, const size_t size);
50 #define SCRealloc(ptr, sz) SCReallocFunc((ptr), (sz))
51 
52 void *SCCallocFunc(const size_t nm, const size_t sz);
53 #define SCCalloc(nm, sz) SCCallocFunc((nm), (sz))
54 
55 char *SCStrdupFunc(const char *s);
56 #define SCStrdup(s) SCStrdupFunc((s))
57 
58 char *SCStrndupFunc(const char *s, size_t n);
59 #define SCStrndup(s, n) SCStrndupFunc((s), (n))
60 
61 #define SCFree(p) free((p))
62 
63 /** \brief wrapper for allocing aligned mem
64  * \param a size
65  * \param b alignment
66  */
67 void *SCMallocAlignedFunc(const size_t size, const size_t align);
68 #define SCMallocAligned(size, align) SCMallocAlignedFunc((size), (align))
69 
70 /** \brief Free aligned memory
71  *
72  * Not needed for mem alloc'd by posix_memalign,
73  * but for possible future use of _mm_malloc needing
74  * _mm_free.
75  */
76 void SCFreeAlignedFunc(void *ptr);
77 #define SCFreeAligned(p) SCFreeAlignedFunc((p))
78 
79 #endif /* CPPCHECK */
80 
81 #endif /* SURICATA_UTIL_MEM_H */
SCStrndupFunc
char * SCStrndupFunc(const char *s, size_t n)
Definition: util-mem.c:87
SCStrdupFunc
char * SCStrdupFunc(const char *s)
Definition: util-mem.c:72
SCCallocFunc
void * SCCallocFunc(const size_t nm, const size_t sz)
Definition: util-mem.c:58
SCReallocFunc
void * SCReallocFunc(void *ptr, const size_t size)
Definition: util-mem.c:44
SCFreeAlignedFunc
void SCFreeAlignedFunc(void *ptr)
Free aligned memory.
Definition: util-mem.c:140
MacSet_::size
int size
Definition: util-macset.c:56
SCMallocFunc
void * SCMallocFunc(const size_t sz)
Definition: util-mem.c:29
SCMallocAlignedFunc
void * SCMallocAlignedFunc(const size_t size, const size_t align)
wrapper for allocing aligned mem
Definition: util-mem.c:109