debugmalloc for cc65

When programming in C you're bound to be bitten by problems related memory management sooner or later. The symptoms can be hard to diagnose, as an error in one part of the program can cause erratic behaviour in another, totally unrelated, part. The (understandable) lack of source level debuggers doesn't help either. debugmalloc tries to alleviate the problem a bit.

Features

Auto-initialization
The library initializes itself when it's first called.
Free memory display
The amount of free memory is displayed on the screen, together with the number of currently allocated chunks.
Allocation counter
The number of allocations - the number of deallocations are displayed on the screen.
Canaries
Magic bytes are inserted before and after every allocated memory chunk. When memory is deallocated, the library will issue a warning if they have been overwritten.
Sanity checks
Calls like malloc(0) and free(NULL) display warnings.
Allocation tracking
Allocated chunks are stored in an array, and every deallocation is checked against it. Deallocation of unallocated memory (such as an erroneous 2nd free call) displays a warning.
Enabled on a file by file basis
The debug routines are only enabled in source files that #include them so you don't have to run the whole application with them, only the parts that you want to debug.

Usage

Simply #include "debugmalloc.h" after #include <stdlib.h> in the source file that you want to debug. Adjust CANARY1, CANARY2, and MAXCHUNKS in debugmalloc.c to suit your needs.

Download

debugmalloc.h
debugmalloc.c

History

2004-07-16 v1.0
First release.

License

Copyright (c) 2004, Per Olofsson, magervalp@fastmail.fm.

Redistribution and use in source and binary forms, with or without modification, are permitted as specified in the standard BSD license.