Chinaunix首页 | 论坛 | 博客

分类: LINUX

2011-10-01 14:44:56

说到这里:static long i100afunc(long *address)这个函数是试探可用堆栈的大小。
[code]
360   while (trailer != 0)
361     {
362       block = (long *) trailer->this_address;
363       size = trailer->this_size;
364       if (block == 0 || size == 0)
365         abort ();
366       trailer = (struct stk_trailer *) trailer->link;  
367       if ((block <= address) && (address < (block + size)))
368         break;
369     }
/*这段函数是找到本程序所用到的堆栈尾*/
这个trailer是个很特别的空间,将堆栈内各个段联系起来
result = address - block;是将从本地址开始的计入可以分配空间。
. .  .
381   do
382     {
383       if (trailer->this_size <= 0)
384         abort ();
385       result += trailer->this_size;
386       trailer = (struct stk_trailer *) trailer->link;
387     }
388   while (trailer != 0);
/*这段函数显然一直到程序堆栈末尾加入可以分配空间大小。内核为此堆栈分配的空间是有限的,不够是可以扩展的do_page_fault()....*/
最终是return result;
[/code]
_____________________________________________________________________________________________
___________------------>返回到alloca()函数 /*/lib/alloca.c*/[code]
[code]
alloca()
{
. . .
#if STACK_DIRECTION == 0
if (STACK_DIR == 0)  /*这是堆栈扩展方向不明*/
  STACK_DIR = find_stack_direction(NULL, (size & 1) + 20); /*/lib/alloca.c__________----------->*/
 . . .
}

static int  find_stack_direction(int *addr, int depth)
{
   101   int dir, dummy = 0;
102   if (! addr)
103     addr = &dummy;
104   *addr = addr < &dummy ? 1 : addr == &dummy ? 0 : -1;
105   dir = depth ? find_stack_direction (addr, depth - 1) : 0;
106   return dir + dummy;
107
 }
/*这段函数是个神奇的函数,堆栈地址0上再分配一个dummy,然后利用104行来判断方向,并且用addr记录此时可分配的空间,然后利用递归来相加得到可以最后得到的空间。*/
[/code]<--------------------________________________
[code]
返回到alloca
{
 . . .
{                          /*这个{是if的,因为看前面的定义这是一定执行的*/
register header *hp; /*Traverses linked list_______------->/lib/alloca.c*/
 .. .. ..
}
__________--------------->
typedef union hdr
{
   char alain[ALAIN_SIZE];   /*#define ALIGN_SIZE sizeof(double) 长度是8,这是对齐作用*/
   struct {
     union hdr *next; /*链上的元素*/
     char *deep;   /*测试堆栈的深度*/
  } h;
}header;
/*这是来收集上次分配所有空间*/
<-------------____________[code]
alloca ()
{
 . . . .
162     for (hp = last_alloca_header; hp != NULL;)  /*#define  *last_alloca_header  = NULL */
163       if ((STACK_DIR > 0 && hp->h.deep > depth)
164           || (STACK_DIR < 0 && hp->h.deep < depth)) /*分别对应堆栈生长的方向*/
165         {
166           register header *np = hp->h.next;
167
168           free (hp);            /* Collect garbage.  */
169
170           hp = np;              /* -> next header.  */
171         }
172       else
173         break;                  /* Rest are not deeper.  */
174
175     last_alloca_header = hp;    /* -> last valid storage.  */
176
         . . .
 
 
180   }
   . . .

189     register header *new;
190
191     size_t combined_size = sizeof (header) + size;   
192     if (combined_size < sizeof (header))  /*如果size + size0f(header) < sizeof(header)说明 内存满*/
193       memory_full ();  /*#define memeory_full() abort() ,就是挂掉*/
194
195     new = malloc (combined_size);   /*堆中非配记录空间*/
196
197     if (! new)
198       memory_full ();   
199
200     new->h.next = last_alloca_header;
201     new->h.deep = depth;  
202
203     last_alloca_header = new;
204
205     /* User storage begins just after header.  */
206
207     return (void *) (new + 1);  /*返回一个记录的地址*/
208   }
209 }
[/code]
阅读(1137) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~