On Tue, 2012-01-17 at 11:05 +0100, Samuel Thibault wrote: > Svante Signell, le Tue 17 Jan 2012 10:52:47 +0100, a écrit :
New version of openbsd_basename_new.c attached. > In addition to that, it might be better for performance to rather > remember how much memory was previously allocated, and only reallocate > if more memory is needed, and twice as much, so as to reduce the amount > of free/malloc calls. I don't think this kind of optimization is needed for the libtar application. It definitely complicates the code. valgrind output below (./test_base+dirname): ==14515== ==14515== HEAP SUMMARY: ==14515== in use at exit: 4 bytes in 2 blocks ==14515== total heap usage: 8 allocs, 6 frees, 49 bytes allocated ==14515== ==14515== LEAK SUMMARY: ==14515== definitely lost: 0 bytes in 0 blocks ==14515== indirectly lost: 0 bytes in 0 blocks ==14515== possibly lost: 0 bytes in 0 blocks ==14515== still reachable: 4 bytes in 2 blocks ==14515== suppressed: 0 bytes in 0 blocks ==14515== Rerun with --leak-check=full to see details of leaked memory ==14515== ==14515== For counts of detected and suppressed errors, rerun with: -v ==14515== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 4 from 4)
/* $OpenBSD: basename.c,v 1.4 1999/05/30 17:10:30 espie Exp $ */ /* * Copyright (c) 1997 Todd C. Miller <[email protected]> * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifndef lint static char rcsid[] = "$OpenBSD: basename.c,v 1.4 1999/05/30 17:10:30 espie Exp $"; #endif /* not lint */ #include <errno.h> #include <string.h> #include <sys/param.h> #include <stdlib.h> char * openbsd_basename_new(path) const char *path; { static char *bname = NULL; register const char *endp, *startp; int len = 0; if (bname != NULL) free(bname); /* Empty or NULL string gets treated as "." */ if (path == NULL || *path == '\0') { bname = strdup("."); return(bname); } /* Strip trailing slashes */ endp = path + len - 1; while (endp > path && *endp == '/') endp--; /* All slashes becomes "/" */ if (endp == path && *endp == '/') { bname = strdup("/"); return(bname); } /* Find the start of the base */ startp = endp; while (startp > path && *(startp - 1) != '/') startp--; len = endp - startp + 1; bname = malloc(len + 1); (void)strncpy(bname, startp, len); bname[len] = '\0'; return(bname); }

