判断堆栈增长方向

判断堆栈增长方向

说明:今天看到alloca.c中find_stack_direction函数,用来判断堆栈的增长方向,记录下来。

#define ADDRESS_FUNCTION(arg) &(arg)

/* Define STACK_DIRECTION if you know the direction of stack
   growth for your system; otherwise it will be automatically
   deduced at run-time.

   STACK_DIRECTION > 0 => grows toward higher addresses
   STACK_DIRECTION < 0 => grows toward lower addresses
   STACK_DIRECTION = 0 => direction of growth unknown  */

#ifndef STACK_DIRECTION
#define    STACK_DIRECTION    0    /* Direction unknown.  */
#endif

#if STACK_DIRECTION != 0

#define    STACK_DIR    STACK_DIRECTION    /* Known at compile-time.  */

#else /* STACK_DIRECTION == 0; need run-time code.  */

static int stack_dir;        /* 1 or -1 once known.  */
#define    STACK_DIR    stack_dir

static void
find_stack_direction ()
{
  static char *addr = NULL;    /* Address of first `dummy', once known.  */
  auto char dummy;        /* To get stack address.  */

  if (addr == NULL)
    {                /* Initial entry.  */
      addr = ADDRESS_FUNCTION (dummy);

      find_stack_direction ();    /* Recurse once.  */
    }
  else
    {
      /* Second entry.  */
      if (ADDRESS_FUNCTION (dummy) > addr)
    stack_dir = 1;        /* Stack grew upward.  */
      else
    stack_dir = -1;        /* Stack grew downward.  */
    }
}

#endif /* STACK_DIRECTION == 0 */

 

你可能感兴趣的:(function,null,System)