sbrk.html 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <html>
  2. <head>
  3. <title>sbrk</title>
  4. <body bgcolor=#ffffff>
  5. <h2 align=center>sbrk</h2>
  6. <h4 align=center>OS/161 Reference Manual</h4>
  7. <h3>Name</h3>
  8. sbrk - set process break (allocate memory)
  9. <h3>Library</h3>
  10. Standard C Library (libc, -lc)
  11. <h3>Synopsis</h3>
  12. #include &lt;unistd.h&gt;<br>
  13. <br>
  14. void *<br>
  15. sbrk(intptr_t <em>amount</em>);
  16. <h3>Description</h3>
  17. The "break" is the end address of a process's heap region. The sbrk
  18. call adjusts the "break" by the amount <em>amount</em>. It returns the
  19. old "break". Thus, to determine the current "break", call sbrk(0).
  20. <p>
  21. The heap region is initially empty, so at process startup, the
  22. beginning of the heap region is the same as the end and may thus be
  23. retrieved using sbrk(0).
  24. <p>
  25. In OS/161, the initial "break" must be page-aligned, and sbrk only
  26. need support values of <em>amount</em> that result in page-aligned
  27. "break" addresses. Other values of <em>amount</em> may be rejected.
  28. (This may simplify the implementation. On the other hand, you may
  29. choose to support unaligned values anyway, as that may simplify your
  30. malloc code.)
  31. <p>
  32. Traditionally, the initial "break" is specifically defined to be the
  33. end of the BSS (uninitialized data) region, and any <em>amount</em>,
  34. page-aligned or not, may legally be used with sbrk.
  35. <p>
  36. Ordinarily, user-level code should call
  37. <A HREF=../libc/malloc.html>malloc</A> for memory allocation. The
  38. sbrk interface is intended only to be the back-end interface for
  39. malloc. Mixing calls to malloc and sbrk will likely confuse malloc and
  40. produces undefined behavior.
  41. <p>
  42. While one can lower the "break" by passing negative values of
  43. <em>amount</em>, one may not set the end of the heap to an address
  44. lower than the beginning of the heap. Attempts to do so must be
  45. rejected.
  46. <p>
  47. <h3>Return Values</h3>
  48. On success, sbrk returns the previous value of the "break". On error,
  49. ((void *)-1) is returned, and <A HREF=errno.html>errno</A> is set
  50. according to the error encountered.
  51. <h3>Errors</h3>
  52. The following error codes should be returned under the conditions
  53. given. Other error codes may be returned for other errors not
  54. mentioned here.
  55. <blockquote><table width=90%>
  56. <td width=10%>&nbsp;</td><td>&nbsp;</td></tr>
  57. <tr><td>ENOMEM</td> <td>Sufficient virtual memory to satisfy the
  58. request was not available, or the
  59. process has reached the limit of the
  60. memory it is allowed to allocate.</td></tr>
  61. <tr><td>EINVAL</td> <td>The request would move the "break" below
  62. its initial value.</td></tr>
  63. </table></blockquote>
  64. <h3>Restrictions</h3>
  65. While you can return pages that happen to be at the end of the heap to
  66. the system, there is no way to use the sbrk interface to return unused
  67. pages in the middle of the heap. If you wish to do this, you will need
  68. to design a new or supplemental interface.
  69. </body>
  70. </html>