[PATCH 1/3] cli: add insert --must-index option

Tomi Ollila tomi.ollila at iki.fi
Thu Oct 24 03:19:22 PDT 2013


On Thu, Oct 24 2013, David Bremner <david at tethera.net> wrote:

> Austin Clements <amdragon at MIT.EDU> writes:
>
>> And in libnotmuch, we would do something like
>>
>> notmuch_status_t
>> notmuch_database_open (const char *path,
>>                        notmuch_database_mode_t mode,
>>                        const notmuch_options_t *options,
>>                        notmuch_database_t **database)
>> {
>>     notmuch_option_t real_options = NOTMUCH_OPTIONS_INIT;
>>     if (real_options.options_length < options.options_length)
>>         return error;
>>     memmove(&real_options, options, options.options_length);
>>     // ...
>> }
>>
>
> Does the C standard guarantee that if two structs have the same initial
> set of members, that they are aligned in a compatible way? I suppose it
> must work, but I'm still curious.

If we have structures

struct s1 { 
       long l1;
       void * vp1;
       short s1;
       int i1;
       char c1;
}

&

struct s2 { 
       long l1;
       void * vp1;
       short s1;
       int i1;
       char c1;
       char c2;
       // void * vp2;
};

The variables l1, vp1, s1, i1 & c1 have same offset from the beginning of
structure and uses same amount of space in both structures...

... now the interesting point is what is the size of the structures (does C
compiler add padding to the end (to make the size multiple of something ?)
In any case all the other data is zeroed in the structure and sizeof data
is copied to "real" structure if Austin's suggestion is used...

> Yet another approach would be to pass in array of descriptors, something
> like the command line argument parsing code does now.
>
> libnotmuch_opt_desc_t options[] = {
>   { LIBNOTMUCH_OPT_LOGHOOK, loghook },
>   { 0, 0} 
> }
>
> I guess passing a (void *) as the second element of the pair?

Voidp and only that. Then we can hack other variables using
((char *)0 + (intvar)) or GINT_TO_POINTER... -- beautiful, eh ? ;)

> Of course it's a bit more work to unpack this way.

That is negligible to the elegance loss -- and what about bindings ! >;)


Tomi


More information about the notmuch mailing list