Power10 test lsb by byte operation
[deliverable/binutils-gdb.git] / gas / sb.c
index 6ec23fcdb923addeb31c7bb3397f78fe61aab710..8b5ea8a56e269e1cf968d5e6c6bb7a0cd529817c 100644 (file)
--- a/gas/sb.c
+++ b/gas/sb.c
@@ -1,5 +1,5 @@
 /* sb.c - string buffer manipulation routines
 /* sb.c - string buffer manipulation routines
-   Copyright (C) 1994, 1995 Free Software Foundation, Inc.
+   Copyright (C) 1994-2020 Free Software Foundation, Inc.
 
    Written by Steve and Judy Chamberlain of Cygnus Support,
       sac@cygnus.com
 
    Written by Steve and Judy Chamberlain of Cygnus Support,
       sac@cygnus.com
@@ -8,7 +8,7 @@
 
    GAS is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
 
    GAS is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2, or (at your option)
+   the Free Software Foundation; either version 3, or (at your option)
    any later version.
 
    GAS is distributed in the hope that it will be useful,
    any later version.
 
    GAS is distributed in the hope that it will be useful,
 
    You should have received a copy of the GNU General Public License
    along with GAS; see the file COPYING.  If not, write to the Free
 
    You should have received a copy of the GNU General Public License
    along with GAS; see the file COPYING.  If not, write to the Free
-   Software Foundation, 59 Temple Place - Suite 330, Boston, MA
-   02111-1307, USA. */
+   Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
+   02110-1301, USA.  */
 
 
-#include "config.h"
-#include <stdio.h>
-#ifdef HAVE_STRING_H
-#include <string.h>
-#else
-#include <strings.h>
-#endif
-#include "libiberty.h"
+#include "as.h"
 #include "sb.h"
 
 #include "sb.h"
 
+#ifdef HAVE_LIMITS_H
+#include <limits.h>
+#endif
+#ifndef CHAR_BIT
+#define CHAR_BIT 8
+#endif
+
 /* These routines are about manipulating strings.
 
    They are managed in things called `sb's which is an abbreviation
 /* These routines are about manipulating strings.
 
    They are managed in things called `sb's which is an abbreviation
    sb_new (&foo);
    sb_grow... (&foo,...);
    use foo->ptr[*];
    sb_new (&foo);
    sb_grow... (&foo,...);
    use foo->ptr[*];
-   sb_kill (&foo);
-
-*/
-
-#define dsize 5
+   sb_kill (&foo);  */
 
 
-static void sb_check PARAMS ((sb *, int));
+/* Buffers start at INIT_ALLOC size, and roughly double each time we
+   go over the current allocation.  MALLOC_OVERHEAD is a guess at the
+   system malloc overhead.  We aim to not waste any memory in the
+   underlying page/chunk allocated by the system malloc.  */
+#define MALLOC_OVERHEAD (2 * sizeof (size_t))
+#define INIT_ALLOC (64 - MALLOC_OVERHEAD - 1)
 
 
-/* Statistics of sb structures.  */
+static void sb_check (sb *, size_t);
 
 
-int string_count[sb_max_power_two];
-
-/* Free list of sb structures.  */
-
-static sb_list_vector free_list;
-
-/* initializes an sb. */
+/* Initializes an sb.  */
 
 void
 
 void
-sb_build (ptr, size)
-     sb *ptr;
-     int size;
+sb_build (sb *ptr, size_t size)
 {
 {
-  /* see if we can find one to allocate */
-  sb_element *e;
-
-  if (size > sb_max_power_two)
-    abort ();
-
-  e = free_list.size[size];
-  if (!e)
-    {
-      /* nothing there, allocate one and stick into the free list */
-      e = (sb_element *) xmalloc (sizeof (sb_element) + (1 << size));
-      e->next = free_list.size[size];
-      e->size = 1 << size;
-      free_list.size[size] = e;
-      string_count[size]++;
-    }
-
-  /* remove from free list */
-
-  free_list.size[size] = e->next;
-
-  /* copy into callers world */
-  ptr->ptr = e->data;
-  ptr->pot = size;
+  ptr->ptr = XNEWVEC (char, size + 1);
+  ptr->max = size;
   ptr->len = 0;
   ptr->len = 0;
-  ptr->item = e;
 }
 
 }
 
-
 void
 void
-sb_new (ptr)
-     sb *ptr;
+sb_new (sb *ptr)
 {
 {
-  sb_build (ptr, dsize);
+  sb_build (ptr, INIT_ALLOC);
 }
 
 }
 
-/* deallocate the sb at ptr */
+/* Deallocate the sb at ptr.  */
 
 void
 
 void
-sb_kill (ptr)
-     sb *ptr;
+sb_kill (sb *ptr)
 {
 {
-  /* return item to free list */
-  ptr->item->next = free_list.size[ptr->pot];
-  free_list.size[ptr->pot] = ptr->item;
+  free (ptr->ptr);
 }
 
 }
 
-/* add the sb at s to the end of the sb at ptr */
+/* Add the sb at s to the end of the sb at ptr.  */
 
 void
 
 void
-sb_add_sb (ptr, s)
-     sb *ptr;
-     sb *s;
+sb_add_sb (sb *ptr, sb *s)
 {
   sb_check (ptr, s->len);
   memcpy (ptr->ptr + ptr->len, s->ptr, s->len);
   ptr->len += s->len;
 }
 
 {
   sb_check (ptr, s->len);
   memcpy (ptr->ptr + ptr->len, s->ptr, s->len);
   ptr->len += s->len;
 }
 
-/* make sure that the sb at ptr has room for another len characters,
-   and grow it if it doesn't. */
+/* Helper for sb_scrub_and_add_sb.  */
+
+static sb *sb_to_scrub;
+static char *scrub_position;
+static size_t
+scrub_from_sb (char *buf, size_t buflen)
+{
+  size_t copy;
+  copy = sb_to_scrub->len - (scrub_position - sb_to_scrub->ptr);
+  if (copy > buflen)
+    copy = buflen;
+  memcpy (buf, scrub_position, copy);
+  scrub_position += copy;
+  return copy;
+}
+
+/* Run the sb at s through do_scrub_chars and add the result to the sb
+   at ptr.  */
+
+void
+sb_scrub_and_add_sb (sb *ptr, sb *s)
+{
+  sb_to_scrub = s;
+  scrub_position = s->ptr;
+
+  sb_check (ptr, s->len);
+  ptr->len += do_scrub_chars (scrub_from_sb, ptr->ptr + ptr->len, s->len);
+
+  sb_to_scrub = 0;
+  scrub_position = 0;
+}
+
+/* Make sure that the sb at ptr has room for another len characters,
+   and grow it if it doesn't.  */
 
 static void
 
 static void
-sb_check (ptr, len)
-     sb *ptr;
-     int len;
+sb_check (sb *ptr, size_t len)
 {
 {
-  if (ptr->len + len >= 1 << ptr->pot)
+  size_t want = ptr->len + len;
+
+  if (want > ptr->max)
     {
     {
-      sb tmp;
-      int pot = ptr->pot;
-      while (ptr->len + len >= 1 << pot)
-       pot++;
-      sb_build (&tmp, pot);
-      sb_add_sb (&tmp, ptr);
-      sb_kill (ptr);
-      *ptr = tmp;
+      size_t max;
+
+      want += MALLOC_OVERHEAD + 1;
+      if ((ssize_t) want < 0)
+       as_fatal ("string buffer overflow");
+#if GCC_VERSION >= 3004
+      max = (size_t) 1 << (CHAR_BIT * sizeof (want)
+                          - (sizeof (want) <= sizeof (long)
+                             ? __builtin_clzl ((long) want)
+                             : __builtin_clzll ((long long) want)));
+#else
+      max = 128;
+      while (want > max)
+       max <<= 1;
+#endif
+      max -= MALLOC_OVERHEAD + 1;
+      ptr->max = max;
+      ptr->ptr = XRESIZEVEC (char, ptr->ptr, max + 1);
     }
 }
 
     }
 }
 
-/* make the sb at ptr point back to the beginning.  */
+/* Make the sb at ptr point back to the beginning.  */
 
 void
 
 void
-sb_reset (ptr)
-     sb *ptr;
+sb_reset (sb *ptr)
 {
   ptr->len = 0;
 }
 
 {
   ptr->len = 0;
 }
 
-/* add character c to the end of the sb at ptr. */
+/* Add character c to the end of the sb at ptr.  */
 
 void
 
 void
-sb_add_char (ptr, c)
-     sb *ptr;
-     int c;
+sb_add_char (sb *ptr, size_t c)
 {
   sb_check (ptr, 1);
   ptr->ptr[ptr->len++] = c;
 }
 
 {
   sb_check (ptr, 1);
   ptr->ptr[ptr->len++] = c;
 }
 
-/* add null terminated string s to the end of sb at ptr. */
+/* Add null terminated string s to the end of sb at ptr.  */
 
 void
 
 void
-sb_add_string (ptr, s)
-     sb *ptr;
-     const char *s;
+sb_add_string (sb *ptr, const char *s)
 {
 {
-  int len = strlen (s);
+  size_t len = strlen (s);
   sb_check (ptr, len);
   memcpy (ptr->ptr + ptr->len, s, len);
   ptr->len += len;
 }
 
   sb_check (ptr, len);
   memcpy (ptr->ptr + ptr->len, s, len);
   ptr->len += len;
 }
 
-/* add string at s of length len to sb at ptr */
+/* Add string at s of length len to sb at ptr */
 
 void
 
 void
-sb_add_buffer (ptr, s, len)
-     sb *ptr;
-     const char *s;
-     int len;
+sb_add_buffer (sb *ptr, const char *s, size_t len)
 {
   sb_check (ptr, len);
   memcpy (ptr->ptr + ptr->len, s, len);
   ptr->len += len;
 }
 
 {
   sb_check (ptr, len);
   memcpy (ptr->ptr + ptr->len, s, len);
   ptr->len += len;
 }
 
-/* print the sb at ptr to the output file */
-
-void
-sb_print (outfile, ptr)
-     FILE *outfile;
-     sb *ptr;
-{
-  int i;
-  int nc = 0;
-
-  for (i = 0; i < ptr->len; i++)
-    {
-      if (nc)
-       {
-         fprintf (outfile, ",");
-       }
-      fprintf (outfile, "%d", ptr->ptr[i]);
-      nc = 1;
-    }
-}
-
-void 
-sb_print_at (outfile, idx, ptr)
-     FILE *outfile;
-     int idx;
-     sb *ptr;
-{
-  int i;
-  for (i = idx; i < ptr->len; i++)
-    putc (ptr->ptr[i], outfile);
-}
-
-/* put a null at the end of the sb at in and return the start of the
-   string, so that it can be used as an arg to printf %s. */
-
-char *
-sb_name (in)
-     sb *in;
-{
-  /* stick a null on the end of the string */
-  sb_add_char (in, 0);
-  return in->ptr;
-}
-
-/* like sb_name, but don't include the null byte in the string.  */
+/* Write terminating NUL and return string.  */
 
 char *
 
 char *
-sb_terminate (in)
-     sb *in;
+sb_terminate (sb *in)
 {
 {
-  sb_add_char (in, 0);
-  --in->len;
+  in->ptr[in->len] = 0;
   return in->ptr;
 }
 
   return in->ptr;
 }
 
-/* start at the index idx into the string in sb at ptr and skip
-   whitespace. return the index of the first non whitespace character */
+/* Start at the index idx into the string in sb at ptr and skip
+   whitespace. return the index of the first non whitespace character */
 
 
-int
-sb_skip_white (idx, ptr)
-     int idx;
-     sb *ptr;
+size_t
+sb_skip_white (size_t idx, sb *ptr)
 {
   while (idx < ptr->len
         && (ptr->ptr[idx] == ' '
 {
   while (idx < ptr->len
         && (ptr->ptr[idx] == ' '
@@ -262,14 +211,12 @@ sb_skip_white (idx, ptr)
   return idx;
 }
 
   return idx;
 }
 
-/* start at the index idx into the sb at ptr. skips whitespace,
-   a comma and any following whitespace. returnes the index of the
-   next character. */
+/* Start at the index idx into the sb at ptr. skips whitespace,
+   a comma and any following whitespace. returns the index of the
+   next character.  */
 
 
-int
-sb_skip_comma (idx, ptr)
-     int idx;
-     sb *ptr;
+size_t
+sb_skip_comma (size_t idx, sb *ptr)
 {
   while (idx < ptr->len
         && (ptr->ptr[idx] == ' '
 {
   while (idx < ptr->len
         && (ptr->ptr[idx] == ' '
This page took 0.03109 seconds and 4 git commands to generate.