# ManagedBuffer alignment?

**URL:** <https://forums.swift.org/t/managedbuffer-alignment/39071>\
**Category:** Using Swift\
**Created:** [August 5, 2020, 3:32am UTC](https://forums.swift.org/t/managedbuffer-alignment/39071 "2020-08-05T03:32:22Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![lorentey](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lorentey/32/1299_2.png) [@lorentey](https://forums.swift.org/u/lorentey)\
**Post date:** [August 5, 2020, 5:27am UTC](https://forums.swift.org/t/managedbuffer-alignment/39071/2 "2020-08-05T05:27:46Z")

</div>

I think what this use case needs is a `ManagedRawBuffer` class, with a `create` method that takes an explicit alignment, like `UnsafeRawBufferPointer.allocate`. Ideally at the same time we'd add this we'd also flesh out the existing raw pointer APIs with e.g. new helpers to help figuring out alignments within such heterogeneous storage. (This would resolve problem #4 in the (only tangentially related) pitch I just posted in [Evolution](https://forums.swift.org/t/ironing-out-managedbuffer-api-wrinkles/39072).)

Alternatively, we could provide standard `ManagedBuffer` variants that allow multiple Element types, but that could be difficult to do well without variadic generics.

If you only ever need to store a single `SIMD16<UInt8>` value, it is most likely a better idea to move it to `Header`, and set the `Element` type to something that holds one row of integers. (Say, `UInt64`, `(UInt32, UInt16, UInt16)`, or an equivalent struct.)

If you have strict layout requirements for the integer rows, then representing them with `UInt64` or struct imported from C are probably the best options.

If you need multiple SIMD16 values before the plain integers (or if you need to guarantee that the SIMD value is laid out immediately before the integer rows), then setting `Element` to `SIMD16<UInt8>` is probably the best you can do! You can then rebind the parts that correspond to the plain integer rows to one of the types above. Be careful when calculating the capacity of the buffer -- it will need some slightly creative accounting.

---

_[View the full topic](https://forums.swift.org/t/managedbuffer-alignment/39071)._
