move repository from m6w6 to awesomized
[awesomized/libmemcached] / src / libmemcached / memory.h
1 /*
2 +--------------------------------------------------------------------+
3 | libmemcached - C/C++ Client Library for memcached |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted under the terms of the BSD license. |
7 | You should have received a copy of the license in a bundled file |
8 | named LICENSE; in case you did not receive a copy you can review |
9 | the terms online at: https://opensource.org/licenses/BSD-3-Clause |
10 +--------------------------------------------------------------------+
11 | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ |
12 | Copyright (c) 2020-2021 Michael Wallner https://awesome.co/ |
13 +--------------------------------------------------------------------+
14 */
15
16 #pragma once
17
18 #include "mem_config.h"
19
20 #include "libmemcached/common.h"
21
22 #ifdef __cplusplus
23 # include <cstddef>
24 # include <cstdlib>
25 #else
26 # include <stddef.h>
27 # include <stdlib.h>
28 #endif
29
30 static inline void libmemcached_free(const memcached_st *self, void *mem) {
31 if (self) {
32 self->allocators.free(self, mem, self->allocators.context);
33 } else if (mem) {
34 #ifdef __cplusplus
35 std::free(mem);
36 #else
37 free(mem);
38 #endif
39 }
40 }
41
42 static inline void *libmemcached_malloc(const memcached_st *self, const size_t size) {
43 if (self) {
44 return self->allocators.malloc(self, size, self->allocators.context);
45 }
46
47 #ifdef __cplusplus
48 return std::malloc(size);
49 #else
50 return malloc(size);
51 #endif
52 }
53 #define libmemcached_xmalloc(__memcachd_st, __type) \
54 ((__type *) libmemcached_malloc((__memcachd_st), sizeof(__type)))
55
56 static inline void *libmemcached_realloc(const memcached_st *self, void *mem, size_t nmemb,
57 const size_t size) {
58 if (self) {
59 return self->allocators.realloc(self, mem, nmemb * size, self->allocators.context);
60 }
61
62 #ifdef __cplusplus
63 return std::realloc(mem, size);
64 #else
65 return realloc(mem, size);
66 #endif
67 }
68 #define libmemcached_xrealloc(__memcachd_st, __mem, __nelem, __type) \
69 ((__type *) libmemcached_realloc((__memcachd_st), (__mem), (__nelem), sizeof(__type)))
70 #define libmemcached_xvalloc(__memcachd_st, __nelem, __type) \
71 ((__type *) libmemcached_realloc((__memcachd_st), NULL, (__nelem), sizeof(__type)))
72
73 static inline void *libmemcached_calloc(const memcached_st *self, size_t nelem, size_t size) {
74 if (self) {
75 return self->allocators.calloc(self, nelem, size, self->allocators.context);
76 }
77
78 #ifdef __cplusplus
79 return std::calloc(nelem, size);
80 #else
81 return calloc(nelem, size);
82 #endif
83 }
84 #define libmemcached_xcalloc(__memcachd_st, __nelem, __type) \
85 ((__type *) libmemcached_calloc((__memcachd_st), (__nelem), sizeof(__type)))