Skip to content

Commit

Permalink
[libc] Provide sys/queue.h (llvm#78081)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
petrhosek authored Jan 19, 2024
1 parent b8967e0 commit dbc0955
Show file tree
Hide file tree
Showing 13 changed files with 533 additions and 1 deletion.
1 change: 1 addition & 0 deletions libc/config/baremetal/arm/headers.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ set(TARGET_PUBLIC_HEADERS
libc.include.stdlib
libc.include.string
libc.include.strings
libc.include.sys_queue
)
1 change: 1 addition & 0 deletions libc/config/baremetal/riscv/headers.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ set(TARGET_PUBLIC_HEADERS
libc.include.stdlib
libc.include.string
libc.include.strings
libc.include.sys_queue
)
1 change: 1 addition & 0 deletions libc/config/linux/riscv/headers.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ set(TARGET_PUBLIC_HEADERS
libc.include.sys_mman
libc.include.sys_prctl
libc.include.sys_random
libc.include.sys_queue
libc.include.sys_resource
libc.include.sys_select
libc.include.sys_socket
Expand Down
1 change: 1 addition & 0 deletions libc/config/linux/x86_64/headers.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ set(TARGET_PUBLIC_HEADERS
libc.include.sys_ioctl
libc.include.sys_mman
libc.include.sys_prctl
libc.include.sys_queue
libc.include.sys_random
libc.include.sys_resource
libc.include.sys_select
Expand Down
8 changes: 8 additions & 0 deletions libc/include/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,14 @@ add_gen_header(
.llvm_libc_common_h
)

add_header(
sys_queue
HDR
sys/queue.h
DEPENDS
.llvm-libc-macros.sys_queue_macros
)

add_gen_header(
sys_random
DEF_FILE sys/random.h.def
Expand Down
28 changes: 27 additions & 1 deletion libc/include/llvm-libc-macros/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ function(add_macro_header name)
"MACRO_HEADER"
"" # Optional arguments
"HDR" # Single value arguments
"" # Multi-value arguments
"DEPENDS" # Multi-value arguments
${ARGN}
)
if(TARGET libc.include.llvm-libc-macros.${LIBC_TARGET_OS}.${name})
Expand All @@ -14,12 +14,15 @@ function(add_macro_header name)
${MACRO_HEADER_HDR}
DEPENDS
.${LIBC_TARGET_OS}.${name}
${MACRO_HEADER_DEPENDS}
)
else()
add_header(
${name}
HDR
${MACRO_HEADER_HDR}
DEPENDS
${MACRO_HEADER_DEPENDS}
)
endif()
endfunction(add_macro_header)
Expand Down Expand Up @@ -70,6 +73,20 @@ add_macro_header(
math-macros.h
)

add_macro_header(
offsetof_macro
HDR
offsetof-macro.h
)

add_macro_header(
containerof_macro
HDR
containerof-macro.h
DEPENDS
.offsetof_macro
)

add_macro_header(
sched_macros
HDR
Expand Down Expand Up @@ -118,6 +135,15 @@ add_macro_header(
sys-mman-macros.h
)

add_macro_header(
sys_queue_macros
HDR
sys-queue-macros.h
DEPENDS
.null_macro
.containerof_macro
)

add_macro_header(
sys_random_macros
HDR
Expand Down
20 changes: 20 additions & 0 deletions libc/include/llvm-libc-macros/containerof-macro.h
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
15 changes: 15 additions & 0 deletions libc/include/llvm-libc-macros/offsetof-macro.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//===-- Definition of the offsetof 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_OFFSETOF_MACRO_H
#define __LLVM_LIBC_MACROS_OFFSETOF_MACRO_H

#define __need_offsetof
#include <stddef.h>

#endif // __LLVM_LIBC_MACROS_OFFSETOF_MACRO_H
Loading

0 comments on commit dbc0955

Please sign in to comment.