forked from 51Degrees/common-cxx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory.h
203 lines (178 loc) · 6.84 KB
/
memory.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/* *********************************************************************
* This Original Work is copyright of 51 Degrees Mobile Experts Limited.
* Copyright 2019 51 Degrees Mobile Experts Limited, 5 Charlotte Close,
* Caversham, Reading, Berkshire, United Kingdom RG4 7BY.
*
* This Original Work is licensed under the European Union Public Licence (EUPL)
* v.1.2 and is subject to its terms as set out below.
*
* If a copy of the EUPL was not distributed with this file, You can obtain
* one at https://opensource.org/licenses/EUPL-1.2.
*
* The 'Compatible Licences' set out in the Appendix to the EUPL (as may be
* amended by the European Commission) shall be deemed incompatible for
* the purposes of the Work and the provisions of the compatibility
* clause in Article 5 of the EUPL shall not apply.
*
* If using the Work as, or as part of, a network application, by
* including the attribution notice(s) required under Article 5 of the EUPL
* in the end user terms of the application under an appropriate heading,
* such notice(s) shall fulfill the requirements of that article.
* ********************************************************************* */
/**
* @ingroup FiftyOneDegreesCommon
* @defgroup FiftyOneDegreesMemory Memory
*
* Utility methods used to handle common memory operations such as allocating
* memory and freeing it, or reading through continuous memory checking for
* buffer over or under runs.
*
* @{
*/
#ifndef FIFTYONE_DEGREES_MEMORY_H_INCLUDED
#define FIFTYONE_DEGREES_MEMORY_H_INCLUDED
#include <stdlib.h>
#include <stdbool.h>
#include "data.h"
#include "threading.h"
#include "tree.h"
#ifdef __cplusplus
#define EXTERNAL extern "C"
#else
#define EXTERNAL
#endif
#ifdef _MSC_VER
#define FIFTYONE_DEGREES_CALL_CONV __cdecl
#else
#define FIFTYONE_DEGREES_CALL_CONV
#endif
/**
* Used to read data from memory in a similar manner to a file handle.
*/
typedef struct fiftyone_degrees_memory_reader_t {
byte *startByte; /**< The first byte in memory */
byte *current; /**< The current byte being read from */
byte *lastByte; /**< The maximum byte that can be read from */
long length; /**< Length of the file in bytes */
} fiftyoneDegreesMemoryReader;
/**
* Used to read continuous memory checking for buffer over or under runs.
* @param reader structure used to check for last byte
* @param advanceBy number of bytes to advance current by
* @return true if the advance succeeded, otherwise false
*/
bool fiftyoneDegreesMemoryAdvance(
fiftyoneDegreesMemoryReader *reader,
size_t advanceBy);
/**
* Allocates memory using the standard malloc method.
* @param __size number of bytes to allocate
* @return pointer to allocated memory or NULL
*/
EXTERNAL void* fiftyoneDegreesMemoryStandardMalloc(size_t __size);
/**
* Allocates memory while keeping track of the memory which has been allocated
* using this method since calling #fiftyoneDegreesMemoryTrackingReset.
* @param __size number of bytes to allocate
* @return pointer to allocated memory or NULL
*/
EXTERNAL void* fiftyoneDegreesMemoryTrackingMalloc(size_t __size);
/**
* Allocated aligned memory using the standard malloc method.
* @param alignment byte boundary to align the allocation to e.g. 16
* @param __size number of bytes to allocate
* @return pointer to allocation memory or NULL
*/
EXTERNAL void* fiftyoneDegreesMemoryStandardMallocAligned(
int alignment,
size_t __size);
/**
* Allocates aligned memory while keeping track of the memory which has been
* allocated using this method since calling
* #fiftyoneDegreesMemoryTrackingReset.
* @param alignment byte boundary to align the allocation to e.g. 16
* @param __size number of bytes to allocate
* @return pointer to allocation memory or NULL
*/
EXTERNAL void* fiftyoneDegreesMemoryTrackingMallocAligned(
int alignment,
size_t __size);
/**
* Frees memory allocated using the #fiftyoneDegreesMemoryTrackingMalloc method,
* noting that it has now been freed and does not contribute to the current
* total.
* @param __ptr data to free
*/
EXTERNAL void fiftyoneDegreesMemoryTrackingFree(void *__ptr);
/**
* Frees memory allocated using the #fiftyoneDegreesMemoryTrackingMallocAligned
* method, noting that it has now been freed and does not contribute to the
* current total.
* @param __ptr data to free
*/
EXTERNAL void fiftyoneDegreesMemoryTrackingFreeAligned(void* __ptr);
/**
* Frees memory using the standard free method.
* @param __ptr data to free
*/
EXTERNAL void fiftyoneDegreesMemoryStandardFree(void *__ptr);
/**
* Frees memory using the standard aligned free method.
* @param __ptr data to free
*/
EXTERNAL void fiftyoneDegreesMemoryStandardFreeAligned(void *__ptr);
/**
* Gets the maximum total number of bytes that have been allocated using the
* #fiftyoneDegreesMemoryTrackingMalloc method. NOTE: this is the total at any
* point in time since the #fiftyoneDegreesMemoryTrackingReset method was
* called, NOT the total allocated in a single allocation.
* @return maximum total allocation
*/
EXTERNAL size_t fiftyoneDegreesMemoryTrackingGetMax();
/**
* Gets the current number of bytes allocated using the tracking malloc and free
* methods.
* @return total bytes currently allocated
*/
EXTERNAL size_t fiftyoneDegreesMemoryTrackingGetAllocated();
/**
* Resets the memory trackers keeping track of the data allocated using the
* #fiftyoneDegreesMemoryTrackingMalloc method. This should always be called
* before tracking memory allocations.
*/
EXTERNAL void fiftyoneDegreesMemoryTrackingReset();
/**
* Pointer to the method used to allocate memory. By default this maps to
* #fiftyoneDegreesMemoryStandardMalloc which calls the standard library malloc.
* @param __size to allocate
* @return pointer to allocated memory or NULL
*/
EXTERNAL void *(FIFTYONE_DEGREES_CALL_CONV *fiftyoneDegreesMalloc)(size_t __size);
/**
* Pointer to the method used to allocate aligned memory. By default this maps
* to #fiftyoneDegreesMemoryStandardMallocAligned which calls the standard
* library malloc, allocating slightly more that requested, then ensures the
* pointer is aligned to a boundary.
* @param alignment byte boundary to align the allocation to e.g. 16
* @param __size to allocate
* @return pointer to allocated memory or NULL
*/
EXTERNAL void* (FIFTYONE_DEGREES_CALL_CONV *fiftyoneDegreesMallocAligned)(
int alignment,
size_t __size);
/**
* Pointer to the method used to free memory. By default this maps to
* #fiftyoneDegreesMemoryStandardFree which calls the standard library free.
* @param __ptr pointer to free
*/
EXTERNAL void (FIFTYONE_DEGREES_CALL_CONV *fiftyoneDegreesFree)(void *__ptr);
/**
* Pointer to the method used to free memory. By default this maps to
* #fiftyoneDegreesMemoryStandardFreeAligned which calls the standard library free.
* @param __ptr pointer to free
*/
EXTERNAL void (FIFTYONE_DEGREES_CALL_CONV *fiftyoneDegreesFreeAligned)(void* __ptr);
/**
* @}
*/
#endif