I am having trouble conceptually understanding what is going on towards the end of this system call, and why. I understand the getstk.c method returns the highest memory address of available space, but don't understand what some of the code is doing. Some clarity on this would be great. The areas of code I don't fully understand are emphasized in asterisks./* 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));**}The struct memblk can be found here: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 */Why are they returning the fits + nbytes - sizeof(uint32)? Why are they casting fits(a struct) to type uint32?
Understanding a XINU system call - getstk.c
linux;kernel;c;system calls