1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 
3 /*  Fluent Bit
4  *  ==========
5  *  Copyright (C) 2019-2021 The Fluent Bit Authors
6  *  Copyright (C) 2015-2018 Treasure Data Inc.
7  *
8  *  Licensed under the Apache License, Version 2.0 (the "License");
9  *  you may not use this file except in compliance with the License.
10  *  You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  *  Unless required by applicable law or agreed to in writing, software
15  *  distributed under the License is distributed on an "AS IS" BASIS,
16  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  *  See the License for the specific language governing permissions and
18  *  limitations under the License.
19  */
20 
21 #ifndef FLB_MEM_H
22 #define FLB_MEM_H
23 
24 #ifndef _DEFAULT_SOURCE
25 #define _DEFAULT_SOURCE
26 #endif
27 
28 #include <fluent-bit/flb_info.h>
29 #include <fluent-bit/flb_macros.h>
30 
31 #ifdef FLB_HAVE_JEMALLOC
32 #include <jemalloc/jemalloc.h>
33 #endif
34 
35 #include <stdlib.h>
36 
37 /*
38  * The following memory handling wrappers, aims to simplify the way to use
39  * the default memory allocator from the libc or an alternative one as Jemalloc.
40  *
41  * Here there is no error logging in case of failures, we defer that task to the
42  * caller.
43  */
44 #ifdef __GNUC__
45   #if ((__GNUC__ * 100 + __GNUC__MINOR__) > 430)  /* gcc version > 4.3 */
46     #define ALLOCSZ_ATTR(x,...) __attribute__ ((alloc_size(x, ##__VA_ARGS__)))
47   #else
48     #define ALLOCSZ_ATTR(x,...)
49   #endif
50 #else
51     #define ALLOCSZ_ATTR(x,...)
52 #endif
53 
54 static inline ALLOCSZ_ATTR(1)
flb_malloc(const size_t size)55 void *flb_malloc(const size_t size) {
56     void *aux;
57 
58     if (size == 0) {
59         return NULL;
60     }
61 
62     aux = malloc(size);
63     if (flb_unlikely(!aux && size)) {
64         return NULL;
65     }
66 
67     return aux;
68 }
69 
70 static inline ALLOCSZ_ATTR(1)
flb_calloc(size_t n,const size_t size)71 void *flb_calloc(size_t n, const size_t size) {
72     void *buf;
73 
74     if (size == 0) {
75         return NULL;
76     }
77 
78     buf = calloc(n, size);
79     if (flb_unlikely(!buf)) {
80         return NULL;
81     }
82 
83     return buf;
84 }
85 
86 static inline ALLOCSZ_ATTR(2)
flb_realloc(void * ptr,const size_t size)87 void *flb_realloc(void *ptr, const size_t size)
88 {
89     void *aux;
90 
91     aux = realloc(ptr, size);
92     if (flb_unlikely(!aux && size)) {
93         return NULL;
94     }
95 
96     return aux;
97 }
98 
99 static inline ALLOCSZ_ATTR(2, 3)
flb_realloc_z(void * ptr,const size_t old_size,const size_t new_size)100 void *flb_realloc_z(void *ptr, const size_t old_size, const size_t new_size)
101 {
102     void *tmp;
103     void *p;
104     size_t diff;
105 
106     tmp = flb_realloc(ptr, new_size);
107     if (!tmp) {
108         return NULL;
109     }
110 
111     if (new_size > old_size) {
112         diff = new_size - old_size;
113         p = ((char *) tmp + old_size);
114         memset(p, 0, diff);
115     }
116 
117     return tmp;
118 }
119 
120 
flb_free(void * ptr)121 static inline void flb_free(void *ptr) {
122     free(ptr);
123 }
124 
125 #endif
126