]> git.ipfire.org Git - thirdparty/valgrind.git/commitdiff
Added VG_(getcwd_alloc)(), which is much easier to use than VG_(getcwd)().
authorNicholas Nethercote <njn@valgrind.org>
Tue, 30 Sep 2003 13:51:23 +0000 (13:51 +0000)
committerNicholas Nethercote <njn@valgrind.org>
Tue, 30 Sep 2003 13:51:23 +0000 (13:51 +0000)
(getcwd() is really a pretty stupid syscall)

git-svn-id: svn://svn.valgrind.org/valgrind/trunk@1867

cachegrind/cg_main.c
coregrind/vg_mylibc.c
include/vg_skin.h

index 87756e1327c8c1ae4d3eeb24126bb30949d520ff..1e584c7958a54839787a18b341e3575d3447a294 100644 (file)
@@ -2022,7 +2022,6 @@ void SK_(print_debug_usage)(void)
 
 void SK_(pre_clo_init)(void)
 {
-   UInt  buf_size = 100;
    Char* base_dir = NULL;
    
    VG_(details_name)            ("Cachegrind");
@@ -2043,17 +2042,14 @@ void SK_(pre_clo_init)(void)
    VG_(register_compact_helper)((Addr) & log_0I_2D_cache_access);
    VG_(register_compact_helper)((Addr) & log_1I_2D_cache_access);
 
-   /* getcwd() fails if the buffer isn't big enough -- keep doubling size
-      until it succeeds. */
-   while (NULL == base_dir) {
-      base_dir = VG_(malloc)(buf_size);
-      if (NULL == VG_(getcwd)(base_dir, buf_size))
-         buf_size *= 2;
-   }
+   /* Get working directory */
+   sk_assert( VG_(getcwd_alloc)(&base_dir) );
+
    /* Block is big enough for dir name + cachegrind.out.<pid> */
    cachegrind_out_file = VG_(malloc)((VG_(strlen)(base_dir) + 32)*sizeof(Char));
    VG_(sprintf)(cachegrind_out_file, "%s/cachegrind.out.%d",
                 base_dir, VG_(getpid)());
+   VG_(free)(base_dir);
 }
 
 void SK_(post_clo_init)(void)
index 7fbf9283c188d7ce75b38eaa0b6b3fb18f329c12..60e3b84fe9905bf328dce19be547c0819ea6f542 100644 (file)
@@ -1212,6 +1212,25 @@ Char* VG_(getcwd) ( Char* buf, Int size )
    return VG_(is_kerror)(res) ? ((Char*)NULL) : (Char*)res;
 }
 
+/* Alternative version that does allocate the memory.  Easier to use. */
+Bool VG_(getcwd_alloc) ( Char** out )
+{
+   UInt size = 4;
+
+   *out = NULL;
+   while (True) {
+      *out = VG_(malloc)(size);
+      if (NULL == VG_(getcwd)(*out, size)) {
+         VG_(free)(*out);
+         if (size > 65535)
+            return False;
+         size *= 2;
+      } else {
+         return True;
+      }
+   }
+}
+
 
 /* ---------------------------------------------------------------------
    Misc functions looking for a proper home.
index 34370a9c000417c97a075ff64ff1e8e20c42b520..d2cae54be2a997217330cee9e97f6716d847cd8d 100644 (file)
@@ -428,6 +428,10 @@ extern Int  VG_(stat)   ( Char* file_name, struct vki_stat* buf );
 
 extern Char* VG_(getcwd) ( Char* buf, Int size );
 
+/* Easier to use than VG_(getcwd)() -- does the buffer fiddling itself.
+   String put into 'cwd' is VG_(malloc)'d, and should be VG_(free)'d.
+   Returns False if it fails.  Will fail if the pathname is > 65535 bytes. */
+extern Bool VG_(getcwd_alloc) ( Char** cwd );
 
 /* ------------------------------------------------------------------ */
 /* assert.h */