This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c/81798] New: Please introduce new attribute to tell that function zeroes returned memory


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81798

            Bug ID: 81798
           Summary: Please introduce new attribute to tell that function
                    zeroes returned memory
           Product: gcc
           Version: 7.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: bugzilla@poradnik-webmastera.com
  Target Milestone: ---

gcc is able to detect case when memory is allocated using calloc and then
zeroed out again using memset - in such case it removes this extra memset.
However it cannot do the same with custom calloc-like functions. Please
introduce new function attribute for this, or allow to pass argument to
existing malloc attribute.

Note: this attribute probably could be useful with other functions too, so it
should have more general name.

Code:

#include <string.h>
#include <stdlib.h>

void* test()
{
    void* p = calloc(10240, 1);
    memset(p, 0, 10240);
    return p;
}

__attribute__((malloc, alloc_size(1)))
void* mycalloc(size_t size);

void* test2()
{
    void* p = mycalloc(10240);
    memset(p, 0, 10240);
    return p;
}

Result:

test():
  mov esi, 1
  mov edi, 10240
  jmp calloc
test2():
  sub rsp, 8
  mov edi, 10240
  call mycalloc(unsigned long)
  mov edx, 10240
  xor esi, esi
  mov rdi, rax
  call memset
  add rsp, 8
  ret

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]