~ubuntu-branches/ubuntu/maverick/wget/maverick

« back to all changes in this revision

Viewing changes to src/alloca.c

  • Committer: Bazaar Package Importer
  • Author(s): Michael Vogt
  • Date: 2008-05-27 11:49:54 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20080527114954-ame070pjhqtofeaf
Tags: 1.11.2-1ubuntu1
* Merge from debian unstable, remaining changes:
  - Add wget-udeb to ship wget.gnu as alternative to busybox wget
    implementation.

Show diffs side-by-side

added added

removed removed

Lines of Context:
74
74
#endif
75
75
 
76
76
#ifndef NULL
77
 
#define NULL    0
 
77
#define NULL    0
78
78
#endif
79
79
 
80
80
/* Different portions of Emacs need to call different versions of
103
103
   STACK_DIRECTION = 0 => direction of growth unknown  */
104
104
 
105
105
#ifndef STACK_DIRECTION
106
 
#define STACK_DIRECTION 0       /* Direction unknown.  */
 
106
#define STACK_DIRECTION 0       /* Direction unknown.  */
107
107
#endif
108
108
 
109
109
#if STACK_DIRECTION != 0
110
110
 
111
 
#define STACK_DIR       STACK_DIRECTION /* Known at compile-time.  */
 
111
#define STACK_DIR       STACK_DIRECTION /* Known at compile-time.  */
112
112
 
113
113
#else /* STACK_DIRECTION == 0; need run-time code.  */
114
114
 
115
 
static int stack_dir;           /* 1 or -1 once known.  */
116
 
#define STACK_DIR       stack_dir
 
115
static int stack_dir;           /* 1 or -1 once known.  */
 
116
#define STACK_DIR       stack_dir
117
117
 
118
118
static void
119
119
find_stack_direction ()
120
120
{
121
 
  static char *addr = NULL;     /* Address of first `dummy', once known.  */
122
 
  auto char dummy;              /* To get stack address.  */
 
121
  static char *addr = NULL;     /* Address of first `dummy', once known.  */
 
122
  auto char dummy;              /* To get stack address.  */
123
123
 
124
124
  if (addr == NULL)
125
 
    {                           /* Initial entry.  */
 
125
    {                           /* Initial entry.  */
126
126
      addr = ADDRESS_FUNCTION (dummy);
127
127
 
128
 
      find_stack_direction ();  /* Recurse once.  */
 
128
      find_stack_direction ();  /* Recurse once.  */
129
129
    }
130
130
  else
131
131
    {
132
132
      /* Second entry.  */
133
133
      if (ADDRESS_FUNCTION (dummy) > addr)
134
 
        stack_dir = 1;          /* Stack grew upward.  */
 
134
        stack_dir = 1;          /* Stack grew upward.  */
135
135
      else
136
 
        stack_dir = -1;         /* Stack grew downward.  */
 
136
        stack_dir = -1;         /* Stack grew downward.  */
137
137
    }
138
138
}
139
139
 
146
146
   It is very important that sizeof(header) agree with malloc
147
147
   alignment chunk size.  The following default should work okay.  */
148
148
 
149
 
#ifndef ALIGN_SIZE
150
 
#define ALIGN_SIZE      sizeof(double)
 
149
#ifndef ALIGN_SIZE
 
150
#define ALIGN_SIZE      sizeof(double)
151
151
#endif
152
152
 
153
153
typedef union hdr
154
154
{
155
 
  char align[ALIGN_SIZE];       /* To force sizeof(header).  */
 
155
  char align[ALIGN_SIZE];       /* To force sizeof(header).  */
156
156
  struct
157
157
    {
158
 
      union hdr *next;          /* For chaining headers.  */
159
 
      char *deep;               /* For stack depth measure.  */
 
158
      union hdr *next;          /* For chaining headers.  */
 
159
      char *deep;               /* For stack depth measure.  */
160
160
    } h;
161
161
} header;
162
162
 
163
 
static header *last_alloca_header = NULL;       /* -> last alloca header.  */
 
163
static header *last_alloca_header = NULL;       /* -> last alloca header.  */
164
164
 
165
165
/* Return a pointer to at least SIZE bytes of storage,
166
166
   which will be automatically reclaimed upon exit from
173
173
alloca (size)
174
174
     unsigned size;
175
175
{
176
 
  auto char probe;              /* Probes stack depth: */
 
176
  auto char probe;              /* Probes stack depth: */
177
177
  register char *depth = ADDRESS_FUNCTION (probe);
178
178
 
179
179
#if STACK_DIRECTION == 0
180
 
  if (STACK_DIR == 0)           /* Unknown growth direction.  */
 
180
  if (STACK_DIR == 0)           /* Unknown growth direction.  */
181
181
    find_stack_direction ();
182
182
#endif
183
183
 
185
185
     was allocated from deeper in the stack than currently.  */
186
186
 
187
187
  {
188
 
    register header *hp;        /* Traverses linked list.  */
 
188
    register header *hp;        /* Traverses linked list.  */
189
189
 
190
190
#ifdef emacs
191
191
    BLOCK_INPUT;
193
193
 
194
194
    for (hp = last_alloca_header; hp != NULL;)
195
195
      if ((STACK_DIR > 0 && hp->h.deep > depth)
196
 
          || (STACK_DIR < 0 && hp->h.deep < depth))
197
 
        {
198
 
          register header *np = hp->h.next;
199
 
 
200
 
          free ((pointer) hp);  /* Collect garbage.  */
201
 
 
202
 
          hp = np;              /* -> next header.  */
203
 
        }
 
196
          || (STACK_DIR < 0 && hp->h.deep < depth))
 
197
        {
 
198
          register header *np = hp->h.next;
 
199
 
 
200
          free ((pointer) hp);  /* Collect garbage.  */
 
201
 
 
202
          hp = np;              /* -> next header.  */
 
203
        }
204
204
      else
205
 
        break;                  /* Rest are not deeper.  */
 
205
        break;                  /* Rest are not deeper.  */
206
206
 
207
 
    last_alloca_header = hp;    /* -> last valid storage.  */
 
207
    last_alloca_header = hp;    /* -> last valid storage.  */
208
208
 
209
209
#ifdef emacs
210
210
    UNBLOCK_INPUT;
212
212
  }
213
213
 
214
214
  if (size == 0)
215
 
    return NULL;                /* No allocation required.  */
 
215
    return NULL;                /* No allocation required.  */
216
216
 
217
217
  /* Allocate combined header + user data storage.  */
218
218
 
246
246
/* Stack structures for CRAY-1, CRAY X-MP, and CRAY Y-MP */
247
247
struct stack_control_header
248
248
  {
249
 
    long shgrow:32;             /* Number of times stack has grown.  */
250
 
    long shaseg:32;             /* Size of increments to stack.  */
251
 
    long shhwm:32;              /* High water mark of stack.  */
252
 
    long shsize:32;             /* Current size of stack (all segments).  */
 
249
    long shgrow:32;             /* Number of times stack has grown.  */
 
250
    long shaseg:32;             /* Size of increments to stack.  */
 
251
    long shhwm:32;              /* High water mark of stack.  */
 
252
    long shsize:32;             /* Current size of stack (all segments).  */
253
253
  };
254
254
 
255
255
/* The stack segment linkage control information occurs at
261
261
 
262
262
struct stack_segment_linkage
263
263
  {
264
 
    long ss[0200];              /* 0200 overflow words.  */
265
 
    long sssize:32;             /* Number of words in this segment.  */
266
 
    long ssbase:32;             /* Offset to stack base.  */
267
 
    long:32;
268
 
    long sspseg:32;             /* Offset to linkage control of previous
269
 
                                   segment of stack.  */
270
 
    long:32;
271
 
    long sstcpt:32;             /* Pointer to task common address block.  */
272
 
    long sscsnm;                /* Private control structure number for
273
 
                                   microtasking.  */
274
 
    long ssusr1;                /* Reserved for user.  */
275
 
    long ssusr2;                /* Reserved for user.  */
276
 
    long sstpid;                /* Process ID for pid based multi-tasking.  */
277
 
    long ssgvup;                /* Pointer to multitasking thread giveup.  */
278
 
    long sscray[7];             /* Reserved for Cray Research.  */
 
264
    long ss[0200];              /* 0200 overflow words.  */
 
265
    long sssize:32;             /* Number of words in this segment.  */
 
266
    long ssbase:32;             /* Offset to stack base.  */
 
267
    long:32;
 
268
    long sspseg:32;             /* Offset to linkage control of previous
 
269
                                   segment of stack.  */
 
270
    long:32;
 
271
    long sstcpt:32;             /* Pointer to task common address block.  */
 
272
    long sscsnm;                /* Private control structure number for
 
273
                                   microtasking.  */
 
274
    long ssusr1;                /* Reserved for user.  */
 
275
    long ssusr2;                /* Reserved for user.  */
 
276
    long sstpid;                /* Process ID for pid based multi-tasking.  */
 
277
    long ssgvup;                /* Pointer to multitasking thread giveup.  */
 
278
    long sscray[7];             /* Reserved for Cray Research.  */
279
279
    long ssa0;
280
280
    long ssa1;
281
281
    long ssa2;
299
299
   returned by the STKSTAT library routine.  */
300
300
struct stk_stat
301
301
  {
302
 
    long now;                   /* Current total stack size.  */
303
 
    long maxc;                  /* Amount of contiguous space which would
304
 
                                   be required to satisfy the maximum
305
 
                                   stack demand to date.  */
306
 
    long high_water;            /* Stack high-water mark.  */
307
 
    long overflows;             /* Number of stack overflow ($STKOFEN) calls.  */
308
 
    long hits;                  /* Number of internal buffer hits.  */
309
 
    long extends;               /* Number of block extensions.  */
310
 
    long stko_mallocs;          /* Block allocations by $STKOFEN.  */
311
 
    long underflows;            /* Number of stack underflow calls ($STKRETN).  */
312
 
    long stko_free;             /* Number of deallocations by $STKRETN.  */
313
 
    long stkm_free;             /* Number of deallocations by $STKMRET.  */
314
 
    long segments;              /* Current number of stack segments.  */
315
 
    long maxs;                  /* Maximum number of stack segments so far.  */
316
 
    long pad_size;              /* Stack pad size.  */
317
 
    long current_address;       /* Current stack segment address.  */
318
 
    long current_size;          /* Current stack segment size.  This
319
 
                                   number is actually corrupted by STKSTAT to
320
 
                                   include the fifteen word trailer area.  */
321
 
    long initial_address;       /* Address of initial segment.  */
322
 
    long initial_size;          /* Size of initial segment.  */
 
302
    long now;                   /* Current total stack size.  */
 
303
    long maxc;                  /* Amount of contiguous space which would
 
304
                                   be required to satisfy the maximum
 
305
                                   stack demand to date.  */
 
306
    long high_water;            /* Stack high-water mark.  */
 
307
    long overflows;             /* Number of stack overflow ($STKOFEN) calls.  */
 
308
    long hits;                  /* Number of internal buffer hits.  */
 
309
    long extends;               /* Number of block extensions.  */
 
310
    long stko_mallocs;          /* Block allocations by $STKOFEN.  */
 
311
    long underflows;            /* Number of stack underflow calls ($STKRETN).  */
 
312
    long stko_free;             /* Number of deallocations by $STKRETN.  */
 
313
    long stkm_free;             /* Number of deallocations by $STKMRET.  */
 
314
    long segments;              /* Current number of stack segments.  */
 
315
    long maxs;                  /* Maximum number of stack segments so far.  */
 
316
    long pad_size;              /* Stack pad size.  */
 
317
    long current_address;       /* Current stack segment address.  */
 
318
    long current_size;          /* Current stack segment size.  This
 
319
                                   number is actually corrupted by STKSTAT to
 
320
                                   include the fifteen word trailer area.  */
 
321
    long initial_address;       /* Address of initial segment.  */
 
322
    long initial_size;          /* Size of initial segment.  */
323
323
  };
324
324
 
325
325
/* The following structure describes the data structure which trails
328
328
 
329
329
struct stk_trailer
330
330
  {
331
 
    long this_address;          /* Address of this block.  */
332
 
    long this_size;             /* Size of this block (does not include
333
 
                                   this trailer).  */
 
331
    long this_address;          /* Address of this block.  */
 
332
    long this_size;             /* Size of this block (does not include
 
333
                                   this trailer).  */
334
334
    long unknown2;
335
335
    long unknown3;
336
 
    long link;                  /* Address of trailer block of previous
337
 
                                   segment.  */
 
336
    long link;                  /* Address of trailer block of previous
 
337
                                   segment.  */
338
338
    long unknown5;
339
339
    long unknown6;
340
340
    long unknown7;
372
372
  /* Set up the iteration.  */
373
373
 
374
374
  trailer = (struct stk_trailer *) (status.current_address
375
 
                                    + status.current_size
376
 
                                    - 15);
 
375
                                    + status.current_size
 
376
                                    - 15);
377
377
 
378
378
  /* There must be at least one stack segment.  Therefore it is
379
379
     a fatal error if "trailer" is null.  */
388
388
      block = (long *) trailer->this_address;
389
389
      size = trailer->this_size;
390
390
      if (block == 0 || size == 0)
391
 
        abort ();
 
391
        abort ();
392
392
      trailer = (struct stk_trailer *) trailer->link;
393
393
      if ((block <= address) && (address < (block + size)))
394
 
        break;
 
394
        break;
395
395
    }
396
396
 
397
397
  /* Set the result to the offset in this segment and add the sizes
407
407
  do
408
408
    {
409
409
      if (trailer->this_size <= 0)
410
 
        abort ();
 
410
        abort ();
411
411
      result += trailer->this_size;
412
412
      trailer = (struct stk_trailer *) trailer->link;
413
413
    }
470
470
      fprintf (stderr, "%011o %011o %011o\n", this_segment, address, stkl);
471
471
#endif
472
472
      if (pseg == 0)
473
 
        break;
 
473
        break;
474
474
      stkl = stkl - pseg;
475
475
      ssptr = (struct stack_segment_linkage *) stkl;
476
476
      size = ssptr->sssize;