我在概念上理解本系统调用结束时会发生什么问题,以及为什么。我明白getstk.c方法返回可用空间的最高内存地址,但不明白一些代码在做什么。这方面的一些清晰度会很好。星号强调我不完全理解的代码区域。XINU - 需要帮助了解系统调用 - 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 */
为什么他们返回拟合+的nbytes - 的sizeof(UINT32)?为什么他们适合(一个结构)来键入uint32?
我还是不太明白为什么这样做: fits =(struct memblk *)((uint32)fits + fits-> mlength); 返回值仍然让我失望:( – kamran619 2013-04-27 18:09:02
@ blackdemon619:'fits'指向分配块的底部,当我们找到一个更大的块时,该块被分成两部分,第二部分从' (uint32)符合+ fit-> mlength'。 – ninjalj 2013-04-27 23:29:30