Skip to content

Cannot take the compile-time sizeof Atomic types #12726

@mratsim

Description

@mratsim

sizeof doesn't work for types that store Atomics. This would be useful to size the memory buffer for thread-safe memory allocators with what is left from the synchronization primitives.

import std/atomics

type
  Node = ptr object
    # works
    next: Atomic[pointer]

  MyChannel = object
    # type not defined completely
    back: Atomic[ptr int]

static:
  echo sizeof(Node)
  echo sizeof(MyChannel)

The workaround is to add a "raw" field for all the trivial types with their actual types so that sizeof works.

There is a similar concern for Locks and Condition Variables though I don't think there is an easy way to make sizeof deduce their size.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions