1brk(2) System Calls brk(2)
2
3
4
6 brk, sbrk - change the amount of space allocated for the calling
7 process's data segment
8
10 #include <unistd.h>
11
12 int brk(void *endds);
13
14
15 void *sbrk(intptr_t incr);
16
17
19 The brk() and sbrk() functions are used to change dynamically the
20 amount of space allocated for the calling process's data segment (see
21 exec(2)). The change is made by resetting the process's break value and
22 allocating the appropriate amount of space. The break value is the
23 address of the first location beyond the end of the data segment. The
24 amount of allocated space increases as the break value increases. Newly
25 allocated space is set to zero. If, however, the same memory space is
26 reallocated to the same process its contents are undefined.
27
28
29 When a program begins execution using execve() the break is set at the
30 highest location defined by the program and data storage areas.
31
32
33 The getrlimit(2) function may be used to determine the maximum permis‐
34 sible size of the data segment; it is not possible to set the break
35 beyond the rlim_max value returned from a call to getrlimit(), that is
36 to say, "end + rlim.rlim_max." See end(3C).
37
38
39 The brk() function sets the break value to endds and changes the allo‐
40 cated space accordingly.
41
42
43 The sbrk() function adds incr function bytes to the break value and
44 changes the allocated space accordingly. The incr function can be nega‐
45 tive, in which case the amount of allocated space is decreased.
46
48 Upon successful completion, brk() returns 0. Otherwise, it returns −1
49 and sets errno to indicate the error.
50
51
52 Upon successful completion, sbrk() returns the prior break value. Oth‐
53 erwise, it returns (void *)−1 and sets errno to indicate the error.
54
56 The brk() and sbrk() functions will fail and no additional memory will
57 be allocated if:
58
59 ENOMEM The data segment size limit as set by setrlimit() (see getr‐
60 limit(2)) would be exceeded; the maximum possible size of a
61 data segment (compiled into the system) would be exceeded;
62 insufficient space exists in the swap area to support the
63 expansion; or the new break value would extend into an area
64 of the address space defined by some previously established
65 mapping (see mmap(2)).
66
67
68 EAGAIN Total amount of system memory available for private pages is
69 temporarily insufficient. This may occur even though the
70 space requested was less than the maximum data segment size
71 (see ulimit(2)).
72
73
75 The behavior of brk() and sbrk() is unspecified if an application also
76 uses any other memory functions (such as malloc(3C), mmap(2),
77 free(3C)). The brk() and sbrk() functions have been used in specialized
78 cases where no other memory allocation function provided the same capa‐
79 bility. The use of mmap(2) is now preferred because it can be used
80 portably with all other memory allocation functions and with any func‐
81 tion that uses other allocation functions.
82
83
84 It is unspecified whether the pointer returned by sbrk() is aligned
85 suitably for any purpose.
86
88 See attributes(5) for descriptions of the following attributes:
89
90
91
92
93 ┌─────────────────────────────┬─────────────────────────────┐
94 │ ATTRIBUTE TYPE │ ATTRIBUTE VALUE │
95 ├─────────────────────────────┼─────────────────────────────┤
96 │MT-Level │MT-Safe │
97 └─────────────────────────────┴─────────────────────────────┘
98
100 exec(2), getrlimit(2), mmap(2), shmop(2), ulimit(2), end(3C), free(3C),
101 malloc(3C)
102
104 The value of incr may be adjusted by the system before setting the new
105 break value. Upon successful completion, the implementation guarantees
106 a minimum of incr bytes will be added to the data segment if incr is a
107 positive value. If incr is a negative value, a maximum of incr bytes
108 will be removed from the data segment. This adjustment may not be nec‐
109 essary for all machine architectures.
110
111
112 The value of the arguments to both brk() and sbrk() are rounded up for
113 alignment with eight-byte boundaries.
114
116 Setting the break may fail due to a temporary lack of swap space. It is
117 not possible to distinguish this from a failure caused by exceeding the
118 maximum size of the data segment without consulting getrlimit().
119
120
121
122SunOS 5.11 14 Jan 1997 brk(2)