[riot-notifications] [RIOT-OS/RIOT] sys: Add helpers for pointer tagging (#15440)

Marian Buschsieweke notifications at github.com
Fri Nov 13 12:29:58 CET 2020

### Contribution description

#### Background

Pointer tagging refers to (ab)using the unused least significant bits in pointers. E.g. on 32 pointers to word-aligned `struct` (or any other word aligned memory chunk), the two least significant bits have to be zero in any case. Sometimes using these two bits to encode additional data can help saving some RAM.

However, RIOT also supports 8-bit and 16-bit platforms, which have only 0 or 1 bit unused in pointers to word-aligned memory. Without enforcing an alignment to (at least) four bytes, architecture independent pointer tagging is impossible.

#### The Solution

This PR adds an `PTRTAG` attribute that can be used to enforce an 4 byte alignment. This allows using pointer tagging to be used platform independent. And also one helper function for wrapping up a pointer and a two-bit tag into a tagged pointer and two helper function to unwrap the original pointer and the tag for the tagged pointer are also added. This should be a bit more readable than manual bit-fiddling with pointers.

### Testing procedure

The provided test in `tests/sys_ptrtag` should pass. It intentionally uses memory that normally is only byte aligned on all architectures. So if the test passes on a 32 bit arch, it will also pass on 16 bit and 8 bit platforms. (Tested myself on ARM and AVR.)

### Issues/PRs references

You can view, comment on, or merge this pull request online at:


-- Commit Summary --

  * sys: Add helpers for pointer tagging
  * tests: Add test for sys/ptrtag

-- File Changes --

    A sys/include/ptrtag.h (126)
    A tests/sys_ptrtag/Makefile (3)
    A tests/sys_ptrtag/main.c (61)
    A tests/sys_ptrtag/tests/01-run.py (20)

-- Patch Links --


You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.riot-os.org/pipermail/notifications/attachments/20201113/a5628871/attachment.htm>

More information about the notifications mailing list