From 4e147a4eafb2e5d5543b57352b23a49a815cc0d5 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Tue, 30 Sep 2003 13:51:23 +0000 Subject: [PATCH] Added VG_(getcwd_alloc)(), which is much easier to use than VG_(getcwd)(). (getcwd() is really a pretty stupid syscall) git-svn-id: svn://svn.valgrind.org/valgrind/trunk@1867 --- cachegrind/cg_main.c | 12 ++++-------- coregrind/vg_mylibc.c | 19 +++++++++++++++++++ include/vg_skin.h | 4 ++++ 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/cachegrind/cg_main.c b/cachegrind/cg_main.c index 87756e132..1e584c795 100644 --- a/cachegrind/cg_main.c +++ b/cachegrind/cg_main.c @@ -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. */ 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) diff --git a/coregrind/vg_mylibc.c b/coregrind/vg_mylibc.c index 7fbf9283c..60e3b84fe 100644 --- a/coregrind/vg_mylibc.c +++ b/coregrind/vg_mylibc.c @@ -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. diff --git a/include/vg_skin.h b/include/vg_skin.h index 34370a9c0..d2cae54be 100644 --- a/include/vg_skin.h +++ b/include/vg_skin.h @@ -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 */