alloca: one step towards thread-safety
authorPaul Eggert <eggert@cs.ucla.edu>
Wed, 29 Dec 2010 04:41:30 +0000 (20:41 -0800)
committerPaul Eggert <eggert@cs.ucla.edu>
Wed, 29 Dec 2010 04:41:56 +0000 (20:41 -0800)
* lib/alloca.c (find_stack_direction): New arg PTR, to avoid the
need for a static variable.  All callers changed.  This does not
make the alloca replacement thread-safe, but it's one step.

ChangeLog
lib/alloca.c

index 2c9d4c7..5c95bc4 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,10 @@
 2010-12-28  Paul Eggert  <eggert@cs.ucla.edu>
 
+       alloca: one step towards thread-safety
+       * lib/alloca.c (find_stack_direction): New arg PTR, to avoid the
+       need for a static variable.  All callers changed.  This does not
+       make the alloca replacement thread-safe, but it's one step.
+
        tests: minor indenting change
        * tests/init.sh: Sync from coreutils housekeeping patch
        <http://lists.gnu.org/archive/html/coreutils/2010-12/msg00116.html>
index b652765..2f5e27e 100644 (file)
@@ -94,21 +94,20 @@ static int stack_dir;           /* 1 or -1 once known.  */
 #   define STACK_DIR    stack_dir
 
 static void
-find_stack_direction (void)
+find_stack_direction (char **ptr)
 {
-  static char *addr = NULL;     /* Address of first `dummy', once known.  */
   auto char dummy;              /* To get stack address.  */
 
-  if (addr == NULL)
+  if (*ptr == NULL)
     {                           /* Initial entry.  */
-      addr = ADDRESS_FUNCTION (dummy);
+      *ptr = ADDRESS_FUNCTION (dummy);
 
-      find_stack_direction ();  /* Recurse once.  */
+      find_stack_direction (ptr);  /* Recurse once.  */
     }
   else
     {
       /* Second entry.  */
-      if (ADDRESS_FUNCTION (dummy) > addr)
+      if (ADDRESS_FUNCTION (dummy) > *ptr)
         stack_dir = 1;          /* Stack grew upward.  */
       else
         stack_dir = -1;         /* Stack grew downward.  */
@@ -155,7 +154,10 @@ alloca (size_t size)
 
 #  if STACK_DIRECTION == 0
   if (STACK_DIR == 0)           /* Unknown growth direction.  */
-    find_stack_direction ();
+    {
+      char *addr = NULL;        /* Address of first `dummy', once known.  */
+      find_stack_direction (&addr);
+    }
 #  endif
 
   /* Reclaim garbage, defined as all alloca'd storage that