Hacker News new | ask | show | jobs
by DSMan195276 3301 days ago
His allocating an fd_set to an arbitrary size is somewhat iffy standards wise, but if it works it works - especially for a solution for specific OSs like Linux, where this won't be broken in the future. On that note, it is worth wondering how expensive the copying is, but I would wager he is right in that the copying isn't actually that expensive - it's only an 8K buffer.

Also, when creating an array that is indexed by fd, if you want to only use the memory you need instead of creating an extremely large array for the max fd of the system you can just allocate the memory using `mmap`. Systems like Linux won't actually allocate all the memory right when you do the `mmap`, instead it will map in a zero-page in each spot. When you write to it, the kernel will automatically allocate a page to back that memory. When you combine that with the kernel always selecting the lowest fd, it means that the memory backing your array will only grow to be as large as needed for the maximum number of fd's you use. (Note: It's likely that such a large array would be allocated via `mmap` in it's own spot by malloc/calloc anyway, but using `mmap` ensures you get the functionality you intended.)