Reduce code duplication.
[gnulib.git] / lib / alloca.c
index 6ad425a..3a1f4e2 100644 (file)
    allocating any.  It is a good idea to use alloca(0) in
    your main control loop, etc. to force garbage collection.  */
 
-#ifdef HAVE_CONFIG_H
-# include <config.h>
-#endif
+#include <config.h>
 
-#if HAVE_STRING_H
-# include <string.h>
-#endif
-#if HAVE_STDLIB_H
-# include <stdlib.h>
-#endif
+#include <alloca.h>
+
+#include <string.h>
+#include <stdlib.h>
 
 #ifdef emacs
+# include "lisp.h"
 # include "blockinput.h"
+# ifdef EMACS_FREE
+#  undef free
+#  define free EMACS_FREE
+# endif
+#else
+# define memory_full() abort ()
 #endif
 
 /* If compiling with GCC 2, this file's not needed.  */
@@ -53,6 +56,8 @@
 you
 lose
 -- must know STACK_DIRECTION at compile-time
+/* Using #error here is not wise since this file should work for
+   old and obscure compilers.  */
 #    endif /* STACK_DIRECTION undefined */
 #   endif /* static */
 #  endif /* emacs */
@@ -67,32 +72,6 @@ long i00afunc ();
 #   define ADDRESS_FUNCTION(arg) &(arg)
 #  endif
 
-#  if __STDC__
-typedef void *pointer;
-#  else
-typedef char *pointer;
-#  endif
-
-#  ifndef NULL
-#   define NULL 0
-#  endif
-
-/* Different portions of Emacs need to call different versions of
-   malloc.  The Emacs executable needs alloca to call xmalloc, because
-   ordinary malloc isn't protected from input signals.  On the other
-   hand, the utilities in lib-src need alloca to call malloc; some of
-   them are very simple, and don't have an xmalloc routine.
-
-   Non-Emacs programs expect this to call xmalloc.
-
-   Callers below should use malloc.  */
-
-#  ifndef emacs
-#   undef malloc
-#   define malloc xmalloc
-#  endif
-extern pointer malloc ();
-
 /* Define STACK_DIRECTION if you know the direction of stack
    growth for your system; otherwise it will be automatically
    deduced at run-time.
@@ -115,7 +94,7 @@ static int stack_dir;                /* 1 or -1 once known.  */
 #   define STACK_DIR   stack_dir
 
 static void
-find_stack_direction ()
+find_stack_direction (void)
 {
   static char *addr = NULL;    /* Address of first `dummy', once known.  */
   auto char dummy;             /* To get stack address.  */
@@ -168,7 +147,7 @@ static header *last_alloca_header = NULL;   /* -> last alloca header.  */
    caller, but that method cannot be made to work for some
    implementations of C, for example under Gould's UTX/32.  */
 
-pointer
+void *
 alloca (size_t size)
 {
   auto char probe;             /* Probes stack depth: */
@@ -195,7 +174,7 @@ alloca (size_t size)
        {
          register header *np = hp->h.next;
 
-         free ((pointer) hp);  /* Collect garbage.  */
+         free (hp);            /* Collect garbage.  */
 
          hp = np;              /* -> next header.  */
        }
@@ -215,20 +194,26 @@ alloca (size_t size)
   /* Allocate combined header + user data storage.  */
 
   {
-    register pointer new = malloc (sizeof (header) + size);
     /* Address of header.  */
+    register header *new;
+
+    size_t combined_size = sizeof (header) + size;
+    if (combined_size < sizeof (header))
+      memory_full ();
+
+    new = malloc (combined_size);
 
-    if (new == 0)
-      abort();
+    if (! new)
+      memory_full ();
 
-    ((header *) new)->h.next = last_alloca_header;
-    ((header *) new)->h.deep = depth;
+    new->h.next = last_alloca_header;
+    new->h.deep = depth;
 
-    last_alloca_header = (header *) new;
+    last_alloca_header = new;
 
     /* User storage begins just after header.  */
 
-    return (pointer) ((char *) new + sizeof (header));
+    return (void *) (new + 1);
   }
 }