1. bitsquid
  2. foundation
  3. Issues
Issue #9 new

ScratchAllocator, Array<char> and alignof() conflicts

Gyula László
created an issue

If trying to use an Array<char> (aka. string_stream::Buffer) with ScratchAllocator, there is a little problem:

    alignof(char) == 1

The problem is the conflict between ScratchAllocator::allocate

    virtual void *allocate(uint32_t size, uint32_t align) {
        assert(align % 4 == 0);

and array::set_capacity

    template<typename T> void set_capacity(Array<T> &a, uint32_t new_capacity)
        // [...]
        if (new_capacity > 0) {
            new_data = (T *)a._allocator->allocate(sizeof(T)*new_capacity, alignof(T));
            // [...]

Comments (1)

  1. Gyula László reporter

    This made me think about hardcoding alignment inside the library.

    In this specific case IMHO the target alignment for arrays and allocators (4 in most cases) should be either:

    • a preprocessor definition (so it can be changed from the build system)

    • a member variable (I think its bad, cannot think of a real-world scenario requiring instance-based alignment requirements)

    • a template parameter (I feel its the right kind of granularity, but introduces clutter) with a sensible default value

  2. Log in to comment