1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 
3 /*  Monkey HTTP Server
4  *  ==================
5  *  Copyright 2001-2017 Eduardo Silva <eduardo@monkey.io>
6  *
7  *  Licensed under the Apache License, Version 2.0 (the "License");
8  *  you may not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *  Unless required by applicable law or agreed to in writing, software
14  *  distributed under the License is distributed on an "AS IS" BASIS,
15  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *  See the License for the specific language governing permissions and
17  *  limitations under the License.
18  */
19 
20 #ifndef MK_MEM_H
21 #define MK_MEM_H
22 
23 #include <stdio.h>
24 
25 #ifdef MALLOC_JEMALLOC
26 #include <jemalloc/jemalloc.h>
27 #endif
28 
29 #include "mk_macros.h"
30 
31 typedef struct
32 {
33     char *data;
34     unsigned long len;
35 } mk_ptr_t;
36 
37 #ifdef __GNUC__
38   #if ((__GNUC__ * 100 + __GNUC__MINOR__) > 430)  /* gcc version > 4.3 */
39     #define ALLOCSZ_ATTR(x,...) __attribute__ ((alloc_size(x, ##__VA_ARGS__)))
40   #else
41     #define ALLOCSZ_ATTR(x,...)
42   #endif
43 #else
44     #define ALLOCSZ_ATTR(x,...)
45 #endif
46 
47 static inline ALLOCSZ_ATTR(1)
mk_mem_alloc(const size_t size)48 void *mk_mem_alloc(const size_t size)
49 {
50 #ifdef MALLOC_JEMALLOC
51     void *aux = je_malloc(size);
52 #else
53     void *aux = malloc(size);
54 #endif
55 
56     if (mk_unlikely(!aux && size)) {
57         perror("malloc");
58         return NULL;
59     }
60 
61     return aux;
62 }
63 
64 static inline ALLOCSZ_ATTR(1)
mk_mem_alloc_z(const size_t size)65 void *mk_mem_alloc_z(const size_t size)
66 {
67 #ifdef MALLOC_JEMALLOC
68     void *buf = je_calloc(1, size);
69 #else
70     void *buf = calloc(1, size);
71 #endif
72 
73     if (mk_unlikely(!buf)) {
74         return NULL;
75     }
76 
77     return buf;
78 }
79 
80 static inline ALLOCSZ_ATTR(2)
mk_mem_realloc(void * ptr,const size_t size)81 void *mk_mem_realloc(void *ptr, const size_t size)
82 {
83 #ifdef MALLOC_JEMALLOC
84     void *aux = je_realloc(ptr, size);
85 #else
86     void *aux = realloc(ptr, size);
87 #endif
88 
89     if (mk_unlikely(!aux && size)) {
90         perror("realloc");
91         return NULL;
92     }
93 
94     return aux;
95 }
96 
mk_mem_free(void * ptr)97 static inline void mk_mem_free(void *ptr)
98 {
99 #ifdef MALLOC_JEMALLOC
100     je_free(ptr);
101 #else
102     free(ptr);
103 #endif
104 }
105 
106 void mk_mem_free(void *ptr);
107 void mk_mem_pointers_init(void);
108 
109 /* mk_ptr_t_* */
110 mk_ptr_t mk_ptr_create(char *buf, long init, long end);
111 void mk_ptr_free(mk_ptr_t * p);
112 void mk_ptr_print(mk_ptr_t p);
113 char *mk_ptr_to_buf(mk_ptr_t p);
114 void mk_ptr_set(mk_ptr_t * p, char *data);
115 
mk_ptr_reset(mk_ptr_t * p)116 static inline void mk_ptr_reset(mk_ptr_t * p)
117 {
118     p->data = NULL;
119     p->len = 0;
120 }
121 
122 
123 #define mk_ptr_init(a) {a, sizeof(a) - 1}
124 
125 #endif
126