This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: malloc attributes and realloc
Here's another data point for what is currently implemented in GCC for
__attribute__ (malloc).
The pointers returned by an __attribute__ (malloc) function are not
considered distinct, see the pointer comparison example in the foobar
function below.
It seems that this might the reason that having __attribute__ (malloc)
on the realloc function in glibc has not created any problems up to now.
But if code is added to GCC to optimize the comparison in foobar, then
having __attribute__ (malloc) for realloc might not be appropriate
anymore.
extern void *myalloc_withattribute (unsigned int size) __attribute__((malloc));
extern int link_error (void);
struct str {
int *aaaa;
int *bbbb;
};
int
foobar (unsigned int SZ, struct str *test)
{
int *aaaa;
int *bbbb;
aaaa = myalloc_withattribute (SZ*sizeof(int));
bbbb = myalloc_withattribute (SZ*sizeof(int));
/* this is not resolved at compile time at this time */
if (aaaa == bbbb)
return link_error ();
test->aaaa = aaaa;
test->bbbb = bbbb;
return 0;
}
the generated x86 assembly (using -O2 -fomit-frame-pointer):
foobar:
subl $28, %esp
movl %ebx, 16(%esp)
movl 32(%esp), %ebx
movl %esi, 20(%esp)
movl %edi, 24(%esp)
sall $2, %ebx
movl 36(%esp), %edi
movl %ebx, (%esp)
call myalloc_withattribute
movl %ebx, (%esp)
movl %eax, %esi
call myalloc_withattribute
cmpl %eax, %esi
je .L7
movl %eax, 4(%edi)
xorl %eax, %eax
movl %esi, (%edi)
movl 16(%esp), %ebx
movl 20(%esp), %esi
movl 24(%esp), %edi
addl $28, %esp
ret
.p2align 4,,7
.L7:
movl 16(%esp), %ebx
movl 20(%esp), %esi
movl 24(%esp), %edi
addl $28, %esp
jmp link_error