void SK_(pre_clo_init)(void)
{
- UInt buf_size = 100;
Char* base_dir = NULL;
VG_(details_name) ("Cachegrind");
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)
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.
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 */