Skip to content

Commit 73138e9

Browse files
committed
[libc] Make static_assert() available via assert.h
Expose static_assert() via assert.h and migrate link-time assertions to build-time assertions where possible. Signed-off-by: Michael Brown <[email protected]>
1 parent 08fcb0e commit 73138e9

File tree

5 files changed

+15
-7
lines changed

5 files changed

+15
-7
lines changed

src/drivers/infiniband/arbel.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -545,8 +545,8 @@ static int arbel_mad ( struct ib_device *ibdev, union ib_mad *mad ) {
545545
union arbelprm_mad mad_ifc;
546546
int rc;
547547

548-
linker_assert ( sizeof ( *mad ) == sizeof ( mad_ifc.mad ),
549-
mad_size_mismatch );
548+
/* Sanity check */
549+
static_assert ( sizeof ( *mad ) == sizeof ( mad_ifc.mad ) );
550550

551551
/* Copy in request packet */
552552
memcpy ( &mad_ifc.mad, mad, sizeof ( mad_ifc.mad ) );

src/drivers/infiniband/hermon.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -779,8 +779,8 @@ static int hermon_mad ( struct ib_device *ibdev, union ib_mad *mad ) {
779779
union hermonprm_mad mad_ifc;
780780
int rc;
781781

782-
linker_assert ( sizeof ( *mad ) == sizeof ( mad_ifc.mad ),
783-
mad_size_mismatch );
782+
/* Sanity check */
783+
static_assert ( sizeof ( *mad ) == sizeof ( mad_ifc.mad ) );
784784

785785
/* Copy in request packet */
786786
memcpy ( &mad_ifc.mad, mad, sizeof ( mad_ifc.mad ) );

src/image/script.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,7 @@ static int script_exec ( struct image *image ) {
219219
static int script_probe ( struct image *image ) {
220220
static const char ipxe_magic[] = "#!ipxe";
221221
static const char gpxe_magic[] = "#!gpxe";
222-
linker_assert ( sizeof ( ipxe_magic ) == sizeof ( gpxe_magic ),
223-
magic_size_mismatch );
222+
static_assert ( sizeof ( ipxe_magic ) == sizeof ( gpxe_magic ) );
224223
char test[ sizeof ( ipxe_magic ) - 1 /* NUL */
225224
+ 1 /* terminating space */];
226225

src/include/assert.h

+9
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,15 @@ assert_printf ( const char *fmt, ... ) asm ( "printf" );
5555
} \
5656
} while ( 0 )
5757

58+
/**
59+
* Assert a condition at build time
60+
*
61+
* If the compiler cannot prove that the condition is true, the build
62+
* will fail with an error message.
63+
*/
64+
#undef static_assert
65+
#define static_assert(x) _Static_assert( x, #x )
66+
5867
/**
5968
* Assert a condition at link-time.
6069
*

src/net/aoe.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ static void aoecmd_ata_cmd ( struct aoe_command *aoecmd,
374374
struct aoeata *aoeata = &aoehdr->payload[0].ata;
375375

376376
/* Sanity check */
377-
linker_assert ( AOE_FL_DEV_HEAD == ATA_DEV_SLAVE, __fix_ata_h__ );
377+
static_assert ( AOE_FL_DEV_HEAD == ATA_DEV_SLAVE );
378378
assert ( len == ( sizeof ( *aoehdr ) + sizeof ( *aoeata ) +
379379
command->data_out_len ) );
380380

0 commit comments

Comments
 (0)