Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

So malloc doesn't invoke any syscall?

Related code:

  write(-1, "test", sizeof("test"));
  void * p = malloc(1024);
  void * p2 = malloc(510);
  write(-1, "hi", sizeof("hi"));

Related strace output:

write(4294967295, "test\0", 5)          = -1 EBADF (Bad file descriptor)
brk(0)                                  = 0x601000
brk(0x622000)                           = 0x622000
write(4294967295, "hi\0", 3)            = -1 EBADF (Bad file descriptor)

I'm surprised such low level operation doesn't involve syscall?

like image 477
cpuer Avatar asked Jun 13 '11 03:06

cpuer


2 Answers

Not every call to malloc invokes a syscall. On my linux desktop malloc allocates a space in 128KB blocks and then distributes the space. So I will see a syscall every 100-200 malloc calls. On freebsd malloc allocates by 2MB blocks. On your machine numbers will likely differ.

If you want to see syscall on every malloc allocate large amounts of memory (malloc(10*1024*1024*1024))

like image 192
Andrey Avatar answered Oct 21 '22 04:10

Andrey


What do you think brk is? malloc absolutely is invoking a syscall in this example, the syscall just isn't "malloc".

like image 28
Logan Capaldo Avatar answered Oct 21 '22 05:10

Logan Capaldo