Back to index

glibc  2.9
Defines | Functions | Variables
brk.c File Reference
#include <errno.h>
#include <hurd.h>
#include <hurd/resource.h>
#include <cthreads.h>

Go to the source code of this file.

Defines

#define DATA_SIZE   (128 * 1024 * 1024)

Functions

 weak_alias (_hurd_brk, ___brk_addr)
 weak_alias (__brk, brk)
static void init_brk (void)
 text_set_element (_hurd_preinit_hook, init_brk)

Variables

vm_address_t _hurd_data_end
vm_address_t _hurd_brk = 0

Define Documentation

#define DATA_SIZE   (128 * 1024 * 1024)

Definition at line 26 of file brk.c.


Function Documentation

static void init_brk ( void  ) [static]

Definition at line 122 of file brk.c.

{
  vm_address_t pagend;

  __mutex_init (&_hurd_brk_lock);

  static_data_start = (vm_address_t) (&__data_start ?: &_end);

  /* If _hurd_brk is already set, don't change it.  The assumption is that
     it was set in a previous run before something like Emacs's unexec was
     called and dumped all the data up to the break at that point.  */
  if (_hurd_brk == 0)
    _hurd_brk = (vm_address_t) &_end;

  pagend = round_page (_hurd_brk);

  _hurd_data_end = round_page (static_data_start + DATA_SIZE);

  if (pagend < _hurd_data_end)
    {
      /* We use vm_map to allocate and change permissions atomically.  */
      if (__vm_map (__mach_task_self (), &pagend, _hurd_data_end - pagend,
                  0, 0, MACH_PORT_NULL, 0, 0,
                  0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE,
                  VM_INHERIT_COPY))
       /* Couldn't allocate the memory.  The break will be very short.  */
       _hurd_data_end = pagend;
    }

  (void) &init_brk;         /* Avoid ``defined but not used'' warning.  */
}

Here is the call graph for this function:

text_set_element ( _hurd_preinit_hook  ,
init_brk   
)
weak_alias ( _hurd_brk  ,
___brk_addr   
)

Definition at line 39 of file brk.c.

{
  int ret;
  HURD_CRITICAL_BEGIN;
  __mutex_lock (&_hurd_brk_lock);
  ret = _hurd_set_brk ((vm_address_t) inaddr);
  __mutex_unlock (&_hurd_brk_lock);
  HURD_CRITICAL_END;
  return ret;
}

Here is the call graph for this function:

weak_alias ( __brk  ,
brk   
)

Definition at line 61 of file brk.c.

{
  error_t err;
  vm_address_t pagend = round_page (addr);
  vm_address_t pagebrk = round_page (_hurd_brk);
  long int rlimit;

  if (pagend <= pagebrk)
    {
      if (pagend < pagebrk)
       {
         /* XXX wish this were atomic... */
         /* First deallocate the memory to release its backing space.  */
         __vm_deallocate (__mach_task_self (), pagend, pagebrk - pagend);
         /* Now reallocate it with no access allowed.  */
         err = __vm_map (__mach_task_self (),
                       &pagend, pagebrk - pagend,
                       0, 0, MACH_PORT_NULL, 0, 0,
                       0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE,
                       VM_INHERIT_COPY);
         /* XXX what if error? */
       }
      _hurd_brk = addr;
      return 0;
    }

  __mutex_lock (&_hurd_rlimit_lock);
  rlimit = _hurd_rlimits[RLIMIT_DATA].rlim_cur;
  __mutex_unlock (&_hurd_rlimit_lock);

  if (addr - static_data_start > rlimit)
    {
      /* Need to increase the resource limit.  */
      errno = ENOMEM;
      return -1;
    }

  if (pagend > _hurd_data_end)
    {
      /* We didn't allocate enough space!  Hopefully we can get some more!  */
      err = __vm_allocate (__mach_task_self (), &pagebrk, pagend - pagebrk, 0);
      if (! err)
       _hurd_data_end = pagend;
    }
  else
    /* Make the memory accessible.  */
    err = __vm_protect (__mach_task_self (), pagebrk, pagend - pagebrk,
                     0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);

  if (err)
    return __hurd_fail (err);

  _hurd_brk = addr;
  return 0;
}

Here is the call graph for this function:


Variable Documentation

vm_address_t _hurd_brk = 0

Definition at line 34 of file brk.c.

vm_address_t _hurd_data_end

Definition at line 30 of file brk.c.