Pelles C forum

Pelles C => Add-ins => Topic started by: frankie on February 19, 2015, 06:40:28 pm

Title: PellesC _msize fix
Post by: frankie on February 19, 2015, 06:40:28 pm
This is not an addin, but there is no other section that exacly fit a compiler patch.
So a patch could be an adding to the compiler...
BTW these two simple files can be used to fix the brooken _msize of PellesC.

EDIT: This is upgraded version to make its use easier

How to use: Add the C file to your project, and include the header file to each module of the project, taking care to insert it just as the very first header even before of windows.h or stdio.h. This arrangement allow the correct redefinition of memory functions for all your code. Failing to do so can create mixed memory functions (especially when c modules are included) leading to crash-hangs.

How it works: the functions malloc, realloc, calloc, free and _msize are redefined to the routines in the C file. The patched routines add, for each allocation, an extra space for an unsigned 64 bits integer where it keeps the real allocation size.
I used a 64 bits variable not because such large counting was required, but because it is 8 bytes long and guarantee correct memory alignement for objects. Then the patched memory is passed through the conventional memory functions.
The disadvantage of this approach is that for each allocation are required 8 more bytes.

Caution! Be sure to apply the patch to all your modules, passing pointers got from patched routines to real crt functions, and the reverse, will give unpredictable results (normally crash  ;D)

PS: this can also be the start for memory profiling that wasn't available in PellesC ;D
Title: Re: PellesC _msize fix
Post by: frankie on February 20, 2015, 02:47:42 pm
I updated the code so you have simply insert the header as the very first header in your modules.
Download the new version from above.