1.\" Copyright (c) 1980, 1991 Regents of the University of California. 2.\" All rights reserved. 3.\" 4.\" Redistribution and use in source and binary forms, with or without 5.\" modification, are permitted provided that the following conditions 6.\" are met: 7.\" 1. Redistributions of source code must retain the above copyright 8.\" notice, this list of conditions and the following disclaimer. 9.\" 2. Redistributions in binary form must reproduce the above copyright 10.\" notice, this list of conditions and the following disclaimer in the 11.\" documentation and/or other materials provided with the distribution. 12.\" 3. All advertising materials mentioning features or use of this software 13.\" must display the following acknowledgement: 14.\" This product includes software developed by the University of 15.\" California, Berkeley and its contributors. 16.\" 4. Neither the name of the University nor the names of its contributors 17.\" may be used to endorse or promote products derived from this software 18.\" without specific prior written permission. 19.\" 20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30.\" SUCH DAMAGE. 31.\" 32.\" from: @(#)brk.2 6.5 (Berkeley) 3/10/91 33.\" $Id: brk.2,v 1.6 1994/01/10 23:54:10 jtc Exp $ 34.\" 35.Dd March 10, 1991 36.Dt BRK 2 37.Os BSD 4 38.Sh NAME 39.Nm brk , 40.Nm sbrk 41.Nd change data segment size 42.Sh SYNOPSIS 43.Fd #include <sys/types.h> 44.Ft int 45.Fn brk "const char *addr" 46.Ft char * 47.Fn sbrk "int incr" 48.Sh DESCRIPTION 49.Bf -symbolic 50The brk and sbrk functions are historical curiosities 51left over from earlier days before the advent of virtual memory management. 52.Ef 53The 54.Fn brk 55function 56sets the break or lowest address 57of a process's data segment (uninitialized data) to 58.Fa addr 59(immediately above bss). 60Data addressing is restricted between 61.Fa addr 62and the lowest stack pointer to the stack segment. 63Memory is allocated by 64.Fa brk 65in page size pieces; 66if 67.Fa addr 68is not evenly divisible by the system page size, it is 69increased to the next page boundary. 70.Pp 71.\" The 72.\" .Nm sbrk 73.\" function 74.\" allocates chunks of 75.\" .Fa incr 76.\" bytes 77.\" to the process's data space 78.\" and returns an address pointer. 79.\" The 80.\" .Xr malloc 3 81.\" function utilizes 82.\" .Nm sbrk . 83.\" .Pp 84The current value of the program break is reliably returned by 85.Dq Li sbrk(0) 86(see also 87.Xr end 3 ) . 88The 89.Xr getrlimit 2 90system call may be used to determine 91the maximum permissible size of the 92.Em data 93segment; 94it will not be possible to set the break 95beyond the 96.Em rlim_max 97value returned from a call to 98.Xr getrlimit , 99e.g. 100.Dq qetext + rlp\(->rlim_max. 101(see 102.Xr end 3 103for the definition of 104.Em etext ) . 105.Sh RETURN VALUES 106.Nm Brk 107returns 0 if successful; -1 if the process requests more memory than 108than allowed by the system limit. 109The 110.Nm sbrk 111function returns 0 if successful, otherwise the error 112.Er EOPNOTSUPP 113is returned. 114.\" .Sh ERRORS 115.\" .Xr Sbrk 116.\" returns -1 if the break could not be set. 117.\" will fail and no additional memory will be allocated if 118.\" one of the following are true: 119.\" .Bl -tag -width Er 120.\" .It Bq Er ENOMEM 121.\" The limit, as set by 122.\" .Xr setrlimit 2 , 123.\" was exceeded. 124.\" .It Bq Er ENOMEM 125.\" The maximum possible size of a data segment (compiled into the 126.\" system) was exceeded. 127.\" .It Bq Er ENOMEM 128.\" Insufficient space existed in the swap area 129.\" to support the expansion. 130.\" .El 131.Sh SEE ALSO 132.Xr execve 2 , 133.Xr getrlimit 2 , 134.Xr malloc 3 , 135.Xr end 3 136.Sh BUGS 137Setting the break may fail due to a temporary lack of 138swap space. It is not possible to distinguish this 139from a failure caused by exceeding the maximum size of 140the data segment without consulting 141.Xr getrlimit . 142.Sh HISTORY 143A 144.Fn brk 145function call appeared in 146.At v7 . 147