2

Ultimately I am just trying to figure out how to dynamically allocate heap memory from within assembly.

If I call Linux sbrk() from assembly code, can I use the address returned as I would use an address of a statically (ie in the .data section of my program listing) declared chunk of memory?

I know Linux uses the hardware MMU if present, so I am not sure if what sbrk returns is a 'raw' pointer to real RAM, or is it a cooked pointer to RAM that may be modified by Linux's VM system?

I read this: How are sbrk/brk implemented in Linux?. I suspect I can not use the return value from sbrk() without worry: the MMU fault on access-non-allocated-address must cause the VM to alter the real location in RAM being addressed. Thus assy, not linked against libc or what-have-you, would not know the address has changed.

Does this make sense, or am I out to lunch?

Community
  • 1
  • 1
Eric M
  • 1,027
  • 2
  • 8
  • 21
  • Is there still a true sbrk syscall? I thought it had been deprecated (in favour of mmap) ages ago. E.g. FreeBSD doesn't have one anymore. Could be that the linux flavour is x86 specific. – Marco van de Voort Jan 09 '11 at 17:00
  • x86 Linux (OpenSuSE 11.3) still has a man page for sbrk(2) & brk(2). But it does say this: "CONFORMING TO 4.3BSD; SUSv1, marked LEGACY in SUSv2, removed in POSIX.1-2001." So not sure. I only started considering calling something lower level than malloc() yesterday, if I find sbrk/brk is not used I'll try to post back here. – Eric M Jan 09 '11 at 18:30
  • Hmm, it also says: Avoid using brk() and sbrk(): the malloc(3) memory allocation package is the portable and comfortable way of allocating memory. Various systems use various types for the argument of sbrk(). Common are int, ssize_t, ptrdiff_t, intptr_t. – Eric M Jan 09 '11 at 19:47

1 Answers1

1

Unix user processes live in virtual memory, no matter if written in assembler of Fortran, and should not care about physical addresses. That's kernel's business - kernel sets up and manages the MMU. You don't have to worry about it. Page faults are handled automatically and transparently.

sbrk(2) returns a virtual address specific to the process, if that's what you were asking.

Nikolai Fetissov
  • 82,306
  • 11
  • 110
  • 171