Skip to content

Commit dbc0955

Browse files
authored
[libc] Provide sys/queue.h (#78081)
This header first appeared in 4.4BSD and is provided by a number of C libraries including Newlib. Several of our embedded projects use this header and so to make LLVM libc a drop-in replacement, we need to provide it as well. For the initial commit, we only implement singly linked variants (SLIST and STAILQ). The doubly linked variants (LIST, TAILQ and CIRCLEQ) can be implemented in the future as needed.
1 parent b8967e0 commit dbc0955

File tree

13 files changed

+533
-1
lines changed

13 files changed

+533
-1
lines changed

libc/config/baremetal/arm/headers.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ set(TARGET_PUBLIC_HEADERS
88
libc.include.stdlib
99
libc.include.string
1010
libc.include.strings
11+
libc.include.sys_queue
1112
)

libc/config/baremetal/riscv/headers.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ set(TARGET_PUBLIC_HEADERS
88
libc.include.stdlib
99
libc.include.string
1010
libc.include.strings
11+
libc.include.sys_queue
1112
)

libc/config/linux/riscv/headers.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ set(TARGET_PUBLIC_HEADERS
3131
libc.include.sys_mman
3232
libc.include.sys_prctl
3333
libc.include.sys_random
34+
libc.include.sys_queue
3435
libc.include.sys_resource
3536
libc.include.sys_select
3637
libc.include.sys_socket

libc/config/linux/x86_64/headers.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ set(TARGET_PUBLIC_HEADERS
3030
libc.include.sys_ioctl
3131
libc.include.sys_mman
3232
libc.include.sys_prctl
33+
libc.include.sys_queue
3334
libc.include.sys_random
3435
libc.include.sys_resource
3536
libc.include.sys_select

libc/include/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,14 @@ add_gen_header(
345345
.llvm_libc_common_h
346346
)
347347

348+
add_header(
349+
sys_queue
350+
HDR
351+
sys/queue.h
352+
DEPENDS
353+
.llvm-libc-macros.sys_queue_macros
354+
)
355+
348356
add_gen_header(
349357
sys_random
350358
DEF_FILE sys/random.h.def

libc/include/llvm-libc-macros/CMakeLists.txt

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ function(add_macro_header name)
44
"MACRO_HEADER"
55
"" # Optional arguments
66
"HDR" # Single value arguments
7-
"" # Multi-value arguments
7+
"DEPENDS" # Multi-value arguments
88
${ARGN}
99
)
1010
if(TARGET libc.include.llvm-libc-macros.${LIBC_TARGET_OS}.${name})
@@ -14,12 +14,15 @@ function(add_macro_header name)
1414
${MACRO_HEADER_HDR}
1515
DEPENDS
1616
.${LIBC_TARGET_OS}.${name}
17+
${MACRO_HEADER_DEPENDS}
1718
)
1819
else()
1920
add_header(
2021
${name}
2122
HDR
2223
${MACRO_HEADER_HDR}
24+
DEPENDS
25+
${MACRO_HEADER_DEPENDS}
2326
)
2427
endif()
2528
endfunction(add_macro_header)
@@ -70,6 +73,20 @@ add_macro_header(
7073
math-macros.h
7174
)
7275

76+
add_macro_header(
77+
offsetof_macro
78+
HDR
79+
offsetof-macro.h
80+
)
81+
82+
add_macro_header(
83+
containerof_macro
84+
HDR
85+
containerof-macro.h
86+
DEPENDS
87+
.offsetof_macro
88+
)
89+
7390
add_macro_header(
7491
sched_macros
7592
HDR
@@ -118,6 +135,15 @@ add_macro_header(
118135
sys-mman-macros.h
119136
)
120137

138+
add_macro_header(
139+
sys_queue_macros
140+
HDR
141+
sys-queue-macros.h
142+
DEPENDS
143+
.null_macro
144+
.containerof_macro
145+
)
146+
121147
add_macro_header(
122148
sys_random_macros
123149
HDR
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Definition of the containerof macro -------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef __LLVM_LIBC_MACROS_CONTAINEROF_MACRO_H
10+
#define __LLVM_LIBC_MACROS_CONTAINEROF_MACRO_H
11+
12+
#include <llvm-libc-macros/offsetof-macro.h>
13+
14+
#define __containerof(ptr, type, member) \
15+
({ \
16+
const __typeof(((type *)0)->member) *__ptr = (ptr); \
17+
(type *)(void *)((const char *)__ptr - offsetof(type, member)); \
18+
})
19+
20+
#endif // __LLVM_LIBC_MACROS_CONTAINEROF_MACRO_H
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//===-- Definition of the offsetof macro ----------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef __LLVM_LIBC_MACROS_OFFSETOF_MACRO_H
10+
#define __LLVM_LIBC_MACROS_OFFSETOF_MACRO_H
11+
12+
#define __need_offsetof
13+
#include <stddef.h>
14+
15+
#endif // __LLVM_LIBC_MACROS_OFFSETOF_MACRO_H

0 commit comments

Comments
 (0)