3 ms·
Nit pick: On modern systems malloc isn't a syscall, it's implemented in userspace. (Sorry, I couldn't help it) That's not to say you're safe to call other sysc
by cycloptic 6y ago
Nit pick: On modern systems malloc isn't a syscall, it's implemented in userspace. (Sorry, I couldn't help it)
That's not to say you're safe to call other syscalls, many of them either require memory allocations in-kernel (see ENOMEM) or can block indefinitely.
- fanf2 6y agoNever mind modern systems, malloc() was never a syscall :-) One of the great things about K&R is that it shows you how to implement parts of the C library, including a simple malloc(), demonstrating that the library does not need to be magical.
- petr_tik 6y agoThanks you and your parent for pointing this out! I should be more precise, sbrk is the underlying system call that might be invoked inside malloc
- voldacar 6y agommap usually these days
- fanf2 6y agoYes, and often mmap() for large allocations and other parts of the heap. There has been an interesting discussion about memory management in Ritchie’s PDP11 C compiler on the TUHS list this month https://minnie.tuhs.org/pipermail/tuhs/2020-August/thread.html https://minnie.tuhs.org/pipermail/tuhs/2020-August/thread.ht... from the era when large programs could not necessarily afford the overhead of malloc() so sometimes used sbrk() directly.