123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- #include <config.h>
- #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 !(defined __GNUC__ || defined __clang__)
- # ifndef alloca
- # ifdef emacs
- # ifdef static
- # ifndef STACK_DIRECTION
- you
- lose
- -- must know STACK_DIRECTION at compile-time
- # endif
- # endif
- # endif
- # ifndef STACK_DIRECTION
- # define STACK_DIRECTION 0
- # endif
- # if STACK_DIRECTION != 0
- # define STACK_DIR STACK_DIRECTION
- # else
- static int stack_dir;
- # define STACK_DIR stack_dir
- static int
- find_stack_direction (int *addr, int depth)
- {
- int dir, dummy = 0;
- if (! addr)
- addr = &dummy;
- *addr = addr < &dummy ? 1 : addr == &dummy ? 0 : -1;
- dir = depth ? find_stack_direction (addr, depth - 1) : 0;
- return dir + dummy;
- }
- # endif
- # ifndef ALIGN_SIZE
- # define ALIGN_SIZE sizeof(double)
- # endif
- typedef union hdr
- {
- char align[ALIGN_SIZE];
- struct
- {
- union hdr *next;
- char *deep;
- } h;
- } header;
- static header *last_alloca_header = NULL;
- void *
- alloca (size_t size)
- {
- auto char probe;
- register char *depth = &probe;
- # if STACK_DIRECTION == 0
- if (STACK_DIR == 0)
- STACK_DIR = find_stack_direction (NULL, (size & 1) + 20);
- # endif
-
- {
- register header *hp;
- # ifdef emacs
- BLOCK_INPUT;
- # endif
- for (hp = last_alloca_header; hp != NULL;)
- if ((STACK_DIR > 0 && hp->h.deep > depth)
- || (STACK_DIR < 0 && hp->h.deep < depth))
- {
- register header *np = hp->h.next;
- free (hp);
- hp = np;
- }
- else
- break;
- last_alloca_header = hp;
- # ifdef emacs
- UNBLOCK_INPUT;
- # endif
- }
- if (size == 0)
- return NULL;
-
- {
-
- register header *new;
- size_t combined_size = sizeof (header) + size;
- if (combined_size < sizeof (header))
- memory_full ();
- new = malloc (combined_size);
- if (! new)
- memory_full ();
- new->h.next = last_alloca_header;
- new->h.deep = depth;
- last_alloca_header = new;
-
- return (void *) (new + 1);
- }
- }
- # endif
- #endif
|