[clang] [WIP][Clang] Add __builtin_get_counted_by builtin (PR #102549)
Bill Wendling via cfe-commits
cfe-commits at lists.llvm.org
Thu Aug 8 16:47:10 PDT 2024
bwendling wrote:
> I'd expect some kind of diagnostic when the specified field doesn't have a corresponding counted_by field.
So there's a complication with that. The use case for this builtin is to automatically set the `count` field during allocation in the Linux kernel. (It could be used elsewhere, but Linux is why we're doing this now.) From my understanding, @kees wants to have a way to get the `count` field without having to change the allocator's prototype. (Is that correct?) That's not currently possible, or at least we haven't found a way to do it yet.
The idea is to do something like:
```c
#define kmalloc(type, COUNT) ({ \
... \
if (__builtin_get_counted_by(__p->FAM)) \
*__builtin_get_counted_by(__p->FAM) = COUNT; \
__p; \
})
```
We want the builtin to return a `nullptr` for this reason (because Clang doesn't have a `__builtin_has_attribute` builtin). But also we can use the same `kmalloc` for all allocations and not have to have one special `kmalloc` for a FAM that uses `counted_by` and one for every other allocation.
As you can see, this builtin has a very limited utility. Obviously, one wouldn't write:
```c
*__builtin_get_counted_by(ptr->FAM) = 42;
```
when using `ptr->count = 42` is far easier.
https://github.com/llvm/llvm-project/pull/102549
More information about the cfe-commits
mailing list