__ctype_b_loc Definition + Documentation

Adhemerval Zanella Netto adhemerval.zanella@linaro.org
Mon Sep 16 21:38:08 GMT 2024



On 16/09/24 17:35, Andrii Dokhniak via Libc-help wrote:
> Hi, I am a noob. Sorry about that.
> I started by trying to figure out why `isalpha()` returns 1024 instead of 1
> in case of true. I expanded that macro, and it lead me to a call to
> `__ctype_b_loc()`.
> 
> When reading file `ctype.h` (e64a1e81aadf6c401), line 63, I saw this
> ```c
> /* These are defined in ctype-info.c.
>    The declarations here must match those in localeinfo.h.
> ....
> */
> extern const unsigned short int **__ctype_b_loc (void)
>      __THROW __attribute__ ((__const__));
> ```
> But when reading file `ctype-info.c` (e64a1e81aadf6c401), or any other `.c`
> file in `/ctype` directory I have not found another mention of
> `__ctype_b_loc`.
> 
> So my question is: Where is `__ctype_b_loc` actually defined, is it some
> sort of compiler built in? And if the documentation is wrong, where can I
> read more about it?.
> 
> I found some old thread on reddit, with some source code of that function,
> but I have no idea where they got it

They are implemented by ctype/ctype-info.c:

18 #define CTYPE_EXTERN_INLINE /* Define real functions for accessors.  */
19 #include <ctype.h>

The CTYPE_EXTERN_INLINE make the function to no act as 'extern inline'
while building glibc itself:

 29 #  ifndef CTYPE_EXTERN_INLINE   /* Used by ctype/ctype-info.c, which see.  */
 30 #   define CTYPE_EXTERN_INLINE extern inline
 31 #  endif
[...]
 38 CTYPE_EXTERN_INLINE const uint16_t ** __attribute__ ((const))
 39 __ctype_b_loc (void)
 40 {
 41   return __libc_tsd_address (const uint16_t *, CTYPE_B);
 42 }

The __libc_tsd_address is a macro before TLS was added, it just expand to
an empty macro nowadays.  So the function is just:


extern __thread const uint16_t * __libc_tsd_CTYPE_B __attribute__ ((tls_model ("initial-exec")));

const uint16_t ** __attribute__ ((const))
__ctype_b_loc (void)
{
  return (&__libc_tsd_CTYPE_B);
}




More information about the Libc-help mailing list