Little conveniences

ghudson@MIT.EDU ghudson at MIT.EDU
Thu Oct 1 11:22:38 EDT 2009

As part of the encryption performance work, I'm finding myself dearly
wanting a couple of internal conveniences.  So, these are likely to
hit k5-int.h in the trunk in the next few weeks.  First:

  static inline void zapfree(void *ptr, size_t len)
      if (ptr) {
          zap(ptr, len);

This builds on the existing zap() macro or function.  Particularly in
the crypto code, it avoids the need for a lot of repeated four-line


  /* Allocate zeroed memory; set *code to 0 on success or ENOMEM on failure. */
  static inline void *k5alloc(size_t size, krb5_error_code *code)
      void *ptr;

      ptr = calloc(size, 1);
      *code = (ptr == NULL) ? ENOMEM : 0;
      return ptr;

This is to save three lines in the (very common) allocate/check/error
pattern used all over the code.  With k5alloc that can be three lines
instead of five:

  ptr = k5alloc(size, &ret);
  if (!ptr)
      goto cleanup;

The downside is that everyone knows what malloc() does, but not
everyone will immediately know what k5alloc does.  It also uses
krb5_error_code in a funny way, since the more common construction
("ret = k5alloc(size, &ptr);") can't be written as a C function due to
the way C void pointers work.  So I'm not 100% pleased with this one.
But the alternative is to spend an awful lot of code space when
allocating multiple chunks of memory, as is commonly done in the
crypto code.

More information about the krbdev mailing list