4 ms·
Of course malloc is implemented in C. Look at the glibc source code.
by jra_samba 4y ago
Of course malloc is implemented in C. Look at the glibc source code.
- teddyh 4y agoMalloc cannot, IIRC, be implemented in standard C only. It needs non-standard system calls. Specifically, how does malloc() itself allocate new memory?
- anyfoo 4y agoI mean, this is just being pedantic, but you could in theory declare a giant uninitialized non-local array uint8_t my_memory[4*1024*1024*1024]; and some additional globals for housekeeping[1], and use that as your backing memory for your malloc implementation written entirely in "standard C"[2]. And that usually does not even waste any memory, since my_memory will be in the zero-filled BSS section and the actual backing pages only allocated on demand by the OS once you write on them for the first time. Of course in reality there's probably a few "non-standard"[2] system calls in the path, mostly mmap. [1] Not necessarily, you can place them at special places in your my_memory array by convention, e.g. at the start of the array. [2] I assume that we're excluding standards on other layers like POSIX, otherwise it's trivial.
- jra_samba 4y agoIt uses the mmap, sbrk or brk system calls. All perfectly callable from C.
- jra_samba 4y agommap, brk, sbrk are standard POSIX system calls.
- teddyh 4y agoIIUC, brk and sbrk have been removed from POSIX, and using mmap() just to allocate memory is a pretty weird way to use mmap(), and probably not what you would want for a implementation of malloc().
- jra_samba 4y agoNot true. mmap is commonly used in malloc implementations. Look at this man page for jemalloc. http://jemalloc.net/jemalloc.3.html http://jemalloc.net/jemalloc.3.html "Traditionally, allocators have used sbrk(2) to obtain memory, which is suboptimal for several reasons, including race conditions, increased fragmentation, and artificial limitations on maximum usable memory. If sbrk(2) is supported by the operating system, this allocator uses both mmap(2) and sbrk(2), in that order of preference; otherwise only mmap(2) is used." Also, Google's tcmalloc uses mmap to get system memory: https://github.com/google/tcmalloc/blob/master/docs/design.md#tcmalloc-backend https://github.com/google/tcmalloc/blob/master/docs/design.m... "An allocation for k pages is satisfied by looking in the kth free list. If that free list is empty, we look in the next free list, and so forth. Eventually, we look in the last free list if necessary. If that fails, we fetch memory from the system mmap." In fact I'd be surprised to see a modern malloc implementation that doesn't use mmap under the hood.