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

--- Comment #44 from qinzhao at gcc dot gnu.org ---
(In reply to uecker from comment #41)
> I also do not yet understand why this feature is needed. The count should be
> set anyway. 
Yes. But setting the count in _every_ place where a structure with a FAM is
allocated will be a pain if there are a lot of places need to be modified to
add such setting (open-coded assignment as Kees mentioned)

>  Is the reason that in existing kernel code the array is
> initialized before the count is set, and adding it to "alloc" will then
> initialize it earlier, so that the code does not have to be modified?

from Kees description, I guess that in the current kernel, the structures with
FAMs is allocated by a MACRO:

  #define alloc(P, FAM, COUNT) ({ \
    size_t __size = sizeof(*P) + sizeof(*P->FAM) * COUNT; \
    kmalloc(__size, GFP); \
  })

when adding "counted-by" attribute to the FAMs, each structure with a FAM need
to explicitly set its counted-by field in the source code, which is a very
tedious coding experience. 

With this new builtin, the above allocation MACRO can be modified as following:

  #define alloc(P, FAM, COUNT) ({ \
    typeof(P) __p; \
    size_t __size = sizeof(*P) + sizeof(*P->FAM) * COUNT; \
    __p = kmalloc(__size, GFP); \
    if __builtin_has_attribute (__p->FAM, counted_by) \
      *__builtin_get_counted_by(__p->FAM) = COUNT; \
    __p; \
  })

Then eliminate the need to add a open-coded assignment for each structure with
a FAM. 
hope this makes sense.

Reply via email to