Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Jump to content

Sbrk: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
No edit summary
 
(41 intermediate revisions by 35 users not shown)
Line 1: Line 1:
{{Short description|Basic memory management system calls used in Unix}}
{{redirect|brk|the NYSE stock symbol BRK|Berkshire Hathaway| the software interrupt BRK| Interrupts in 65xx processors}}
{{lowercase}}
{{lowercase title}}
{{cleanup rewrite|date=February 2012}}


'''{{mono|brk}}''' and '''{{mono|sbrk}}''' are basic [[memory management]] [[system call]]s used in [[Unix]] and [[Unix-like]] operating systems to control the amount of memory allocated to the heap segment of the [[process (computing)|process]].<ref>{{cite web|url=https://www.gnu.org/software/libc/manual/html_node/Memory-Concepts.html|title=Process Memory Concepts|publisher=Free Software Foundation|accessdate=9 October 2015}}</ref> These functions are typically called from a higher-level memory management library function such as {{mono|[[malloc]]}}. In the original Unix system, {{mono|brk}} and {{mono|sbrk}} were the only ways in which applications could acquire additional heap space; later versions allowed this to also be done using the {{mono|[[mmap]]}} call.<ref name=bsdmalloc>{{cite web|title=A new malloc(3) for OpenBSD|url=http://www.openbsd.org/papers/eurobsdcon2009/otto-malloc.pdf|accessdate=13 June 2018}}</ref><ref>{{cite web |title=POSIX Memory Management |url=http://blog.pr4tt.com/2016/02/01/posix-memory-management/ |website=blog.pr4tt.com}}</ref>
{{cleanup-rewrite|date=February 2012}}
{{Technical|date=August 2009}}

'''brk''' and '''sbrk''' are basic [[memory management]] [[system call]]s used in [[Unix]] and [[Unix-like]] operating systems to control the amount of memory allocated to the [[data segment]] of the [[Process (computing)|process]]. These calls are typically made from a higher-level memory management library such as [[malloc]]. In the original Unix system, brk and sbrk were the only ways in which applications could acquire additional data space; later versions allowed this to also be done using the [[mmap]] call.


==Description==
==Description==
The brk and sbrk calls dynamically change the amount of space allocated for the data segment of the calling process. The change is made by resetting the [[program break]] of the process, which determines the maximum space that can be allocated. The program break is the address of the first location beyond the current end of the data region. The amount of available space increases as the break value increases. The available space is initialized to a value of zero. The break value can be automatically rounded up to a size appropriate for the [[memory management]] architecture.
The brk and sbrk calls [[dynamic memory allocation|dynamically change]] the amount of space allocated for the heap segment of the calling process. The change is made by resetting the program break of the process, which determines the maximum space that can be allocated. The program break is the address of the first location beyond the current end of the data region. The amount of available space increases as the break value increases. The available space is initialized to a value of zero, unless the break is lowered and then increased, as it may reuse the same pages in some unspecified way. The break value can be automatically rounded up to a size appropriate for the [[memory management]] architecture.<ref name=OpenGrpStdSIH>{{cite book|title=X/Open CAE Specification, System Interfaces and Headers |date=September 1994|issue=4|edition=2|publisher=X/Open Company Ltd., U.K.|page=64|url=http://pubs.opengroup.org/onlinepubs/9695969499/toc.pdf|accessdate=9 October 2015}}</ref>

{{mono|sbrk}} and {{mono|brk}} were considered legacy even by 1997 standards ([[Single UNIX Specification]] v2 or POSIX.1-1998).<ref name=SUSv2>{{cite web |title=brk, sbrk - change space allocation (LEGACY) |url=https://pubs.opengroup.org/onlinepubs/7908799/xsh/brk.html |website=The Single UNIX ® Specification, Version 2 |accessdate=30 November 2019 |date=1997}}</ref> They were removed in POSIX.1-2001.<ref>{{man|2|brk|Linux}}</ref>


==Function signatures and behavior==
==Function signatures and behavior==
<syntaxhighlight lang="c">
<pre>
#include <unistd.h>
#include <unistd.h>


int brk(void *end_data_segment);
int brk(void* end_data_segment);

void *sbrk(intptr_t increment);
void *sbrk(intptr_t increment);
</syntaxhighlight>
</pre>
The brk subroutine sets the program break value to the value of the ''end_data_segment'' parameter and changes the amount of available space accordingly.
{{mono|sbrk}} is used to adjust the program break value by adding a possibly negative size, while {{mono|brk}} is used to set the break value to the value of a pointer. Set {{var|increment}} parameter to zero to fetch the current value of the program break.


Upon successful completion, the {{mono|brk}} subroutine returns a value of 0, and the {{mono|sbrk}} subroutine returns the prior value of the program break (if the available space is increased then this prior value also points to the start of the new area). If either subroutine is unsuccessful, a value of {{num|−1}}<!-- this is a running text in Wikipedia, not a C code, so avoid ASCII surrogates please --> is returned and the {{mono|[[errno.h|errno]]}} [[global variable]] is set to indicate the error.<ref name=SUSv2/>
The sbrk subroutine adds to the program break value the number of bytes contained in the ''increment'' parameter and changes the amount of available space accordingly. The ''increment'' parameter can be a [[negative number]], in which case the amount of available space is decreased.


Not every Unix-like system entertains the concept of having the user control the data segment. The [[Mac OS X]] implementation of {{mono|sbrk}} is an emulation and has a maximum allocation of 4 megabytes. On first call an area exactly this large is allocated to hold the simulated segment. When this limit is reached, −1 is returned and the {{mono|errno}} is set to {{mono|ENOMEM}}. {{mono|brk}} always errors.<ref>{{Cite web|url=https://opensource.apple.com/source/Libc/Libc-1272.250.1/emulated/brk.c.auto.html|title=BRK.c}}</ref>
Upon successful completion, the brk subroutine returns a value of 0, and the sbrk subroutine returns the prior value of the program break (if the available space is increased the return value points to the start of the new area). If either subroutine is unsuccessful, a value of -1 is returned and the [[Errno.h|errno]] [[global variable]] is set to indicate the error.

The current [[Mac OS X]] implementation of sbrk is an emulation, and has a maximum allocation of 4 Megabytes.<ref>http://www.opensource.apple.com/source/Libc/Libc-763.12/emulated/brk.c</ref> When this limit is reached, -1 is returned and the errno is not set.


==Error codes==
==Error codes==
The error ''ENOMEM'' is set and the allocated space remains unchanged if one or more of the following are true:
The error {{mono|ENOMEM}} is set and the allocated space remains unchanged if one or more of the following are true:
* The requested change allocates more space than is allowed by a system-imposed maximum.
* The requested change allocates more space than is allowed by a system-imposed maximum.
* The requested change sets the break value to a value [[inequality (mathematics)|greater than or equal to]] the start address of any attached [[shared memory]] segment.
* The requested change sets the break value to a value [[inequality (mathematics)|greater than or equal to]] the start address of any attached [[Shared memory (interprocess communication)|shared memory]] segment.


== See also ==
== See also ==


* [[Exec (operating system)]]
* [[Exec (computing)]]
* {{section link|Memory address|Address space in application programming}}
* [[Shared memory]]


==References==
==References==

* [http://linux.die.net/man/2/sbrk sbrk &ndash; Linux man page]
{{Reflist}}
{{Reflist}}


[[Category:Memory management software]]
[[Category:Memory management]]
[[Category:Unix process and task management-related software]]
[[Category:Operating system APIs]]


{{Unix-stub}}

Latest revision as of 07:51, 15 May 2024

brk and sbrk are basic memory management system calls used in Unix and Unix-like operating systems to control the amount of memory allocated to the heap segment of the process.[1] These functions are typically called from a higher-level memory management library function such as malloc. In the original Unix system, brk and sbrk were the only ways in which applications could acquire additional heap space; later versions allowed this to also be done using the mmap call.[2][3]

Description

[edit]

The brk and sbrk calls dynamically change the amount of space allocated for the heap segment of the calling process. The change is made by resetting the program break of the process, which determines the maximum space that can be allocated. The program break is the address of the first location beyond the current end of the data region. The amount of available space increases as the break value increases. The available space is initialized to a value of zero, unless the break is lowered and then increased, as it may reuse the same pages in some unspecified way. The break value can be automatically rounded up to a size appropriate for the memory management architecture.[4]

sbrk and brk were considered legacy even by 1997 standards (Single UNIX Specification v2 or POSIX.1-1998).[5] They were removed in POSIX.1-2001.[6]

Function signatures and behavior

[edit]
#include <unistd.h>

int brk(void* end_data_segment);
void *sbrk(intptr_t increment);

sbrk is used to adjust the program break value by adding a possibly negative size, while brk is used to set the break value to the value of a pointer. Set increment parameter to zero to fetch the current value of the program break.

Upon successful completion, the brk subroutine returns a value of 0, and the sbrk subroutine returns the prior value of the program break (if the available space is increased then this prior value also points to the start of the new area). If either subroutine is unsuccessful, a value of −1 is returned and the errno global variable is set to indicate the error.[5]

Not every Unix-like system entertains the concept of having the user control the data segment. The Mac OS X implementation of sbrk is an emulation and has a maximum allocation of 4 megabytes. On first call an area exactly this large is allocated to hold the simulated segment. When this limit is reached, −1 is returned and the errno is set to ENOMEM. brk always errors.[7]

Error codes

[edit]

The error ENOMEM is set and the allocated space remains unchanged if one or more of the following are true:

  • The requested change allocates more space than is allowed by a system-imposed maximum.
  • The requested change sets the break value to a value greater than or equal to the start address of any attached shared memory segment.

See also

[edit]

References

[edit]
  1. ^ "Process Memory Concepts". Free Software Foundation. Retrieved 9 October 2015.
  2. ^ "A new malloc(3) for OpenBSD" (PDF). Retrieved 13 June 2018.
  3. ^ "POSIX Memory Management". blog.pr4tt.com.
  4. ^ X/Open CAE Specification, System Interfaces and Headers (PDF) (2 ed.). X/Open Company Ltd., U.K. September 1994. p. 64. Retrieved 9 October 2015.
  5. ^ a b "brk, sbrk - change space allocation (LEGACY)". The Single UNIX ® Specification, Version 2. 1997. Retrieved 30 November 2019.
  6. ^ brk(2) – Linux Programmer's Manual – System Calls
  7. ^ "BRK.c".