Rick Altherr wrote: > Ironically, my day job has been exclusively on non-x86 platforms for > years and only recently started with x86. > > Pointers to data types should be aligned such that the data type is > naturally aligned. That would mean that a 16-bit type should be > 16-bit aligned. The question was related to structures. Since they > are not a native data type, I wasn't sure that they required word > alignment. I'll dig up my copy of the C standard and check. Structures require the alignment of the largest type that is inside - ie. a structure that contains only chars does not need any alignment, a struct that containt integers requires integer alignment etc..
> In practice, most malloc() implementations return word-alignment at a > minimum. This command queue allocation does not do any alignment > today. So, the first item allocated in a command queue page will be > malloc() aligned, but subsequent allocations are not guaranteed to be. Malloc needs to align to the largest machine type - that may be word (if "word" is a machine word, not a 16-bit value). > A few approaches can be taken to resolve this. One is to pad the > allocations to a word-sized quantity. Another is to align the start > of an allocation but not adjust the size used. Yet another is to have > separate aligned and unaligned allocators. Yet another is to allow > the required alignment to be specified. Unless there are really compelling reasons why someone would need unaligned allocation, I think always aligning like malloc does is the best way. > To keep with malloc() semantics, guaranteeing that the allocations are > at least word aligned is probably a good idea. That allows the > pointer to be used for any types (except vector types) and requires > minimal change in the code base. Padding the allocation size is > arguably smaller in terms of code, but if that route is used, a > detailed comment should proceed it explaining why the size is being > padded. It should also be done as a modification of the used member > of the page structure rather than as part of the requested size. That > way it is clear that the size of the object allocated isn't changed, > but that padding was added to force alignment. Agreed. Both are fine IMHO - the chosen method depends a bit on taste. cu Michael _______________________________________________ Openocd-development mailing list Openocd-development@lists.berlios.de https://lists.berlios.de/mailman/listinfo/openocd-development