mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Joe Perches <joe@perches.com>
To: Jim Cromie <jim.cromie@gmail.com>
Cc: linux-kernel@vger.kernel.org
Subject: Re: [00/02] add BUILD_BUG_DECL assertion (for 3.4??)
Date: Sun, 08 Apr 2012 15:52:15 -0700	[thread overview]
Message-ID: <1333925535.2508.15.camel@joe2Laptop> (raw)
In-Reply-To: <1333924698-3894-1-git-send-email-jim.cromie@gmail.com>

On Sun, 2012-04-08 at 16:38 -0600, Jim Cromie wrote:

> this patch (0001) adds new bug.h macro, BUILD_BUG_DECL(name, cond),
> which unlike other *BUG* assertions is usable at file scope.  Its
> primary purpose is to enforce identical sizes of 2 separate arrays,
> which but for considerations of packing/padding/section, would be
> together in a struct.
> 
> const char const *names[] = { "bart", "lisa", "homer", "marge" };
> int a[] = {1,2,3,4};
> int b[] = {1,2,3,5};
> long d[] = {1,2};
> 
> BUILD_BUG_DECL(foo, ARRAY_SIZE(a) != ARRAY_SIZE(b));
> BUILD_BUG_DECL(buz, sizeof(a) != sizeof(b));	// good
> BUILD_BUG_DECL(a, sizeof(a) != sizeof(d));	// ok on x32, error x64
> BUILD_BUG_DECL(b, ARRAY_SIZE(a) != ARRAY_SIZE(names));	// good
> 
> macro expands as:
> static __attribute__ ((__section__(".init.data"))) struct {
>        int BUILD_BUG_DECL_buz[1 - 2*!!(sizeof(a) != sizeof(b))];
> } BUILD_BUG_DECL_buz[0] __attribute__((unused));
> 

If possible, it might be better to wrap the
declarations themselves in a macro that ensures
the sizes are the same.

Something like:

declare_same_size_arrays(
	typeof array1[] = {...},
	typeof array2[] = {...}
);



  parent reply	other threads:[~2012-04-08 22:52 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-04-08 22:38 Jim Cromie
2012-04-08 22:38 ` [PATCH 1/2] bug.h: add BUILD_BUG_DECL, usable at file scope Jim Cromie
2012-04-08 22:38 ` =?y?q?=5BPATCH=202/2=5D=20bug=2Eh=3A=20add=20test/demo=20module?= Jim Cromie
2012-04-08 22:52 ` Joe Perches [this message]
2012-04-08 23:59   ` [00/02] add BUILD_BUG_DECL assertion (for 3.4??) Jim Cromie
2012-04-09  2:37     ` Joe Perches
2012-04-09 19:52       ` Jim Cromie
2012-04-09 20:08         ` Joe Perches

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1333925535.2508.15.camel@joe2Laptop \
    --to=joe@perches.com \
    --cc=jim.cromie@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®