Re: Sizeof class instance

2009-10-03 Thread Jeremie Pelletier

Justin Johansson wrote:

How does one determine the sizeof (in bytes) of an instance of a class in D?

.sizeof works as advertised for structs, but for reference types,
.sizeof yields the sizeof the referencing variable (effectively same as size of 
a pointer)
and not the size of the underlying instance.

I did try scanning the NG and read spec_D1.00.pdf.  Perhaps I missed it in the 
latter.

btw. I was poking under the hood of std.xml and though, wow, instances of 
Element
class look humongous, and so I'm interested to how exactly how humongous.

Thanks for all help.
Justin



The only way I know of is to access the init array of the classinfo at 
runtime and get its length property.


Re: Sizeof class instance

2009-10-03 Thread Jarrett Billingsley
On Sat, Oct 3, 2009 at 5:50 PM, Justin Johansson n...@spam.com wrote:
 How does one determine the sizeof (in bytes) of an instance of a class in D?

 .sizeof works as advertised for structs, but for reference types,
 .sizeof yields the sizeof the referencing variable (effectively same as size 
 of a pointer)
 and not the size of the underlying instance.

 I did try scanning the NG and read spec_D1.00.pdf.  Perhaps I missed it in 
 the latter.

 btw. I was poking under the hood of std.xml and though, wow, instances of 
 Element
 class look humongous, and so I'm interested to how exactly how humongous.

 Thanks for all help.
 Justin

There's no way to get it at compile-time in D1. The best you can do is
Class.classinfo.init.length.

In D2, you can use __traits(classInstanceSize, Class).


Re: Sizeof class instance

2009-10-03 Thread Justin Johansson
Jarrett Billingsley Wrote:

 On Sat, Oct 3, 2009 at 5:50 PM, Justin Johansson n...@spam.com wrote:
  How does one determine the sizeof (in bytes) of an instance of a class in D?
 
  .sizeof works as advertised for structs, but for reference types,
  .sizeof yields the sizeof the referencing variable (effectively same as 
  size of a pointer)
  and not the size of the underlying instance.
 
  I did try scanning the NG and read spec_D1.00.pdf.  Perhaps I missed it in 
  the latter.
 
  btw. I was poking under the hood of std.xml and though, wow, instances of 
  Element
  class look humongous, and so I'm interested to how exactly how humongous.

 There's no way to get it at compile-time in D1. The best you can do is
 Class.classinfo.init.length.
 
 In D2, you can use __traits(classInstanceSize, Class).


Thanks Jeremie and Jarrett for answers.

For investigative purposes (rather than adding up class member sizes in my 
head),
would I get a fair answer if I copied the class data members into a struct, did 
a struct
sizeof and added 4 bytes to allow for a virtual function table pointer (in the 
class and
assuming the class has a VFT)?




Re: Sizeof class instance

2009-10-03 Thread Jeremie Pelletier

Justin Johansson wrote:

Jarrett Billingsley Wrote:


On Sat, Oct 3, 2009 at 5:50 PM, Justin Johansson n...@spam.com wrote:

How does one determine the sizeof (in bytes) of an instance of a class in D?

.sizeof works as advertised for structs, but for reference types,
.sizeof yields the sizeof the referencing variable (effectively same as size of 
a pointer)
and not the size of the underlying instance.

I did try scanning the NG and read spec_D1.00.pdf.  Perhaps I missed it in the 
latter.

btw. I was poking under the hood of std.xml and though, wow, instances of 
Element
class look humongous, and so I'm interested to how exactly how humongous.



There's no way to get it at compile-time in D1. The best you can do is
Class.classinfo.init.length.

In D2, you can use __traits(classInstanceSize, Class).



Thanks Jeremie and Jarrett for answers.

For investigative purposes (rather than adding up class member sizes in my 
head),
would I get a fair answer if I copied the class data members into a struct, did 
a struct
sizeof and added 4 bytes to allow for a virtual function table pointer (in the 
class and
assuming the class has a VFT)?


You forgot the monitor pointer of the class, so thats (size_t.sizeof * 
2) to add to the size of the struct.


I wasn't aware of the traits method either, I just made this helper 
template to simplify its syntax:


template SizeOf(alias C) if(is(C == class)) {
enum ClassSizeof = __traits(classInstanceSize, C);
}


Re: Sizeof class instance

2009-10-03 Thread Justin Johansson
Jeremie Pelletier Wrote:

 Justin Johansson wrote:
  Jarrett Billingsley Wrote:
  
  On Sat, Oct 3, 2009 at 5:50 PM, Justin Johansson n...@spam.com wrote:
  How does one determine the sizeof (in bytes) of an instance of a class in 
  D?
 
  .sizeof works as advertised for structs, but for reference types,
  .sizeof yields the sizeof the referencing variable (effectively same as 
  size of a pointer)
  and not the size of the underlying instance.
 
  I did try scanning the NG and read spec_D1.00.pdf.  Perhaps I missed it 
  in the latter.
 
  btw. I was poking under the hood of std.xml and though, wow, instances of 
  Element
  class look humongous, and so I'm interested to how exactly how humongous.
  
  There's no way to get it at compile-time in D1. The best you can do is
  Class.classinfo.init.length.
 
  In D2, you can use __traits(classInstanceSize, Class).
  
  
  Thanks Jeremie and Jarrett for answers.
  
  For investigative purposes (rather than adding up class member sizes in my 
  head),
  would I get a fair answer if I copied the class data members into a struct, 
  did a struct
  sizeof and added 4 bytes to allow for a virtual function table pointer (in 
  the class and
  assuming the class has a VFT)?
 
 You forgot the monitor pointer of the class, so thats (size_t.sizeof * 
 2) to add to the size of the struct.
 
 I wasn't aware of the traits method either, I just made this helper 
 template to simplify its syntax:
 
 template SizeOf(alias C) if(is(C == class)) {
   enum ClassSizeof = __traits(classInstanceSize, C);
 }


Sorry I misread the earlier answer.  This works for my investigative purposes 
as explained:

   writefln( Element instance size = %d, Element.classinfo.init.length);

Still glad I asked this last question though because I hadn't thought of 
monitor.  Also I imagine that
if a class has interfaces, this can impact upon the size as well.

My early years with microprocessors (8085, 8086, 6809 etc) and assembly 
language has always made
me curious about how compilers realize their magic at the bit and byte level.




Getting started - D meta-program question

2009-10-03 Thread Justin Johansson
There was mention** on the general discussion group that the D foreach_reverse
language construct could be replaced (emulated?) with a (D) meta-program.

** Even a novice programmer can write a meta-program to replace
foreach_reverse without any runtime performance hit.

   
http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.Darticle_id=97362
  
As I'm less than a novice with the D meta-programming facilities (at this stage 
of my journey into D),
if someone would kindly show me the D meta-program solution to do this,
I'd really appreciate the enlightenment.

Thanks again.




Re: Getting started - D meta-program question

2009-10-03 Thread Daniel Keep

Justin Johansson wrote:
 There was mention** on the general discussion group that the D foreach_reverse
 language construct could be replaced (emulated?) with a (D) meta-program.
 
 ** Even a novice programmer can write a meta-program to replace
 foreach_reverse without any runtime performance hit.
 

 http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.Darticle_id=97362
   
 As I'm less than a novice with the D meta-programming facilities (at this 
 stage of my journey into D),
 if someone would kindly show me the D meta-program solution to do this,
 I'd really appreciate the enlightenment.
 
 Thanks again.

Short answer: you can't.

Long answer: you can, provided you aren't trying to reverse an opApply,
which is patently impossible.

As for the meta-program, I would suspect whoever said that was talking
about writing a templated type or function to handle it.  You would need
to use template specialisation or static ifs to switch on what type
you've been given to reverse.

http://digitalmars.com/d/1.0/template.html

http://digitalmars.com/d/1.0/version.html#staticif


Re: Sizeof class instance

2009-10-03 Thread Daniel Keep


Jarrett Billingsley wrote:
 On Sat, Oct 3, 2009 at 5:50 PM, Justin Johansson n...@spam.com wrote:
 How does one determine the sizeof (in bytes) of an instance of a class in D?

 .sizeof works as advertised for structs, but for reference types,
 .sizeof yields the sizeof the referencing variable (effectively same as size 
 of a pointer)
 and not the size of the underlying instance.

 I did try scanning the NG and read spec_D1.00.pdf.  Perhaps I missed it in 
 the latter.

 btw. I was poking under the hood of std.xml and though, wow, instances of 
 Element
 class look humongous, and so I'm interested to how exactly how humongous.

 Thanks for all help.
 Justin
 
 There's no way to get it at compile-time in D1. The best you can do is
 Class.classinfo.init.length.

What nonsense, of course there is!

http://gist.github.com/140531

Note: this is VERY old code, but I have no reason to think it won't
still work.  I may need a little dusting off...


Re: Sizeof class instance

2009-10-03 Thread Daniel Keep


Daniel Keep wrote:
 ...
 Note: this is VERY old code, but I have no reason to think it won't
 still work.  I may need a little dusting off...

*It* may need a little dusting off.  Argh.