Back to index

glibc  2.9
tst-tls1.c
Go to the documentation of this file.
00001 /* Copyright (C) 2003 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Jakub Jelinek <jakub@redhat.com>, 2003.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 /* Check alignment, overlapping and layout of TLS variables.  */
00021 #include <stdint.h>
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 #include <pthread.h>
00025 #include <sys/param.h>
00026 
00027 #include "tst-tls1.h"
00028 
00029 #ifdef TLS_REGISTER
00030 
00031 struct tls_obj tls_registry[64];
00032 
00033 static int
00034 tls_addr_cmp (const void *a, const void *b)
00035 {
00036   if (((struct tls_obj *)a)->addr < ((struct tls_obj *)b)->addr)
00037     return -1;
00038   if (((struct tls_obj *)a)->addr > ((struct tls_obj *)b)->addr)
00039     return 1;
00040   return 0;
00041 }
00042 
00043 static int
00044 do_test (void)
00045 {
00046   size_t cnt, i;
00047   int res = 0;
00048   uintptr_t min_addr = ~(uintptr_t) 0, max_addr = 0;
00049 
00050   for (cnt = 0; tls_registry[cnt].name; ++cnt);
00051 
00052   qsort (tls_registry, cnt, sizeof (struct tls_obj), tls_addr_cmp);
00053 
00054   for (i = 0; i < cnt; ++i)
00055     {
00056       printf ("%s = %p, size %zd, align %zd",
00057              tls_registry[i].name, (void *) tls_registry[i].addr,
00058              tls_registry[i].size, tls_registry[i].align);
00059       if (tls_registry[i].addr & (tls_registry[i].align - 1))
00060        {
00061          fputs (", WRONG ALIGNMENT", stdout);
00062          res = 1;
00063        }
00064       if (i > 0
00065          && (tls_registry[i - 1].addr + tls_registry[i - 1].size
00066              > tls_registry[i].addr))
00067        {
00068          fputs (", ADDRESS OVERLAP", stdout);
00069          res = 1;
00070        }
00071       puts ("");
00072       min_addr = MIN (tls_registry[i].addr, min_addr);
00073       max_addr = MAX (tls_registry[i].addr + tls_registry[i].size,
00074                     max_addr);
00075     }
00076 
00077   if (cnt > 1)
00078     printf ("Initial TLS used block size %zd\n",
00079            (size_t) (max_addr - min_addr));
00080   return res;
00081 }
00082 
00083 #define TEST_FUNCTION do_test ()
00084 
00085 #else
00086 
00087 #define TEST_FUNCTION 0
00088 
00089 #endif
00090 
00091 #include "../test-skeleton.c"