VecInitAlignedWithDeepCopyStack

Table of Contents

VecInitAlignedWithDeepCopyStack

Description

Initialize given vector with given alignment. It is mandatory to initialize vectors before use. Not doing so is undefined behaviour. Provided alignment is used to keep all objects at an aligned memory location, avoiding UB in some cases. It’s recommended to use aligned vector when dealing with structs containing unions. These vectors are best used where user doesn’t get a chance to or does not want to deinit vector, given that no data in vector needs to be deinitialized. Example includes, but does not limit to a Vec(i8), Vec(f32), etc…

Parameters

NameDirectionDescription
vin,outVector that needs to be initialized.
neinNumber of elements to allocate aligned stack memory for.
ciinCopy init method for copying over elements in vector.
cdinCopy deinit method for deiniting elements in vector.
alninAlignment value to align all emenets to.

Usage example (from documentation)

  Vec(Node*) nodes;

  // initialize vector with stack memory, aligned with 124 byte boundary
  VecInitAlignedWithDeepCopyStack(&nodes, 24, NodeInitCopy, NodeDeinit, 124, {
        // scope where vector memory is available
        FindAndFillAllNodes(&nodes, ... /* some other relevant data */);

        UseNodes(&nodes);

        VecForeach(&nodes, node, {
        });

        // vector deinit will be called for you after this automatically
        // so any data held by the vector in this scope is invalid outside
  });

Usage example (Cross-references)

    /// TAGS: Init, Vec, Stack, Length, Size, Array
    ///
    #define VecInitStack(v, ne, scoped_body) VecInitAlignedWithDeepCopyStack(v, ne, NULL, NULL, 1, scoped_body)
    
    ///
    ///
    #define VecInitAlignedStack(v, ne, aln, scoped_body)                                                                   \
    VecInitAlignedWithDeepCopyStack(v, ne, NULL, NULL, aln, scoped_body)
    
    ///
    ///
    #define VecInitWithDeepCopyStack(v, ne, ci, cd, scoped_body)                                                           \
    VecInitAlignedWithDeepCopyStack(v, ne, ci, cd, 1, scoped_body)
    
    ///

Share :

Related Posts

Vec

Vec Description Typesafe vector definition. This is much like C++ template std::vector

Read More

VecInitWithDeepCopy

VecInitWithDeepCopy Description Initialize vector. Default alignment is 1 It is mandatory to initialize vectors before use. Not doing so is undefined behaviour.

Read More

VecInitAligned

VecInitAligned Description Initialize vector with given alignment. It is mandatory to initialize vectors before use. Not doing so is undefined behaviour. Provided alignment is used to keep all objects at an aligned memory location, avoiding UB in some cases. It’s recommended to use aligned vector when dealing with structs containing unions.

Read More