This is the mail archive of the libc-alpha@sourceware.cygnus.com mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

__builtin_expect in gettext



Here is a patch to add __builtin_expect marks to the intl subdirectory.

2000-04-29  Bruno Haible  <clisp.cons.org>

	* intl/gettextP.h (__builtin_expect): Define as empty if not a
	compiler builtin.
	* intl/loadinfo.h (__builtin_expect): Likewise.
	* intl/dcigettext.c (ADD_BLOCK, dcigettext, _nl_find_msg): Use
	__builtin_expect where appropriate.
	* intl/finddomain.c (_nl_find_domain): Likewise.
	* intl/l10nflist.c (_nl_make_l10nflist, _nl_normalize_codeset):
	Likewise.
	* intl/loadmsgcat.c (_nl_load_domain): Likewise.
	* intl/textdomain.c (textdomain): Likewise.
	* intl/localealias.c (ADD_BLOCK, read_alias_file): Likewise.
	(extend_alias_table): Return an error indicator.
	(read_alias_file): Bail out if extend_alias_table fails.

*** intl/gettextP.h.bak	Mon Feb 28 11:34:05 2000
--- intl/gettextP.h	Sun Apr 30 00:25:13 2000
***************
*** 44,49 ****
--- 44,55 ----
  # define internal_function
  #endif
  
+ /* Tell the compiler when a conditional or integer expression is
+    almost always true or almost always false.  */
+ #ifndef HAVE_BUILTIN_EXPECT
+ # define __builtin_expect(expr, val) (expr)
+ #endif
+ 
  #ifndef W
  # define W(flag, data) ((flag) ? SWAP (data) : (data))
  #endif
*** intl/loadinfo.h.bak	Sun Apr 23 18:01:14 2000
--- intl/loadinfo.h	Sun Apr 30 10:30:44 2000
***************
*** 32,37 ****
--- 32,43 ----
  # define internal_function
  #endif
  
+ /* Tell the compiler when a conditional or integer expression is
+    almost always true or almost always false.  */
+ #ifndef HAVE_BUILTIN_EXPECT
+ # define __builtin_expect(expr, val) (expr)
+ #endif
+ 
  /* Encoding of locale name parts.  */
  #define CEN_REVISION		1
  #define CEN_SPONSOR		2
*** intl/dcigettext.c.bak	Sat Apr 29 09:02:02 2000
--- intl/dcigettext.c	Sun Apr 30 00:21:55 2000
***************
*** 286,292 ****
      struct block_list *newp = (struct block_list *) malloc (sizeof (*newp));  \
      /* If we cannot get a free block we cannot add the new element to	      \
         the list.  */							      \
!     if (newp != NULL) {							      \
        newp->address = (addr);						      \
        newp->next = (list);						      \
        (list) = newp;							      \
--- 286,292 ----
      struct block_list *newp = (struct block_list *) malloc (sizeof (*newp));  \
      /* If we cannot get a free block we cannot add the new element to	      \
         the list.  */							      \
!     if (__builtin_expect (newp != NULL, 1)) {				      \
        newp->address = (addr);						      \
        newp->next = (list);						      \
        (list) = newp;							      \
***************
*** 367,373 ****
    size_t domainname_len;
  
    /* If no real MSGID is given return NULL.  */
!   if (msgid1 == NULL)
      return NULL;
  
    __libc_rwlock_rdlock (_nl_state_lock);
--- 367,373 ----
    size_t domainname_len;
  
    /* If no real MSGID is given return NULL.  */
!   if (__builtin_expect (msgid1 == NULL, 0))
      return NULL;
  
    __libc_rwlock_rdlock (_nl_state_lock);
***************
*** 446,452 ****
  	  __set_errno (0);
  	}
  
!       if (ret == NULL)
  	{
  	  /* We cannot get the current working directory.  Don't signal an
  	     error but simply return the default string.  */
--- 448,454 ----
  	  __set_errno (0);
  	}
  
!       if (__builtin_expect (ret == NULL, 0))
  	{
  	  /* We cannot get the current working directory.  Don't signal an
  	     error but simply return the default string.  */
***************
*** 593,599 ****
  		  newp = (struct known_translation_t *)
  		    malloc (sizeof (*newp) + msgid_len
  			    + domainname_len + 1 - ZERO);
! 		  if (newp != NULL)
  		    {
  		      newp->domain = mempcpy (newp->msgid, msgid1, msgid_len);
  		      memcpy (newp->domain, domainname, domainname_len + 1);
--- 595,601 ----
  		  newp = (struct known_translation_t *)
  		    malloc (sizeof (*newp) + msgid_len
  			    + domainname_len + 1 - ZERO);
! 		  if (__builtin_expect (newp != NULL, 1))
  		    {
  		      newp->domain = mempcpy (newp->msgid, msgid1, msgid_len);
  		      memcpy (newp->domain, domainname, domainname_len + 1);
***************
*** 605,611 ****
  		      /* Insert the entry in the search tree.  */
  		      foundp = (struct known_translation_t **)
  			tsearch (newp, &root, transcmp);
! 		      if (&newp != foundp)
  			/* The insert failed.  */
  			free (newp);
  		    }
--- 607,613 ----
  		      /* Insert the entry in the search tree.  */
  		      foundp = (struct known_translation_t **)
  			tsearch (newp, &root, transcmp);
! 		      if (__builtin_expect (&newp != foundp, 0))
  			/* The insert failed.  */
  			free (newp);
  		    }
***************
*** 744,757 ****
  	 a priori which entries are plural entries. Therefore at any
  	 moment we can only translate the variants 0 .. INDEX.  */
  
!       if (domain->conv_tab == NULL
  	  && ((domain->conv_tab = (char **) calloc (domain->nstrings,
  						    sizeof (char *)))
  	      == NULL))
  	/* Mark that we didn't succeed allocating a table.  */
  	domain->conv_tab = (char **) -1;
  
!       if (domain->conv_tab == (char **) -1)
  	/* Nothing we can do, no more memory.  */
  	goto converted;
  
--- 746,759 ----
  	 a priori which entries are plural entries. Therefore at any
  	 moment we can only translate the variants 0 .. INDEX.  */
  
!       if (__builtin_expect (domain->conv_tab == NULL, 0)
  	  && ((domain->conv_tab = (char **) calloc (domain->nstrings,
  						    sizeof (char *)))
  	      == NULL))
  	/* Mark that we didn't succeed allocating a table.  */
  	domain->conv_tab = (char **) -1;
  
!       if (__builtin_expect (domain->conv_tab == (char **) -1, 0))
  	/* Nothing we can do, no more memory.  */
  	goto converted;
  
*** intl/finddomain.c.bak	Mon Feb 28 11:34:04 2000
--- intl/finddomain.c	Sat Apr 29 23:55:36 2000
***************
*** 141,152 ****
      {
  #if defined _LIBC || defined HAVE_STRDUP
        locale = strdup (alias_value);
!       if (locale == NULL)
  	return NULL;
  #else
        size_t len = strlen (alias_value) + 1;
        locale = (char *) malloc (len);
!       if (locale == NULL)
  	return NULL;
  
        memcpy (locale, alias_value, len);
--- 143,154 ----
      {
  #if defined _LIBC || defined HAVE_STRDUP
        locale = strdup (alias_value);
!       if (__builtin_expect (locale == NULL, 0))
  	return NULL;
  #else
        size_t len = strlen (alias_value) + 1;
        locale = (char *) malloc (len);
!       if (__builtin_expect (locale == NULL, 0))
  	return NULL;
  
        memcpy (locale, alias_value, len);
*** intl/l10nflist.c.bak	Sun Apr 23 22:23:26 2000
--- intl/l10nflist.c	Sun Apr 30 10:31:28 2000
***************
*** 220,226 ****
  					   ? strlen (revision) + 1 : 0)) : 0)
  				  + 1 + strlen (filename) + 1);
  
!   if (abs_filename == NULL)
      return NULL;
  
    retval = NULL;
--- 221,227 ----
  					   ? strlen (revision) + 1 : 0)) : 0)
  				  + 1 + strlen (filename) + 1);
  
!   if (__builtin_expect (abs_filename == NULL, 0))
      return NULL;
  
    retval = NULL;
***************
*** 305,311 ****
      malloc (sizeof (*retval) + (__argz_count (dirlist, dirlist_len)
  				* (1 << pop (mask))
  				* sizeof (struct loaded_l10nfile *)));
!   if (retval == NULL)
      return NULL;
  
    retval->filename = abs_filename;
--- 306,312 ----
      malloc (sizeof (*retval) + (__argz_count (dirlist, dirlist_len)
  				* (1 << pop (mask))
  				* sizeof (struct loaded_l10nfile *)));
!   if (__builtin_expect (retval == NULL, 0))
      return NULL;
  
    retval->filename = abs_filename;
***************
*** 377,383 ****
  
    retval = (char *) malloc ((only_digit ? 3 : 0) + len + 1);
  
!   if (retval != NULL)
      {
        if (only_digit)
  	wp = stpcpy (retval, "iso");
--- 380,386 ----
  
    retval = (char *) malloc ((only_digit ? 3 : 0) + len + 1);
  
!   if (__builtin_expect (retval != NULL, 1))
      {
        if (only_digit)
  	wp = stpcpy (retval, "iso");
*** intl/loadmsgcat.c.bak	Sun Apr 23 18:39:06 2000
--- intl/loadmsgcat.c	Sun Apr 30 00:14:31 2000
***************
*** 138,146 ****
      return;
  
    /* We must know about the size of the file.  */
!   if (fstat (fd, &st) != 0
!       || (size = (size_t) st.st_size) != st.st_size
!       || size < sizeof (struct mo_file_header))
      {
        /* Something went wrong.  */
        close (fd);
--- 142,150 ----
      return;
  
    /* We must know about the size of the file.  */
!   if (__builtin_expect (fstat (fd, &st) != 0, 0)
!       || __builtin_expect ((size = (size_t) st.st_size) != st.st_size, 0)
!       || __builtin_expect (size < sizeof (struct mo_file_header), 0))
      {
        /* Something went wrong.  */
        close (fd);
***************
*** 153,159 ****
    data = (struct mo_file_header *) mmap (NULL, size, PROT_READ,
  					 MAP_PRIVATE, fd, 0);
  
!   if (data != (struct mo_file_header *) -1)
      {
        /* mmap() call was successful.  */
        close (fd);
--- 157,163 ----
    data = (struct mo_file_header *) mmap (NULL, size, PROT_READ,
  					 MAP_PRIVATE, fd, 0);
  
!   if (__builtin_expect (data != (struct mo_file_header *) -1, 1))
      {
        /* mmap() call was successful.  */
        close (fd);
***************
*** 169,175 ****
        char *read_ptr;
  
        data = (struct mo_file_header *) malloc (size);
!       if (data == NULL)
  	return;
  
        to_read = size;
--- 173,179 ----
        char *read_ptr;
  
        data = (struct mo_file_header *) malloc (size);
!       if (__builtin_expect (data == NULL, 0))
  	return;
  
        to_read = size;
***************
*** 193,199 ****
  
    /* Using the magic number we can test whether it really is a message
       catalog file.  */
!   if (data->magic != _MAGIC && data->magic != _MAGIC_SWAPPED)
      {
        /* The magic number is wrong: not a message catalog file.  */
  #ifdef HAVE_MMAP
--- 204,211 ----
  
    /* Using the magic number we can test whether it really is a message
       catalog file.  */
!   if (__builtin_expect (data->magic != _MAGIC && data->magic != _MAGIC_SWAPPED,
! 			0))
      {
        /* The magic number is wrong: not a message catalog file.  */
  #ifdef HAVE_MMAP
***************
*** 207,213 ****
  
    domain_file->data
      = (struct loaded_domain *) malloc (sizeof (struct loaded_domain));
!   if (domain_file->data == NULL)
      return;
  
    domain = (struct loaded_domain *) domain_file->data;
--- 219,225 ----
  
    domain_file->data
      = (struct loaded_domain *) malloc (sizeof (struct loaded_domain));
!   if (__builtin_expect (domain_file->data == NULL, 0))
      return;
  
    domain = (struct loaded_domain *) domain_file->data;
*** intl/textdomain.c.bak	Sun Apr 23 18:21:00 2000
--- intl/textdomain.c	Sun Apr 30 00:04:14 2000
***************
*** 115,132 ****
  #else
        size_t len = strlen (domainname) + 1;
        new_domain = (char *) malloc (len);
!       if (new_domain != NULL)
  	memcpy (new_domain, domainname, len);
  #endif
  
!       if (new_domain != NULL)
  	_nl_current_default_domain = new_domain;
      }
  
    /* We use this possibility to signal a change of the loaded catalogs
       since this is most likely the case and there is no other easy we
       to do it.  Do it only when the call was successful.  */
!   if (new_domain != NULL)
      {
        ++_nl_msg_cat_cntr;
  
--- 115,132 ----
  #else
        size_t len = strlen (domainname) + 1;
        new_domain = (char *) malloc (len);
!       if (__builtin_expect (new_domain != NULL, 1))
  	memcpy (new_domain, domainname, len);
  #endif
  
!       if (__builtin_expect (new_domain != NULL, 1))
  	_nl_current_default_domain = new_domain;
      }
  
    /* We use this possibility to signal a change of the loaded catalogs
       since this is most likely the case and there is no other easy we
       to do it.  Do it only when the call was successful.  */
!   if (__builtin_expect (new_domain != NULL, 1))
      {
        ++_nl_msg_cat_cntr;
  
*** intl/localealias.c.bak	Mon Feb 28 11:34:06 2000
--- intl/localealias.c	Sun Apr 30 00:02:31 2000
***************
*** 112,118 ****
      struct block_list *newp = (struct block_list *) malloc (sizeof (*newp));  \
      /* If we cannot get a free block we cannot add the new element to	      \
         the list.  */							      \
!     if (newp != NULL) {							      \
        newp->address = (addr);						      \
        newp->next = (list);						      \
        (list) = newp;							      \
--- 116,122 ----
      struct block_list *newp = (struct block_list *) malloc (sizeof (*newp));  \
      /* If we cannot get a free block we cannot add the new element to	      \
         the list.  */							      \
!     if (__builtin_expect (newp != NULL, 1)) {				      \
        newp->address = (addr);						      \
        newp->next = (list);						      \
        (list) = newp;							      \
***************
*** 162,168 ****
  /* Prototypes for local functions.  */
  static size_t read_alias_file PARAMS ((const char *fname, int fname_len))
       internal_function;
! static void extend_alias_table PARAMS ((void));
  static int alias_compare PARAMS ((const struct alias_map *map1,
  				  const struct alias_map *map2));
  
--- 162,168 ----
  /* Prototypes for local functions.  */
  static size_t read_alias_file PARAMS ((const char *fname, int fname_len))
       internal_function;
! static int extend_alias_table PARAMS ((void));
  static int alias_compare PARAMS ((const struct alias_map *map1,
  				  const struct alias_map *map2));
  
***************
*** 250,256 ****
  #endif
  
    fp = fopen (full_fname, "r");
!   if (fp == NULL)
      {
        FREE_BLOCKS (block_list);
        return 0;
--- 254,260 ----
  #endif
  
    fp = fopen (full_fname, "r");
!   if (__builtin_expect (fp == NULL, 0))
      {
        FREE_BLOCKS (block_list);
        return 0;
***************
*** 326,332 ****
  		*cp++ = '\0';
  
  	      if (nmap >= maxmap)
! 		extend_alias_table ();
  
  	      alias_len = strlen (alias) + 1;
  	      value_len = strlen (value) + 1;
--- 330,340 ----
  		*cp++ = '\0';
  
  	      if (nmap >= maxmap)
! 		if (__builtin_expect (extend_alias_table (), 0))
! 		  {
! 		    FREE_BLOCKS (block_list);
! 		    return added;
! 		  }
  
  	      alias_len = strlen (alias) + 1;
  	      value_len = strlen (value) + 1;
***************
*** 338,344 ****
  				     + (alias_len + value_len > 1024
  					? alias_len + value_len : 1024));
  		  char *new_pool = (char *) realloc (string_space, new_size);
! 		  if (new_pool == NULL)
  		    {
  		      FREE_BLOCKS (block_list);
  		      return added;
--- 346,352 ----
  				     + (alias_len + value_len > 1024
  					? alias_len + value_len : 1024));
  		  char *new_pool = (char *) realloc (string_space, new_size);
! 		  if (__builtin_expect (new_pool == NULL, 0))
  		    {
  		      FREE_BLOCKS (block_list);
  		      return added;
***************
*** 374,380 ****
  }
  
  
! static void
  extend_alias_table ()
  {
    size_t new_size;
--- 382,388 ----
  }
  
  
! static int
  extend_alias_table ()
  {
    size_t new_size;
***************
*** 383,394 ****
    new_size = maxmap == 0 ? 100 : 2 * maxmap;
    new_map = (struct alias_map *) realloc (map, (new_size
  						* sizeof (struct alias_map)));
!   if (new_map == NULL)
      /* Simply don't extend: we don't have any more core.  */
!     return;
  
    map = new_map;
    maxmap = new_size;
  }
  
  
--- 391,403 ----
    new_size = maxmap == 0 ? 100 : 2 * maxmap;
    new_map = (struct alias_map *) realloc (map, (new_size
  						* sizeof (struct alias_map)));
!   if (__builtin_expect (new_map == NULL, 0))
      /* Simply don't extend: we don't have any more core.  */
!     return -1;
  
    map = new_map;
    maxmap = new_size;
+   return 0;
  }
  
  

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]