Author: Bleep <tomh@inxpress.net>
[ircu2.10.12-pk.git] / ircd / fda_t.c
1 /*
2  * fdatest.c - Free Debug Allocator
3  * Copyright (C) 1997 Thomas Helvey <tomh@inxpress.net>
4  *
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)
8  * any later version.
9  *
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.
14  *
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.
18  */
19 #include "fda.h"
20 #include <stdio.h>
21 #include <assert.h>
22
23 #define ALLOC_SIZE 100000
24
25 static int empty_slots   = 0;
26 static int longest_chain = 0;
27
28 void location_enumerator(const char* file, int line, int count)
29 {
30   printf("%s line: %d count: %d\n", file, line, count);
31 }
32
33 void leak_enumerator(const char* file, int line, size_t size, void* ptr)
34 {
35   printf("Memory leak: %s: %d - %d bytes (%p)\n", file, line, size, ptr);
36 }
37
38 void hash_enumerator(int slot, int links)
39 {
40   /* printf("s: %d l: %d\n", slot, links); */
41   if (0 == links)
42     ++empty_slots;
43   else if (longest_chain < links)
44     longest_chain = links;
45 }
46
47 void test_hash(int count)
48 {
49   int i = 0;
50   void** bufs = (void**) malloc(sizeof(void*) * count);
51   memset(bufs, 0, sizeof(void*) * count);
52
53   for (i = 0; i < count; ++i)
54     bufs[i] = malloc(i * 2 + 1);
55
56   empty_slots   = 0;
57   longest_chain = 0;
58   
59   fda_dump_hash(hash_enumerator);
60   printf("empty: %d longest: %d\n", empty_slots, longest_chain);
61   
62   for (i = 0; i < count; ++i)
63     bufs[i] = realloc(bufs[i], i * 3 + 1);
64
65   empty_slots   = 0;
66   longest_chain = 0;
67
68   fda_dump_hash(hash_enumerator);
69   printf("empty: %d longest: %d\n", empty_slots, longest_chain);
70   
71   for (i = 0; i < count; ++i) {
72     free(bufs[i]);
73     bufs[i] = malloc(i * 4 + 1);
74   }
75
76   empty_slots   = 0;
77   longest_chain = 0;
78
79   fda_dump_hash(hash_enumerator);
80   printf("empty: %d longest: %d\n", empty_slots, longest_chain);
81   
82   for (i = 0; i < count; ++i)
83     free(bufs[i]);
84   free((void*)bufs);
85 }
86
87 int main(void)
88 {
89   static void* allocations[100];
90   char* str;
91   char* realloc_ptr;
92   int i;
93   
94   test_hash(100); 
95   test_hash(256); 
96   test_hash(800); 
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);
101   }
102   
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));
108   test_hash(10); 
109   for (i = 0; i < 100; ++i)
110     fda_set_ref(allocations[i]);
111   fda_set_ref(str);
112   fda_set_ref(realloc_ptr);
113   test_hash(100); 
114   printf("Location listing\n");
115   i = fda_enum_locations(location_enumerator);
116   printf("Total locations: %d\n", i);
117   fda_assert_refs();
118   fda_clear_refs();
119   for (i = 0; i < 100; ++i)
120     free(allocations[i]);
121   realloc_ptr = realloc(realloc_ptr, 100);
122   fda_set_ref(str);
123   fda_set_ref(realloc_ptr);
124   assert(valid_ptr(realloc_ptr, 100));
125   fda_assert_refs();
126   free(str);
127   free(realloc_ptr);
128   fda_assert_refs();
129   str = strdup("Hello There");
130   realloc_ptr = str++;
131   str++;
132   printf("checking offset ptr\n");
133   assert(valid_ptr(str, 9));
134   free(realloc_ptr);
135   printf("Listing memory leaks\n");
136   i = fda_enum_leaks(leak_enumerator);
137   printf("Total Leaks: %d\n", i);
138   fda_assert_refs();
139   printf("fdatest completed with no errors\n");
140   return 0;
141 }
142