code for 1.9.19alphas

Luke Kenneth Casson Leighton lkcl at regent.push.net
Thu Apr 16 22:56:24 GMT 1998


On Thu, 16 Apr 1998, Christopher R. Hertel wrote:

> Ooops.  Fingers slipped.
> 
> What I meant to say was...
> 
> > 
> > ok, further comments (this is posted to samba-technical, too).
> > 
> > instead of a separate free_xxx function, add an extra BOOL parameter to
> > make_xxxx.
> > 
> > if True, malloc.  if False, free.  call other make_xxxx sub-functions with
> > the extra BOOL parameter too.
> 
> Eeeuuwww.  Yech.  Nasty.
> 
> > reason: this will keep mallocs and frees in the same place.  this is
> > important so that future development doesn't get out of step (resulting in
> > loss of memory stuff).
> 
> Mallocs and frees should be in the same file, but as complimentary 
> calls.  Okay, this is *principle*, and practice sometimes strays, but 
> it's generally a bad idea to overload a single function.  A function 
> should perform *a function*.

offset that against the potential nasty of people writing two identical
functions (effectively) except that one does a free, the other does a
malloc.

example:

make_some_container(SOME_CTR *ctr)
{
	prs_uint16(ctr->first_uint16);
	prs_uint16(ctr->2nd_uint16  );

	make_unistr2(ctr->first_string);
	make_unistr2(ctr->2nd_string);
}

cut-and-paste to produce totally stupidly identical free_xxx equivalent:

free_some_container(SOME_CTR *ctr)
{
	prs_uint16(ctr->first_uint16);
	prs_uint16(ctr->2nd_uint16  );

	free_unistr2(ctr->first_string);
	free_unistr2(ctr->2nd_string);
}

and:

make_unistr2(UNISTR2 *str)
{
	malloc(str->buffer, len);
}

free_unistr2(UNISTR2 *str)
{
	free(str->buffer, len);
}

why bother?  a bit of work with sed will turn this:

make_some_container(SOME_CTR *ctr)
{
	prs_uint16(ctr->first_uint16);
	prs_uint16(ctr->2nd_uint16  );

	make_unistr2(ctr->first_string);
	make_unistr2(ctr->2nd_string);
}

into this:

make_some_container(BOOL alloc, SOME_CTR *ctr)
{
	prs_uint16(ctr->first_uint16);
	prs_uint16(ctr->2nd_uint16  );

	make_unistr2(alloc, ctr->first_string);
	make_unistr2(alloc, ctr->2nd_string);
}

make_unistr2(BOOL alloc, UNISTR2 *str)
{
	if (alloc)
	{
		malloc(str->buffer, len);
	}
	else
	{
		free(str->buffer, len);
	}
}

see?


[you have _no_ idea how difficult it was to write that using pine's
editor:  my fingers kept typing things like yypd$iBOOL alloc, ^[ and
stuff!]

luke




More information about the samba-technical mailing list