sbrk
Script error: No such module "Unsubst".
<templatestyles src="Mono/styles.css" />brk and <templatestyles src="Mono/styles.css" />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 <templatestyles src="Mono/styles.css" />malloc. In the original Unix system, <templatestyles src="Mono/styles.css" />brk and <templatestyles src="Mono/styles.css" />sbrk were the only ways in which applications could acquire additional heap space; later versions allowed this to also be done using the <templatestyles src="Mono/styles.css" />mmap call.[2][3]
Description
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]
<templatestyles src="Mono/styles.css" />sbrk and <templatestyles src="Mono/styles.css" />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
#include <unistd.h>
int brk(void* end_data_segment);
void *sbrk(intptr_t increment);
<templatestyles src="Mono/styles.css" />sbrk is used to adjust the program break value by adding a possibly negative size, while <templatestyles src="Mono/styles.css" />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 <templatestyles src="Mono/styles.css" />brk subroutine returns a value of 0, and the <templatestyles src="Mono/styles.css" />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 Template:Num is returned and the <templatestyles src="Mono/styles.css" />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 <templatestyles src="Mono/styles.css" />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 <templatestyles src="Mono/styles.css" />errno is set to <templatestyles src="Mono/styles.css" />ENOMEM. <templatestyles src="Mono/styles.css" />brk always errors.[7]
Error codes
The error <templatestyles src="Mono/styles.css" />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
References
<templatestyles src="Reflist/styles.css" />
- ↑ Script error: No such module "citation/CS1".
- ↑ Script error: No such module "citation/CS1".
- ↑ Script error: No such module "citation/CS1".
- ↑ Script error: No such module "citation/CS1".
- ↑ a b Script error: No such module "citation/CS1".
- ↑ – Linux Programmer's Manual – System Calls
- ↑ Script error: No such module "citation/CS1".
Script error: No such module "Check for unknown parameters".