Add NUM2PTR and PTR2NUM macros

These macros have been defined here and there, so collect them.
This commit is contained in:
Nobuyoshi Nakada 2025-12-10 12:09:50 +09:00
parent 3bb97e7707
commit 3636277dc5
No known key found for this signature in database
GPG Key ID: 3582D74E1FEE4465
Notes: git 2025-12-10 04:12:15 +00:00
10 changed files with 18 additions and 31 deletions

2
box.c
View File

@ -760,7 +760,7 @@ static int
cleanup_local_extension_i(VALUE key, VALUE value, VALUE arg)
{
#if defined(_WIN32)
HMODULE h = (HMODULE)NUM2SVALUE(value);
HMODULE h = (HMODULE)NUM2PTR(value);
WCHAR module_path[MAXPATHLEN];
DWORD len = GetModuleFileNameW(h, module_path, numberof(module_path));

View File

@ -610,8 +610,6 @@ branch_coverage_valid_p(rb_iseq_t *iseq, int first_line)
return 1;
}
#define PTR2NUM(x) (rb_int2inum((intptr_t)(void *)(x)))
static VALUE
setup_branch(const rb_code_location_t *loc, const char *type, VALUE structure, VALUE key)
{

View File

@ -1,11 +1,5 @@
#include <ruby.h>
#if SIZEOF_LONG == SIZEOF_VOIDP
# define NUM2PTR(x) NUM2ULONG(x)
#elif SIZEOF_LONG_LONG == SIZEOF_VOIDP
# define NUM2PTR(x) NUM2ULL(x)
#endif
static VALUE
invalid_call(VALUE obj, VALUE address)
{

7
gc.c
View File

@ -2122,14 +2122,9 @@ rb_gc_obj_free_vm_weak_references(VALUE obj)
static VALUE
id2ref(VALUE objid)
{
#if SIZEOF_LONG == SIZEOF_VOIDP
#define NUM2PTR(x) NUM2ULONG(x)
#elif SIZEOF_LONG_LONG == SIZEOF_VOIDP
#define NUM2PTR(x) NUM2ULL(x)
#endif
objid = rb_to_int(objid);
if (FIXNUM_P(objid) || rb_big_size(objid) <= SIZEOF_VOIDP) {
VALUE ptr = NUM2PTR(objid);
VALUE ptr = (VALUE)NUM2PTR(objid);
if (SPECIAL_CONST_P(ptr)) {
if (ptr == Qtrue) return Qtrue;
if (ptr == Qfalse) return Qfalse;

View File

@ -32,6 +32,18 @@
#define rb_int_new rb_int2inum /**< @alias{rb_int2inum} */
#define rb_uint_new rb_uint2inum /**< @alias{rb_uint2inum} */
// These definitions are same as fiddle/conversions.h
#if SIZEOF_VOIDP <= SIZEOF_LONG
# define PTR2NUM(x) (LONG2NUM((long)(x)))
# define NUM2PTR(x) ((void*)(NUM2ULONG(x)))
#elif SIZEOF_VOIDP <= SIZEOF_LONG_LONG
# define PTR2NUM(x) (LL2NUM((LONG_LONG)(x)))
# define NUM2PTR(x) ((void*)(NUM2ULL(x)))
#else
// should have been an error in ruby/internal/value.h
# error Need integer for VALUE
#endif
RBIMPL_SYMBOL_EXPORT_BEGIN()
/**

View File

@ -3,16 +3,6 @@
#include "ruby/ruby.h" /* for VALUE */
#if SIZEOF_VALUE <= SIZEOF_LONG
# define SVALUE2NUM(x) LONG2NUM((long)(x))
# define NUM2SVALUE(x) (SIGNED_VALUE)NUM2LONG(x)
#elif SIZEOF_VALUE <= SIZEOF_LONG_LONG
# define SVALUE2NUM(x) LL2NUM((LONG_LONG)(x))
# define NUM2SVALUE(x) (SIGNED_VALUE)NUM2LL(x)
#else
# error Need integer for VALUE
#endif
/**
* @author Ruby developers <ruby-core@ruby-lang.org>
* @copyright This file is a part of the programming language Ruby.

4
load.c
View File

@ -1345,7 +1345,7 @@ require_internal(rb_execution_context_t *ec, VALUE fname, int exception, bool wa
reset_ext_config = true;
ext_config_push(th, &prev_ext_config);
handle = rb_vm_call_cfunc_in_box(box->top_self, load_ext, path, fname, path, box);
rb_hash_aset(box->ruby_dln_libmap, path, SVALUE2NUM((SIGNED_VALUE)handle));
rb_hash_aset(box->ruby_dln_libmap, path, PTR2NUM(handle));
break;
}
result = TAG_RETURN;
@ -1666,7 +1666,7 @@ rb_ext_resolve_symbol(const char* fname, const char* symbol)
if (NIL_P(handle)) {
return NULL;
}
return dln_symbol((void *)NUM2SVALUE(handle), symbol);
return dln_symbol(NUM2PTR(handle), symbol);
}
void

View File

@ -135,7 +135,9 @@ VALUE digest_spec_context_size(VALUE self, VALUE meta) {
return SIZET2NUM(algo->ctx_size);
}
#ifndef PTR2NUM
#define PTR2NUM(x) (rb_int2inum((intptr_t)(void *)(x)))
#endif
VALUE digest_spec_context(VALUE self, VALUE digest) {
return PTR2NUM(context);

2
yjit.c
View File

@ -64,8 +64,6 @@ STATIC_ASSERT(pointer_tagging_scheme, USE_FLONUM);
// The "_yjit_" part is for trying to be informative. We might want different
// suffixes for symbols meant for Rust and symbols meant for broader CRuby.
# define PTR2NUM(x) (rb_int2inum((intptr_t)(void *)(x)))
// For a given raw_sample (frame), set the hash with the caller's
// name, file, and line number. Return the hash with collected frame_info.
static void

2
zjit.c
View File

@ -35,8 +35,6 @@ enum zjit_struct_offsets {
ISEQ_BODY_OFFSET_PARAM = offsetof(struct rb_iseq_constant_body, param)
};
#define PTR2NUM(x) (rb_int2inum((intptr_t)(void *)(x)))
// For a given raw_sample (frame), set the hash with the caller's
// name, file, and line number. Return the hash with collected frame_info.
static void