develooper Front page | perl.perl5.porters | Postings from July 2016

'a' (malloc) flag in embed.fnc misused?

Thread Next
From:
Dave Mitchell
Date:
July 14, 2016 09:41
Subject:
'a' (malloc) flag in embed.fnc misused?
Message ID:
20160714094107.GE3477@iabyn.com
In embed.fnc, we mark about 80 functions with the a flag, which we
document as meaning:

:   a  Allocates memory a la malloc/calloc.  Also implies "R":
:
:         proto.h: add __attribute__malloc__


Now, the current gcc docs for the malloc attribute states:

    This tells the compiler that a function is malloc-like, i.e., that the
    pointer P returned by the function cannot alias any other pointer
    valid when the function returns, and moreover no pointers to valid
    objects occur in any storage addressed by P.

    Using this attribute can improve optimization. Functions like malloc
    and calloc have this property because they return a pointer to
    uninitialized or zeroed-out storage. However, functions like realloc
    do not have this property, as they can return a pointer to storage
    containing pointers.

This implies to me that we should only flag functions as 'a' if both:

    1) it returns a pointer to memory that isn't pointed to from elsewhere;
    2) that the block of memory itself contains no pointers.

I think we are failing condition (2) for many 'a' functions; for example
the op.c newXXXOP() functions return an alloced op struct (condition 1
probably satisfied), but which contain pointers (condition 2 fails).

If people agree with my analysis, I'll tighten up embed.fnc by removing
'a' from some functions, and update the description of the 'a' flag at the
top of embed.fnc.


-- 
Technology is dominated by two types of people: those who understand what
they do not manage, and those who manage what they do not understand. 

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About