Add static_assert fallback for older compilers

_Static_assert is new in C11, and older versions of GCC don't support
it.  Detect whether static_assert is available in <assert.h> and if not,
define a fallback macro.

Change-Id: Iaf513677e5f356832e0a3d1f6b1f4f9ea07e6627
Signed-off-by: Daniel Verkamp <daniel.verkamp@intel.com>
This commit is contained in:
Daniel Verkamp 2016-01-08 14:37:12 -07:00
parent 3d6b230ae9
commit 16a45d2591
4 changed files with 92 additions and 31 deletions

55
include/spdk/assert.h Normal file
View File

@ -0,0 +1,55 @@
/*-
* BSD LICENSE
*
* Copyright(c) 2016 Intel Corporation. All rights reserved.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Intel Corporation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef SPDK_ASSERT_H
#define SPDK_ASSERT_H
#include <assert.h>
#define SPDK_CONCAT_(x, y) x##y
#define SPDK_CONCAT(x, y) SPDK_CONCAT_(x, y)
#ifdef static_assert
#define SPDK_STATIC_ASSERT(cond, msg) static_assert(cond, msg)
#else
/*
* Fallback for older compilers that don't support static_assert
*
* The array size will expand to 0 if the condition is true, or
* -1 if the condition is false (causing compilation to fail).
*/
#define SPDK_STATIC_ASSERT(cond, msg) \
typedef char SPDK_CONCAT(SPDK_STATIC_ASSERT_, __LINE__)[!!(cond) - 1]
#endif
#endif /* SPDK_ASSERT_H */

View File

@ -36,6 +36,8 @@
#include <inttypes.h>
#include "spdk/assert.h"
#define IOAT_INTRCTRL_MASTER_INT_EN 0x01
#define IOAT_VER_3_0 0x30
@ -301,6 +303,6 @@ union ioat_hw_descriptor {
struct ioat_pq_ext_hw_descriptor pq_ext;
struct ioat_pq_update_hw_descriptor pq_update;
};
_Static_assert(sizeof(union ioat_hw_descriptor) == 64, "incorrect ioat_hw_descriptor layout");
SPDK_STATIC_ASSERT(sizeof(union ioat_hw_descriptor) == 64, "incorrect ioat_hw_descriptor layout");
#endif /* __IOAT_SPEC_H__ */

View File

@ -37,6 +37,8 @@
#include <stdint.h>
#include <stddef.h>
#include "spdk/assert.h"
/**
* \file
*
@ -86,7 +88,7 @@ struct nvme_intel_rw_latency_page {
uint32_t buckets_1ms[31];
uint32_t buckets_32ms[31];
};
_Static_assert(sizeof(struct nvme_intel_rw_latency_page) == 380, "Incorrect size");
SPDK_STATIC_ASSERT(sizeof(struct nvme_intel_rw_latency_page) == 380, "Incorrect size");
struct nvme_intel_temperature_page {
uint64_t current_temperature;
@ -100,7 +102,7 @@ struct nvme_intel_temperature_page {
uint64_t specified_min_op_temperature;
uint64_t estimated_offset;
};
_Static_assert(sizeof(struct nvme_intel_temperature_page) == 112, "Incorrect size");
SPDK_STATIC_ASSERT(sizeof(struct nvme_intel_temperature_page) == 112, "Incorrect size");
struct nvme_intel_smart_attribute {
uint8_t code;
@ -114,6 +116,6 @@ struct nvme_intel_smart_attribute {
struct __attribute__((packed)) nvme_intel_smart_information_page {
struct nvme_intel_smart_attribute nvme_intel_smart_attributes[13];
};
_Static_assert(sizeof(struct nvme_intel_smart_information_page) == 156, "Incorrect size");
SPDK_STATIC_ASSERT(sizeof(struct nvme_intel_smart_information_page) == 156, "Incorrect size");
#endif

View File

@ -37,6 +37,8 @@
#include <stdint.h>
#include <stddef.h>
#include "spdk/assert.h"
/**
* \file
*
@ -83,7 +85,7 @@ union nvme_cap_lo_register {
uint32_t to : 8;
} bits;
};
_Static_assert(sizeof(union nvme_cap_lo_register) == 4, "Incorrect size");
SPDK_STATIC_ASSERT(sizeof(union nvme_cap_lo_register) == 4, "Incorrect size");
union nvme_cap_hi_register {
uint32_t raw;
@ -108,7 +110,7 @@ union nvme_cap_hi_register {
uint32_t reserved1 : 8;
} bits;
};
_Static_assert(sizeof(union nvme_cap_hi_register) == 4, "Incorrect size");
SPDK_STATIC_ASSERT(sizeof(union nvme_cap_hi_register) == 4, "Incorrect size");
union nvme_cc_register {
uint32_t raw;
@ -139,7 +141,7 @@ union nvme_cc_register {
uint32_t reserved2 : 8;
} bits;
};
_Static_assert(sizeof(union nvme_cc_register) == 4, "Incorrect size");
SPDK_STATIC_ASSERT(sizeof(union nvme_cc_register) == 4, "Incorrect size");
enum nvme_shn_value {
NVME_SHN_NORMAL = 0x1,
@ -161,7 +163,7 @@ union nvme_csts_register {
uint32_t reserved1 : 28;
} bits;
};
_Static_assert(sizeof(union nvme_csts_register) == 4, "Incorrect size");
SPDK_STATIC_ASSERT(sizeof(union nvme_csts_register) == 4, "Incorrect size");
enum nvme_shst_value {
NVME_SHST_NORMAL = 0x0,
@ -183,7 +185,7 @@ union nvme_aqa_register {
uint32_t reserved2 : 4;
} bits;
};
_Static_assert(sizeof(union nvme_aqa_register) == 4, "Incorrect size");
SPDK_STATIC_ASSERT(sizeof(union nvme_aqa_register) == 4, "Incorrect size");
struct nvme_registers {
/** controller capabilities */
@ -215,16 +217,16 @@ struct nvme_registers {
};
/* NVMe controller register space offsets */
_Static_assert(0x00 == offsetof(struct nvme_registers, cap_lo), "Incorrect register offset");
_Static_assert(0x08 == offsetof(struct nvme_registers, vs), "Incorrect register offset");
_Static_assert(0x0C == offsetof(struct nvme_registers, intms), "Incorrect register offset");
_Static_assert(0x10 == offsetof(struct nvme_registers, intmc), "Incorrect register offset");
_Static_assert(0x14 == offsetof(struct nvme_registers, cc), "Incorrect register offset");
_Static_assert(0x1C == offsetof(struct nvme_registers, csts), "Incorrect register offset");
_Static_assert(0x20 == offsetof(struct nvme_registers, nssr), "Incorrect register offset");
_Static_assert(0x24 == offsetof(struct nvme_registers, aqa), "Incorrect register offset");
_Static_assert(0x28 == offsetof(struct nvme_registers, asq), "Incorrect register offset");
_Static_assert(0x30 == offsetof(struct nvme_registers, acq), "Incorrect register offset");
SPDK_STATIC_ASSERT(0x00 == offsetof(struct nvme_registers, cap_lo), "Incorrect register offset");
SPDK_STATIC_ASSERT(0x08 == offsetof(struct nvme_registers, vs), "Incorrect register offset");
SPDK_STATIC_ASSERT(0x0C == offsetof(struct nvme_registers, intms), "Incorrect register offset");
SPDK_STATIC_ASSERT(0x10 == offsetof(struct nvme_registers, intmc), "Incorrect register offset");
SPDK_STATIC_ASSERT(0x14 == offsetof(struct nvme_registers, cc), "Incorrect register offset");
SPDK_STATIC_ASSERT(0x1C == offsetof(struct nvme_registers, csts), "Incorrect register offset");
SPDK_STATIC_ASSERT(0x20 == offsetof(struct nvme_registers, nssr), "Incorrect register offset");
SPDK_STATIC_ASSERT(0x24 == offsetof(struct nvme_registers, aqa), "Incorrect register offset");
SPDK_STATIC_ASSERT(0x28 == offsetof(struct nvme_registers, asq), "Incorrect register offset");
SPDK_STATIC_ASSERT(0x30 == offsetof(struct nvme_registers, acq), "Incorrect register offset");
enum nvme_sgl_descriptor_type {
NVME_SGL_TYPE_DATA_BLOCK = 0x0,
@ -246,7 +248,7 @@ struct __attribute__((packed)) nvme_sgl_descriptor {
/** SGL descriptor type specific */
uint8_t type_specific : 4;
};
_Static_assert(sizeof(struct nvme_sgl_descriptor) == 16, "Incorrect size");
SPDK_STATIC_ASSERT(sizeof(struct nvme_sgl_descriptor) == 16, "Incorrect size");
enum nvme_psdt_value {
NVME_PSDT_PRP = 0x0,
@ -291,7 +293,7 @@ struct nvme_command {
uint32_t cdw14; /* command-specific */
uint32_t cdw15; /* command-specific */
};
_Static_assert(sizeof(struct nvme_command) == 64, "Incorrect size");
SPDK_STATIC_ASSERT(sizeof(struct nvme_command) == 64, "Incorrect size");
struct nvme_status {
uint16_t p : 1; /* phase tag */
@ -301,7 +303,7 @@ struct nvme_status {
uint16_t m : 1; /* more */
uint16_t dnr : 1; /* do not retry */
};
_Static_assert(sizeof(struct nvme_status) == 2, "Incorrect size");
SPDK_STATIC_ASSERT(sizeof(struct nvme_status) == 2, "Incorrect size");
struct nvme_completion {
/* dword 0 */
@ -318,14 +320,14 @@ struct nvme_completion {
uint16_t cid; /* command identifier */
struct nvme_status status;
};
_Static_assert(sizeof(struct nvme_completion) == 16, "Incorrect size");
SPDK_STATIC_ASSERT(sizeof(struct nvme_completion) == 16, "Incorrect size");
struct nvme_dsm_range {
uint32_t attributes;
uint32_t length;
uint64_t starting_lba;
};
_Static_assert(sizeof(struct nvme_dsm_range) == 16, "Incorrect size");
SPDK_STATIC_ASSERT(sizeof(struct nvme_dsm_range) == 16, "Incorrect size");
/* status code types */
enum nvme_status_code_type {
@ -485,7 +487,7 @@ struct nvme_power_state {
uint8_t reserved7[16];
};
_Static_assert(sizeof(struct nvme_power_state) == 32, "Incorrect size");
SPDK_STATIC_ASSERT(sizeof(struct nvme_power_state) == 32, "Incorrect size");
struct __attribute__((packed)) nvme_controller_data {
/* bytes 0-255: controller capabilities and features */
@ -710,7 +712,7 @@ struct __attribute__((packed)) nvme_controller_data {
/* bytes 3072-4095: vendor specific */
uint8_t vs[1024];
};
_Static_assert(sizeof(struct nvme_controller_data) == 4096, "Incorrect size");
SPDK_STATIC_ASSERT(sizeof(struct nvme_controller_data) == 4096, "Incorrect size");
struct nvme_namespace_data {
/** namespace size */
@ -867,7 +869,7 @@ struct nvme_namespace_data {
uint8_t vendor_specific[3712];
};
_Static_assert(sizeof(struct nvme_namespace_data) == 4096, "Incorrect size");
SPDK_STATIC_ASSERT(sizeof(struct nvme_namespace_data) == 4096, "Incorrect size");
enum nvme_log_page {
/* 0x00 - reserved */
@ -890,7 +892,7 @@ struct nvme_error_information_entry {
uint8_t vendor_specific;
uint8_t reserved[35];
};
_Static_assert(sizeof(struct nvme_error_information_entry) == 64, "Incorrect size");
SPDK_STATIC_ASSERT(sizeof(struct nvme_error_information_entry) == 64, "Incorrect size");
union nvme_critical_warning_state {
uint8_t raw;
@ -904,7 +906,7 @@ union nvme_critical_warning_state {
uint8_t reserved : 3;
} bits;
};
_Static_assert(sizeof(union nvme_critical_warning_state) == 1, "Incorrect size");
SPDK_STATIC_ASSERT(sizeof(union nvme_critical_warning_state) == 1, "Incorrect size");
struct __attribute__((packed)) nvme_health_information_page {
union nvme_critical_warning_state critical_warning;
@ -937,7 +939,7 @@ struct __attribute__((packed)) nvme_health_information_page {
uint8_t reserved2[320];
};
_Static_assert(sizeof(struct nvme_health_information_page) == 512, "Incorrect size");
SPDK_STATIC_ASSERT(sizeof(struct nvme_health_information_page) == 512, "Incorrect size");
struct nvme_firmware_page {
struct {
@ -949,7 +951,7 @@ struct nvme_firmware_page {
uint64_t revision[7]; /* revisions for 7 slots */
uint8_t reserved2[448];
};
_Static_assert(sizeof(struct nvme_firmware_page) == 512, "Incorrect size");
SPDK_STATIC_ASSERT(sizeof(struct nvme_firmware_page) == 512, "Incorrect size");
#define nvme_completion_is_error(cpl) \
((cpl)->status.sc != 0 || (cpl)->status.sct != 0)