[Date Prev][Date Next] [Chronological] [Thread] [Top]

Re: An optimization for another day (and developer)



At 07:05 AM 2001-12-28, Pierangelo Masarati wrote:
>> BTW, one optimization I intended to implement but haven't
>> the time is to allow:
>>       struct berval bv = { 0, NULL }
>>       struct berval *bvp = &bv;
>> 
>>       rc = dnPretty( NULL, &dn, &bvp );
>>       rc = dnNormalize( NULL, &dn, &bvp );
>> 
>> That is, allow call to provide, optionally, a
>> pre-allocated struct berval instead of relying
>> on the caller to malloc on up.  Of course, in
>> some cases, the existing behavior is preferred.
>
>I note that relying on developers (I speak mostly of myself :)
>to supply a NULL pointer if alloc is required or non-NULL pointer
>if it can be safely used is dangerous: no more than five minutes
>ago I fixed this bug in back-monitor.  I'd rather use a alloc/use
>flag in an ancillary function, or radically change the API
>to accept only pre-allocated structs, which wouldn't be a big issue 
>since the big allocation takes place for the bv_val, not for the 
>struct berval.

Yes, that's a consideration.  One approach is to have two
prototypes, one expecting a struct berval *, the other a
struct berval **... with the latter being a wrapper which
did the allocation before calling the former...

>Of course, by using appropriate safety checks and tools your
>suggestion would provide an interesting optimization.

I would (if done the way I originally intended) replace the
assert( *bvp == NULL ) in the current code with:
  if (*bvp) assert( (*bvp)->bv_len == 0 && (*bvp)->bv_val == NULL );

This will catch (at run time) many abuses.

Kurt