From: Nicholas N. <n.n...@gm...> - 2009-07-20 03:42:36
|
On Sat, Jul 18, 2009 at 12:55 AM, John Reiser<jr...@bi...> wrote: > Nicholas Nethercote wrote: > > 0) sbrk() can *decrease* process address space. No zero fill is done > for a decrease, not even the fragment on the high end of the last page > that is beyond the new highest address. For maximum safety and > portability, then the bytes in the last page that reside above [the new] > sbrk(0) should be considered to be uninitialized, but in practice > it is exceedingly likely that they will retain their previous contents. > > 1) If an increase is large enough to require new whole pages, > then those new whole pages (like all new pages) are zero-filled > by the operating system. So if sbrk(0) already is page aligned, > then sbrk(PAGE_SIZE) *does* zero-fill the new memory. > > 2) Any increase that lies within an existing allocated page > is not changed. So if (x= sbrk(0)) is not page aligned, then > sbrk(PAGE_SIZE) yields ((PAGE_SIZE -1) & -x) bytes which > keep their existing contents, and an additional PAGE_SIZE > bytes which are zeroed. ((PAGE_SIZE -1) & x) of them are > "covered" by the sbrk(), and the rest of them come along for > the ride because the operating system deals only in whole pages. > Again, for maximum safety and portability, then anything that > lives above [the new] sbrk(0) should be considered uninitialized, > but in practice will retain previous contents [zero in this case.] > > Also remember that sbrk(...) can fail :-) Thanks, John. Is this info specific to Linux/glibc? AFAICT sbrk/brk isn't even POSIX, which makes it even harder to determine what it should do. Nick |