libgit2/libgit2 — pull request #856 by vmg (2012-08-06 11:26:44+00:00)
Title: Windows: Perform UTF-8 path conversion on the Stack

:sparkles::sparkles::sparkles: Yo sexy bitches :sparkles::sparkles::sparkles:

This is my take on #851, and some notes:

- I've killed the `windows.h` header and dropped support for custom codepages as part of the conversion to UTf8-only. Codepage conversion has never belonged on the library (or anywhere, really).

- All the `malloc`s are gone and conversion now happens on the stack. This was my main goal. As cited before, I understand that the malloc cost is inconsequential when compared to the actual I/O time of these syscalls, but I was worried with heap fragmentation, specially in multi-threaded applications.

I decided to go with stack buffers because `GIT_WIN_PATH` is a surprisingly low 260 characters (520 bytes on the stack as `wchar_t`. Plan B has always been keeping a global path in TLS, which in retrospect is not such a bad idea either. Once again, the overhead of the TLS access is inconsequential when compared to the syscall times. I personally have no concerns regarding stack overflows at the moment (specially after all the time we've spent with 4096-byte buffers on the stack), but if anybody has, I'll gladly look into TLS.

- I'm not 100% happy with `git__utf8_to_16` because right now it has no overflow checks. I think it's missing a `buf_size` parameter -- I'm going to get to that asap.

- It's worth noting that inverse conversion (UTF16 -> 8) is still performed with the Windows API. I'm OK with this, because UTF16 conversion is quite a big chunk of code and this path is not used frequently enough to be worth it.

That's it for now. Let me finish adding the overflow checks and I think this is ready to merge.