-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
chunk.h
328 lines (310 loc) · 9.21 KB
/
chunk.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
* Defines for low level memory management routines
*
* Copyright 2020 by Gray Watson
*
* This file is part of the dmalloc package.
*
* Permission to use, copy, modify, and distribute this software for
* any purpose and without fee is hereby granted, provided that the
* above copyright notice and this permission notice appear in all
* copies, and that the name of Gray Watson not be used in advertising
* or publicity pertaining to distribution of the document or software
* without specific, written prior permission.
*
* Gray Watson makes no representations about the suitability of the
* software described herein for any purpose. It is provided "as is"
* without express or implied warranty.
*
* The author may be contacted via https://dmalloc.com/
*/
#ifndef __CHUNK_H__
#define __CHUNK_H__
/*<<<<<<<<<< The below prototypes are auto-generated by fillproto */
/* limit in how much memory we are allowed to allocate */
extern
unsigned long _dmalloc_memory_limit;
/* total number of bytes that the heap has allocated */
extern
unsigned long _dmalloc_alloc_total;
/*
* int _dmalloc_chunk_startup
*
* Startup the low level malloc routines.
*
* Returns 1 on success or 0 on failure.
*/
extern
int _dmalloc_chunk_startup(void);
/*
* char *_dmalloc_chunk_desc_pnt
*
* Write into a buffer a pointer description with file and line-number.
*
* Returns a pointer to buffer 1st argument.
*
* ARGUMENTS:
*
* buf <-> Passed in buffer which will be filled with a description of
* the pointer.
*
* buf_size -> Size of the buffer in bytes.
*
* file -> File name, return address, or NULL.
*
* line -> Line number or 0.
*/
extern
char *_dmalloc_chunk_desc_pnt(char *buf, const int buf_size,
const char *file, const unsigned int line);
/*
* int _dmalloc_chunk_read_info
*
* Return some information associated with a pointer.
*
* Returns 1 if the pointer is okay or 0 if a problem with pointer
*
* ARGUMENTS:
*
* user_pnt -> Pointer we are checking.
*
* where <- Where the check is being made from.
*
* user_size_p <- Pointer to an unsigned int which, if not NULL, will
* be set to the size of bytes that the user requested.
*
* alloc_size_p <- Pointer to an unsigned int which, if not NULL, will
* be set to the total given size of bytes including block overhead.
*
* file_p <- Pointer to a character pointer which, if not NULL, will
* be set to the file where the pointer was allocated.
*
* line_p <- Pointer to a character pointer which, if not NULL, will
* be set to the line-number where the pointer was allocated.
*
* ret_attr_p <- Pointer to a void pointer, if not NULL, will be set
* to the return-address where the pointer was allocated.
*
* seen_cp <- Pointer to an unsigned long which, if not NULL, will be
* set to the number of times the pointer has been "seen".
*
* used_p <- Pointer to an unsigned long which, if not NULL, will be
* set to the last time the pointer was "used".
*
* valloc_bp <- Pointer to an integer which, if not NULL, will be set
* to 1 if the pointer was allocated with valloc otherwise 0.
*
* fence_bp <- Pointer to an integer which, if not NULL, will be set
* to 1 if the pointer has the fence bit set otherwise 0.
*/
extern
int _dmalloc_chunk_read_info(const void *user_pnt, const char *where,
unsigned int *user_size_p,
unsigned int *alloc_size_p, char **file_p,
unsigned int *line_p, void **ret_attr_p,
unsigned long **seen_cp,
unsigned long *used_p, int *valloc_bp,
int *fence_bp);
/*
* int _dmalloc_chunk_heap_check
*
* Run extensive tests on the entire heap.
*
* Returns 1 if the heap is okay or 0 if a problem was detected
*/
extern
int _dmalloc_chunk_heap_check(void);
/*
* int _dmalloc_chunk_pnt_check
*
* Run extensive tests on a pointer.
*
* Returns 1 if the pointer is okay or 0 if not
*
* ARGUMENTS:
*
* func -> Function string which is checking the pointer.
*
* user_pnt -> Pointer we are checking.
*
* exact_b -> Set to 1 to find the pointer specifically. Otherwise we
* can find the pointer inside of an allocation.
*
* strlen_b -> Make sure that pnt can hold at least a strlen + 1
* bytes. If 0 then ignore.
*
* min_size -> Make sure that pnt can hold at least that many bytes.
* If 0 then ignore.
*/
extern
int _dmalloc_chunk_pnt_check(const char *func, const void *user_pnt,
const int exact_b, const int strlen_b,
const int min_size);
/*
* void *_dmalloc_chunk_malloc
*
* Allocate a chunk of memory.
*
* Returns a valid pointer on success or NULL on failure.
*
* ARGUMENTS:
*
* file -> File-name or return-address location of the allocation.
*
* line -> Line-number location of the allocation.
*
* size -> Number of bytes to allocate.
*
* func_id -> Calling function-id as defined in dmalloc.h.
*
* alignment -> If greater than 0 then try to align the returned
* block.
*/
extern
void *_dmalloc_chunk_malloc(const char *file, const unsigned int line,
const unsigned long size, const int func_id,
const unsigned int alignment);
/*
* int _dmalloc_chunk_free
*
* Free a user pointer from the heap.
*
* Returns FREE_NOERROR on success or FREE_ERROR on failure
*
* ARGUMENTS:
*
* file -> File-name or return-address location of the allocation.
*
* line -> Line-number location of the allocation.
*
* user_pnt -> Pointer we are freeing.
*
* func_id -> Function ID
*/
extern
int _dmalloc_chunk_free(const char *file, const unsigned int line,
void *user_pnt, const int func_id);
/*
* void *_dmalloc_chunk_realloc
*
* Re-allocate a chunk of memory either shrinking or expanding it.
*
* Returns a valid pointer on success of NULL on failure.
*
* ARGUMENTS:
*
* file -> File-name or return-address location of the allocation.
*
* line -> Line-number location of the allocation.
*
* old_user_pnt -> Old user pointer that we are reallocating.
*
* new_size -> New-size to change the pointer.
*
* func_id -> Calling function-id as defined in dmalloc.h.
*/
extern
void *_dmalloc_chunk_realloc(const char *file, const unsigned int line,
void *old_user_pnt,
const unsigned long new_size,
const int func_id);
/*
* void _dmalloc_chunk_log_stats
*
* Log general statistics from the heap to the logfile.
*/
extern
void _dmalloc_chunk_log_stats(void);
/*
* void _dmalloc_chunk_log_changed
*
* Log the pointers that has changed since a pointer in time.
*
* ARGUMENTS:
*
* mark -> Dmalloc counter used to mark a specific time so that
* servers can check on the changed pointers.
*
* log_non_free_b -> If set to 1 then log the new not-freed
* (i.e. used) pointers.
*
* log_free_b -> If set to 1 then log the new freed pointers.
*
* details_b -> If set to 1 then dump the individual pointer entries
* instead of just the summary.
*/
extern
void _dmalloc_chunk_log_changed(const unsigned long mark,
const int log_not_freed_b,
const int log_freed_b, const int details_b);
/*
* unsigned long _dmalloc_chunk_count_changed
*
* Return the pointers that has changed since a pointer in time.
*
* Returns the number of bytes changed since mark.
*
* ARGUMENTS:
*
* mark -> Dmalloc counter used to mark a specific time so that
* servers can check on the changed pointers.
*
* count_non_free_b -> If set to 1 then count the new not-freed
* (i.e. used) pointers.
*
* count_free_b -> If set to 1 then count the new freed pointers.
*/
extern
unsigned long _dmalloc_chunk_count_changed(const unsigned long mark,
const int count_not_freed_b,
const int count_freed_b);
/*
* void _dmalloc_chunk_get_stats
*
* Return a number of statistics about the current heap.
*
* ARGUMENTS:
*
* heap_low_p <- Pointer to pointer which, if not 0L, will be set to
* the low address in the heap.
*
* heap_high_p <- Pointer to pointer which, if not 0L, will be set to
* the high address in the heap.
*
* total_space_p <- Pointer to an unsigned long which, if not 0L, will
* be set to the total space managed by the library including user
* space, administrative space, and overhead.
*
* user_space_p <- Pointer to an unsigned long which, if not 0L, will
* be set to the space given to the user process (allocated and free).
*
* current_allocated_p <- Pointer to an unsigned long which, if not
* 0L, will be set to the current allocated space given to the user
* process.
*
* current_pnt_np <- Pointer to an unsigned long which, if not 0L,
* will be set to the current number of pointers allocated by the user
* process.
*
* max_allocated_p <- Pointer to an unsigned long which, if not 0L,
* will be set to the maximum allocated space given to the user
* process.
*
* max_pnt_np <- Pointer to an unsigned long which, if not 0L, will be
* set to the maximum number of pointers allocated by the user
* process.
*
* max_one_p <- Pointer to an unsigned long which, if not 0L, will be
* set to the maximum allocated with 1 call by the user process.
*/
extern
void _dmalloc_chunk_get_stats(void **heap_low_p, void **heap_high_p,
unsigned long *total_space_p,
unsigned long *user_space_p,
unsigned long *current_allocated_p,
unsigned long *current_pnt_np,
unsigned long *max_allocated_p,
unsigned long *max_pnt_np,
unsigned long *max_one_p);
/*<<<<<<<<<< This is end of the auto-generated output from fillproto. */
#endif /* ! __CHUNK_H__ */