]> jfr.im git - irc/quakenet/snircd.git/blame - doc/fda.txt
Merged revisions 122-126 via svnmerge from
[irc/quakenet/snircd.git] / doc / fda.txt
CommitLineData
189935b1 1fdaman.txt - brief usage information for FDA (Free Debug Allocator)
2
3Copyright (C) 1998 Thomas Helvey <tomh@inxpress.net>
4
51. Base Functionality
6Basic use of the fda tools is as simple as including the header
7and source file with your source defining DEBUG and using capitalized
8versions of malloc(), calloc(), realloc(), and free().
9The fda allocation functions verify all your arguments and will call
10assert() if something is wrong. FDA trashes all allocated memory
11in a predictable manner and applies prefix and postfix bounds checking
12signatures to every allocation. When a pointer is freed it's validated,
13and checked for overflows and underflows. The fda Realloc function
14does not allow malloc or free through realloc and forces reallocation
15if the required memory is larger than the current allocation.
16
17In both the DEBUG and non-debug versions of fda, if a memory allocation
18fails once, a low memory callback function is called to allow you to
19release some memory and allow malloc to succeed, the default version
20prints a warning message to stderr. If the low memory callback returns
21the allocation is attempted again, if the second allocation fails a
22no memory callback function is called to allow you to clean up before
23terminating the application, if you allow the no memory callback to
24return the results are undefined. (a NULL ptr will be returned from the
25allocation call) The default no memory handler prints an error message
26to stderr and calls abort(). The DEBUG version will assert if you allow
27the no memory function to return.
28Both the low memory and no memory callbacks have the signature of:
29void handler(void)
30
31The debug version of fda will not allow you to do the following:
32Allocate a zero length chunk of memory.
33Free a non-allocated pointer.
34Free a pointer twice.
35Free a pointer at the wrong offset.
36Use realloc to free memory. (realloc(p, 0))
37Use realloc to malloc memory. (realloc(0, s))
38Overwrite the bounds of the memory you allocated. (checked on free)
39
40The base functions for fda are:
41void* malloc(size_t)
42void* realloc(void*, size_t)
43void* calloc(size_t, size_t)
44void free(void*)
45char* strdup(const char*)
46void set_lowmem_handler(void (*fn)(void))
47void set_nomem_handler(void (*fn)(void))
48
49FDA uses a hash table to lookup pointers. The size of the hash table can
50be changed at compile time by using -DFDA_HASH_TABLE_SIZE <prime>
51where prime is a prime number somewhere around the number of allocations
52expected. The default hash table size is 16339 which should be large enough
53to hold most medium sized applications. FDA allocates memory for it's
54debugging records so if your application uses a lot of memory you
55may want to make sure you have enough memory available to use the debug
56version. FDA allocates 20 bytes to store each allocation and 20 bytes
57to store location information (file and line info). This overhead only
58applies if you have DEBUG or _DEBUG defined.
59
602. Extended functionality
61FDA provides a few handy functions for validating pointers and
62checking for overruns before they occur when debugging.
63The first function fda_sizeof(ptr) returns the size of the buffer
64pointed to by ptr, this allows you to verify that your pointer
65is what it says it is. fda_sizeof() will call assert() if the
66pointer you pass it is not at the start of an allocation.
67
68The second function valid_ptr(ptr, size) verifies that ptr lies within
69allocated memory and there are at least size bytes available in the buffer.
70You can pass valid_ptr() a pointer to any location in allocated memory.
71valid_ptr() calls assert if the pointer points outside of allocated memory
72or the remaining size is less than the size specified.
73valid_ptr() is more efficient if the pointer argument is the value
74returned from malloc because it's a simple hash table lookup, a more
75exhaustive algorithm is used if it's not found in the hash table.
76
77FDA extended functions:
78size_t fda_sizeof(const void*)
79int valid_ptr(const void*, size_t)
80
81Typical usage for the fda extended functions:
82Note: the assert macro compiles to nothing if NDEBUG is defined.
83Verify a foo_ptr:
84assert(sizeof(struct foo) == fda_sizeof(foo_ptr));
85assert(valid_ptr(foo_ptr, sizeof(struct foo)));
86Check for room to write:
87assert(valid_ptr(buf, len));
88
893. Leak checking and block validation
90FDA has several functions for leak checking, and reference marking.
91fda_clear_refs() iterates through all of the allocated blocks of
92memory and clears the referenced flag.
93fda_set_ref() marks a single allocation(block) as being referenced or
94in use by somebody.
95fda_assert_refs() iterates through all the allocated blocks of
96memory and calls assert() if it finds one that's not referenced.
97
98Typical usage of the block validation functions:
99fda_clear_refs(); /* clear all block references */
100
101for each allocation do
102fda_set_ref(allocation); /* mark allocation in use */
103done
104
105fda_assert_refs(); /* this will assert if a leak is found */
106
1074. Reporting functions:
108FDA has 4 functions for reporting various aspects of allocation
109status.
110fda_get_byte_count() tells you the current total number of bytes
111your application has allocated. (this does not include debugging records)
112This will give you an idea of how much memory your application is
113using at any given time.
114
115fda_get_block_count() returns the total count of current allocations.
116
117fda_enum_locations() calls a user supplied enumeration function with
118file, line, count information, this allows you to see your file by
119file allocation density. ;) fda_enum_locations() returns the total
120number of locations that have memory allocated.
121
122fda_enum_leaks() calls a user supplied enumeration function with
123file, line, size, and ptr for every block not marked as referenced.
124One use for fda_enum_leaks() is checking for leaks when your program
125exits:
126void enum_fn(const char* file, int line, size_t size, void* ptr)
127{
128 printf("Memory leak: %s: %d - %d bytes at (%p)\n", file, line, size, ptr);
129}
130
131int main(void)
132{
133 ...
134#if defined(DEBUG)
135 /* check for memory leaks before exiting */
136 fda_clear_refs();
137 fda_enum_leaks(enum_fn);
138#endif
139 return 0; /* return from main */
140}
141
142The test file fdatest.c gives examples of usage for most of the functions
143available with FDA.
144
145Please let me know if you encounter any problems with FDA.
146So far FDA has been built and tested on linux and Windows NT.
147If you find that it works with or without change on other platforms
148please let me know so I can make the appropriate changes to the
149code and add it to the list of tested platforms.
150
151