Skip to content

ALIGN_UP_POW2

Description

Aligns the given value up to the nearest power-of-two multiple.

Parameters

Name Direction Description
value in The value to be aligned up.
alignment in The power-of-two alignment boundary.

Success

Returns power-of-two aligned-up value when alignment > 1.

Failure

Function cannot fail - returns original value if alignment <= 1.

Usage example (Cross-references)

Usage examples (Cross-references)
        size page_size = PageAllocatorPageSize(self);
        if (align > page_size) {
            return ALIGN_UP_POW2(bytes, align);
        }
        return ALIGN_UP_POW2(bytes, page_size);
            return ALIGN_UP_POW2(bytes, align);
        }
        return ALIGN_UP_POW2(bytes, page_size);
    }
            want_bytes = *bytes_p * 2;
        }
        size new_rounded = ALIGN_UP_POW2(want_bytes, page_size);
        if (!new_rounded) {
            return false;
        }
        size  align  = self->base.alignment;
        size  padded = ALIGN_UP_POW2(bytes, align);
        void *result = NULL;
        }
        ArenaChunk *chunk      = self->tail;
        size        padded_new = ALIGN_UP_POW2(new_size, align);
        size        last_off   = (size)((u8 *)ptr - chunk->base);
        if (last_off + padded_new > chunk->capacity) {
            return item_size;
        }
        return v->allocator->alignment > 1 ? ALIGN_UP_POW2(item_size, v->allocator->alignment) : item_size;
    }
    ///
    /// TAGS: Memory, Alignment, PowerOfTwo, Pointer
    #define PTR_ALIGN_UP_POW2(ptr, alignment) ((void *)ALIGN_UP_POW2((u64)(ptr), (alignment)))
    
    ///
         ),                                                                                                                \
         ASSERT_OR_FATAL(                                                                                                  \
             (size)(total_bytes) >= (size)(ALIGN_UP_POW2((u64)(buf_ptr), 8u) - (u64)(buf_ptr)) + sizeof(u64) +             \
                                        ALIGN_UP_POW2((slot_size_bytes), (alignment_value)) + ((alignment_value) - 1),     \
             "BudgetAllocatorInit: buffer too small for bitmap + one padded slot"                                          \
         ASSERT_OR_FATAL(                                                                                                  \
             (size)(total_bytes) >= (size)(ALIGN_UP_POW2((u64)(buf_ptr), 8u) - (u64)(buf_ptr)) + sizeof(u64) +             \
                                        ALIGN_UP_POW2((slot_size_bytes), (alignment_value)) + ((alignment_value) - 1),     \
             "BudgetAllocatorInit: buffer too small for bitmap + one padded slot"                                          \
         ),                                                                                                                \
             0,                                                                                                            \
             CEIL_DIV(                                                                                                     \
                 ((size)(total_bytes) - (size)(ALIGN_UP_POW2((u64)(buf_ptr), 8u) - (u64)(buf_ptr))) /                      \
                     ALIGN_UP_POW2((slot_size_bytes), (alignment_value)),                                                  \
                 64u                                                                                                       \
             CEIL_DIV(                                                                                                     \
                 ((size)(total_bytes) - (size)(ALIGN_UP_POW2((u64)(buf_ptr), 8u) - (u64)(buf_ptr))) /                      \
                     ALIGN_UP_POW2((slot_size_bytes), (alignment_value)),                                                  \
                 64u                                                                                                       \
             ) * sizeof(u64)                                                                                               \
             .bitmap       = (u64 *)PTR_ALIGN_UP_POW2((buf_ptr), 8u),                                                      \
             .bitmap_words = (u32)CEIL_DIV(                                                                                \
                 ((size)(total_bytes) - (size)(ALIGN_UP_POW2((u64)(buf_ptr), 8u) - (u64)(buf_ptr))) /                      \
                     ALIGN_UP_POW2((slot_size_bytes), (alignment_value)),                                                  \
                 64u                                                                                                       \
             .bitmap_words = (u32)CEIL_DIV(                                                                                \
                 ((size)(total_bytes) - (size)(ALIGN_UP_POW2((u64)(buf_ptr), 8u) - (u64)(buf_ptr))) /                      \
                     ALIGN_UP_POW2((slot_size_bytes), (alignment_value)),                                                  \
                 64u                                                                                                       \
             ),                                                                                                            \
                 64u                                                                                                       \
             ),                                                                                                            \
             .slots = (u8 *)ALIGN_UP_POW2(                                                                                 \
                 ALIGN_UP_POW2((u64)(buf_ptr), 8u) +                                                                       \
                     CEIL_DIV(                                                                                             \
             ),                                                                                                            \
             .slots = (u8 *)ALIGN_UP_POW2(                                                                                 \
                 ALIGN_UP_POW2((u64)(buf_ptr), 8u) +                                                                       \
                     CEIL_DIV(                                                                                             \
                         ((size)(total_bytes) - (size)(ALIGN_UP_POW2((u64)(buf_ptr), 8u) - (u64)(buf_ptr))) /              \
                 ALIGN_UP_POW2((u64)(buf_ptr), 8u) +                                                                       \
                     CEIL_DIV(                                                                                             \
                         ((size)(total_bytes) - (size)(ALIGN_UP_POW2((u64)(buf_ptr), 8u) - (u64)(buf_ptr))) /              \
                             ALIGN_UP_POW2((slot_size_bytes), (alignment_value)),                                          \
                         64u                                                                                               \
                     CEIL_DIV(                                                                                             \
                         ((size)(total_bytes) - (size)(ALIGN_UP_POW2((u64)(buf_ptr), 8u) - (u64)(buf_ptr))) /              \
                             ALIGN_UP_POW2((slot_size_bytes), (alignment_value)),                                          \
                         64u                                                                                               \
                     ) * sizeof(u64),                                                                                      \
                 (alignment_value)                                                                                         \
             ),                                                                                                            \
             .slot_size = ALIGN_UP_POW2((slot_size_bytes), (alignment_value)),                                             \
             .slot_count =                                                                                                 \
                 ((size)(total_bytes) - (size)(ALIGN_UP_POW2((u64)(buf_ptr), 8u) - (u64)(buf_ptr)) -                       \
             .slot_size = ALIGN_UP_POW2((slot_size_bytes), (alignment_value)),                                             \
             .slot_count =                                                                                                 \
                 ((size)(total_bytes) - (size)(ALIGN_UP_POW2((u64)(buf_ptr), 8u) - (u64)(buf_ptr)) -                       \
                  CEIL_DIV(                                                                                                \
                      ((size)(total_bytes) - (size)(ALIGN_UP_POW2((u64)(buf_ptr), 8u) - (u64)(buf_ptr))) /                 \
                 ((size)(total_bytes) - (size)(ALIGN_UP_POW2((u64)(buf_ptr), 8u) - (u64)(buf_ptr)) -                       \
                  CEIL_DIV(                                                                                                \
                      ((size)(total_bytes) - (size)(ALIGN_UP_POW2((u64)(buf_ptr), 8u) - (u64)(buf_ptr))) /                 \
                          ALIGN_UP_POW2((slot_size_bytes), (alignment_value)),                                             \
                      64u                                                                                                  \
                  CEIL_DIV(                                                                                                \
                      ((size)(total_bytes) - (size)(ALIGN_UP_POW2((u64)(buf_ptr), 8u) - (u64)(buf_ptr))) /                 \
                          ALIGN_UP_POW2((slot_size_bytes), (alignment_value)),                                             \
                      64u                                                                                                  \
                  ) * sizeof(u64) -                                                                                        \
                      64u                                                                                                  \
                  ) * sizeof(u64) -                                                                                        \
                  (size)(ALIGN_UP_POW2(                                                                                    \
                             ALIGN_UP_POW2((u64)(buf_ptr), 8u) +                                                           \
                                 CEIL_DIV(                                                                                 \
                  ) * sizeof(u64) -                                                                                        \
                  (size)(ALIGN_UP_POW2(                                                                                    \
                             ALIGN_UP_POW2((u64)(buf_ptr), 8u) +                                                           \
                                 CEIL_DIV(                                                                                 \
                                     ((size)(total_bytes) - (size)(ALIGN_UP_POW2((u64)(buf_ptr), 8u) - (u64)(buf_ptr))) /  \
                             ALIGN_UP_POW2((u64)(buf_ptr), 8u) +                                                           \
                                 CEIL_DIV(                                                                                 \
                                     ((size)(total_bytes) - (size)(ALIGN_UP_POW2((u64)(buf_ptr), 8u) - (u64)(buf_ptr))) /  \
                                         ALIGN_UP_POW2((slot_size_bytes), (alignment_value)),                              \
                                     64u                                                                                   \
                                 CEIL_DIV(                                                                                 \
                                     ((size)(total_bytes) - (size)(ALIGN_UP_POW2((u64)(buf_ptr), 8u) - (u64)(buf_ptr))) /  \
                                         ALIGN_UP_POW2((slot_size_bytes), (alignment_value)),                              \
                                     64u                                                                                   \
                                 ) * sizeof(u64),                                                                          \
                             (alignment_value)                                                                             \
                         ) -                                                                                               \
                         ALIGN_UP_POW2((u64)(buf_ptr), 8u) -                                                               \
                         CEIL_DIV(                                                                                         \
                             ((size)(total_bytes) - (size)(ALIGN_UP_POW2((u64)(buf_ptr), 8u) - (u64)(buf_ptr))) /          \
                         ALIGN_UP_POW2((u64)(buf_ptr), 8u) -                                                               \
                         CEIL_DIV(                                                                                         \
                             ((size)(total_bytes) - (size)(ALIGN_UP_POW2((u64)(buf_ptr), 8u) - (u64)(buf_ptr))) /          \
                                 ALIGN_UP_POW2((slot_size_bytes), (alignment_value)),                                      \
                             64u                                                                                           \
                         CEIL_DIV(                                                                                         \
                             ((size)(total_bytes) - (size)(ALIGN_UP_POW2((u64)(buf_ptr), 8u) - (u64)(buf_ptr))) /          \
                                 ALIGN_UP_POW2((slot_size_bytes), (alignment_value)),                                      \
                             64u                                                                                           \
                         ) * sizeof(u64))) /                                                                               \
                             64u                                                                                           \
                         ) * sizeof(u64))) /                                                                               \
                 ALIGN_UP_POW2((slot_size_bytes), (alignment_value)),                                                      \
        }))
    #define VecAlignedOffsetAt(v, idx)                                                                                     \
        ((idx) * (((v)->allocator && (v)->allocator->alignment > 1) ?                                                      \
                      ALIGN_UP_POW2(sizeof(VEC_DATATYPE(v)), (v)->allocator->alignment) :                                  \
                      sizeof(VEC_DATATYPE(v))))
Last updated on