File: Malloc.h

package info (click to toggle)
similarity-tester 2.77-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 572 kB
  • ctags: 512
  • sloc: ansic: 3,197; lex: 1,562; makefile: 301
file content (103 lines) | stat: -rw-r--r-- 4,224 bytes parent folder | download
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
/*	This file is part of the memory management and leak detector MALLOC.
	Written by Dick Grune, Vrije Universiteit, Amsterdam.
	$Id: Malloc.h,v 1.8 2013-05-12 09:58:34 Gebruiker Exp $
*/

#ifndef	_MALLOC_H_
#define _MALLOC_H_

#include	<stdio.h>

/*****
The files Malloc.[ch] provide several functionalities:

- checking for "out of memory":        to simplify programming
- allocating memory using new(type)    "     "        " "
- detecting memory leaks:              to obtain cleaner programs
- clobbering freshly allocated memory: to obtain safer programs

The module defines several sets of routines:

1.  void *Malloc(size_t s)
    void *Calloc(size_t n, size_t s)
    void *Realloc(void *p, size_t s)
    void Free(void *p)

2.  void *TryMalloc(size_t s)
    void *TryCalloc(size_t n, size_t s)
    void *TryRealloc(void *p, size_t s)

3.  T *new(T)
    char *new_string(const char *s)

4.  void ReportMemoryLeaks(FILE *f)
    void MemClobber(void *p, size_t size)

* The members of the first set act like their Unix counterparts, except that
  they never return NULL; upon out-of-memory an error message is given on
  standard error, showing the file name and the line number of the call. Since
  in almost all cases there is nothing more intelligent to do, this is almost
  always adequate, and makes for simpler and safer programming.

  In those rare cases that the program *can* continue when out of memory, the
  routines in the second set can be used; they act exactly like their Unix
  counterparts.

  Note that automatic out-of-memory detection is active, regardless of the
  -DMEM... flags described below.

* A call of new(T), with T any type, yields a pointer of type T* to a block
  of type T, allocated using Malloc().
  A call of new_string(s), with s a string, yields a pointer to a copy of s,
  allocated using Malloc(); it is equivalent to strdup() except that it uses
  Malloc().

* Normally, a call of ReportMemoryLeaks() does nothing, but when Malloc.c is
  compiled with -DMEMLEAK, it produces a compacted list of allocated but not
  yet freed blocks on the stream f, with information about where they were
  allocated.
  This is useful to get insight into memory use and abuse.

* When Malloc.c is compiled with -DMEMCLOBBER, it clobbers all newly allocated
  memory from Malloc() and Realloc() just after allocation, and all freed
  memory just before freeing it.  An area is clobbered by overwriting it with
  a wacky bit pattern. This is done in the hope that improper use of memory
  will cause some evident error somewhere.

  The routine that performs the clobbering, MemClobber(void *p, size_t size),
  is available regardless of the -DMEMCLOBBER compilation option. It can be
  used to create comparison patterns.

* Compiled with any of the -DMEM... flags, Malloc will also produce run-time
  error messages for multiple Free()s of the same block, and Realloc()s on
  not-allocated blocks. It then allows the program to continue.

* The system consumes hardly any time and is fast enough to be kept active all
  the time.
*****/

/* Private entries */
extern void *_leak_malloc(int chk, size_t size, const char *fname, int l_nmb);
extern void *_leak_calloc(int chk, size_t n, size_t size, const char *fname, int l_nmb);
extern void *_leak_realloc(int chk, void *addr, size_t size, const char *fname, int l_nmb);
extern void _leak_free(void *addr, const char *fname, int l_nmb);

extern char *_new_string(const char *s, const char *fname, int l_nmb);

/* Public entries */
#define	Malloc(s)	(_leak_malloc(1, (s), __FILE__, __LINE__))
#define	Calloc(n,s)	(_leak_calloc(1, (n), (s), __FILE__, __LINE__))
#define	Realloc(p,s)	(_leak_realloc(1, (void *)(p), (s), __FILE__, __LINE__))
#define	Free(p)		(_leak_free((void *)(p), __FILE__, __LINE__))

#define	TryMalloc(s)	(_leak_malloc(0, (s), __FILE__, __LINE__))
#define	TryCalloc(n,s)	(_leak_calloc(0, (n), (s), __FILE__, __LINE__))
#define	TryRealloc(p,s)	(_leak_realloc(0, (void *)(p), (s), __FILE__, __LINE__))

#define	new(type)	((type *)Malloc(sizeof (type)))
#define	new_string(s)	(_new_string((s), __FILE__, __LINE__))

extern void ReportMemoryLeaks(FILE *f);
extern void MemClobber(void *p, size_t size);

#endif	/* _MALLOC_H_ */