<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv=Content-Type content="text/html; charset=utf8">
<title>/usr/web/sources/contrib/vsrinivas/stackalloc.c - Plan 9 from Bell Labs</title>
<!-- THIS FILE IS AUTOMATICALLY GENERATED. -->
<!-- EDIT sources.tr INSTEAD. -->
</meta>
</head>
<body>
<p style="margin-top: 0; margin-bottom: 0.17in"></p>
<p style="line-height: 1.2em; margin-left: 1.00in; text-indent: 0.00in; margin-right: 1.00in; margin-top: 0; margin-bottom: 0; text-align: center;">
<span style="font-size: 10pt"><a href="/plan9/">Plan 9 from Bell Labs</a>&rsquo;s /usr/web/sources/contrib/vsrinivas/stackalloc.c</span></p>
<p style="margin-top: 0; margin-bottom: 0.17in"></p>
<p style="margin-top: 0; margin-bottom: 0.17in"></p>
<center><font size=-1>
Copyright © 2009 Alcatel-Lucent.<br />
Distributed under the
<a href="/plan9/license.html">Lucent Public License version 1.02</a>.
<br />
<a href="/plan9/download.html">Download the Plan 9 distribution.</a>
</font>
</center>
<p style="margin-top: 0; margin-bottom: 0.17in"></p>
<table width="100%" cellspacing=0 border=0><tr><td align="center">
<table cellspacing=0 cellpadding=5 bgcolor="#eeeeff"><tr><td align="left">
<pre>
<!-- END HEADER -->
/*
* Allocate thread stacks
*
* The Plan 9 virtual address allocator works from the highest address below
* the bottom of the user stack downwards. That is:
*      (0x0) | TTTT             ...SSSUUU | KKKKKK | (~0x0)
*
* Each thread stack is flanked by two red zones, each one page, immediately
* above and below. We bypass the virtual address allocator by requesting
* stacks and redzones at specific addresses.
*
* To allocate the first thread stack, we call segattach(SG_NONE...) and use
* the address returned by the VM as a 'base' for the red zones. We then
* allocate a red zone prior to the start of the thread stack, followed by
* the stack itself:
*      (0x0) | TTTT            ....RSRUUU | KKKKKK | (~0x0)
*
* Subsequent thread stack allocations are at lower addresses, reusing the
* lower redzone of an adjacent thread stack if possible.
*
* We keep an address-ordered list of redzones; on stack allocation, we scan
* the space between unused redzones for a hole, before attempting to allocate
* a new one.
*
* Stack frees release the pages backing the stack but not the redzones, unless
* we free the lowest stack.
*/

#include &lt;u.h&gt;
#include &lt;libc.h&gt;
#include &lt;thread.h&gt;
#include "threadimpl.h"

#define BY2PG (4096)

struct redzone {
       struct redzone *next;
       char *addr; /* Redzone address */
       int right;
};

static Lock stklock;
static struct redzone *low;

void *_stackmalloc(unsigned long size) {
       int pgssize;
       char *base, *rztop, *rzbottom, *p, *bp;
       struct redzone *rzt;

       pgssize = (size) + (BY2PG - 1) &amp; ~(BY2PG - 1);

       lock(&amp;stklock);

       if (low == nil) {
               low = mallocz(sizeof(struct redzone), 1);
               if (low == nil) {
                       unlock(&amp;stklock);
                       return nil;
               }

               rztop = segattach(SG_NONE | SG_SAS | SG_CEXEC, "memory", nil, BY2PG);
               if (rztop == (void *) -1) {
                       free(low);
                       low = nil;
                       unlock(&amp;stklock);
                       return nil;
               }

               low-&gt;addr = rztop;
       }

       if(low-&gt;next == nil) {
               rztop = low-&gt;addr;
               p = rztop - pgssize - BY2PG;
               rzbottom = segattach(SG_NONE | SG_SAS | SG_CEXEC, "memory", p, BY2PG);
               if (rzbottom == (void *) -1) {
                       unlock(&amp;stklock);
                       return nil;
               }

               rzt = mallocz(sizeof(struct redzone), 1);
               if (rzt == nil) {
                       segdetach(rzbottom);
                       unlock(&amp;stklock);
                       return nil;
               }

               rzt-&gt;addr = rzbottom;
               rzt-&gt;next = low;
               low = rzt;
       }

       for(rzt = low; rzt-&gt;next != nil;) {
               if (((rzt-&gt;next-&gt;addr - rzt-&gt;addr + BY2PG) &gt;= pgssize) &amp;&amp; !rzt-&gt;right) {
                       break;
               }
               rzt = rzt-&gt;next;
       }

       if (rzt-&gt;next == nil) {
               /* We could not find a hole; we must allocate below 'low' */
               bp = low-&gt;addr - pgssize;
               base = segattach(SG_COMMIT | SG_SAS | SG_CEXEC, "memory", bp, pgssize);
               if (base == (void *) -1) {
                       unlock(&amp;stklock);
                       return nil;
               }

               p = base - BY2PG;
               rzbottom = segattach(SG_NONE | SG_SAS | SG_CEXEC, "memory", p, BY2PG);
               if (rzbottom == (void *) -1) {
                       segdetach(base);
                       unlock(&amp;stklock);
                       return nil;
               }

               rzt = mallocz(sizeof(struct redzone), 1);
               if (rzt == nil) {
                       segdetach(rzbottom);
                       segdetach(base);
                       unlock(&amp;stklock);
                       return nil;
               }

               rzt-&gt;next = low;
               rzt-&gt;addr = rzbottom;
               low = rzt;
               low-&gt;right = 1;

               unlock(&amp;stklock);
               return base;

       } else {
               /* We found a hole */
               bp = rzt-&gt;addr + BY2PG;
               base = segattach(SG_COMMIT | SG_SAS | SG_CEXEC, "memory", bp, pgssize);
               if (base == (void *) -1) {
                       unlock(&amp;stklock);
                       return nil;
               }
               rzt-&gt;right = 1;

               unlock(&amp;stklock);
               return base;
       }

       /* NOTREACHED */
}

void _stackfree(void *stack, unsigned long size) {
       char *stk;
       struct redzone *nz;
       int found;

       stk = stack;

       if (stack == nil)
               return;

       /* Freeing the lowest stack segment */
       if ((stk - BY2PG) == low-&gt;addr) {
               nz = low-&gt;next;

               segdetach(low-&gt;addr);
               segdetach(stk);

               free(low);
               low = nz;
       } else {
               segdetach(stk);

               found = 0;
               for (nz = low; nz-&gt;next != nil; nz = nz-&gt;next)
                       if (nz-&gt;addr == (stk - BY2PG)) {
                               if (found == 1) {
                                       found = 0;
                                       break;
                               }
                               found = 1;
                               nz-&gt;right = 0;
                       }
               if (found == 0)
                       print("Redzone list error \n");
       }
}
<!-- BEGIN TAIL -->
</pre>
</td></tr></table>
</td></tr></table>
<p style="margin-top: 0; margin-bottom: 0.17in"></p>
<p style="line-height: 1.2em; margin-left: 1.00in; text-indent: 0.00in; margin-right: 1.00in; margin-top: 0; margin-bottom: 0; text-align: center;">
<span style="font-size: 10pt"></span></p>
<p style="margin-top: 0; margin-bottom: 0.50in"></p>
<p style="margin-top: 0; margin-bottom: 0.33in"></p>
<center><table border="0"><tr>
<td valign="middle"><a href="http://www.alcatel-lucent.com/"><img border="0" src="/plan9/img/logo_ft.gif" alt="Bell Labs" />
</a></td>
<td valign="middle"><a href="http://www.opensource.org"><img border="0" alt="OSI certified" src="/plan9/img/osi-certified-60x50.gif" />
</a></td>
<td><img style="padding-right: 45px;" alt="Powered by Plan 9" src="/plan9/img/power36.gif" />
</td>
</tr></table></center>
<p style="margin-top: 0; margin-bottom: 0.17in"></p>
<center>
<span style="font-size: 10pt">(<a href="/plan9/">Return to Plan 9 Home Page</a>)</span>
</center>
<p style="margin-top: 0; margin-bottom: 0.17in"></p>
<center><font size=-1>
<span style="font-size: 10pt"><a href="http://www.lucent.com/copyright.html">Copyright</a></span>
<span style="font-size: 10pt">© 2009 Alcatel-Lucent.</span>
<span style="font-size: 10pt">All Rights Reserved.</span>
<br />
<span style="font-size: 10pt">Comments to</span>
<span style="font-size: 10pt"><a href="mailto:[email protected]">[email protected]</a>.</span>
</font></center>
</body>
</html>