Feature #8568

Introduce RbConfig value for native word size, to avoid Fixnum#size use

Added by Charles Nutter 10 months ago. Updated 7 months ago.

[ruby-core:55638]
Status:Closed
Priority:Normal
Assignee:-
Category:-
Target version:Ruby 2.1.0

Description

Fixnum#size is not really useful to represent native word size across all platforms and implementations. On JRuby, for example, our Fixnum is always represented as a 64-bit Java "long" value, regardless of the underlying native platform. There may be other implementations that fix Fixnum's size to a specific bit width as well. Therefore, using Fixnum#size to determine the size of a word on the underlying native platform is not reliable.

I propose two additions to Ruby:

  1. RbConfig value "word_size" for native word size. This could reflect bit size (32, 64) or byte size (4, 8).

  2. A constant, somewhere in Ruby, to provide access to this value more directly. I'm not sure where this should go. ObjectSpace::WORD_SIZE?

sizeof.diff Magnifier (2.11 KB) Nobuyoshi Nakada, 07/03/2013 04:56 PM


Related issues

Related to ruby-trunk - Feature #8553: Bignum#size (and Fixnum#size) Closed 06/21/2013 08/31/2013

History

#1 Updated by Nobuyoshi Nakada 10 months ago

In current CRuby, Fixnum is always limited as long, so Fixnum#size represents sizeof(long), not word size.
But it depends on what you define "word" as, of course.
If you mean SIZEOF_VALUE by "word size", it's simply a bug to use Fixnum#size for that purpose.

A way to achieve size of a pointer is [""].pack("p").size currently.

#2 Updated by Charles Nutter 10 months ago

Size of a pointer is probably what I'm looking for, since that's usually what people will be using it for. However, implementations that can't give out real pointers can't support pack("p"), so that's not a portable option either.

So... RbConfig::CONFIG['pointer_size'] and some constant?

#3 Updated by Nobuyoshi Nakada 10 months ago

'pointer' or 'value'?

I'm curious for what purpose people use that size.
Pointers are not used usually in ruby, I think.

#4 Updated by Yui NARUSE 10 months ago

The place where word size info should be seems RbConfig, if it is needed.

nobu (Nobuyoshi Nakada) wrote:

'pointer' or 'value'?

I'm curious for what purpose people use that size.
Pointers are not used usually in ruby, I think.

See #8553

#5 Updated by Akira Tanaka 10 months ago

2013/6/25 headius (Charles Nutter) headius@headius.com:

Issue #8568 has been reported by headius (Charles Nutter).

I propose two additions to Ruby:

  1. RbConfig value "word_size" for native word size. This could reflect bit size (32, 64) or byte size (4, 8).

  2. A constant, somewhere in Ruby, to provide access to this value more directly. I'm not sure where this should go. ObjectSpace::WORD_SIZE?

Although "word" is ambiguous, providing sizeof(long), sizeof(char),
etc. via RbConfig
with clear names seems good feature:
RbConfig["sizeof(long)"], RbConfig["sieof(char
)"],
RbConfig["sizeof(time_t)"], ...
--
Tanaka Akira

#6 Updated by Nobuyoshi Nakada 10 months ago

akr (Akira Tanaka) wrote:

Although "word" is ambiguous, providing sizeof(long), sizeof(char),
etc. via RbConfig
with clear names seems good feature:
RbConfig["sizeof(long)"], RbConfig["sieof(char
)"],
RbConfig["sizeof(time_t)"], ...

RbConfig is a module, so RbConfig.[] doesn't seem good.
What about RbConfig::SIZEOF instead?

#7 Updated by Nobuyoshi Nakada 10 months ago

naruse (Yui NARUSE) wrote:

I'm curious for what purpose people use that size.
Pointers are not used usually in ruby, I think.

See #8553

I can't see any needs in ruby level there.

#8 Updated by Akira Tanaka 10 months ago

2013/7/3 nobu (Nobuyoshi Nakada) nobu@ruby-lang.org:

RbConfig is a module, so RbConfig.[] doesn't seem good.
What about RbConfig::SIZEOF instead?

No problem.

They would be useful for ioctl and socket option.
--
Tanaka Akira

#9 Updated by Charles Nutter 7 months ago

In for 2.1?

#10 Updated by Charles Nutter 7 months ago

  • Target version set to Ruby 2.1.0

#11 Updated by Nobuyoshi Nakada 7 months ago

  • Status changed from Open to Closed

r42685

#12 Updated by Charles Nutter 7 months ago

nobu (Nobuyoshi Nakada) wrote:

r42685

Thanks! I do have one question, though.. are the names of the types (acquired via #type in sizes.c) guaranteed to be consistent across platforms/compilers?

#13 Updated by Nobuyoshi Nakada 7 months ago

=begin
They are extracted from ((%configure.in%)) script automatically.
And ANSI/ISO C standard requires stringize operator to turn the argument into a string literal ((before)) expanding it.
=end

#14 Updated by Charles Nutter 7 months ago

nobu (Nobuyoshi Nakada) wrote:

=begin
They are extracted from ((%configure.in%)) script automatically.
And ANSI/ISO C standard requires stringize operator to turn the argument into a string literal ((before)) expanding it.
=end

I interpret this to mean that the names of the types should be ANSI/ISO C standard names. If that's the case, I'm satisfied.

My concern is if the type names vary across platforms; they would be much less useful if one platform reports "void*" and another platform reports "void *".

#15 Updated by Nobuyoshi Nakada 7 months ago

Sorry that it's unclear.

It means that type names come from a distributed file, configure.in, so they are invariant on all platforms.
As for C standard, all names will become strings as-is, even if a type is defined by macro.

#16 Updated by Charles Nutter 7 months ago

Ok...so hopefully we have tests for those names and we can consider them "spec".

BTW, I'm asking because we'll want to provide the same SIZEOF hash in JRuby, and I need to know the names will be consistent and not change in the future.

Thanks!

#17 Updated by Nobuyoshi Nakada 7 months ago

Almost of those names are defined by C standard or POSIX, so they won't change.
I think what names are included is not a spec, however.
New names may be added or existing names may be deleted, without any notice.

I think all I can guarantee is, in a same CRuby revision, same names are defined on all platforms where they are supported.

Also available in: Atom PDF