Skip to content

Commit 3b70e0d

Browse files
authored
Use u64 for size_t and cut down on noise in bindgen output (ruby#188)
Using a fixed width type has better ergonomic than usize. The static asserts give build errors when we want to port to 32 bit machines. The "__.*" type pruning is attempt to keep output consistent between my Mac and GNU/Linux systems. Let's see how much they diverge as we go. We can always use CI to bless one particular setup to avoid dealing with divergence.
1 parent 1d18464 commit 3b70e0d

File tree

4 files changed

+17
-2
lines changed

4 files changed

+17
-2
lines changed

yjit.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212
#include "vm_sync.h"
1313
#include "yjit.h"
1414

15+
// We need size_t to have a known size to simplify code generation and FFI.
16+
// TODO(alan): check this in configure.ac to fail fast on 32 bit platforms.
17+
STATIC_ASSERT(64b_size_t, SIZE_MAX == UINT64_MAX);
18+
// I don't know any C implementation that has uint64_t and puts padding bits
19+
// into size_t but the standard seems to allow it.
20+
STATIC_ASSERT(size_t_no_padding_bits, sizeof(size_t) == sizeof(uint64_t));
21+
1522
#ifndef YJIT_CHECK_MODE
1623
# define YJIT_CHECK_MODE 0
1724
#endif

yjit/bindgen/src/main.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ fn main() {
3232
// Don't want layout tests as they are platform dependent
3333
.layout_tests(false)
3434

35+
// Block for stability since output is different on Darwin and Linux
36+
.blocklist_type("size_t")
37+
38+
// Prune these types since they are system dependant and we don't use them
39+
.blocklist_type("__.*")
40+
3541
// This struct is public to Ruby C extensions
3642
.allowlist_type("RBasic")
3743

yjit/src/cruby.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@
8585
use std::convert::From;
8686
use std::os::raw::{c_int, c_uint, c_long};
8787

88+
// We check that we can do this with the configure script and a couple of
89+
// static asserts. u64 and not usize to play nice with lowering to x86.
90+
pub type size_t = u64;
91+
8892
// Textually include output from rust-bindgen as suggested by its user guide.
8993
include!("cruby_bindings.inc.rs");
9094

yjit/src/cruby_bindings.inc.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ pub const NIL_REDEFINED_OP_FLAG: u32 = 512;
1212
pub const TRUE_REDEFINED_OP_FLAG: u32 = 1024;
1313
pub const FALSE_REDEFINED_OP_FLAG: u32 = 2048;
1414
pub const PROC_REDEFINED_OP_FLAG: u32 = 4096;
15-
pub type size_t = ::std::os::raw::c_ulong;
16-
pub type __uint32_t = ::std::os::raw::c_uint;
1715
pub type ID = ::std::os::raw::c_ulong;
1816
pub type rb_alloc_func_t = ::std::option::Option<unsafe extern "C" fn(klass: VALUE) -> VALUE>;
1917
extern "C" {

0 commit comments

Comments
 (0)