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 #include "util-validate.h"
82 
83 static inline void SCMemmove(void *dest, size_t dest_size, const void *src, size_t len)
84 {
85 #if HAVE_MEMMOVE_S
86  memmove_s(dest, dest_size, src, len);
87 #else
88  if (len > dest_size) {
90  len = dest_size;
91  }
92  memmove(dest, src, len);
93 #endif
94 }
95 
96 #endif /* SURICATA_UTIL_MEM_H */
len
uint8_t len
Definition: app-layer-dnp3.h:2
SCStrndupFunc
char * SCStrndupFunc(const char *s, size_t n)
Definition: util-mem.c:86
SCStrdupFunc
char * SCStrdupFunc(const char *s)
Definition: util-mem.c:71
SCCallocFunc
void * SCCallocFunc(const size_t nm, const size_t sz)
Definition: util-mem.c:57
SCReallocFunc
void * SCReallocFunc(void *ptr, const size_t size)
Definition: util-mem.c:43
SCFreeAlignedFunc
void SCFreeAlignedFunc(void *ptr)
Free aligned memory.
Definition: util-mem.c:139
MacSet_::size
int size
Definition: util-macset.c:55
util-validate.h
SCMallocFunc
void * SCMallocFunc(const size_t sz)
Definition: util-mem.c:28
src
uint16_t src
Definition: app-layer-dnp3.h:5
SCMallocAlignedFunc
void * SCMallocAlignedFunc(const size_t size, const size_t align)
wrapper for allocing aligned mem
Definition: util-mem.c:108
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:109