Skip to content

Commit e4d40ce

Browse files
nbdd0121fbq
authored andcommitted
rust: bindings: rename const binding using sed
Current for consts that bindgen don't recognise, we define a helper constant with const <TYPE> BINDINGS_<NAME> = <NAME>; in `bindings_helper.h` and then we put pub const <NAME>: <TYPE> = BINDINGS_<NAME>; in `bindings/lib.rs`. This is fine that we currently only have 3 constants that are defined this way, but is going to be more annoying when more constants are added since every new constant needs to be defined in two places. This patch changes the way we define constant helpers to const <TYPE> RUST_CONST_HELPER_<NAME> = <NAME>; and then use `sed` to postprocess Rust code by generated by bindgen to remove the distinct prefix, so user of the binding crate can refer to the name directly. Reviewed-by: Benno Lossin <[email protected]> Reviewed-by: Andreas Hindborg <[email protected]> Reviewed-by: Martin Rodriguez Reboredo <[email protected]> Signed-off-by: Gary Guo <[email protected]> Reviewed-by: Alice Ryhl <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent ec3c9cd commit e4d40ce

File tree

4 files changed

+6
-7
lines changed

4 files changed

+6
-7
lines changed

rust/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,8 @@ quiet_cmd_bindgen = BINDGEN $@
337337

338338
$(obj)/bindings/bindings_generated.rs: private bindgen_target_flags = \
339339
$(shell grep -Ev '^#|^$$' $(srctree)/$(src)/bindgen_parameters)
340+
$(obj)/bindings/bindings_generated.rs: private bindgen_target_extra = ; \
341+
sed -Ei 's/pub const RUST_CONST_HELPER_([a-zA-Z0-9_]*)/pub const \1/g' $@
340342
$(obj)/bindings/bindings_generated.rs: $(src)/bindings/bindings_helper.h \
341343
$(src)/bindgen_parameters FORCE
342344
$(call if_changed_dep,bindgen)

rust/bindings/bindings_helper.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@
1515
#include <linux/workqueue.h>
1616

1717
/* `bindgen` gets confused at certain things. */
18-
const size_t BINDINGS_ARCH_SLAB_MINALIGN = ARCH_SLAB_MINALIGN;
19-
const gfp_t BINDINGS_GFP_KERNEL = GFP_KERNEL;
20-
const gfp_t BINDINGS___GFP_ZERO = __GFP_ZERO;
18+
const size_t RUST_CONST_HELPER_ARCH_SLAB_MINALIGN = ARCH_SLAB_MINALIGN;
19+
const gfp_t RUST_CONST_HELPER_GFP_KERNEL = GFP_KERNEL;
20+
const gfp_t RUST_CONST_HELPER___GFP_ZERO = __GFP_ZERO;

rust/bindings/lib.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,3 @@ mod bindings_helper {
4848
}
4949

5050
pub use bindings_raw::*;
51-
52-
pub const GFP_KERNEL: gfp_t = BINDINGS_GFP_KERNEL;
53-
pub const __GFP_ZERO: gfp_t = BINDINGS___GFP_ZERO;

rust/kernel/allocator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ unsafe fn krealloc_aligned(ptr: *mut u8, new_layout: Layout, flags: bindings::gf
2121

2222
let mut size = layout.size();
2323

24-
if layout.align() > bindings::BINDINGS_ARCH_SLAB_MINALIGN {
24+
if layout.align() > bindings::ARCH_SLAB_MINALIGN {
2525
// The alignment requirement exceeds the slab guarantee, thus try to enlarge the size
2626
// to use the "power-of-two" size/alignment guarantee (see comments in `kmalloc()` for
2727
// more information).

0 commit comments

Comments
 (0)