[llvm-commits] [llvm] r101781 - /llvm/trunk/lib/System/Unix/Path.inc

Chris Lattner clattner at apple.com
Mon Apr 19 11:22:51 PDT 2010


On Apr 19, 2010, at 8:54 AM, Dan Gohman wrote:

> Author: djg
> Date: Mon Apr 19 10:54:44 2010
> New Revision: 101781
> 
> URL: http://llvm.org/viewvc/llvm-project?rev=101781&view=rev
> Log:
> Revert 91528 and use a std::vector instead, fixing an abuse of std::string.

How is std::vector better than std::string here?

-Chris

> 
> Modified:
>    llvm/trunk/lib/System/Unix/Path.inc
> 
> Modified: llvm/trunk/lib/System/Unix/Path.inc
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/System/Unix/Path.inc?rev=101781&r1=101780&r2=101781&view=diff
> ==============================================================================
> --- llvm/trunk/lib/System/Unix/Path.inc (original)
> +++ llvm/trunk/lib/System/Unix/Path.inc Mon Apr 19 10:54:44 2010
> @@ -16,6 +16,7 @@
> //===          is guaranteed to work on *all* UNIX variants.
> //===----------------------------------------------------------------------===//
> 
> +#include "llvm/ADT/SmallVector.h"
> #include "Unix.h"
> #if HAVE_SYS_STAT_H
> #include <sys/stat.h>
> @@ -858,15 +859,20 @@
> 
>   // Append an XXXXXX pattern to the end of the file for use with mkstemp,
>   // mktemp or our own implementation.
> -  std::string Buf(path);
> +  // This uses std::vector instead of SmallVector to avoid a dependence on
> +  // libSupport. And performance isn't critical here.
> +  std::vector<char> Buf;
> +  Buf.resize(path.size()+8);
> +  char *FNBuffer = Buf.data();
> +    path.copy(FNBuffer,path.size());
>   if (isDirectory())
> -    Buf += "/XXXXXX";
> +    strcpy(FNBuffer+path.size(), "/XXXXXX");
>   else
> -    Buf += "-XXXXXX";
> +    strcpy(FNBuffer+path.size(), "-XXXXXX");
> 
> #if defined(HAVE_MKSTEMP)
>   int TempFD;
> -  if ((TempFD = mkstemp((char*)Buf.c_str())) == -1)
> +  if ((TempFD = mkstemp(FNBuffer)) == -1)
>     return MakeErrMsg(ErrMsg, path + ": can't make unique filename");
> 
>   // We don't need to hold the temp file descriptor... we will trust that no one
> @@ -874,21 +880,21 @@
>   close(TempFD);
> 
>   // Save the name
> -  path = Buf;
> +  path = FNBuffer;
> #elif defined(HAVE_MKTEMP)
>   // If we don't have mkstemp, use the old and obsolete mktemp function.
> -  if (mktemp(Buf.c_str()) == 0)
> +  if (mktemp(FNBuffer) == 0)
>     return MakeErrMsg(ErrMsg, path + ": can't make unique filename");
> 
>   // Save the name
> -  path = Buf;
> +  path = FNBuffer;
> #else
>   // Okay, looks like we have to do it all by our lonesome.
>   static unsigned FCounter = 0;
>   unsigned offset = path.size() + 1;
> -  while (FCounter < 999999 && exists()) {
> -    sprintf(Buf.data()+offset, "%06u", ++FCounter);
> -    path = Buf;
> +  while ( FCounter < 999999 && exists()) {
> +    sprintf(FNBuffer+offset,"%06u",++FCounter);
> +    path = FNBuffer;
>   }
>   if (FCounter > 999999)
>     return MakeErrMsg(ErrMsg,
> 
> 
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits





More information about the llvm-commits mailing list