0

我无法从概念上理解这个系统调用结束时发生了什么,以及为什么。我了解 getstk.c 方法返回可用空间的最高内存地址,但不明白某些代码在做什么。对此进行一些澄清会很棒。我不完全理解的代码区域用星号强调。

/* getstk.c - getstk */

#include <xinu.h>

/*------------------------------------------------------------------------
 *  getstk  -  Allocate stack memory, returning highest word address
 *------------------------------------------------------------------------
 */
char    *getstk(
          uint32        nbytes          /* size of memory requested     */
        )
{
        intmask mask;                   /* saved interrupt mask         */
        struct  memblk  *prev, *curr;   /* walk through memory list     */
        struct  memblk  *fits, *fitsprev; /* record block that fits     */

        mask = disable();
        if (nbytes == 0) {
                restore(mask);
                return (char *)SYSERR;
        }

        nbytes = (uint32) roundmb(nbytes);      /* use mblock multiples */

        prev = &memlist;
        curr = memlist.mnext;
        fits = NULL;
        fitsprev = NULL;  /* to avoid a compiler warning */

        while (curr != NULL) {                  /* scan entire list     */
                if (curr->mlength >= nbytes) {  /* record block address */
                        fits = curr;            /*   when request fits  */
                        fitsprev = prev;
                }
                prev = curr;
                curr = curr->mnext;
        }

        if (fits == NULL) {                     /* no block was found   */
                restore(mask);
                return (char *)SYSERR;
        }
        if (nbytes == fits->mlength) {          /* block is exact match */
                fitsprev->mnext = fits->mnext;
        **} else {                                /* remove top section   */
                fits->mlength -= nbytes;
                fits = (struct memblk *)((uint32)fits + fits->mlength);
        }**
        memlist.mlength -= nbytes;
        restore(mask);
        **return (char *)((uint32) fits + nbytes - sizeof(uint32));**
}

可以在这里找到结构 memblk:

struct  memblk  {           /* see roundmb & truncmb    */
    struct  memblk  *mnext;     /* ptr to next free memory blk  */
    uint32  mlength;        /* size of blk (includes memblk)*/
    };
extern  struct  memblk  memlist;    /* head of free memory list */

为什么他们返回 fit + nbytes - sizeof(uint32)?为什么他们将 fit(a struct) 转换为 uint32 类型?

4

1 回答 1

1
    if (nbytes == fits->mlength) {          /* block is exact match */
            fitsprev->mnext = fits->mnext;
    **} else {                                /* remove top section   */
            fits->mlength -= nbytes;
            fits = (struct memblk *)((uint32)fits + fits->mlength);
    }**
    memlist.mlength -= nbytes;
    restore(mask);
    **return (char *)((uint32) fits + nbytes - sizeof(uint32));**

如果找到了完美匹配,则该块将从空闲列表中删除。如果找到更大的块,则将该块拆分为两个块:一个nbytes小于原始块的空闲块(fits->mlength -= nbytes);nbytes以及在新的空闲块 ( ) 之后开始分配的块,fits = (struct memblk *)((uint32)fits + fits->mlength)由函数返回。

为什么他们返回 fit + nbytes - sizeof(uint32)?为什么他们将 fit(a struct) 转换为 uint32 类型?

由于在这种情况下堆栈会向下增长,因此该函数返回一个指向堆栈顶部的指针,该指针是分配块末尾的单词,即:

(uint32)fits /* start of allocated block */ + nbytes /* size of allocated block */ - sizeof(uint32) /* size of a word */

强制转换(uint32)是使用整数算术而不是指针算术,否则fits+1会产生一个指向sizeof(struct memblk)超出的指针fits。转换为(char *)可能会更惯用。

于 2013-04-27T12:47:30.883 回答