Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
ccan
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
mirror
ccan
Commits
224a8f1f
Commit
224a8f1f
authored
May 18, 2008
by
Rusty Russell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ALIGNOF() helper
parent
b4be5752
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
126 additions
and
0 deletions
+126
-0
alignof/_info.c
alignof/_info.c
+45
-0
alignof/alignof.h
alignof/alignof.h
+19
-0
alignof/test/run.c
alignof/test/run.c
+62
-0
No files found.
alignof/_info.c
0 → 100644
View file @
224a8f1f
#include <stdio.h>
#include <string.h>
#include "config.h"
/**
* alignof - ALIGNOF() macro to determine alignment of a type.
*
* Many platforms have requirements that certain types must be aligned
* to certain address boundaries, such as ints needing to be on 4-byte
* boundaries. Attempting to access variables with incorrect
* alignment may cause performance loss or even program failure (eg. a
* bus signal).
*
* There are times which it's useful to be able to programatically
* access these requirements, such as for dynamic allocators.
*
* Example:
* #include <stdio.h>
* #include "alignof/alignoff.h"
*
* int main(int argc, char *argv[])
* {
* char arr[sizeof(int)];
*
* if ((unsigned long)arr % ALIGNOF(int)) {
* printf("arr %p CANNOT hold an int\n", arr);
* exit(1);
* } else {
* printf("arr %p CAN hold an int\n", arr);
* exit(0);
* }
* }
*/
int
main
(
int
argc
,
char
*
argv
[])
{
if
(
argc
!=
2
)
return
1
;
if
(
strcmp
(
argv
[
1
],
"depends"
)
==
0
)
{
printf
(
"build_assert
\n
"
);
return
0
;
}
return
1
;
}
alignof/alignof.h
0 → 100644
View file @
224a8f1f
#ifndef CCAN_ALIGNOF_H
#define CCAN_ALIGNOF_H
#include "config.h"
/**
* ALIGNOF - get the alignment of a type
* @t: the type to test
*
* This returns a safe alignment for the given type.
*/
#if HAVE_ALIGNOF
/* A GCC extension. */
#define ALIGNOF(t) __alignof__(t)
#else
/* Alignment by measuring structure padding. */
#define ALIGNOF(t) ((char *)(&((struct { char c; t _h; } *)0)->_h) - (char *)0)
#endif
#endif
/* CCAN_ALIGNOF_H */
alignof/test/run.c
0 → 100644
View file @
224a8f1f
#include <stdlib.h>
#include <stddef.h>
#include <tap/tap.h>
#include "alignof/alignof.h"
/* Alignment is remarkably difficult to test. The rules may be more
* complex than ALIGNOF() can know: eg. on i386 __alignof__(double) == 8, but
* __alignof__(struct containing double) == 4.
*
* Technically, we can only test that we give *at least* the alignment which
* naturally occurs, and that accesses work.
*
* For the moment, we work around double. */
struct
lots_of_types
{
char
c
;
short
s
;
char
c2
;
int
i
;
char
c3
;
float
f
;
char
c4
;
double
d
;
char
c5
;
};
int
main
(
int
argc
,
char
*
argv
[])
{
struct
lots_of_types
lots_of_types
,
*
lp
=
malloc
(
sizeof
(
*
lp
));
char
c
;
short
s
;
char
c2
;
int
i
;
char
c3
;
float
f
;
char
c4
;
double
d
;
/* Make sure we use all the variables. */
c
=
0
;
c2
=
c3
=
c4
=
c
;
plan_tests
(
15
);
ok1
((
unsigned
long
)
&
c
%
ALIGNOF
(
char
)
==
0
);
ok1
((
unsigned
long
)
&
s
%
ALIGNOF
(
short
)
==
0
);
ok1
((
unsigned
long
)
&
i
%
ALIGNOF
(
int
)
==
0
);
ok1
((
unsigned
long
)
&
f
%
ALIGNOF
(
float
)
==
0
);
ok1
((
unsigned
long
)
&
d
%
ALIGNOF
(
double
)
==
0
);
ok1
((
unsigned
long
)
&
lots_of_types
.
c
%
ALIGNOF
(
char
)
==
0
);
ok1
((
unsigned
long
)
&
lots_of_types
.
s
%
ALIGNOF
(
short
)
==
0
);
ok1
((
unsigned
long
)
&
lots_of_types
.
i
%
ALIGNOF
(
int
)
==
0
);
ok1
((
unsigned
long
)
&
lots_of_types
.
f
%
ALIGNOF
(
float
)
==
0
);
ok1
(
offsetof
(
struct
lots_of_types
,
d
)
%
ALIGNOF
(
double
)
==
0
);
ok1
((
unsigned
long
)
&
lp
->
c
%
ALIGNOF
(
char
)
==
0
);
ok1
((
unsigned
long
)
&
lp
->
s
%
ALIGNOF
(
short
)
==
0
);
ok1
((
unsigned
long
)
&
lp
->
i
%
ALIGNOF
(
int
)
==
0
);
ok1
((
unsigned
long
)
&
lp
->
f
%
ALIGNOF
(
float
)
==
0
);
ok1
((
unsigned
long
)
&
lp
->
d
%
ALIGNOF
(
double
)
==
0
);
exit
(
exit_status
());
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment