about summary refs log tree commit diff
path: root/sysdeps/htl/pt-key-create.c
diff options
context:
space:
mode:
Diffstat (limited to 'sysdeps/htl/pt-key-create.c')
-rw-r--r--sysdeps/htl/pt-key-create.c25
1 files changed, 17 insertions, 8 deletions
diff --git a/sysdeps/htl/pt-key-create.c b/sysdeps/htl/pt-key-create.c
index 51c0ef72b8..b7057434e3 100644
--- a/sysdeps/htl/pt-key-create.c
+++ b/sysdeps/htl/pt-key-create.c
@@ -26,10 +26,11 @@
 pthread_mutex_t __pthread_key_lock;
 pthread_once_t __pthread_key_once = PTHREAD_ONCE_INIT;
 
-void (**__pthread_key_destructors) (void *arg);
-int __pthread_key_size;
+void (*__pthread_static_key_destructors [PTHREAD_STATIC_KEYS]) (void *arg);
+void (**__pthread_key_destructors) (void *arg) = __pthread_static_key_destructors;
+int __pthread_key_size = PTHREAD_STATIC_KEYS;
 int __pthread_key_count;
-int __pthread_key_invalid_count;
+int __pthread_key_invalid_count = PTHREAD_STATIC_KEYS;
 
 int
 __pthread_key_create (pthread_key_t *key, void (*destructor) (void *))
@@ -80,13 +81,21 @@ do_search:
 	void *t;
 	int newsize;
 
-	if (__pthread_key_size == 0)
-	  newsize = 8;
+	newsize = __pthread_key_size * 2;
+
+	if (__pthread_key_destructors == __pthread_static_key_destructors)
+	  {
+	    /* We were still using the static array.  Switch to dynamic.  */
+	    t = malloc (newsize * sizeof (*__pthread_key_destructors));
+
+	    if (t != NULL)
+	      memcpy (t, __pthread_key_destructors,
+		      __pthread_key_size * sizeof (*__pthread_key_destructors));
+	  }
 	else
-	  newsize = __pthread_key_size * 2;
+	  t = realloc (__pthread_key_destructors,
+		       newsize * sizeof (*__pthread_key_destructors));
 
-	t = realloc (__pthread_key_destructors,
-		     newsize * sizeof (*__pthread_key_destructors));
 	if (t == NULL)
 	  {
 	    __pthread_mutex_unlock (&__pthread_key_lock);