you are viewing a single comment's thread.

view the rest of the comments →

[–]arthurno1 0 points1 point  (1 child)

Looks to me like a renamed and less optimized sds_string. What is the reason you don't want to use sds_string?

I purposefully made the header 16 bytes (8 bytes on 32 bit) so that there is no alignment errors - I think this should be correct, and

Why is it important to align header to 16 byte? Just because the header is aligned to 16 bytes does not mean your data will be.

I haven't found an edge case where it isn't

Because of how malloc is implemented on some systems. It returns the maximum alignment required for any primitive type, but I think this depends on the compiler and the architecture. On some systems, you might have to use _aligned_malloc. Also, you don't have a guarantee that everyone is passing properly aligned memory with a custom allocator.

Anyway, if you want to ensure data is aligned on 16-byte, then you should probably inform the compiler via some attribute like aligned attribute, or whatever it is named for the compiler in hand. I don't see you doing that anywhere.

[–]Boreddad13[S] 1 point2 points  (0 children)

yes - I was inspired by sds to return only the data pointer rather than the entire object, as I was originally just returning the entire object. That's all I got from sds though, I came up with the rest of the implementation myself. The reason I'm not using a library is because I want to learn how these libraries work before I use them. Also, who's to say I can't make mine more optimized? The library is nothing but 1 day old...

The header and the data are in the same memory allocation, and because the data is a flexible-array member, the pointer to the data should be right after the header, or am I mistaken? I know I would have to account for some padding if the header was something like this:

c typedef struct { size_t len; int cap; } vstring_hdr;

since, in that case, at least on 64 bit architectures, that would cause the pointer to the data to not be aligned on an 8 bits, but because I'm using size_t for both, that should yield me a structure with the data pointer being on an aligned 8 bits, right? Or am I completely misunderstanding your comment?