-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathos_malloc.h
139 lines (126 loc) · 4.16 KB
/
os_malloc.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
* @file os_malloc.h
* @author TheSomeMan
* @date 2020-10-01
* @copyright Ruuvi Innovations Ltd, license BSD-3-Clause.
*/
#ifndef OS_MALLOC_H
#define OS_MALLOC_H
#include <stdbool.h>
#include <stdint.h>
#include <stddef.h>
#include "attribs.h"
#ifdef __cplusplus
extern "C" {
#endif
#define OS_MALLOC_TRACE 0
/**
* This is a wrap for malloc - it allocates a block of memory of size 'size' bytes.
* @param size - the size the memory block
* @return pointer to the allocated memory block or NULL.
*/
ATTR_MALLOC
ATTR_MALLOC_SIZE(1)
#if OS_MALLOC_TRACE
void*
os_malloc_internal(const size_t size, const char* const p_file, const int32_t line);
#define os_malloc(size) os_malloc_internal(size, __FILE__, __LINE__)
#else
void*
os_malloc(const size_t size);
#endif
/**
* This is a wrap for calloc - it allocates memory block for an array of 'nmemb' elements,
* each 'size' bytes and fills the array with zeroes.
* @param nmemb - the number of elements in the array.
* @param size - the size of one array element.
* @return pointer to the allocated memory block or NULL.
*/
ATTR_MALLOC
ATTR_CALLOC_SIZE(1, 2)
#if OS_MALLOC_TRACE
void*
os_calloc_internal(const size_t nmemb, const size_t size, const char* const p_file, const int32_t line);
#define os_calloc(nmemb, size) os_calloc_internal(nmemb, size, __FILE__, __LINE__)
#else
void*
os_calloc(const size_t nmemb, const size_t size);
#endif
/**
* @brief This is a safer wrap for realloc,
* it changes the size of the memory block and checks the result of the reallocation.
* @note This function checks if realloc returns NULL and overwrites value in p_ptr if a new memory block was allocated.
* @param[IN,OUT] p_ptr - ptr to a variable which points to the memory block,
* the pointer to the new memory block will be saved to p_ptr.
* In case if the reallocation failed, then the value in p_ptr will not be changed.
* @param size - new size of the memory block.
* @return true if the reallocation was successful.
*/
ATTR_WARN_UNUSED_RESULT
#if OS_MALLOC_TRACE
bool
os_realloc_safe_internal(void** const p_ptr, const size_t size, const char* const p_file, const int32_t line);
#define os_realloc_safe(p_ptr, size) os_realloc_safe_internal(p_ptr, size, __FILE__, __LINE__)
#else
bool
os_realloc_safe(void** const p_ptr, const size_t size);
#endif
/**
* @brief This is a safer wrap for realloc,
* it changes the size of the memory block and checks the result of the reallocation,
* it automatically frees the original memory pointer in case if realloc fails.
* @note This function checks if realloc returns NULL and overwrites value in p_ptr if a new memory block was allocated.
* @param[IN,OUT] p_ptr - ptr to a variable which points to the memory block,
* the pointer to the new memory block will be saved to p_ptr.
* In case if the reallocation failed, then the value in p_ptr will not be changed.
* @param size - new size of the memory block.
* @return true if the reallocation was successful.
*/
ATTR_WARN_UNUSED_RESULT
ATTR_NONNULL(1)
#if OS_MALLOC_TRACE
bool
os_realloc_safe_and_clean_internal(void** const p_ptr, const size_t size, const char* const p_file, const int32_t line);
#define os_realloc_safe_and_clean(p_ptr, size) os_realloc_safe_and_clean_internal(p_ptr, size, __FILE__, __LINE__)
#else
bool
os_realloc_safe_and_clean(void** const p_ptr, const size_t size);
#endif
/**
* This is a wrap for 'free' - it deallocates a block of memory
* @note This function should not be used, use macro @ref os_free instead.
* @param ptr - pointer to the memory block
*/
#if OS_MALLOC_TRACE
void
os_free_internal(void* ptr, const char* const p_file, const int32_t line);
#else
void
os_free_internal(void* ptr);
#endif
/**
* @brief os_free - is a wrap for 'free' which automatically sets pointer to NULL after the memory freeing.
*/
#if OS_MALLOC_TRACE
#define os_free(ptr) \
do \
{ \
os_free_internal((void*)(ptr), __FILE__, __LINE__); \
ptr = NULL; \
} while (0)
#else
#define os_free(ptr) \
do \
{ \
os_free_internal((void*)(ptr)); \
ptr = NULL; \
} while (0)
#endif
#if OS_MALLOC_TRACE
void
os_malloc_trace_dump(void);
#endif
#ifdef __cplusplus
}
#endif
#endif // OS_MALLOC_H