Back to index

glibc  2.9
sbrk.c
Go to the documentation of this file.
00001 /* Copyright (C) 1991,1995,1996,1997,2000,2002 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <unistd.h>
00020 #include <errno.h>
00021 
00022 /* Defined in brk.c.  */
00023 extern void *__curbrk;
00024 extern int __brk (void *addr);
00025 
00026 /* Defined in init-first.c.  */
00027 extern int __libc_multiple_libcs attribute_hidden;
00028 
00029 /* Extend the process's data space by INCREMENT.
00030    If INCREMENT is negative, shrink data space by - INCREMENT.
00031    Return start of new space allocated, or -1 for errors.  */
00032 void *
00033 __sbrk (intptr_t increment)
00034 {
00035   void *oldbrk;
00036 
00037   /* If this is not part of the dynamic library or the library is used
00038      via dynamic loading in a statically linked program update
00039      __curbrk from the kernel's brk value.  That way two separate
00040      instances of __brk and __sbrk can share the heap, returning
00041      interleaved pieces of it.  */
00042   if (__curbrk == NULL || __libc_multiple_libcs)
00043     if (__brk (0) < 0)             /* Initialize the break.  */
00044       return (void *) -1;
00045 
00046   if (increment == 0)
00047     return __curbrk;
00048 
00049   oldbrk = __curbrk;
00050   if (__brk (oldbrk + increment) < 0)
00051     return (void *) -1;
00052 
00053   return oldbrk;
00054 }
00055 libc_hidden_def (__sbrk)
00056 weak_alias (__sbrk, sbrk)