[fpc-devel] cmem not aligning memory

C Western l at c-m-w.me.uk
Thu Apr 8 15:05:14 CEST 2010


On 03/04/10 13:12, Jonas Maebe wrote:
>
> On 03 Apr 2010, at 14:09, Micha Nelissen wrote:
>
>> Do C memory managers guarantee any alignment anyway? Not for SSE (16 bytes) I'm sure, but 8 bytes I don't know.
>
>  From Linux' malloc man page:
>
> For calloc() and malloc(), the value returned is a pointer to the allo-
> cated memory, which is suitably aligned for any kind of variable, or
> NULL if the request fails.
>
> Mac OS X:
>
> The allocated memory is aligned such that it can be
> used for any data type, including AltiVec- and SSE-related types.
>

This means that the current cmem provides optimally bad alignment for 
doubles (and possibly invalid on some systems), as the alignment is 
offset by SizeOf(Integer).

How about an alternative implementation, without the integer recording 
size? I had a quick try at this - the only use of the MemSize routine in 
fpc+lazarus seems to be in an optimization in resizing strings. If this 
routine is adjusted to cope with MemSize returning 0 to indicate "not 
known" then most applications should work. This implementation as the 
additional advantage that mixing malloc and free's between C and Pascal 
then work.

PS: The CFreeMemSize seems to be incorrectly implemented - it does not 
offset the pointer.

Colin



More information about the fpc-devel mailing list