CMP EMBEDDED.COM

Embedded On Demand Login | Register     Welcome Guest Embedded On Demand
HOME DESIGN PRODUCTS COLUMNS E-LEARNING CONFERENCES CODE FORUMS/BLOGS NEWSLETTERS CONTACT FEATURES RSS RSS

Thread: Comments for: "Computing properly aligned pointer values"

 

Permlink Replies: 4 - Pages: 1 - Last Post: Nov 9, 2009 7:31 AM Last Post By: D_Lundin Threads: [ Previous | Next ]
D_Lundin

Posts: 11
Registered: 08/11/09
Comments for: "Computing properly aligned pointer values"
Posted: Nov 2, 2009 7:37 AM
  Click to reply to this thread Reply
D_Lundin

Posts: 11
Registered: 08/11/09
Re: Comments for: "Computing properly aligned pointer values"
Posted: Nov 2, 2009 7:37 AM   in response to: D_Lundin in response to: D_Lundin
  Click to reply to this thread Reply
To stir up some discussion... wouldn't the following be an acceptable implementation of the same?

Assume that I know that my widget is a small one, maybe small enough to even fit inside a the minimum alignment. Lets say it is a C struct with a few integers in it, something like this:

struct widget
{
uint8_t x;
uint8_t y;
};

It would then be easiest to simply make the widget like this:

typedef union
{
size_t size;

struct widget
{
/* members */
};
} Widget_t;

size_t size = n * sizeof(Widget_t);
Widget_t *ps = (Widget_t*) malloc(size);

ps->size = n;
pw = (Widget_t*) ++ps;

The above code relies solely on "struct padding" (or union padding if you will), ie the padding is decided automatically by the compiler based on the alignment requirements, and appended to the end of the struct/union. This padding is something we can be sure is present on every system, if the system requires memory alignment.

With this method we don't have to worry about pointer casts nor to find the maximum alignment of the system manually: the code becomes easier to read. The only drawback with the above is if the widgets are large. It will then be a waste of memory to store only the size in one such a large memory area.

Thoughts? :)

E111

Posts: 1
Registered: 11/06/09
Re: Comments for: "Computing properly aligned pointer values"
Posted: Nov 6, 2009 6:36 PM   in response to: D_Lundin in response to: D_Lundin
  Click to reply to this thread Reply
I would probably go with something like this:

typedef struct {
// widget definition...
} widget_t;

typedef struct {
int count;
widget_t index_0;
} widget_array_alloc_header_t;

Then in new_widget_array:
widget_t *new_widget_array(int count){
size_t size = sizeof(widget_array_alloc_header_t) + (count-1)*sizeof(widget_t);

widget_array_alloc_header_t *ps = malloc(size);
ps->count = count;

return &ps->index_0;
}

If you do that won't the compiler correctly align/pad the widget_array_alloc_header_t structure such that the index_0 member is on the appropriate boundary.

Then all the following elements should equally be correctly aligned?

D_Lundin

Posts: 11
Registered: 08/11/09
Re: Comments for: "Computing properly aligned pointer values"
Posted: Nov 9, 2009 7:24 AM   in response to: E111 in response to: E111
  Click to reply to this thread Reply
Yeah it will be correctly aligned, but... why would you want the count variable allocated for each element? The code looks the same as mine except that you replaced union with struct. Perhaps you meant to allocate the header separately from the array of data?

Either version will have the same advantage however: portable, automatic alignment calculation done by the struct padding.
D_Lundin

Posts: 11
Registered: 08/11/09
Re: Comments for: "Computing properly aligned pointer values"
Posted: Nov 9, 2009 7:31 AM   in response to: D_Lundin in response to: D_Lundin
  Click to reply to this thread Reply
Perhaps you meant to allocate the header separately from the array of data?

I just realized this is exactly what you do, but in the same line of code :)

The "gap" between the header and the array should be handled by struct padding in your example as well, as sizeof() includes the number of padding bytes. A bit prettier than my example indeed.

Point your RSS reader here for a feed of the latest messages in all forums




 :