-
Notifications
You must be signed in to change notification settings - Fork 5
/
bmalloc.c
96 lines (76 loc) · 2.2 KB
/
bmalloc.c
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
/* Copyright (C) 1997, 1998, 1999 Andrew McCallum
Written by: Andrew Kachites McCallum <[email protected]>
This file is part of the Bag-Of-Words Library, `libbow'.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License
as published by the Free Software Foundation, version 2.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA */
#define _BOW_MALLOC_INLINE_EXTERN
#include <bow/libbow.h>
/* This will compile the `malloc' functions without "inline extern" */
#if BOW_MCHECK
#include <mcheck.h>
#define ptrs_size 100000
static int ptrs_length = 0;
static void *ptrs[ptrs_size];
void
bow_malloc_check_all ()
{
int i;
enum mcheck_status status;
for (i = 0; i < ptrs_length; i++)
{
if (ptrs[i])
{
status = mprobe (ptrs[i]);
if (status != MCHECK_OK)
bow_error ("Bad heap");
}
}
}
void
_bow_malloc_hook (void *ptr)
{
assert (ptrs_length < ptrs_size);
ptrs[ptrs_length++] = ptr;
bow_malloc_check_all ();
}
void
_bow_realloc_hook (void *old, void *new)
{
int i;
assert (ptrs_length < ptrs_size);
for (i = 0; i < ptrs_length && ptrs[i] != old; i++);
if (ptrs[i] == old)
ptrs[i] = 0;
ptrs[ptrs_length++] = new;
bow_malloc_check_all ();
}
void
_bow_free_hook (void *ptr)
{
int i;
for (i = 0; i < ptrs_length; i++)
{
if (ptrs[i] == ptr)
{
ptrs[i] = 0;
break;
}
}
bow_malloc_check_all ();
}
void (*bow_malloc_hook) (void *ptr) = _bow_malloc_hook;
void (*bow_realloc_hook) (void *old, void *new) = _bow_realloc_hook;
void (*bow_free_hook) (void *ptr) = _bow_free_hook;
#else /* BOW_MCHECK */
void (*bow_malloc_hook) (void *ptr) = NULL;
void (*bow_realloc_hook) (void *old, void *new) = NULL;
void (*bow_free_hook) (void *ptr) = NULL;
#endif /* BOW_MCHECK */