This patch in the msvcrt-A?? series, moves frequently used "marking the position of the next free fd" into a function of it's own. License: LGPL Changelog: * dlls/msvcrt/file.c: Jaco Greeff <jaco@puxedo.org> - Move the frequently used check for the next available fd into an internal function of its own to avoid code duplication --[ inline patch ]-- diff -aurN msvcrt-A10/dlls/msvcrt/file.c msvcrt-A11/dlls/msvcrt/file.c --- msvcrt-A10/dlls/msvcrt/file.c Mon Nov 4 14:34:28 2002 +++ msvcrt-A11/dlls/msvcrt/file.c Mon Nov 4 15:06:39 2002 @@ -146,6 +146,18 @@ MSVCRT_fdstart = fd; } +/* INTERNAL: mark the next free slot */ +static void msvcrt_next_free(int fd) +{ + /* locate next free slot as in msvcrt_alloc_fd */ + if (fd == MSVCRT_fdend) + MSVCRT_fdstart = ++MSVCRT_fdend; + else + while((MSVCRT_fdstart < MSVCRT_fdend) && + (MSVCRT_handles[MSVCRT_fdstart] != INVALID_HANDLE_VALUE)) + MSVCRT_fdstart++; +} + /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */ static int msvcrt_alloc_fd(HANDLE hand, int flag) { @@ -160,13 +172,7 @@ MSVCRT_handles[fd] = hand; MSVCRT_flags[fd] = flag; - /* locate next free slot */ - if (fd == MSVCRT_fdend) - MSVCRT_fdstart = ++MSVCRT_fdend; - else - while(MSVCRT_fdstart < MSVCRT_fdend && - MSVCRT_handles[MSVCRT_fdstart] != INVALID_HANDLE_VALUE) - MSVCRT_fdstart++; + msvcrt_next_free(fd); return fd; } @@ -208,13 +214,7 @@ MSVCRT_files[newfd] = NULL; MSVCRT_tempfiles[newfd] = NULL; - /* locate next free slot as in msvcrt_alloc_fd */ - if (newfd == MSVCRT_fdend) - MSVCRT_fdstart = ++MSVCRT_fdend; - else - while((MSVCRT_fdstart < MSVCRT_fdend) && - (MSVCRT_handles[MSVCRT_fdstart] != INVALID_HANDLE_VALUE)) - MSVCRT_fdstart++; + msvcrt_next_free(newfd); return newfd; } @@ -754,6 +754,12 @@ MSVCRT_files[fd2] = NULL; MSVCRT_tempfiles[fd2] = NULL; + /* If fd2 was marked as the first free file descriptor, + * re-allocate it as not to be used + */ + if (fd2 == MSVCRT_fdstart) + msvcrt_next_free(fd2); + return 0; }