malloc does not map memory. It is just a sub-allocator that sits on top of the OS's memory map, which you can instead just call directly. You call VirtualAlloc on Windows or mmap on Linux. For example, in the refterm stuff I just posted:https://github.com/cmuratori/refterm/blob/91e932f011e12c02a6c609ac59570f5c19fe4727/refterm_example_terminal.c#L6 …
-
-
Furthermore, the CRT actually doesn't give you access to the powerful memory mapping features of your OS. Again, in refterm, here is a circular buffer implemented entirely with OS allocation calls:https://github.com/cmuratori/refterm/blob/91e932f011e12c02a6c609ac59570f5c19fe4727/refterm_example_source_buffer.c#L15 …
2 replies 2 retweets 16 likes -
That small set of calls is all you need for a circular buffer that works "automatically" - meaning that you never need to check where you are in the buffer, because the memory is mapped twice, so reading off the end of the buffer just reads the beginning of the buffer.
1 reply 1 retweet 13 likes -
This is all stuff that the CPU does automatically for you, but which people don't realize you can do because of libraries like the CRT which were written for the lowest common denominator (eg., chips with no MMU).
1 reply 0 retweets 12 likes -
Replying to @cmuratori @JustSlavic
Well, holy shit. This is quite the revelation.
1 reply 0 retweets 6 likes -
Replying to @beast_pixels @JustSlavic
Now I am wondering what people thought malloc was doing :) If you didn't know it called the OS to change the virtual memory mapping for your process, what _did_ you think it was doing?
3 replies 0 retweets 6 likes -
Replying to @cmuratori @beast_pixels
I just didn't know. It was a black box to me, I knew that it gives me more virtual memory, but I didn't know how it does this. Once I tried to read the source code of malloc, but it is so complicated, I couldn't find the exact place where the magic was happening... :(
1 reply 0 retweets 1 like -
Replying to @JustSlavic @beast_pixels
Not sure if you're on Windows or Linux, but here is a Linux example: https://github.com/bminor/glibc/blob/ff417d40178b7363b08516091f74c0b6615456ee/malloc/malloc.c#L2502 … It has a whole macro dance it does to make syscalls, but this is the actual syscall it will end up doing: https://man7.org/linux/man-pages/man2/mmap.2.html …
1 reply 1 retweet 4 likes -
So every time you call malloc, it looks to see if it has some memory it can give you already. If it doesn't, it will eventually call mmap, which asks the OS for more virtual memory to be mapped for your process. Then malloc takes that new memory, and gives you some of it.
2 replies 0 retweets 6 likes -
Replying to @cmuratori @JustSlavic
The crazy thing about this is that mmap is always seen as something way more high level than malloc. Like you use it do this tricky load file into memory thingy, not just allocate some memory.
1 reply 0 retweets 2 likes
Loading seems to be taking a while.
Twitter may be over capacity or experiencing a momentary hiccup. Try again or visit Twitter Status for more information.