2 * fdatest.c - Free Debug Allocator
3 * Copyright (C) 1997 Thomas Helvey <tomh@inxpress.net>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #define ALLOC_SIZE 100000
25 static int empty_slots = 0;
26 static int longest_chain = 0;
28 void location_enumerator(const char* file, int line, int count)
30 printf("%s line: %d count: %d\n", file, line, count);
33 void leak_enumerator(const char* file, int line, size_t size, void* ptr)
35 printf("Memory leak: %s: %d - %d bytes (%p)\n", file, line, size, ptr);
38 void hash_enumerator(int slot, int links)
40 /* printf("s: %d l: %d\n", slot, links); */
43 else if (longest_chain < links)
44 longest_chain = links;
47 void test_hash(int count)
50 void** bufs = (void**) malloc(sizeof(void*) * count);
51 memset(bufs, 0, sizeof(void*) * count);
53 for (i = 0; i < count; ++i)
54 bufs[i] = malloc(i * 2 + 1);
59 fda_dump_hash(hash_enumerator);
60 printf("empty: %d longest: %d\n", empty_slots, longest_chain);
62 for (i = 0; i < count; ++i)
63 bufs[i] = realloc(bufs[i], i * 3 + 1);
68 fda_dump_hash(hash_enumerator);
69 printf("empty: %d longest: %d\n", empty_slots, longest_chain);
71 for (i = 0; i < count; ++i) {
73 bufs[i] = malloc(i * 4 + 1);
79 fda_dump_hash(hash_enumerator);
80 printf("empty: %d longest: %d\n", empty_slots, longest_chain);
82 for (i = 0; i < count; ++i)
89 static void* allocations[100];
97 for (i = 0; i < 100; ++i) {
98 allocations[i] = malloc(ALLOC_SIZE);
99 assert(valid_ptr(allocations[i], ALLOC_SIZE));
100 assert(fda_sizeof(allocations[i]) == ALLOC_SIZE);
103 str = strdup("This is a string test");
104 realloc_ptr = malloc(100);
105 realloc_ptr = realloc(realloc_ptr, 1000);
106 printf("Allocations ok\n");
107 printf("str has %d bytes allocated\n", fda_sizeof(str));
109 for (i = 0; i < 100; ++i)
110 fda_set_ref(allocations[i]);
112 fda_set_ref(realloc_ptr);
114 printf("Location listing\n");
115 i = fda_enum_locations(location_enumerator);
116 printf("Total locations: %d\n", i);
119 for (i = 0; i < 100; ++i)
120 free(allocations[i]);
121 realloc_ptr = realloc(realloc_ptr, 100);
123 fda_set_ref(realloc_ptr);
124 assert(valid_ptr(realloc_ptr, 100));
129 str = strdup("Hello There");
132 printf("checking offset ptr\n");
133 assert(valid_ptr(str, 9));
135 printf("Listing memory leaks\n");
136 i = fda_enum_leaks(leak_enumerator);
137 printf("Total Leaks: %d\n", i);
139 printf("fdatest completed with no errors\n");