[Swan-dev] Use of "struct chunk", "chunk_t", or "chunk_t *" or ...?

D. Hugh Redelmeier hugh at mimosa.com
Fri Dec 19 21:48:13 EET 2014


| From: Andrew Cagney <andrew.cagney at gmail.com>

| I'm looking to change the "dat/datasize" and "Iv" parameters in:
| 
|         void (*do_crypt)(u_int8_t *dat,
|                          size_t datasize,
|                          PK11SymKey *key,
|                          u_int8_t *iv,
|                          bool enc);
| 
| to chunks.  While the first is somewhat cosmetic, the second ("iv") is
| motivated by a bug - for aes_ctr the caller/callee didn't agree on the
| length of the parameter named "iv" (ctr calls it counter-block).

Generally that sounds good to me (without reading the code).  Unless
the called routines are from some other source and we want to minimize
divergence from upstream.

Mind you, I don't think that we do any tracking of upstream.  That's a
bug not a feature.

====

I like making pointer argument types pointers to const where possible.
This makes it clear whether the function can change the thing the
pointer points to.

C has slight genericity in this regard: you can pass a pointer to
non-const for an argument that is declared as pointer to const.  That
means we don't need two routines: one for pointer to non-const and one
for pointer-to-const.

Unfortunately, that genericity does "look into" structs.  So all
chunks have u_char *ptr (no const).  This is one reason to not use
chunks.  Not a veto, a reason.

====

I don't like having to bundle up a pointer and a length into a chunk_t
just for one call.  I like to use chunks if they actually cut down the
ammount of code.

| To that end I've a few questions:
| 
| - is chunk_t or "struct chunk" prefered?
| I'm guessing chunk_t but that seems to contradict Chapter 5: Typedefs  in
| https://www.kernel.org/doc/Documentation/CodingStyle :-)

chunk_t.  I think that we don't have to slavishly follow kernel style.

| - for a parameter, is pass by value (struct chunk) or reference
| prefered (struct chunk *)?

I'm not sure that there is consistency in the existing code.  Perhaps
that's why you ask.

If it is just being passed along, obviously a chunk_t * is cheaper.


More information about the Swan-dev mailing list