dustin / multisniff

A packet sniffing tool I wrote for an interop event.

This URL has Read+Write access

multisniff / mymalloc.c
100644 217 lines (188 sloc) 4.505 kb
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
/*
* Copyright (c) 2006 Dustin Sallings
*/
 
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <limits.h>
#include <assert.h>
 
/* #define NUM_MEMBUCKETS 16369 */
/* #define NUM_MEMBUCKETS 24571 */
#define NUM_MEMBUCKETS 12289
 
#ifdef MYMALLOC
static struct memories {
    char *p;
    size_t size;
    struct memories *next;
    char *file;
    int line;
} **memories = NULL;
 
static int *_mem_stats = NULL;
 
void _mdebug_dump(void);
 
static int
_getmemindex(void *p)
{
    return ((int) p % NUM_MEMBUCKETS);
}
 
static void
_register_mem(void *p, size_t size, char *file, int line)
{
    struct memories *m, *c;
    int index;
 
    assert(p);
    m = malloc(sizeof(struct memories));
    assert(m);
    m->p = p;
    m->size = size;
    m->next = NULL;
    m->file = file;
    m->line = line;
 
    if (memories == NULL) {
        memories = calloc(NUM_MEMBUCKETS, sizeof(struct memories *));
        assert(memories);
        _mem_stats = calloc(NUM_MEMBUCKETS, sizeof(int));
        assert(_mem_stats);
    }
    index = _getmemindex(p);
 
    /* Gather statistics */
    _mem_stats[index]++;
 
    c = memories[index];
 
    if (c == NULL) {
        memories[index] = m;
    } else {
        for (; c->next != NULL; c = c->next);
        c->next = m;
    }
}
 
void *
_lookup_mem(void *p)
{
    struct memories *c;
    int index;
    index = _getmemindex(p);
    for (c = memories[index]; c && c->p != p; c = c->next);
    return (c);
}
 
static void
_unregister_mem(void *p)
{
    struct memories *c, *tmp;
    int index;
 
    index = _getmemindex(p);
 
    /* Special case for first thingy */
    if (memories[index]->p == p) {
        if (memories[index]->next) {
            tmp = memories[index]->next;
            free(memories[index]);
            memories[index] = tmp;
        } else {
            free(memories[index]);
            memories[index] = NULL;
        }
    } else {
        for (c = memories[index]; c && c->next->p != p; c = c->next);
        assert(c);
        tmp = c->next;
        c->next = c->next->next;
        free(tmp);
    }
}
 
void
_mdebug_dump(void)
{
    struct memories *c;
    int i, count = 0, min, max, avg, empty;
 
    for (i = 0; i < NUM_MEMBUCKETS; i++) {
        for (c = memories[i]; c; c = c->next) {
            printf("Found memory at %d (%p)\n", i, (void *) c);
            count++;
            printf("MEM: %p is %d bytes (%p)\n", (void *) c->p, (int)c->size,
                   (void *) c->p);
            printf("\t%s line %d\n", c->file, c->line);
        }
    }
 
    if (count == 0)
        printf("No registered memory\n");
 
    max = 0;
    min = INT_MAX;
    avg = 0;
    empty = 0;
    for (i = 0; i < NUM_MEMBUCKETS; i++) {
        if (_mem_stats[i] == 0)
            empty++;
        if (_mem_stats[i] > max)
            max = _mem_stats[i];
        if (_mem_stats[i] < min)
            min = _mem_stats[i];
        avg += _mem_stats[i];
    }
 
    printf("Hash size was %d buckets\n"
           "Highest: %d\n"
           "Lowest: %d\n"
           "Average: %d\n"
           "Empty: %d\n",
           NUM_MEMBUCKETS, max, min, avg / NUM_MEMBUCKETS, empty);
}
 
void
_mdebug_long_stats(void)
{
    int i;
    for (i = 0; i < NUM_MEMBUCKETS; i++) {
        if (_mem_stats[i] > 0)
            printf("MEM: %d got %d hits\n", i, _mem_stats[i]);
    }
}
 
void *
_my_malloc(size_t size, char *file, int line)
{
    void *p;
    p = malloc(size);
    assert(p);
    _register_mem(p, size, file, line);
    return (p);
}
 
void *
_my_calloc(size_t n, size_t size, char *file, int line)
{
    void *p;
    p = calloc(n, size);
    assert(p);
    _register_mem(p, size * n, file, line);
    return (p);
}
 
char *
_my_strdup(char *str, char *file, int line)
{
    char *p;
    p = strdup(str);
    _register_mem(p, strlen(p), file, line);
    return (p);
}
 
void *
_my_realloc(void *p, size_t size, char *file, int line)
{
    void *ret;
    assert(_lookup_mem(p));
    _unregister_mem(p);
    ret = realloc(p, size);
    assert(ret);
    _register_mem(ret, size, file, line);
    return (ret);
}
 
void
_my_free(void *p, char *file, int line)
{
    void *tmp;
    tmp = _lookup_mem(p);
    if (tmp == NULL) {
        printf("Trying to free something that isn't mine: %p (%s) (%s:%d)\n",
               p, (char *) p,
               file, line);
        _mdebug_dump();
        abort();
    }
    _unregister_mem(p);
    free(p);
}
 
#endif /* MYMALLOC */