I have never seen software NOT using the C runtime library. I thought that you cannot ship a program without it. For example, I thought malloc is the only way to allocate memory, and every program has to call it (calloc and new call it too, right?)
-
-
Replying to @JustSlavic @beast_pixels
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 …
1 reply 0 retweets 16 likes -
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
The same thing happens when you free. The CRT will just say "thanks" and keep that memory around to give to the next person who calls "malloc", presumably using some heuristic where if there is a lot of freed memory, eventually call the OS unmap call to give some back to the OS.
-
-
Replying to @cmuratori @beast_pixels
That clears so many things! Thank you very much! I only don't understand why this is not common knowledge... I even feel a little embarrassed.
2 replies 0 retweets 5 likes -
I want to ask one more thing: if you don't use CRT, don't you have to make your own, which will just repeat a lot of stuff in CRT, that is really complicated (it surely looks complicated when I look at it), and probably make more bugs/inefficiencies?
3 replies 0 retweets 0 likes - Show replies
New conversation -
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.