<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="http://wiki.darenet.org/skins/common/feed.css?12"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
	<channel>
		<title>Free Debug Allocator (FDA) - Revision history</title>
		<link>http://wiki.darenet.org/index.php?title=Free_Debug_Allocator_(FDA)&amp;action=history</link>
		<description>Revision history for this page on the wiki</description>
		<language>en</language>
		<generator>MediaWiki 1.15.1</generator>
		<lastBuildDate>Sun, 05 Apr 2026 20:57:21 GMT</lastBuildDate>
		<item>
			<title>Secretagent:&amp;#32;New page: &lt;pre&gt;fdaman.txt - brief usage information for FDA (Free Debug Allocator)  Copyright (C) 1998 Thomas Helvey &lt;tomh@inxpress.net&gt;  1. Base Functionality Basic use of the fda tools is as simpl...</title>
			<link>http://wiki.darenet.org/index.php?title=Free_Debug_Allocator_(FDA)&amp;diff=2234&amp;oldid=prev</link>
			<description>&lt;p&gt;New page: &amp;lt;pre&amp;gt;fdaman.txt - brief usage information for FDA (Free Debug Allocator)  Copyright (C) 1998 Thomas Helvey &amp;lt;tomh@inxpress.net&amp;gt;  1. Base Functionality Basic use of the fda tools is as simpl...&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&amp;lt;pre&amp;gt;fdaman.txt - brief usage information for FDA (Free Debug Allocator)&lt;br /&gt;
&lt;br /&gt;
Copyright (C) 1998 Thomas Helvey &amp;lt;tomh@inxpress.net&amp;gt;&lt;br /&gt;
&lt;br /&gt;
1. Base Functionality&lt;br /&gt;
Basic use of the fda tools is as simple as including the header&lt;br /&gt;
and source file with your source defining DEBUG and using capitalized&lt;br /&gt;
versions of malloc(), calloc(), realloc(), and free().&lt;br /&gt;
The fda allocation functions verify all your arguments and will call&lt;br /&gt;
assert() if something is wrong. FDA trashes all allocated memory&lt;br /&gt;
in a predictable manner and applies prefix and postfix bounds checking&lt;br /&gt;
signatures to every allocation. When a pointer is freed it's validated,&lt;br /&gt;
and checked for overflows and underflows. The fda Realloc function&lt;br /&gt;
does not allow malloc or free through realloc and forces reallocation&lt;br /&gt;
if the required memory is larger than the current allocation.&lt;br /&gt;
&lt;br /&gt;
In both the DEBUG and non-debug versions of fda, if a memory allocation&lt;br /&gt;
fails once, a low memory callback function is called to allow you to&lt;br /&gt;
release some memory and allow malloc to succeed, the default version&lt;br /&gt;
prints a warning message to stderr. If the low memory callback returns&lt;br /&gt;
the allocation is attempted again, if the second allocation fails a&lt;br /&gt;
no memory callback function is called to allow you to clean up before&lt;br /&gt;
terminating the application, if you allow the no memory callback to&lt;br /&gt;
return the results are undefined. (a NULL ptr will be returned from the&lt;br /&gt;
allocation call) The default no memory handler prints an error message&lt;br /&gt;
to stderr and calls abort(). The DEBUG version will assert if you allow&lt;br /&gt;
the no memory function to return.&lt;br /&gt;
Both the low memory and no memory callbacks have the signature of:&lt;br /&gt;
void handler(void)&lt;br /&gt;
&lt;br /&gt;
The debug version of fda will not allow you to do the following:&lt;br /&gt;
Allocate a zero length chunk of memory.&lt;br /&gt;
Free a non-allocated pointer.&lt;br /&gt;
Free a pointer twice.&lt;br /&gt;
Free a pointer at the wrong offset.&lt;br /&gt;
Use realloc to free memory. (realloc(p, 0))&lt;br /&gt;
Use realloc to malloc memory. (realloc(0, s))&lt;br /&gt;
Overwrite the bounds of the memory you allocated. (checked on free)&lt;br /&gt;
&lt;br /&gt;
The base functions for fda are:&lt;br /&gt;
void* malloc(size_t)&lt;br /&gt;
void* realloc(void*, size_t)&lt;br /&gt;
void* calloc(size_t, size_t)&lt;br /&gt;
void  free(void*)&lt;br /&gt;
char* strdup(const char*)&lt;br /&gt;
void  set_lowmem_handler(void (*fn)(void))&lt;br /&gt;
void  set_nomem_handler(void (*fn)(void))&lt;br /&gt;
&lt;br /&gt;
FDA uses a hash table to lookup pointers. The size of the hash table can&lt;br /&gt;
be changed at compile time by using -DFDA_HASH_TABLE_SIZE &amp;lt;prime&amp;gt;&lt;br /&gt;
where prime is a prime number somewhere around the number of allocations&lt;br /&gt;
expected. The default hash table size is 16339 which should be large enough&lt;br /&gt;
to hold most medium sized applications. FDA allocates memory for it's&lt;br /&gt;
debugging records so if your application uses a lot of memory you&lt;br /&gt;
may want to make sure you have enough memory available to use the debug&lt;br /&gt;
version. FDA allocates 20 bytes to store each allocation and 20 bytes&lt;br /&gt;
to store location information (file and line info). This overhead only&lt;br /&gt;
applies if you have DEBUG or _DEBUG defined.&lt;br /&gt;
&lt;br /&gt;
2. Extended functionality&lt;br /&gt;
FDA provides a few handy functions for validating pointers and&lt;br /&gt;
checking for overruns before they occur when debugging.&lt;br /&gt;
The first function fda_sizeof(ptr) returns the size of the buffer&lt;br /&gt;
pointed to by ptr, this allows you to verify that your pointer&lt;br /&gt;
is what it says it is. fda_sizeof() will call assert() if the&lt;br /&gt;
pointer you pass it is not at the start of an allocation.&lt;br /&gt;
&lt;br /&gt;
The second function valid_ptr(ptr, size) verifies that ptr lies within&lt;br /&gt;
allocated memory and there are at least size bytes available in the buffer.&lt;br /&gt;
You can pass valid_ptr() a pointer to any location in allocated memory.&lt;br /&gt;
valid_ptr() calls assert if the pointer points outside of allocated memory&lt;br /&gt;
or the remaining size is less than the size specified.&lt;br /&gt;
valid_ptr() is more efficient if the pointer argument is the value&lt;br /&gt;
returned from malloc because it's a simple hash table lookup, a more&lt;br /&gt;
exhaustive algorithm is used if it's not found in the hash table.&lt;br /&gt;
&lt;br /&gt;
FDA extended functions:&lt;br /&gt;
size_t fda_sizeof(const void*)&lt;br /&gt;
int    valid_ptr(const void*, size_t)&lt;br /&gt;
&lt;br /&gt;
Typical usage for the fda extended functions:&lt;br /&gt;
Note: the assert macro compiles to nothing if NDEBUG is defined.&lt;br /&gt;
Verify a foo_ptr:&lt;br /&gt;
assert(sizeof(struct foo) == fda_sizeof(foo_ptr));&lt;br /&gt;
assert(valid_ptr(foo_ptr, sizeof(struct foo)));&lt;br /&gt;
Check for room to write:&lt;br /&gt;
assert(valid_ptr(buf, len));&lt;br /&gt;
&lt;br /&gt;
3. Leak checking and block validation&lt;br /&gt;
FDA has several functions for leak checking, and reference marking.&lt;br /&gt;
fda_clear_refs() iterates through all of the allocated blocks of&lt;br /&gt;
memory and clears the referenced flag.&lt;br /&gt;
fda_set_ref() marks a single allocation(block) as being referenced or&lt;br /&gt;
in use by somebody.&lt;br /&gt;
fda_assert_refs() iterates through all the allocated blocks of&lt;br /&gt;
memory and calls assert() if it finds one that's not referenced.&lt;br /&gt;
&lt;br /&gt;
Typical usage of the block validation functions:&lt;br /&gt;
fda_clear_refs();   /* clear all block references */&lt;br /&gt;
&lt;br /&gt;
for each allocation do&lt;br /&gt;
fda_set_ref(allocation);  /* mark allocation in use */&lt;br /&gt;
done&lt;br /&gt;
&lt;br /&gt;
fda_assert_refs();  /* this will assert if a leak is found */&lt;br /&gt;
&lt;br /&gt;
4. Reporting functions:&lt;br /&gt;
FDA has 4 functions for reporting various aspects of allocation&lt;br /&gt;
status.&lt;br /&gt;
fda_get_byte_count() tells you the current total number of bytes&lt;br /&gt;
your application has allocated. (this does not include debugging records)&lt;br /&gt;
This will give you an idea of how much memory your application is&lt;br /&gt;
using at any given time.&lt;br /&gt;
&lt;br /&gt;
fda_get_block_count() returns the total count of current allocations.&lt;br /&gt;
&lt;br /&gt;
fda_enum_locations() calls a user supplied enumeration function with&lt;br /&gt;
file, line, count information, this allows you to see your file by&lt;br /&gt;
file allocation density. ;) fda_enum_locations() returns the total&lt;br /&gt;
number of locations that have memory allocated.&lt;br /&gt;
&lt;br /&gt;
fda_enum_leaks() calls a user supplied enumeration function with&lt;br /&gt;
file, line, size, and ptr for every block not marked as referenced.&lt;br /&gt;
One use for fda_enum_leaks() is checking for leaks when your program&lt;br /&gt;
exits:&lt;br /&gt;
void enum_fn(const char* file, int line, size_t size, void* ptr)&lt;br /&gt;
{&lt;br /&gt;
  printf(&amp;quot;Memory leak: %s: %d - %d bytes at (%p)\n&amp;quot;, file, line, size, ptr);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main(void)&lt;br /&gt;
{&lt;br /&gt;
  ...&lt;br /&gt;
#if defined(DEBUG)&lt;br /&gt;
  /* check for memory leaks before exiting */&lt;br /&gt;
  fda_clear_refs();&lt;br /&gt;
  fda_enum_leaks(enum_fn);&lt;br /&gt;
#endif&lt;br /&gt;
  return 0;  /* return from main */&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
The test file fdatest.c gives examples of usage for most of the functions&lt;br /&gt;
available with FDA.&lt;br /&gt;
&lt;br /&gt;
Please let me know if you encounter any problems with FDA.&lt;br /&gt;
So far FDA has been built and tested on linux and Windows NT.&lt;br /&gt;
If you find that it works with or without change on other platforms&lt;br /&gt;
please let me know so I can make the appropriate changes to the&lt;br /&gt;
code and add it to the list of tested platforms.&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:Technical]]&lt;/div&gt;</description>
			<pubDate>Fri, 25 Apr 2008 02:55:59 GMT</pubDate>			<dc:creator>Secretagent</dc:creator>			<comments>http://wiki.darenet.org/Talk:Free_Debug_Allocator_(FDA)</comments>		</item>
	</channel>
</rss>