161181Sbostic.\" Copyright (c) 1980, 1991, 1993 261181Sbostic.\" The Regents of the University of California. All rights reserved. 320011Smckusick.\" 447208Scael.\" %sccs.include.redist.man% 520011Smckusick.\" 6*69148Smckusick.\" @(#)brk.2 8.4 (Berkeley) 05/01/95 747208Scael.\" 847208Scael.Dd 947208Scael.Dt BRK 2 1047208Scael.Os BSD 4 1147208Scael.Sh NAME 1247208Scael.Nm brk , 1347208Scael.Nm sbrk 1447208Scael.Nd change data segment size 1547208Scael.Sh SYNOPSIS 1647208Scael.Fd #include <sys/types.h> 1747208Scael.Ft char 1847208Scael.Fn *brk "const char *addr" 1947208Scael.Ft char * 2047208Scael.Fn *sbrk "int incr" 2147208Scael.Sh DESCRIPTION 2247208Scael.Bf -symbolic 2347208ScaelThe brk and sbrk functions are historical curiosities 2447208Scaelleft over from earlier days before the advent of virtual memory management. 2547208Scael.Ef 2647208ScaelThe 2747208Scael.Fn brk 2847208Scaelfunction 2947208Scaelsets the break or lowest address 3065100Smckusickof a process's data segment (uninitialized data) to 3147208Scael.Fa addr 3247208Scael(immediately above bss). 3347208ScaelData addressing is restricted between 3447208Scael.Fa addr 3547208Scaeland the lowest stack pointer to the stack segment. 3647208ScaelMemory is allocated by 3747208Scael.Fa brk 3847208Scaelin page size pieces; 3947208Scaelif 4047208Scael.Fa addr 4147208Scaelis not evenly divisible by the system page size, it is 4247208Scaelincreased to the next page boundary. 4347208Scael.Pp 4447208Scael.\" The 4547208Scael.\" .Nm sbrk 4647208Scael.\" function 4747208Scael.\" allocates chunks of 4847208Scael.\" .Fa incr 4947208Scael.\" bytes 5047208Scael.\" to the process's data space 5147208Scael.\" and returns an address pointer. 5247208Scael.\" The 5347208Scael.\" .Xr malloc 3 5447208Scael.\" function utilizes 5547208Scael.\" .Nm sbrk . 5647208Scael.\" .Pp 5729411SmckusickThe current value of the program break is reliably returned by 5847208Scael.Dq Li sbrk(0) 5947208Scael(see also 6047208Scael.Xr end 3 ) . 6120011SmckusickThe 6247208Scael.Xr getrlimit 2 6320011Smckusicksystem call may be used to determine 6420011Smckusickthe maximum permissible size of the 6547208Scael.Em data 6620012Smckusicksegment; 6720011Smckusickit will not be possible to set the break 6820012Smckusickbeyond the 6947208Scael.Em rlim_max 7020012Smckusickvalue returned from a call to 7147208Scael.Xr getrlimit , 7247208Scaele.g. 73*69148Smckusick.Dq etext + rlp\(->rlim_max. 7428360Sanne(see 7547208Scael.Xr end 3 7620011Smckusickfor the definition of 7747208Scael.Em etext ) . 7847208Scael.Sh RETURN VALUES 7947208Scael.Nm Brk 8067014Smckusickreturns 0 if successful; 8160016Smckusickotherwise -1 with 8260016Smckusick.Va errno 8360016Smckusickset to indicate why the allocation failed. 8467014Smckusick.Nm Sbrk 8560016Smckusickreturns a pointer to the base of the new storage if successful; 8660016Smckusickotherwise -1 with 8760016Smckusick.Va errno 8860016Smckusickset to indicate why the allocation failed. 8960016Smckusick.Sh ERRORS 9067014Smckusick.Xr Brk 9167014Smckusickor 9267014Smckusick.Xr sbrk 9360016Smckusickwill fail and no additional memory will be allocated if 9460016Smckusickone of the following are true: 9560016Smckusick.Bl -tag -width [ENOMEM] 9660016Smckusick.It Bq Er ENOMEM 9760016SmckusickThe limit, as set by 9860016Smckusick.Xr setrlimit 2 , 9960016Smckusickwas exceeded. 10060016Smckusick.It Bq Er ENOMEM 10160016SmckusickThe maximum possible size of a data segment (compiled into the 10260016Smckusicksystem) was exceeded. 10360016Smckusick.It Bq Er ENOMEM 10460016SmckusickInsufficient space existed in the swap area 10560016Smckusickto support the expansion. 10660016Smckusick.El 10747208Scael.Sh SEE ALSO 10847208Scael.Xr execve 2 , 10947208Scael.Xr getrlimit 2 , 11047208Scael.Xr malloc 3 , 11147208Scael.Xr end 3 11247208Scael.Sh BUGS 11320012SmckusickSetting the break may fail due to a temporary lack of 11420012Smckusickswap space. It is not possible to distinguish this 11520012Smckusickfrom a failure caused by exceeding the maximum size of 11620012Smckusickthe data segment without consulting 11747208Scael.Xr getrlimit . 11847208Scael.Sh HISTORY 11947208ScaelA 12047208Scael.Nm 12147208Scaelfunction call appeared in Version 7 AT&T UNIX. 122