int main() { printf("a: size = %u, alignment = %u, address mod 16 = %u\n", (unsigned) sizeof(a), (unsigned) _Alignof(a), ((unsigned) &a) & 0xF); printf("b: size = %u, alignment = %u, address mod 16 = %u\n", (unsigned) sizeof(b), (unsigned) _Alignof(b), ((unsigned) &b) & 0xF); printf("bb: size = %u, alignment = %u, address mod 16 = %u\n", (unsigned) sizeof(bb), (unsigned) _Alignof(bb), ((unsigned) &bb) & 0xF); #if 0 printf("bbb: size = %u, alignment = %u, address mod 16 = %u\n", (unsigned) sizeof(bbb), (unsigned) _Alignof(bbb), ((unsigned) &bbb) & 0xF); #endif printf("c: size = %u, alignment = %u, address mod 16 = %u\n", (unsigned) sizeof(c), (unsigned) _Alignof(c), ((unsigned) &c) & 0xF); printf("d: size = %u, alignment = %u, address mod 64 = %u\n", (unsigned) sizeof(d), (unsigned) _Alignof(d), ((unsigned) &d) & 0x3F); printf("e: size = %u, alignment = %u, address mod 16 = %u\n", (unsigned) sizeof(e), (unsigned) _Alignof(e), ((unsigned) &e) & 0xF); printf("f: size = %u, alignment = %u, address mod 32 = %u\n", (unsigned) sizeof(f), (unsigned) _Alignof(f), ((unsigned) &f) & 0x1F); printf("g: size = %u, alignment = %u, address mod 16 = %u\n", (unsigned) sizeof(g), (unsigned) _Alignof(g), ((unsigned) &g) & 0xF); printf("h: size = %u, alignment = %u, address mod 64 = %u\n", (unsigned) sizeof(h), (unsigned) _Alignof(h), ((unsigned) &h) & 0x3F); return 0; }
/** * Return the native host alignment for values of @p type. * * @param type The type to query. */ size_t bhnd_nvram_type_host_align(bhnd_nvram_type type) { switch (type) { case BHND_NVRAM_TYPE_CHAR: case BHND_NVRAM_TYPE_CHAR_ARRAY: case BHND_NVRAM_TYPE_DATA: case BHND_NVRAM_TYPE_STRING: case BHND_NVRAM_TYPE_STRING_ARRAY: return (_Alignof(uint8_t)); case BHND_NVRAM_TYPE_BOOL: case BHND_NVRAM_TYPE_BOOL_ARRAY: { _Static_assert(sizeof(bhnd_nvram_bool_t) == sizeof(uint8_t), "bhnd_nvram_bool_t must be uint8-representable"); return (_Alignof(uint8_t)); } case BHND_NVRAM_TYPE_NULL: return (1); case BHND_NVRAM_TYPE_UINT8: case BHND_NVRAM_TYPE_UINT8_ARRAY: return (_Alignof(uint8_t)); case BHND_NVRAM_TYPE_UINT16: case BHND_NVRAM_TYPE_UINT16_ARRAY: return (_Alignof(uint16_t)); case BHND_NVRAM_TYPE_UINT32: case BHND_NVRAM_TYPE_UINT32_ARRAY: return (_Alignof(uint32_t)); case BHND_NVRAM_TYPE_UINT64: case BHND_NVRAM_TYPE_UINT64_ARRAY: return (_Alignof(uint64_t)); case BHND_NVRAM_TYPE_INT8: case BHND_NVRAM_TYPE_INT8_ARRAY: return (_Alignof(int8_t)); case BHND_NVRAM_TYPE_INT16: case BHND_NVRAM_TYPE_INT16_ARRAY: return (_Alignof(int16_t)); case BHND_NVRAM_TYPE_INT32: case BHND_NVRAM_TYPE_INT32_ARRAY: return (_Alignof(int32_t)); case BHND_NVRAM_TYPE_INT64: case BHND_NVRAM_TYPE_INT64_ARRAY: return (_Alignof(int64_t)); } /* Quiesce gcc4.2 */ BHND_NV_PANIC("bhnd nvram type %u unknown", type); }
int cloudabi32_sys_thread_create(struct thread *td, struct cloudabi32_sys_thread_create_args *uap) { struct thread_create_args args; int error; error = copyin(uap->attr, &args.attr, sizeof(args.attr)); if (error != 0) return (error); /* Remove some space on the top of the stack for the TCB. */ args.tcb = rounddown(args.attr.stack + args.attr.stack_size - sizeof(cloudabi32_tcb_t), _Alignof(cloudabi32_tcb_t)); args.attr.stack_size = args.tcb - args.attr.stack; error = thread_create(td, NULL, initialize_thread, &args); if (error != 0) return (error); td->td_retval[0] = args.tid; return (0); }
/* Test C1X alignment support. Test invalid code. */ /* { dg-do compile } */ /* { dg-options "-std=c1x -pedantic-errors" } */ int a = _Alignof (void (void)); /* { dg-error "function" } */ struct s; int b = _Alignof (struct s); /* { dg-error "incomplete" } */ int c = _Alignof (void); /* { dg-error "void" } */ int d = _Alignof (a); /* { dg-error "expression" } */ _Alignas (void (void)) char e; /* { dg-error "function" } */ _Alignas (struct s) char f; /* { dg-error "incomplete" } */ _Alignas (void) char g; /* { dg-error "void" } */ _Alignas (-__INT_MAX__-1) char h; /* { dg-error "too large|power of 2" } */ _Alignas (-__INT_MAX__) char h2; /* { dg-error "too large|power of 2" } */ _Alignas ((-__INT_MAX__-1)/2) char h3; /* { dg-error "too large|power of 2" } */ _Alignas ((-__INT_MAX__-1)/4) char h4; /* { dg-error "too large|power of 2" } */ _Alignas ((-__INT_MAX__-1)/8) char h5; /* { dg-error "too large|power of 2" } */ _Alignas (-__LONG_LONG_MAX__-1) char i; /* { dg-error "too large|power of 2" } */ _Alignas (-(__LONG_LONG_MAX__-1)/2) char i2; /* { dg-error "too large|power of 2" } */ _Alignas (-(__LONG_LONG_MAX__-1)/4) char i3; /* { dg-error "too large|power of 2" } */ _Alignas (-(__LONG_LONG_MAX__-1)/8) char i4; /* { dg-error "too large|power of 2" } */ _Alignas (-(__LONG_LONG_MAX__-1)/16) char i5; /* { dg-error "too large|power of 2" } */ _Alignas (-1) char j; /* { dg-error "power of 2" } */ _Alignas (3) char k; /* { dg-error "power of 2" } */ _Alignas ((void *) 1) char k; /* { dg-error "integer constant" } */ int x; _Alignas (x) char l; /* { dg-error "integer constant" } */
// RUNNOT: %clang_cc1 -fsyntax-only -verify %s -DALIGNOF -DOMITTEDOPERAND -Wgnu-conditional-omitted-operand // RUNNOT: %clang_cc1 -fsyntax-only -verify %s -DALIGNOF -DEMPTYINIT -Wgnu-empty-initializer // RUNNOT: %clang_cc1 -fsyntax-only -verify %s -DALIGNOF -DLABELVALUE -Wgnu-label-as-value // RUNNOT: %clang_cc1 -fsyntax-only -verify %s -DALIGNOF -DSTATEMENTEXP -Wgnu-statement-expression #if NONE // expected-no-diagnostics #endif #if ALL || ALIGNOF // expected-warning@+4 {{'_Alignof' applied to an expression is a GNU extension}} #endif char align; _Static_assert(_Alignof(align) > 0, "align's alignment is wrong"); #if ALL || CASERANGE // expected-warning@+5 {{use of GNU case range extension}} #endif void caserange(int x) { switch (x) { case 42 ... 44: ; } } #if ALL || COMPLEXINT // expected-warning@+3 {{complex integer types are a GNU extension}}
static int do_multiple_queries(bool do_update, bool use_cache, h2o_req_t *req) { thread_context_t * const ctx = H2O_STRUCT_FROM_MEMBER(thread_context_t, event_loop.h2o_ctx, req->conn->ctx); const size_t num_query = get_query_number(req); // MAX_QUERIES is a relatively small number, so assume no overflow in the following // arithmetic operations. assert(num_query <= MAX_QUERIES); size_t base_size = offsetof(multiple_query_ctx_t, res) + num_query * sizeof(query_result_t); base_size = ((base_size + _Alignof(query_param_t) - 1) / _Alignof(query_param_t)); base_size = base_size * _Alignof(query_param_t); const size_t num_query_in_progress = MIN(num_query, ctx->config->max_db_conn_num); size_t sz = base_size + num_query_in_progress * sizeof(query_param_t); if (do_update) { const size_t reuse_size = (num_query_in_progress - 1) * sizeof(query_param_t); const size_t update_query_len = MAX_UPDATE_QUERY_LEN(num_query); if (update_query_len > reuse_size) sz += update_query_len - reuse_size; } multiple_query_ctx_t * const query_ctx = calloc(1, sz); if (query_ctx) { multiple_query_ctx_t ** const p = h2o_mem_alloc_shared(&req->pool, sizeof(*p), cleanup_multiple_query_request); *p = query_ctx; query_ctx->ctx = ctx; query_ctx->num_query = num_query; query_ctx->req = req; query_ctx->do_update = do_update; query_ctx->use_cache = use_cache; query_ctx->query_param = (query_param_t *) ((char *) query_ctx + base_size); initialize_ids(num_query, query_ctx->res, &ctx->random_seed); if (use_cache) { fetch_from_cache(h2o_now(ctx->event_loop.h2o_ctx.loop), &ctx->global_data->world_cache, query_ctx); if (query_ctx->num_result == query_ctx->num_query) { complete_multiple_query(query_ctx); return 0; } } query_ctx->num_query_in_progress = MIN(num_query_in_progress, query_ctx->num_query - query_ctx->num_result); for (size_t i = 0; i < query_ctx->num_query_in_progress; i++) { query_ctx->query_param[i].ctx = query_ctx; // We need a copy of id because the original may be overwritten // by a completed query. query_ctx->query_param[i].id = htonl(query_ctx->res[query_ctx->num_result + i].id); query_ctx->query_param[i].id_format = 1; query_ctx->query_param[i].id_len = sizeof(query_ctx->query_param[i].id); query_ctx->query_param[i].id_pointer = (const char *) &query_ctx->query_param[i].id; query_ctx->query_param[i].param.command = WORLD_TABLE_NAME; query_ctx->query_param[i].param.nParams = 1; query_ctx->query_param[i].param.on_error = on_multiple_query_error; query_ctx->query_param[i].param.on_result = on_multiple_query_result; query_ctx->query_param[i].param.on_timeout = on_multiple_query_timeout; query_ctx->query_param[i].param.paramFormats = &query_ctx->query_param[i].id_format; query_ctx->query_param[i].param.paramLengths = &query_ctx->query_param[i].id_len; query_ctx->query_param[i].param.paramValues = &query_ctx->query_param[i].id_pointer; query_ctx->query_param[i].param.flags = IS_PREPARED; query_ctx->query_param[i].param.resultFormat = 1; if (execute_query(ctx, &query_ctx->query_param[i].param)) { query_ctx->num_query_in_progress = i; query_ctx->cleanup = true; send_service_unavailable_error(DB_REQ_ERROR, req); return 0; } } } else send_error(INTERNAL_SERVER_ERROR, REQ_ERROR, req); return 0; }
// RUN: %check %s -Wgnu int x; int y = _Alignof(x); // CHECK: warning: _Alignof applied to expression is a GNU extension int z = _Alignof(int){0}; // CHECK: warning: _Alignof applied to expression is a GNU extension
#include <stdint.h> #include "avl.h" #ifndef __has_feature #define __has_feature(x) 0 #endif #if __STDC_VERSION__ >= 201112L || (__has_feature(c_static_assert) && __has_feature(c_alignof)) _Static_assert(_Alignof(avl_node_t) >= 4, "Incompatible avl_node_t alignment"); #elif __GNUC__ typedef char static_assertion_avl_node_t_alignment[!!(__alignof__(avl_node_t)>4)*2-1]; #else typedef char static_assertion_avl_node_t_alignment[!!(offsetof(struct {char c;avl_node_t t;}, t)>4)*2-1]; #endif static int avl_abs(int n) { int mask = n >> ((sizeof(int) * 8) - 1); return (mask + n) ^ mask; } static inline int avl_max(int lhs, int rhs) { return lhs > rhs ? lhs : rhs; } static inline void avl_set_parent(avl_node_t *node, avl_node_t *parent) { node->parent = (uintptr_t)parent | (node->parent & 3); } static inline avl_node_t *avl_get_parent(const avl_node_t *const node) { return (avl_node_t*)(node->parent & ~3); }
// RUN: %layout_check %s typedef int _AlignT; enum { _Len = sizeof(int), _Align = _Alignof(_AlignT), }; union U1 { unsigned char __data[_Len]; struct __attribute__((__aligned__((_Align)))) S1 { } __align; } s1; union U2 { unsigned char __data[_Len]; _Alignas(_Align) struct S2 { } __align; } s2; union U3 { unsigned char __data[_Len]; _Alignas(__alignof(_AlignT)) struct S3 { } __align; } s3; union U4 { unsigned char __data[_Len]; _Alignas(_AlignT) struct S4 { } __align; } s4;