adjustToWorstCaseAlignment() simplify
Add some STATIC_ASSERT to check assumptions. Only in-tree use is freeListLib to ensure chunks in a malloc()'d block are aligned.
This commit is contained in:
@@ -15,40 +15,25 @@
|
||||
#include <stdlib.h>
|
||||
#include <stddef.h>
|
||||
|
||||
/* Up to now epicsShareThings have been declared as imports
|
||||
* (Appropriate for other stuff)
|
||||
* After setting the following they will be declared as exports
|
||||
* (Appropriate for what we implement)
|
||||
*/
|
||||
#include <epicsAssert.h>
|
||||
#include "adjustment.h"
|
||||
|
||||
LIBCOM_API size_t adjustToWorstCaseAlignment(size_t size)
|
||||
size_t adjustToWorstCaseAlignment(size_t size)
|
||||
{
|
||||
int align_size, adjust;
|
||||
struct test_long_word { char c; long lw; };
|
||||
struct test_double { char c; double d; };
|
||||
struct test_ptr { char c; void *p; };
|
||||
int test_long_size = sizeof(struct test_long_word) - sizeof(long);
|
||||
int test_double_size = sizeof(struct test_double) - sizeof(double);
|
||||
int test_ptr_size = sizeof(struct test_ptr) - sizeof(void *);
|
||||
size_t adjusted_size = size;
|
||||
union aline {
|
||||
/* largest primative types (so far...) */
|
||||
double dval;
|
||||
size_t uval;
|
||||
char *ptr;
|
||||
};
|
||||
|
||||
/*
|
||||
* Use Jeff's alignment tests to determine worst case of long,
|
||||
* double or pointer alignment requirements.
|
||||
*/
|
||||
align_size = test_long_size > test_ptr_size ?
|
||||
test_long_size : test_ptr_size;
|
||||
/* assert that alignment size is a power of 2 */
|
||||
STATIC_ASSERT((sizeof(union aline) & (sizeof(union aline)-1))==0);
|
||||
|
||||
align_size = align_size > test_double_size ?
|
||||
align_size : test_double_size;
|
||||
/* round up to aligment size */
|
||||
size--;
|
||||
size |= sizeof(union aline)-1;
|
||||
size++;
|
||||
|
||||
/*
|
||||
* Increase the size to fit worst case alignment if not already
|
||||
* properly aligned.
|
||||
*/
|
||||
adjust = align_size - size%align_size;
|
||||
if (adjust != align_size) adjusted_size += adjust;
|
||||
|
||||
return (adjusted_size);
|
||||
return size;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user