| ▲ | TZubiri 2 hours ago | |||||||||||||||||||
like many C functions you must remember that C is not low level, you won’t learn how memory is allocated by inspecting malloc() anymore than you would learn how characters are printed to the screen by reading printf() or how a file is written by reading write(). you will only learn about the thin or heavy wrapper that C is responsible for, the real magic is happening in the kernel and the device firmware | ||||||||||||||||||||
| ▲ | netbsdusers 12 minutes ago | parent | next [-] | |||||||||||||||||||
Notwithstanding that most kernels today are written almost entirely in C anyway, you'll see learn a great deal about memory allocation by inspecting malloc(). The usual division of labour in most operating systems is the kernel manages the virtual memory and physical pages and provides a mechanism for coarse page-aligned allocation of virtual address for userland. These coarse mappings userland (malloc or something else) is responsible for carving up, and in that is much design space. Just look at mimalloc for example. The kernel's own page allocation is often very simple (many get by with a simple freelist, some use buddies.) The allocators for the kernel's own datastructures are usually relatively simple at least conceptually. Bonwick explains the slab allocator - the almost universally acknowledged queen of all kernel memory allocators and mother of those of Linux, BSD, and beyond - in a 12-page paper. As for the firmware there's basically nothing of an ongoing role for it after the kernel is in charge. Its role is limited for the most part to providing the kernel with a map of the physical addres space. By reading malloc() you will not learn everything about allocation - that is a problem that spans multiple layers - but you will certainly learn a lot about it. To call it a mere wrapper is not reasonable. | ||||||||||||||||||||
| ▲ | 100111011111 7 minutes ago | parent | prev | next [-] | |||||||||||||||||||
Well, yes, but its low level , no matter what, I mean whats left thats lower? | ||||||||||||||||||||
| ▲ | foobarian 2 hours ago | parent | prev | next [-] | |||||||||||||||||||
Disagree, I found malloc source useful, and writing a toy allocator especially useful because you learn about the brk() syscall which is a great breadcrumb into kernel land and a great bridge to connect the memory mapping lessons from computer architecture with application code. | ||||||||||||||||||||
| ||||||||||||||||||||
| ▲ | Someone 2 hours ago | parent | prev [-] | |||||||||||||||||||
Malloc needs the kernel to obtain memory, but in any reasonable implementation (you do not want to make a syscall for every malloc and free) is mostly user space, and studying that will tell you a lot about how your malloc works. | ||||||||||||||||||||