-
Notifications
You must be signed in to change notification settings - Fork 10.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[libc] Provide sys/queue.h #78081
Merged
Merged
[libc] Provide sys/queue.h #78081
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7c725a9
[libc] Provide sys/queue.h
petrhosek fe8a1d1
Implement the remaining SLIST and STAILQ macros
petrhosek 9a13c0a
Address review feedback
petrhosek 5b89ca8
Fix tests
petrhosek 668192b
Address review feedback
petrhosek 6230748
Address review feedback
petrhosek File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
//===-- Definition of the containerof macro -------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef __LLVM_LIBC_MACROS_CONTAINEROF_MACRO_H | ||
#define __LLVM_LIBC_MACROS_CONTAINEROF_MACRO_H | ||
|
||
#include <llvm-libc-macros/offsetof-macro.h> | ||
|
||
#define __containerof(ptr, type, member) \ | ||
({ \ | ||
const __typeof(((type *)0)->member) *__ptr = ptr; \ | ||
(type *)(void *)((const char *)__ptr - offsetof(type, member)); \ | ||
}) | ||
|
||
#endif // __LLVM_LIBC_MACROS_CONTAINEROF_MACRO_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,260 @@ | ||
//===-- Macros defined in sys/queue.h header file -------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef __LLVM_LIBC_MACROS_SYS_QUEUE_MACROS_H | ||
#define __LLVM_LIBC_MACROS_SYS_QUEUE_MACROS_H | ||
|
||
#include <llvm-libc-macros/containerof-macro.h> | ||
#include <llvm-libc-macros/null-macro.h> | ||
|
||
#ifdef __cplusplus | ||
#define QUEUE_TYPEOF(type) type | ||
#else | ||
#define QUEUE_TYPEOF(type) struct type | ||
#endif | ||
|
||
// Singly-linked list definitions. | ||
|
||
#define SLIST_HEAD(name, type) \ | ||
struct name { \ | ||
struct type *first; \ | ||
} | ||
|
||
#define SLIST_CLASS_HEAD(name, type) \ | ||
struct name { \ | ||
class type *first; \ | ||
} | ||
|
||
#define SLIST_HEAD_INITIALIZER(head) \ | ||
{ NULL } | ||
|
||
#define SLIST_ENTRY(type) \ | ||
struct { \ | ||
struct type *next; \ | ||
} | ||
|
||
#define SLIST_CLASS_ENTRY(type) \ | ||
struct { \ | ||
class type *next; \ | ||
} | ||
|
||
// Singly-linked list access methods. | ||
|
||
#define SLIST_EMPTY(head) ((head)->first == NULL) | ||
#define SLIST_FIRST(head) ((head)->first) | ||
#define SLIST_NEXT(elem, field) ((elem)->field.next) | ||
|
||
#define SLIST_FOREACH(var, head, field) \ | ||
for ((var) = SLIST_FIRST(head); (var); (var) = SLIST_NEXT(var, field)) | ||
|
||
#define SLIST_FOREACH_FROM(var, head, field) \ | ||
if (!(var)) \ | ||
(var) = SLIST_FIRST(head); \ | ||
for (; (var); (var) = SLIST_NEXT(var, field)) | ||
|
||
#define SLIST_FOREACH_SAFE(var, head, field, tvar) \ | ||
for ((var) = SLIST_FIRST(head); \ | ||
(var) && ((tvar) = SLIST_NEXT(var, field), 1); (var) = (tvar)) | ||
|
||
#define SLIST_FOREACH_FROM_SAFE(var, head, field, tvar) \ | ||
if (!(var)) \ | ||
(var) = SLIST_FIRST(head); \ | ||
for (; (var) && ((tvar) = SLIST_NEXT(var, field), 1); (var) = (tvar)) | ||
|
||
// Singly-linked list functions. | ||
|
||
#define SLIST_CONCAT(head1, head2, type, field) \ | ||
do { \ | ||
if (SLIST_EMPTY(head1)) { \ | ||
if ((SLIST_FIRST(head1) = SLIST_FIRST(head2)) != NULL) \ | ||
SLIST_INIT(head2); \ | ||
} else if (!SLIST_EMPTY(head2)) { \ | ||
QUEUE_TYPEOF(type) *cur = SLIST_FIRST(head1); \ | ||
while (SLIST_NEXT(cur, field) != NULL) \ | ||
cur = SLIST_NEXT(cur, field); \ | ||
SLIST_NEXT(cur, field) = SLIST_FIRST(head2); \ | ||
SLIST_INIT(head2); \ | ||
} \ | ||
} while (0) | ||
|
||
#define SLIST_INIT(head) \ | ||
do { \ | ||
SLIST_FIRST(head) = NULL; \ | ||
} while (0) | ||
|
||
#define SLIST_INSERT_AFTER(slistelem, elem, field) \ | ||
do { \ | ||
SLIST_NEXT(elem, field) = SLIST_NEXT(slistelem, field); \ | ||
SLIST_NEXT(slistelem, field) = (elem); \ | ||
} while (0) | ||
|
||
#define SLIST_INSERT_HEAD(head, elem, field) \ | ||
do { \ | ||
SLIST_NEXT(elem, field) = SLIST_FIRST(head); \ | ||
SLIST_FIRST(head) = (elem); \ | ||
} while (0) | ||
|
||
#define SLIST_REMOVE(head, elem, type, field) \ | ||
do { \ | ||
if (SLIST_FIRST(head) == (elem)) { \ | ||
SLIST_REMOVE_HEAD(head, field); \ | ||
} else { \ | ||
QUEUE_TYPEOF(type) *cur = SLIST_FIRST(head); \ | ||
while (SLIST_NEXT(elem, field) != (elem)) \ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should also be while (SLIST_NEXT(cur, field) != (elem)) \ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
cur = SLIST_NEXT(elem, field); \ | ||
SLIST_REMOVE_AFTER(cur, field); \ | ||
} \ | ||
} while (0) | ||
|
||
#define SLIST_REMOVE_AFTER(elem, field) \ | ||
do { \ | ||
SLIST_NEXT(elem, field) = SLIST_NEXT(SLIST_NEXT(elem, field), field); \ | ||
} while (0) | ||
|
||
#define SLIST_REMOVE_HEAD(head, field) \ | ||
do { \ | ||
SLIST_FIRST(head) = SLIST_NEXT(SLIST_FIRST(head), field); \ | ||
} while (0) | ||
|
||
#define SLIST_SWAP(head1, head2, type) \ | ||
do { \ | ||
QUEUE_TYPEOF(type) *first = SLIST_FIRST(head1); \ | ||
SLIST_FIRST(head1) = SLIST_FIRST(head2); \ | ||
SLIST_FIRST(head2) = first; \ | ||
} while (0) | ||
|
||
// Singly-linked tail queue definitions. | ||
|
||
#define STAILQ_HEAD(name, type) \ | ||
struct name { \ | ||
struct type *first; \ | ||
struct type **last; \ | ||
} | ||
|
||
#define STAILQ_CLASS_HEAD(name, type) \ | ||
struct name { \ | ||
class type *first; \ | ||
class type **last; \ | ||
} | ||
|
||
#define STAILQ_HEAD_INITIALIZER(head) \ | ||
{ NULL, &(head).first } | ||
|
||
#define STAILQ_ENTRY(type) \ | ||
struct { \ | ||
struct type *next; \ | ||
} | ||
|
||
#define STAILQ_CLASS_ENTRY(type) \ | ||
struct { \ | ||
class type *next; \ | ||
} | ||
|
||
// Singly-linked tail queue access methods. | ||
|
||
#define STAILQ_EMPTY(head) ((head)->first == NULL) | ||
#define STAILQ_FIRST(head) ((head)->first) | ||
#define STAILQ_LAST(head, type, field) \ | ||
(STAILQ_EMPTY(head) ? NULL : __containerof((head)->last, type, field.next)) | ||
#define STAILQ_NEXT(elem, field) ((elem)->field.next) | ||
|
||
#define STAILQ_FOREACH(var, head, field) \ | ||
for ((var) = STAILQ_FIRST(head); (var); (var) = STAILQ_NEXT(var, field)) | ||
|
||
#define STAILQ_FOREACH_FROM(var, head, field) \ | ||
if (!(var)) \ | ||
(var) = STAILQ_FIRST(head); \ | ||
for (; (var); (var) = STAILQ_NEXT(var, field)) | ||
|
||
#define STAILQ_FOREACH_SAFE(var, head, field, tvar) \ | ||
for ((var) = STAILQ_FIRST(head); \ | ||
(var) && ((tvar) = STAILQ_NEXT(var, field), 1); (var) = (tvar)) | ||
|
||
#define STAILQ_FOREACH_FROM_SAFE(var, head, field, tvar) \ | ||
if (!(var)) \ | ||
(var) = STAILQ_FIRST(head); \ | ||
for (; (var) && ((tvar) = STAILQ_NEXT(var, field), 1); (var) = (tvar)) | ||
|
||
// Singly-linked tail queue functions. | ||
|
||
#define STAILQ_CONCAT(head1, head2, type, field) \ | ||
do { \ | ||
if (!STAILQ_EMPTY(head2)) { \ | ||
*(head1)->last = (head2)->first; \ | ||
(head1)->last = (head2)->last; \ | ||
STAILQ_INIT(head2); \ | ||
} \ | ||
} while (0) | ||
|
||
#define STAILQ_INIT(head) \ | ||
do { \ | ||
STAILQ_FIRST(head) = NULL; \ | ||
(head)->last = &STAILQ_FIRST(head); \ | ||
} while (0) | ||
|
||
#define STAILQ_INSERT_AFTER(head, listelem, elem, field) \ | ||
do { \ | ||
if ((STAILQ_NEXT(elem, field) = STAILQ_NEXT(listelem, field)) == NULL) \ | ||
(head)->last = &STAILQ_NEXT(elem, field); \ | ||
STAILQ_NEXT(listelem, field) = (elem); \ | ||
} while (0) | ||
|
||
#define STAILQ_INSERT_HEAD(head, elem, field) \ | ||
do { \ | ||
if ((STAILQ_NEXT(elem, field) = STAILQ_FIRST(head)) == NULL) \ | ||
(head)->last = &STAILQ_NEXT(elem, field); \ | ||
STAILQ_FIRST(head) = (elem); \ | ||
} while (0) | ||
|
||
#define STAILQ_INSERT_TAIL(head, elem, field) \ | ||
do { \ | ||
STAILQ_NEXT(elem, field) = NULL; \ | ||
*(head)->last = (elem); \ | ||
(head)->last = &STAILQ_NEXT(elem, field); \ | ||
} while (0) | ||
|
||
#define STAILQ_REMOVE(head, elem, type, field) \ | ||
do { \ | ||
if (STAILQ_FIRST(head) == (elem)) { \ | ||
STAILQ_REMOVE_HEAD(head, field); \ | ||
} else { \ | ||
QUEUE_TYPEOF(type) *cur = STAILQ_FIRST(head); \ | ||
while (STAILQ_NEXT(cur, field) != (elem)) \ | ||
cur = STAILQ_NEXT(cur, field); \ | ||
STAILQ_REMOVE_AFTER(head, cur, field); \ | ||
} \ | ||
} while (0) | ||
|
||
#define STAILQ_REMOVE_AFTER(head, elem, field) \ | ||
do { \ | ||
if ((STAILQ_NEXT(elem, field) = \ | ||
STAILQ_NEXT(STAILQ_NEXT(elem, field), field)) == NULL) \ | ||
(head)->last = &STAILQ_NEXT(elem, field); \ | ||
} while (0) | ||
|
||
#define STAILQ_REMOVE_HEAD(head, field) \ | ||
do { \ | ||
if ((STAILQ_FIRST(head) = STAILQ_NEXT(STAILQ_FIRST(head), field)) == NULL) \ | ||
(head)->last = &STAILQ_FIRST(head); \ | ||
} while (0) | ||
|
||
#define STAILQ_SWAP(head1, head2, type) \ | ||
do { \ | ||
QUEUE_TYPEOF(type) *first = STAILQ_FIRST(head1); \ | ||
QUEUE_TYPEOF(type) **last = (head1)->last; \ | ||
STAILQ_FIRST(head1) = STAILQ_FIRST(head2); \ | ||
(head1)->last = (head2)->last; \ | ||
STAILQ_FIRST(head2) = first; \ | ||
(head2)->last = last; \ | ||
if (STAILQ_EMPTY(head1)) \ | ||
(head1)->last = &STAILQ_FIRST(head1); \ | ||
if (STAILQ_EMPTY(head2)) \ | ||
(head2)->last = &STAILQ_FIRST(head2); \ | ||
} while (0) | ||
|
||
#endif // __LLVM_LIBC_MACROS_SYS_QUEUE_MACROS_H |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: the Linux kernel has some updates to this macro, such as:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/include/linux/kernel.h?id=c7acec713d14c6ce8a20154f9dfda258d6bcad3b
I don't think we need those per-se at the moment, but I do wonder when we will.