4 ms·
I used to work with someone who was an expert in writing cryptography libraries. He insisted on never using any system calls for maximum portability (he also wr
by jacobkg 4y ago
I used to work with someone who was an expert in writing cryptography libraries. He insisted on never using any system calls for maximum portability (he also wrote his own memory allocators and optimized for small binary size). Seemed quirky to me but he was proven right multiple times as our ultraportable library let us book some big deals that would have been otherwise infeasible without a big rewrite
- l33t233372 4y agoHow can you write your own memory allocator in a way that works on Linux and Windows? Don’t you need to request heap space from operating system via system call?
- ShroudedNight 4y agoI suppose if you're willing to malloc big chunks and then potentially never have them released until the process terminates (as free often doesn't do this), one might call that writing one's own memory allocator despite not using mmap / VirtualAlloc.
- ablob 4y agoIf you know how much memory you need, you can just have a section of zero-initialized-data for that, which the allocator then handles. The allocator serves also as a proxy, as in: only the allocator needs to know how to request heap space. Portable in that case probably means that you have little to no code change required for a port. Something else to consider is that there may not even be an operating system the program was run on.
- l33t233372 4y ago> there may not even be an operating system the program was run on. Sure, in this case you can just implement malloc the same way a kernel implements me. My failure of understanding is when there _is_ a kernel that your program needs to interact with.
- wongarsu 4y agoIn the executable file you can specify that the operating system should give you a writable space of a given size, initialized with whatever you want. I think that's frequently used to initialize static variables, so you might get the desired result by just telling the compiler you want a large static array.
- yurymik 4y agoYou don't need to avoid _all_ system calls. Just call sbrk/VirtualAlloc/vm_allocate once and then dole out smaller chunks yourself. That way if you need to add new platform, it's only one place; you're shielded against weird platform bugs (Apple goes one way, Microsoft the other), so the app behaves the same; you can tweak system to your particular use.
- Thoreandan 4y agohttps://www.hboehm.info/gc/ https://www.hboehm.info/gc/ is a windows+linux+unix allocator implementation - it uses VirtualAlloc on Windows and mmap or sbrk on Unix. For performance reasons (avoiding a syscall) almost all malloc implementations only use sbrk / mmap occasionally and keep the malloc calls in userspace.
- Dwedit 4y agoOn Windows, most malloc calls are the Win32 API function HeapAlloc.
- 10000truths 4y agoThomas Pornin, the author of BearSSL?