-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrmalloc.h
137 lines (108 loc) · 4.51 KB
/
rmalloc.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
/* =====================================================================
File: rmalloc.h
Author: Rammi <rammi@quincunx.escape.de>
Date: 11/16/1995
Disclaimer: Use at your own risk.
Content: Switching on/off of malloc debug library. This is done
by preprocessor switch MALLOC_DEBUG.
If MALLOC_DEBUG is defined, special debug versions for
the functions of the standard malloc lib are used.
They try to get hold of the following things:
* Overwrite borders of allocated block
* Multiple free of a block
* free of an unallocated block
All sources must include this file. No more changes necessary.
Special macros:
RM_TEST Force testing of all allocated blocks.
RM_STAT Show a statistic of allocated blocks.
RM_RETAG(p) change position for allocated block to pos of
this macro
RM_SET(p, f) Set flag f for block p. f can be a combination of
the following:
RM_STATIC Block is considered static. Is only summarized
in statistics.
RM_STRING Block is meant to hold a string.
The global behaviour can be changed by editing, recompiling and
linking rmalloc.c. See information there.
Changes:
See rmalloc.c
===================================================================== */
#ifndef R_MALLOC_H
#define R_MALLOC_H
/* =========
INCLUDEs:
========= */
#include <stdlib.h>
#include <string.h>
/* ========
DEFINEs:
======== */
/* #define RM_UPPERSTYLE */ /* this is only used for backward compatibility */
/* once again useful: INT2STRING(prepro_macro_containing_number)-->"number" */
#define FUNCTIONIZE(a,b) a(b)
#define STRINGIZE(a) #a
#define INT2STRING(i) FUNCTIONIZE(STRINGIZE,i)
/* Flags in header (if WITH_FLAGS defined, see rmalloc.c) */
#define RM_STATIC (0x0001 << 0) /* static memory */
#define RM_STRING (0x0001 << 1) /* contains string */
/* Flags used internally only */
#define RM_EXTERNAL (0x0001 << 15) /* called by external function */
#ifdef MALLOC_DEBUG
/* Useful to build 1 string from __FILE__ & __LINE__ at compilation time: */
#define RM_FILE_POS __FILE__ ":" INT2STRING(__LINE__)
#ifdef RM_UPPERSTYLE
/* Deprecated: Only used for backward compatibility: */
# define MALLOC(s) Rmalloc((s), RM_FILE_POS)
# define CALLOC(n,s) Rcalloc((n), (s), RM_FILE_POS)
# define REALLOC(p,s) Rrealloc((p), (s), RM_FILE_POS)
# define FREE(p) Rfree((p), RM_FILE_POS)
# define FREE0(p) Rfree((p), RM_FILE_POS),(p)=NULL
# define STRDUP(s) Rstrdup((s), RM_FILE_POS)
#else /* !RM_UPPERSTYLE */
/* Wrap with our stuff: */
# define malloc(s) Rmalloc((s), RM_FILE_POS)
# define calloc(n,s) Rcalloc((n), (s), RM_FILE_POS)
# define realloc(p,s) Rrealloc((p), (s), RM_FILE_POS)
# define free(p) Rfree((p), RM_FILE_POS)
# define free0(p) Rfree((p), RM_FILE_POS),(p)=NULL
# define strdup(s) Rstrdup((s), RM_FILE_POS)
# define getcwd(b,s) Rgetcwd((b), (s), RM_FILE_POS)
#endif /* RM_UPPERSTYLE */
#define RM_TEST Rmalloc_test(RM_FILE_POS)
#define RM_STAT Rmalloc_stat(RM_FILE_POS)
#define RM_RETAG(p) Rmalloc_retag((p), RM_FILE_POS)
#define RM_SET(p, f) Rmalloc_set_flags((p), (f), RM_FILE_POS)
#else /* ! MALLOC_DEBUG */
#ifdef RM_UPPERSTYLE
/* The normal stuff (backward compatibility): */
# define MALLOC(s) malloc((s))
# define CALLOC(n,s) calloc((n), (s))
# define REALLOC(p,s) realloc((p), (s))
# define FREE(p) free((p))
# define FREE0(p) free((p)), (p)=NULL
# define STRDUP(s) (char *)strdup((s))
#else /* !RM_UPPERSTYLE */
# define free0(p) free((p)), (p)=NULL
#endif /* RM_UPPERSTYLE */
#define RM_TEST
#define RM_STAT
#define RM_RETAG(p) (p)
#define RM_SET(p, f) (p)
#endif /* ! MALLOC_DEBUG */
/* ===========
PROTOTYPES:
=========== */
#if defined(MALLOC_DEBUG) || defined(RM_NEED_PROTOTYPES)
void *Rmalloc(size_t size, const char *file);
void *Rcalloc(size_t nelem, size_t size, const char *file);
void *Rrealloc(void *p, size_t size, const char *file);
void Rfree(void *p, const char *file);
char *Rstrdup(const char *str, const char *file);
char *Rgetcwd(char *buffer, size_t size, const char *file);
void Rtest_malloc(const char *file);
void Rmalloc_test(const char *file);
void Rmalloc_stat(const char *file);
void *Rmalloc_retag(void *p, const char *file);
void *Rmalloc_set_flags(void *p, unsigned flags, const char *file);
#endif /* MALLOC_DEBUG || RM_NEED_PROTOTYPES */
#endif /* !R_MALLOC_H */