Arrrrrrr
Arrrrrrr

Reputation: 812

Inheritable object structure

From my experiments, i see that inheritable objects start with 4 additional bytes (i have a 32 cpu). From this observation, i'd like to know:

Upvotes: 2

Views: 112

Answers (1)

def-
def-

Reputation: 5403

A good way to figure out this kind of question is to look at the intermediate C file. I compiled this file:

type Foo = object {.inheritable.}
  x: int

var a: Foo
echo sizeof(a)

After compiling it with nim -d:release c x a look into nimcache/x.c reveals:

struct  Foo118004  {
TNimType* m_type;
NI x;
};

So there is simply a pointer to a TNimType object stored. The size of the pointer and the alignment of the Foo object are system and compiler dependent, but it should be 8 bytes for x86_64 and 4 bytes for x86. TNimType itself can be found in lib/system/hti.nim and is defined like this:

TNimType {.codegenType.} = object
  size: int
  kind: TNimKind
  flags: set[TNimTypeFlag]
  base: ptr TNimType
  node: ptr TNimNode # valid for tyRecord, tyObject, tyTuple, tyEnum
  finalizer: pointer # the finalizer for the type
  marker: proc (p: pointer, op: int) {.nimcall, benign.} # marker proc for GC
  deepcopy: proc (p: pointer): pointer {.nimcall, benign.}

Upvotes: 3

Related Questions