From 5c82d20a26d0b283c831dba782731b4273f4d4d8 Mon Sep 17 00:00:00 2001 From: Zack Weinberg Date: Mon, 29 May 2000 20:45:32 +0000 Subject: * hashtab.c, partition.c, xmemdup.c: Include string.h if HAVE_STRING_H. * pexecute.c, xexit.c: Include stdlib.h if HAVE_STDLIB_H. * objalloc.c: Include config.h. Include stdlib.h and don't declare malloc or free if HAVE_STDLIB_H. * strerror.c, strsignal.c: Include stdlib.h if HAVE_STDLIB_H, else declare malloc without prototype. Include string.h if HAVE_STRING_H, else declare memset without prototype. Don't include stddef.h. * sort.c: Bring over from GCC. --- libiberty/ChangeLog | 14 ++++ libiberty/hashtab.c | 4 ++ libiberty/objalloc.c | 7 ++ libiberty/partition.c | 4 ++ libiberty/pexecute.c | 3 + libiberty/sort.c | 185 ++++++++++++++++++++++++++++++++++++++++++++++++++ libiberty/strerror.c | 19 +++--- libiberty/strsignal.c | 19 +++--- libiberty/xexit.c | 3 + libiberty/xmemdup.c | 3 + 10 files changed, 245 insertions(+), 16 deletions(-) create mode 100644 libiberty/sort.c diff --git a/libiberty/ChangeLog b/libiberty/ChangeLog index 316f6a2..8e63f17 100644 --- a/libiberty/ChangeLog +++ b/libiberty/ChangeLog @@ -1,3 +1,17 @@ +2000-05-29 Zack Weinberg + + * hashtab.c, partition.c, xmemdup.c: Include string.h + if HAVE_STRING_H. + * pexecute.c, xexit.c: Include stdlib.h if HAVE_STDLIB_H. + * objalloc.c: Include config.h. Include stdlib.h and don't + declare malloc or free if HAVE_STDLIB_H. + * strerror.c, strsignal.c: Include stdlib.h if HAVE_STDLIB_H, + else declare malloc without prototype. Include string.h if + HAVE_STRING_H, else declare memset without prototype. Don't + include stddef.h. + + * sort.c: Bring over from GCC. + 2000-05-26 Alexandre Oliva * Makefile.in (maintainer-clean-subdir): Fix handling of empty diff --git a/libiberty/hashtab.c b/libiberty/hashtab.c index 16c5d3e..6ae34aa 100644 --- a/libiberty/hashtab.c +++ b/libiberty/hashtab.c @@ -41,6 +41,10 @@ Boston, MA 02111-1307, USA. */ #include #endif +#ifdef HAVE_STRING_H +#include +#endif + #include #include "libiberty.h" diff --git a/libiberty/objalloc.c b/libiberty/objalloc.c index 57754a8..5099569 100644 --- a/libiberty/objalloc.c +++ b/libiberty/objalloc.c @@ -18,6 +18,8 @@ Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include "ansidecl.h" +#include "config.h" + #include "objalloc.h" /* Get a definition for NULL. */ @@ -33,11 +35,16 @@ Boston, MA 02111-1307, USA. */ #include #endif +#ifdef HAVE_STDLIB_H +#include +#else /* For systems with larger pointers than ints, this must be declared. */ extern PTR malloc PARAMS ((size_t)); extern void free PARAMS ((PTR)); #endif +#endif + /* These routines allocate space for an object. Freeing allocated space may or may not free all more recently allocated space. diff --git a/libiberty/partition.c b/libiberty/partition.c index c1d5847..52fb128 100644 --- a/libiberty/partition.c +++ b/libiberty/partition.c @@ -27,6 +27,10 @@ #include #endif +#ifdef HAVE_STRING_H +#include +#endif + #include "libiberty.h" #include "partition.h" diff --git a/libiberty/pexecute.c b/libiberty/pexecute.c index 56ddec7..f1aea9d 100644 --- a/libiberty/pexecute.c +++ b/libiberty/pexecute.c @@ -35,6 +35,9 @@ Boston, MA 02111-1307, USA. */ #ifdef HAVE_UNISTD_H #include #endif +#ifdef HAVE_STDLIB_H +#include +#endif #define ISSPACE (x) isspace(x) #ifdef HAVE_SYS_WAIT_H #include diff --git a/libiberty/sort.c b/libiberty/sort.c new file mode 100644 index 0000000..da2d660 --- /dev/null +++ b/libiberty/sort.c @@ -0,0 +1,185 @@ +/* Sorting algorithms. + Copyright (C) 2000 Free Software Foundation, Inc. + Contributed by Mark Mitchell . + +This file is part of GNU CC. + +GNU CC 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) +any later version. + +GNU CC is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU CC; see the file COPYING. If not, write to +the Free Software Foundation, 59 Temple Place - Suite 330, +Boston, MA 02111-1307, USA. */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif +#include "libiberty.h" +#include "sort.h" +#include +#ifdef HAVE_STDLIB_H +#include +#endif +#ifdef HAVE_STRING_H +#include +#endif + +#ifndef UCHAR_MAX +#define UCHAR_MAX ((unsigned char)(-1)) +#endif + +/* POINTERS and WORK are both arrays of N pointers. When this + function returns POINTERS will be sorted in ascending order. */ + +void sort_pointers (n, pointers, work) + size_t n; + void **pointers; + void **work; +{ + /* The type of a single digit. This can be any unsigned integral + type. When changing this, DIGIT_MAX should be changed as + well. */ + typedef unsigned char digit_t; + + /* The maximum value a single digit can have. */ +#define DIGIT_MAX (UCHAR_MAX + 1) + + /* The Ith entry is the number of elements in *POINTERSP that have I + in the digit on which we are currently sorting. */ + unsigned int count[DIGIT_MAX]; + /* Nonzero if we are running on a big-endian machine. */ + int big_endian_p; + size_t i; + size_t j; + + /* The algorithm used here is radix sort which takes time linear in + the number of elements in the array. */ + + /* The algorithm here depends on being able to swap the two arrays + an even number of times. */ + if ((sizeof (void *) / sizeof (digit_t)) % 2 != 0) + abort (); + + /* Figure out the endianness of the machine. */ + for (i = 0, j = 0; i < sizeof (size_t); ++i) + { + j *= (UCHAR_MAX + 1); + j += i; + } + big_endian_p = (((char *)&j)[0] == 0); + + /* Move through the pointer values from least significant to most + significant digits. */ + for (i = 0; i < sizeof (void *) / sizeof (digit_t); ++i) + { + digit_t *digit; + digit_t *bias; + digit_t *top; + unsigned int *countp; + void **pointerp; + + /* The offset from the start of the pointer will depend on the + endianness of the machine. */ + if (big_endian_p) + j = sizeof (void *) / sizeof (digit_t) - i; + else + j = i; + + /* Now, perform a stable sort on this digit. We use counting + sort. */ + memset (count, 0, DIGIT_MAX * sizeof (unsigned int)); + + /* Compute the address of the appropriate digit in the first and + one-past-the-end elements of the array. On a little-endian + machine, the least-significant digit is closest to the front. */ + bias = ((digit_t *) pointers) + j; + top = ((digit_t *) (pointers + n)) + j; + + /* Count how many there are of each value. At the end of this + loop, COUNT[K] will contain the number of pointers whose Ith + digit is K. */ + for (digit = bias; + digit < top; + digit += sizeof (void *) / sizeof (digit_t)) + ++count[*digit]; + + /* Now, make COUNT[K] contain the number of pointers whose Ith + digit is less than or equal to K. */ + for (countp = count + 1; countp < count + DIGIT_MAX; ++countp) + *countp += countp[-1]; + + /* Now, drop the pointers into their correct locations. */ + for (pointerp = pointers + n - 1; pointerp >= pointers; --pointerp) + work[--count[((digit_t *) pointerp)[j]]] = *pointerp; + + /* Swap WORK and POINTERS so that POINTERS contains the sorted + array. */ + pointerp = pointers; + pointers = work; + work = pointerp; + } +} + +/* Everything below here is a unit test for the routines in this + file. */ + +#ifdef UNIT_TEST + +#include + +void *xmalloc (n) + size_t n; +{ + return malloc (n); +} + +int main (int argc, char **argv) +{ + int k; + int result; + size_t i; + void **pointers; + void **work; + + if (argc > 1) + k = atoi (argv[1]); + else + k = 10; + + pointers = xmalloc (k * sizeof (void *)); + work = xmalloc (k * sizeof (void *)); + + for (i = 0; i < k; ++i) + { + pointers[i] = (void *) random (); + printf ("%x\n", pointers[i]); + } + + sort_pointers (k, pointers, work); + + printf ("\nSorted\n\n"); + + result = 0; + + for (i = 0; i < k; ++i) + { + printf ("%x\n", pointers[i]); + if (i > 0 && (char*) pointers[i] < (char*) pointers[i - 1]) + result = 1; + } + + free (pointers); + free (work); + + return result; +} + +#endif diff --git a/libiberty/strerror.c b/libiberty/strerror.c index 644cc75..0dd2220 100644 --- a/libiberty/strerror.c +++ b/libiberty/strerror.c @@ -25,14 +25,17 @@ /* Routines imported from standard C runtime libraries. */ -#ifdef __STDC__ -#include -extern void *malloc (size_t size); /* 4.10.3.3 */ -extern void *memset (void *s, int c, size_t n); /* 4.11.6.1 */ -#else /* !__STDC__ */ -extern char *malloc (); /* Standard memory allocater */ -extern char *memset (); -#endif /* __STDC__ */ +#ifdef HAVE_STDLIB_H +#include +#else +extern PTR malloc (); +#endif + +#ifdef HAVE_STRING_H +#include +#else +extern PTR memset (); +#endif #ifndef MAX # define MAX(a,b) ((a) > (b) ? (a) : (b)) diff --git a/libiberty/strsignal.c b/libiberty/strsignal.c index c7bb10c..5625323 100644 --- a/libiberty/strsignal.c +++ b/libiberty/strsignal.c @@ -23,14 +23,17 @@ /* Routines imported from standard C runtime libraries. */ -#ifdef __STDC__ -#include -extern void *malloc (size_t size); /* 4.10.3.3 */ -extern void *memset (void *s, int c, size_t n); /* 4.11.6.1 */ -#else /* !__STDC__ */ -extern char *malloc (); /* Standard memory allocater */ -extern char *memset (); -#endif /* __STDC__ */ +#ifdef HAVE_STDLIB_H +#include +#else +extern PTR malloc (); +#endif + +#ifdef HAVE_STRING_H +#include +#else +extern PTR memset (); +#endif /* Undefine the macro we used to hide the definition of sys_siglist found in the system header files. */ diff --git a/libiberty/xexit.c b/libiberty/xexit.c index 431bbe0..7aa52ac 100644 --- a/libiberty/xexit.c +++ b/libiberty/xexit.c @@ -21,6 +21,9 @@ Boston, MA 02111-1307, USA. */ #include "libiberty.h" #include +#ifdef HAVE_STDLIB_H +#include +#endif /* This variable is set by xatexit if it is called. This way, xmalloc doesn't drag xatexit into the link. */ diff --git a/libiberty/xmemdup.c b/libiberty/xmemdup.c index f780041..fdb17a5 100644 --- a/libiberty/xmemdup.c +++ b/libiberty/xmemdup.c @@ -9,6 +9,9 @@ #include "libiberty.h" #include /* For size_t. */ +#ifdef HAVE_STRING_H +#include +#endif PTR xmemdup (input, copy_size, alloc_size) -- cgit v1.1