Sbrk
Encyclopedia
brk and sbrk are basic memory management
Memory management
Memory management is the act of managing computer memory. The essential requirement of memory management is to provide ways to dynamically allocate portions of memory to programs at their request, and freeing it for reuse when no longer needed. This is critical to the computer system.Several...

 system call
System call
In computing, a system call is how a program requests a service from an operating system's kernel. This may include hardware related services , creating and executing new processes, and communicating with integral kernel services...

s used in Unix
Unix
Unix is a multitasking, multi-user computer operating system originally developed in 1969 by a group of AT&T employees at Bell Labs, including Ken Thompson, Dennis Ritchie, Brian Kernighan, Douglas McIlroy, and Joe Ossanna...

 and Unix-like operating systems to control the amount of memory allocated to the data segment
Data segment
A data segment is a portion of virtual address space of a program, which contains the global variables and static variables that are initialized by the programmer...

 of the process
Process (computing)
In computing, a process is an instance of a computer program that is being executed. It contains the program code and its current activity. Depending on the operating system , a process may be made up of multiple threads of execution that execute instructions concurrently.A computer program is a...

. These calls are typically made from a higher-level memory management library such as malloc
Malloc
C dynamic memory allocation refers to performing dynamic memory allocation in the C via a group of functions in the C standard library, namely malloc, realloc, calloc and free....

. 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
Mmap
In computing, mmap is a POSIX-compliant Unix system call that maps files or devices into memory. It is a method of memory-mapped file I/O. It naturally implements demand paging, because initially file contents are not entirely read from disk and do not use physical RAM at all...

 call.

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
Memory management
Memory management is the act of managing computer memory. The essential requirement of memory management is to provide ways to dynamically allocate portions of memory to programs at their request, and freeing it for reuse when no longer needed. This is critical to the computer system.Several...

 architecture.

Function signatures and behavior

  1. include


int brk(void *end_data_segment);

void *sbrk(intptr_t increment);

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.

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.

Upon successful completion, the brk subroutine returns a value of 0, and the sbrk subroutine returns a pointer to the start of the new area. If either subroutine is unsuccessful, a value of -1 is returned and the errno
Errno.h
Errno.h is a header file in the standard library of C programming language. It defines macros to report error conditions through error codes stored in a static location called errno....

 global variable
Global variable
In computer programming, a global variable is a variable that is accessible in every scope . Interaction mechanisms with global variables are called global environment mechanisms...

 is set to indicate the error.

The current Mac OSX implementation of sbrk is an emulation, and has a maximum allocation of 4 Megabytes. When this limit is reached, -1 is returned and the errno is not set.

Error codes

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
    Shared memory
    In computing, shared memory is memory that may be simultaneously accessed by multiple programs with an intent to provide communication among them or avoid redundant copies. Depending on context, programs may run on a single processor or on multiple separate processors...

     segment.

See also

  • For information about segments, see the exec
    Exec (operating system)
    The exec collection of functions of Unix-like operating systems cause the running process to be completely replaced by the program passed as an argument to the function...

     subroutine.
  • For information about the maximum amount of space that can be allocated, see the ulimit and getrlimit subroutines.
  • For information on shared memory
    Shared memory
    In computing, shared memory is memory that may be simultaneously accessed by multiple programs with an intent to provide communication among them or avoid redundant copies. Depending on context, programs may run on a single processor or on multiple separate processors...

     operations, see the shmat subroutine.
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK