register_ptr (void *ptr, const char *file, int line)
{
int i;
- for (i = 0; i < ARRAY_SIZE (malloc_debug); i++)
+ for (i = 0; i < countof (malloc_debug); i++)
if (malloc_debug[i].ptr == NULL)
{
malloc_debug[i].ptr = ptr;
unregister_ptr (void *ptr)
{
int i;
- for (i = 0; i < ARRAY_SIZE (malloc_debug); i++)
+ for (i = 0; i < countof (malloc_debug); i++)
if (malloc_debug[i].ptr == ptr)
{
malloc_debug[i].ptr = NULL;
int i;
printf ("\nMalloc: %d\nFree: %d\nBalance: %d\n\n",
malloc_count, free_count, malloc_count - free_count);
- for (i = 0; i < ARRAY_SIZE (malloc_debug); i++)
+ for (i = 0; i < countof (malloc_debug); i++)
if (malloc_debug[i].ptr != NULL)
printf ("0x%08ld: %s:%d\n", (long)malloc_debug[i].ptr,
malloc_debug[i].file, malloc_debug[i].line);
if (!opt.lfilename)
{
- opt.lfilename = unique_name (DEFAULT_LOGFILE);
+ opt.lfilename = unique_name (DEFAULT_LOGFILE, 0);
changedp = 1;
}
pid = fork ();
return size;
}
-/* Return a unique filename, given a prefix and count */
+/* stat file names named PREFIX.1, PREFIX.2, etc., until one that
+ doesn't exist is found. Return a freshly allocated copy of the
+ unused file name. */
+
static char *
-unique_name_1 (const char *fileprefix, int count)
+unique_name_1 (const char *prefix)
{
- char *filename;
+ int count = 1;
+ int plen = strlen (prefix);
+ char *template = (char *)alloca (plen + 1 + 24);
+ char *template_tail = template + plen;
- if (count)
- {
- filename = (char *)xmalloc (strlen (fileprefix) + numdigit (count) + 2);
- sprintf (filename, "%s.%d", fileprefix, count);
- }
- else
- filename = xstrdup (fileprefix);
+ memcpy (template, prefix, plen);
+ *template_tail++ = '.';
- if (!file_exists_p (filename))
- return filename;
- else
- {
- xfree (filename);
- return NULL;
- }
+ do
+ number_to_string (template_tail, count++);
+ while (file_exists_p (template));
+
+ return xstrdup (template);
}
-/* Return a unique file name, based on PREFIX. */
+/* Return a unique file name, based on FILE.
+
+ More precisely, if FILE doesn't exist, it is returned unmodified.
+ If not, FILE.1 is tried, then FILE.2, etc. The first FILE.<number>
+ file name that doesn't exist is returned.
+
+ The resulting file is not created, only verified that it didn't
+ exist at the point in time when the function was called.
+ Therefore, where security matters, don't rely that the file created
+ by this function exists until you open it with O_EXCL or
+ something.
+
+ If ALLOW_PASSTHROUGH is 0, it always returns a freshly allocated
+ string. Otherwise, it may return FILE if the file doesn't exist
+ (and therefore doesn't need changing). */
+
char *
-unique_name (const char *prefix)
+unique_name (const char *file, int allow_passthrough)
{
- char *file = NULL;
- int count = 0;
+ /* If the FILE itself doesn't exist, return it without
+ modification. */
+ if (!file_exists_p (file))
+ return allow_passthrough ? (char *)file : xstrdup (file);
- while (!file)
- file = unique_name_1 (prefix, count++);
- return file;
+ /* Otherwise, find a numeric suffix that results in unused file name
+ and return it. */
+ return unique_name_1 (file);
}
\f
/* Create DIRECTORY. If some of the pathname components of DIRECTORY
#endif
#ifdef WINDOWS
- return (double)(wst1->QuadPart - wst2->QuadPart) / 10000;
+ /* VC++ 6 doesn't support direct cast of uint64 to double. To work
+ around this, we subtract, then convert to signed, then finally to
+ double. */
+ return (double)(signed __int64)(wst1->QuadPart - wst2->QuadPart) / 10000;
#endif
}
return (int)bounded;
}
+/* Return a random uniformly distributed floating point number in the
+ [0, 1) range. The precision of returned numbers is 9 digits.
+
+ Modify this to use erand48() where available! */
+
+double
+random_float (void)
+{
+ /* We can't rely on any specific value of RAND_MAX, but I'm pretty
+ sure it's greater than 1000. */
+ int rnd1 = random_number (1000);
+ int rnd2 = random_number (1000);
+ int rnd3 = random_number (1000);
+ return rnd1 / 1000.0 + rnd2 / 1000000.0 + rnd3 / 1000000000.0;
+}
+
#if 0
/* A debugging function for checking whether an MD5 library works. */
cnt = 16;
while (cnt--)
{
- *p2++ = XDIGIT_TO_xchar (*p1 >> 4);
- *p2++ = XDIGIT_TO_xchar (*p1 & 0xf);
+ *p2++ = XNUM_TO_digit (*p1 >> 4);
+ *p2++ = XNUM_TO_digit (*p1 & 0xf);
++p1;
}
*p2 = '\0';
#endif /* USE_SIGNAL_TIMEOUT */
int
-run_with_timeout (long timeout, void (*fun) (void *), void *arg)
+run_with_timeout (double timeout, void (*fun) (void *), void *arg)
{
#ifndef USE_SIGNAL_TIMEOUT
fun (arg);
return 0;
}
+ /* Calling alarm() rounds TIMEOUT. If it is smaller than 1, round
+ it to 1, not to 0, because alarm(0) means "never deliver the
+ alarm", i.e. "wait forever", which is not what someone who
+ specifies a 0.5s timeout would expect. */
+ if (timeout < 1)
+ timeout = 1;
+
signal (SIGALRM, abort_run_with_timeout);
if (SETJMP (run_with_timeout_env) != 0)
{
signal (SIGALRM, SIG_DFL);
return 1;
}
- alarm (timeout);
+ alarm ((int) timeout);
fun (arg);
/* Preserve errno in case alarm() or signal() modifies it. */
return 0;
#endif
}
-