1. 09 9月, 2020 1 次提交
  2. 17 8月, 2020 1 次提交
  3. 19 10月, 2018 3 次提交
    • R
      adjust types in FILE struct to make line buffering check less expensive · 8084d6ab
      Rich Felker 提交于
      the choice of signed char for lbf was a theoretically space-saving
      hack that was not helping, and was unwantedly expensive. while
      comparing bytes against a byte-sized member sounds easy, the trick
      here was that the byte to be compared was unsigned while the lbf
      member was signed, making it possible to set lbf negative to disable
      line buffering. however, this imposed a requirement to promote both
      operands, zero-extending one and sign-extending the other, in order to
      compare them.
      
      to fix this, repurpose the waiters count slot (unused since commit
      c21f7507). while we're at it, switch
      mode (orientation) from signed char to int as well. this makes no
      semantic difference (its only possible values are -1, 0, and 1) but it
      might help on archs where byte access is awkward.
      8084d6ab
    • R
      optimize internal putc_unlocked macro used in putc · d8870dcf
      Rich Felker 提交于
      to check whether flush due to line buffering is needed, the int-type
      character argument must be truncated to unsigned char for comparison.
      if the original value is subsequently passed to __overflow, it must be
      preserved, adding to register pressure. since it doesn't matter,
      truncate all uses so the original value is no longer live.
      d8870dcf
    • R
      fix wrong result for putc variants due to operator precedence · a21a6092
      Rich Felker 提交于
      the internal putc_unlocked macro was wrongly returning a meaningless
      boolean result rather than the written character or EOF.
      
      bug was found by reading (very surprising) asm.
      a21a6092
  4. 17 10月, 2018 1 次提交
  5. 17 9月, 2018 1 次提交
    • R
      fix null pointer subtraction and comparison in stdio · 849e7603
      Rich Felker 提交于
      morally, for null pointers a and b, a-b, a<b, and a>b should all be
      defined as 0; however, C does not define any of them.
      
      the stdio implementation makes heavy use of such pointer comparison
      and subtraction for buffer logic, and also uses null pos/base/end
      pointers to indicate that the FILE is not in the corresponding (read
      or write) mode ready for accesses through the buffer.
      
      all of the comparisons are fixed trivially by using != in place of the
      relational operators, since the opposite relation (e.g. pos>end) is
      logically impossible. the subtractions have been reviewed to check
      that they are conditional the stream being in the appropriate reading-
      or writing-through-buffer mode, with checks added where needed.
      
      in fgets and getdelim, the checks added should improve performance for
      unbuffered streams by avoiding a do-nothing call to memchr, and should
      be negligible for buffered streams.
      849e7603
  6. 13 9月, 2018 6 次提交
    • R
      reduce spurious inclusion of libc.h · 5ce37379
      Rich Felker 提交于
      libc.h was intended to be a header for access to global libc state and
      related interfaces, but ended up included all over the place because
      it was the way to get the weak_alias macro. most of the inclusions
      removed here are places where weak_alias was needed. a few were
      recently introduced for hidden. some go all the way back to when
      libc.h defined CANCELPT_BEGIN and _END, and all (wrongly implemented)
      cancellation points had to include it.
      
      remaining spurious users are mostly callers of the LOCK/UNLOCK macros
      and files that use the LFS64 macro to define the awful *64 aliases.
      
      in a few places, new inclusion of libc.h is added because several
      internal headers no longer implicitly include libc.h.
      
      declarations for __lockfile and __unlockfile are moved from libc.h to
      stdio_impl.h so that the latter does not need libc.h. putting them in
      libc.h made no sense at all, since the macros in stdio_impl.h are
      needed to use them correctly anyway.
      5ce37379
    • R
      a3944d3f
    • R
      apply hidden visibility to stdio internals · d06fdd7f
      Rich Felker 提交于
      d06fdd7f
    • R
      declare __getopt_msg in stdio_impl.h · 8c1ac426
      Rich Felker 提交于
      it's not ideal, but the function is essentially an extended stdio
      function specialized to getopt's needs. the only reason it exists is
      avoiding pulling printf code into every program using getopt.
      8c1ac426
    • R
      move __stdio_exit_needed to stdio_impl.h · 50a298ea
      Rich Felker 提交于
      this functions is glue for linking dependency logic.
      50a298ea
    • R
      make internal declarations for flockfile tracking functions checkable · 3b028c28
      Rich Felker 提交于
      logically these belong to the intersection of the stdio and pthread
      subsystems, and either place the declarations could go (stdio_impl.h
      or pthread_impl.h) requires a forward declaration for one of the
      argument types.
      3b028c28
  7. 25 2月, 2018 1 次提交
    • R
      use idiomatic safe form for FUNLOCK macro · 455bd824
      Rich Felker 提交于
      previously this macro used an odd if/else form instead of the more
      idiomatic do/while(0), making it unsafe against omission of trailing
      semicolon. the omission would make the following statement conditional
      instead of producing an error.
      455bd824
  8. 11 2月, 2016 1 次提交
    • R
      fix line-buffered flush omission for odd usage of putc-family functions · 416d1c7a
      Rich Felker 提交于
      as specified, the int argument providing the character to write is
      converted to type unsigned char. for the actual write to buffer,
      conversion happened implicitly via the assignment operator; however,
      the logic to check whether the argument was a newline used the
      original int value. thus usage such as putchar('\n'+0x100) failed to
      produce a flush.
      416d1c7a
  9. 16 6月, 2015 2 次提交
    • R
      refactor stdio open file list handling, move it out of global libc struct · 1b0cdc87
      Rich Felker 提交于
      functions which open in-memory FILE stream variants all shared a tail
      with __fdopen, adding the FILE structure to stdio's open file list.
      replacing this common tail with a function call reduces code size and
      duplication of logic. the list is also partially encapsulated now.
      
      function signatures were chosen to facilitate tail call optimization
      and reduce the need for additional accessor functions.
      
      with these changes, static linked programs that do not use stdio no
      longer have an open file list at all.
      1b0cdc87
    • R
      byte-based C locale, phase 2: stdio and iconv (multibyte callers) · 16f18d03
      Rich Felker 提交于
      this patch adjusts libc components which use the multibyte functions
      internally, and which depend on them operating in a particular
      encoding, to make the appropriate locale changes before calling them
      and restore the calling thread's locale afterwards. activating the
      byte-based C locale without these changes would cause regressions in
      stdio and iconv.
      
      in the case of iconv, the current implementation was simply using the
      multibyte functions as UTF-8 conversions. setting a multibyte UTF-8
      locale for the duration of the iconv operation allows the code to
      continue working.
      
      in the case of stdio, POSIX requires that FILE streams have an
      encoding rule bound at the time of setting wide orientation. as long
      as all locales, including the C locale, used the same encoding,
      treating high bytes as UTF-8, there was no need to store an encoding
      rule as part of the stream's state.
      
      a new locale field in the FILE structure points to the locale that
      should be made active during fgetwc/fputwc/ungetwc on the stream. it
      cannot point to the locale active at the time the stream becomes
      oriented, because this locale could be mutable (the global locale) or
      could be destroyed (locale_t objects produced by newlocale) before the
      stream is closed. instead, a pointer to the static C or C.UTF-8 locale
      object added in commit commit aeeac9ca
      is used. this is valid since categories other than LC_CTYPE will not
      affect these functions.
      16f18d03
  10. 04 3月, 2015 1 次提交
    • R
      make all objects used with atomic operations volatile · 56fbaa3b
      Rich Felker 提交于
      the memory model we use internally for atomics permits plain loads of
      values which may be subject to concurrent modification without
      requiring that a special load function be used. since a compiler is
      free to make transformations that alter the number of loads or the way
      in which loads are performed, the compiler is theoretically free to
      break this usage. the most obvious concern is with atomic cas
      constructs: something of the form tmp=*p;a_cas(p,tmp,f(tmp)); could be
      transformed to a_cas(p,*p,f(*p)); where the latter is intended to show
      multiple loads of *p whose resulting values might fail to be equal;
      this would break the atomicity of the whole operation. but even more
      fundamental breakage is possible.
      
      with the changes being made now, objects that may be modified by
      atomics are modeled as volatile, and the atomic operations performed
      on them by other threads are modeled as asynchronous stores by
      hardware which happens to be acting on the request of another thread.
      such modeling of course does not itself address memory synchronization
      between cores/cpus, but that aspect was already handled. this all
      seems less than ideal, but it's the best we can do without mandating a
      C11 compiler and using the C11 model for atomics.
      
      in the case of pthread_once_t, the ABI type of the underlying object
      is not volatile-qualified. so we are assuming that accessing the
      object through a volatile-qualified lvalue via casts yields volatile
      access semantics. the language of the C standard is somewhat unclear
      on this matter, but this is an assumption the linux kernel also makes,
      and seems to be the correct interpretation of the standard.
      56fbaa3b
  11. 24 8月, 2014 1 次提交
    • R
      fix false ownership of stdio FILEs due to tid reuse · 5345c9b8
      Rich Felker 提交于
      this is analogous commit fffc5cda
      which fixed the corresponding issue for mutexes.
      
      the robust list can't be used here because the locks do not share a
      common layout with mutexes. at some point it may make sense to simply
      incorporate a mutex object into the FILE structure and use it, but
      that would be a much more invasive change, and it doesn't mesh well
      with the current design that uses a simpler code path for internal
      locking and pulls in the recursive-mutex-like code when the flockfile
      API is used explicitly.
      5345c9b8
  12. 07 2月, 2014 1 次提交
    • R
      fix ftello result for append streams with unflushed output · 3af2edee
      Rich Felker 提交于
      when there is unflushed output, ftello (and ftell) compute the logical
      stream position as the underlying file descriptor's offset plus an
      adjustment for the amount of buffered data. however, this can give the
      wrong result for append-mode streams where the unflushed writes should
      adjust the logical position to be at the end of the file, as if a seek
      to end-of-file takes place before the write.
      
      the solution turns out to be a simple trick: when ftello (indirectly)
      calls lseek to determine the current file offset, use SEEK_END instead
      of SEEK_CUR if the stream is append-mode and there's unwritten
      buffered data.
      
      the ISO C rules regarding switching between reading and writing for a
      stream opened in an update mode, along with the POSIX rules regarding
      switching "active handles", conveniently leave undefined the
      hypothetical usage cases where this fix might lead to observably
      incorrect offsets.
      
      the bug being fixed was discovered via the test case for glibc issue
      3af2edee
  13. 22 7月, 2013 1 次提交
    • R
      refactor headers, especially alltypes.h, and improve C++ ABI compat · 9448b051
      Rich Felker 提交于
      the arch-specific bits/alltypes.h.sh has been replaced with a generic
      alltypes.h.in and minimal arch-specific bits/alltypes.h.in.
      
      this commit is intended to have no functional changes except:
      - exposing additional symbols that POSIX allows but does not require
      - changing the C++ name mangling for some types
      - fixing the signedness of blksize_t on powerpc (POSIX requires signed)
      - fixing the limit macros for sig_atomic_t on x86_64
      - making dev_t an unsigned type (ABI matching goal, and more logical)
      
      in addition, some types that were wrongly defined with long on 32-bit
      archs were changed to int, and vice versa; this change is
      non-functional except for the possibility of making pointer types
      mismatch, and only affects programs that were using them incorrectly,
      and only at build-time, not runtime.
      
      the following changes were made in the interest of moving
      non-arch-specific types out of the alltypes system and into the
      headers they're associated with, and also will tend to improve
      application compatibility:
      - netdb.h now includes netinet/in.h (for socklen_t and uint32_t)
      - netinet/in.h now includes sys/socket.h and inttypes.h
      - sys/resource.h now includes sys/time.h (for struct timeval)
      - sys/wait.h now includes signal.h (for siginfo_t)
      - langinfo.h now includes nl_types.h (for nl_item)
      
      for the types in stdint.h:
      - types which are of no interest to other headers were moved out of
        the alltypes system.
      - fast types for 8- and 64-bit are hard-coded (at least for now); only
        the 16- and 32-bit ones have reason to vary by arch.
      
      and the following types have been changed for C++ ABI purposes;
      - mbstate_t now has a struct tag, __mbstate_t
      - FILE's struct tag has been changed to _IO_FILE
      - DIR's struct tag has been changed to __dirstream
      - locale_t's struct tag has been changed to __locale_struct
      - pthread_t is defined as unsigned long in C++ mode only
      - fpos_t now has a struct tag, _G_fpos64_t
      - fsid_t's struct tag has been changed to __fsid_t
      - idtype_t has been made an enum type (also required by POSIX)
      - nl_catd has been changed from long to void *
      - siginfo_t's struct tag has been removed
      - sigset_t's has been given a struct tag, __sigset_t
      - stack_t has been given a struct tag, sigaltstack
      - suseconds_t has been changed to long on 32-bit archs
      - [u]intptr_t have been changed from long to int rank on 32-bit archs
      - dev_t has been made unsigned
      
      summary of tests that have been performed against these changes:
      - nsz's libc-test (diff -u before and after)
      - C++ ABI check symbol dump (diff -u before, after, glibc)
      - grepped for __NEED, made sure types needed are still in alltypes
      - built gcc 3.4.6
      9448b051
  14. 09 11月, 2012 1 次提交
    • R
      clean up stdio_impl.h · 835f9f95
      Rich Felker 提交于
      this header evolved to facilitate the extremely lazy practice of
      omitting explicit includes of the necessary headers in individual
      stdio source files; not only was this sloppy, but it also increased
      build time.
      
      now, stdio_impl.h is only including the headers it needs for its own
      use; any further headers needed by source files are included directly
      where needed.
      835f9f95
  15. 02 11月, 2012 1 次提交
    • R
      fix more unused variable warnings · a617a8e2
      Rich Felker 提交于
      some of these were coming from stdio functions locking files without
      unlocking them. I believe it's useful for this to throw a warning, so
      I added a new macro that's self-documenting that the file will never
      be unlocked to avoid the warning in the few places where it's wrong.
      a617a8e2
  16. 26 10月, 2012 1 次提交
    • R
      use explicit visibility to optimize a few hot-path function calls · 607b05ac
      Rich Felker 提交于
      on x86 and some other archs, functions which make function calls which
      might go through a PLT incur a significant overhead cost loading the
      GOT register prior to making the call. this load is utterly useless in
      musl, since all calls are bound at library-creation time using
      -Bsymbolic-functions, but the compiler has no way of knowing this, and
      attempts to set the default visibility to protected have failed due to
      bugs in GCC and binutils.
      
      this commit simply manually assigns hidden/protected visibility, as
      appropriate, to a few internal-use-only functions which have many
      callers, or which have callers that are hot paths like getc/putc. it
      shaves about 5k off the i386 libc.so with -Os. many of the
      improvements are in syscall wrappers, where the benefit is just size
      and performance improvement is unmeasurable noise amid the syscall
      overhead. however, stdio may be measurably faster.
      
      if in the future there are toolchains that can do the same thing
      globally without introducing linking bugs, it might be worth
      considering removing these workarounds.
      607b05ac
  17. 25 10月, 2012 1 次提交
    • R
      greatly improve freopen behavior · 892cafff
      Rich Felker 提交于
      1. don't open /dev/null just as a basis to copy flags; use shared
      __fmodeflags function to get the right file flags for the mode.
      
      2. handle the case (probably invalid, but whatever) case where the
      original stream's file descriptor was closed; previously, the logic
      re-closed it.
      
      3. accept the "e" mode flag for close-on-exec; update dup3 to fallback
      to using dup2 so we can simply call __dup3 instead of putting fallback
      logic in freopen itself.
      892cafff
  18. 12 8月, 2012 1 次提交
    • R
      add bsd fgetln function · 61718273
      Rich Felker 提交于
      optimized to avoid allocation and return lines directly out of the
      stream buffer whenever possible.
      61718273
  19. 19 6月, 2012 1 次提交
    • R
      remove flush hook cruft that was never used from stdio · 2499cd9d
      Rich Felker 提交于
      there is no need/use for a flush hook. the write function serves this
      purpose already. i originally created the hook for implementing mem
      streams based on a mistaken reading of posix, and later realized it
      wasn't useful but never removed it until now.
      2499cd9d
  20. 25 4月, 2012 1 次提交
    • R
      ditch the priority inheritance locks; use malloc's version of lock · 4750cf42
      Rich Felker 提交于
      i did some testing trying to switch malloc to use the new internal
      lock with priority inheritance, and my malloc contention test got
      20-100 times slower. if priority inheritance futexes are this slow,
      it's simply too high a price to pay for avoiding priority inversion.
      maybe we can consider them somewhere down the road once the kernel
      folks get their act together on this (and perferably don't link it to
      glibc's inefficient lock API)...
      
      as such, i've switch __lock to use malloc's implementation of
      lightweight locks, and updated all the users of the code to use an
      array with a waiter count for their locks. this should give optimal
      performance in the vast majority of cases, and it's simple.
      
      malloc is still using its own internal copy of the lock code because
      it seems to yield measurably better performance with -O3 when it's
      inlined (20% or more difference in the contention stress test).
      4750cf42
  21. 17 4月, 2012 1 次提交
    • R
      new scanf implementation and corresponding integer parser/converter · 18efeb32
      Rich Felker 提交于
      advantages over the old code:
      - correct results for floating point (old code was bogus)
      - wide/regular scanf separated so scanf does not pull in wide code
      - well-defined behavior on integers that overflow dest type
      - support for %[a-b] ranges with %[ (impl-defined by widely used)
      - no intermediate conversion of fmt string to wide string
      - cleaner, easier to share code with strto* functions
      - better standards conformance for corner cases
      
      the old code remains in the source tree, as the wide versions of the
      scanf-family functions are still using it. it will be removed when no
      longer needed.
      18efeb32
  22. 11 4月, 2012 1 次提交
    • R
      add "scan helper getc" and rework strtod, etc. to use it · 2162541f
      Rich Felker 提交于
      the immediate benefit is a significant debloating of the float parsing
      code by moving the responsibility for keeping track of the number of
      characters read to a different module.
      
      by linking shgetc with the stdio buffer logic, counting logic is
      defered to buffer refill time, keeping the calls to shgetc fast and
      light.
      
      in the future, shgetc will also be useful for integrating the new
      float code with scanf, which needs to not only count the characters
      consumed, but also limit the number of characters read based on field
      width specifiers.
      
      shgetc may also become a useful tool for simplifying the integer
      parsing code.
      2162541f
  23. 30 7月, 2011 1 次提交
    • R
      add proper fuxed-based locking for stdio · dba68bf9
      Rich Felker 提交于
      previously, stdio used spinlocks, which would be unacceptable if we
      ever add support for thread priorities, and which yielded
      pathologically bad performance if an application attempted to use
      flockfile on a key file as a major/primary locking mechanism.
      
      i had held off on making this change for fear that it would hurt
      performance in the non-threaded case, but actually support for
      recursive locking had already inflicted that cost. by having the
      internal locking functions store a flag indicating whether they need
      to perform unlocking, rather than using the actual recursive lock
      counter, i was able to combine the conditionals at unlock time,
      eliminating any additional cost, and also avoid a nasty corner case
      where a huge number of calls to ftrylockfile could cause deadlock
      later at the point of internal locking.
      
      this commit also fixes some issues with usage of pthread_self
      conflicting with __attribute__((const)) which resulted in crashes with
      some compiler versions/optimizations, mainly in flockfile prior to
      pthread_create.
      dba68bf9
  24. 18 4月, 2011 1 次提交
  25. 28 3月, 2011 1 次提交
    • R
      major stdio overhaul, using readv/writev, plus other changes · e3cd6c5c
      Rich Felker 提交于
      the biggest change in this commit is that stdio now uses readv to fill
      the caller's buffer and the FILE buffer with a single syscall, and
      likewise writev to flush the FILE buffer and write out the caller's
      buffer in a single syscall.
      
      making this change required fundamental architectural changes to
      stdio, so i also made a number of other improvements in the process:
      
      - the implementation no longer assumes that further io will fail
        following errors, and no longer blocks io when the error flag is set
        (though the latter could easily be changed back if desired)
      
      - unbuffered mode is no longer implemented as a one-byte buffer. as a
        consequence, scanf unreading has to use ungetc, to the unget buffer
        has been enlarged to hold at least 2 wide characters.
      
      - the FILE structure has been rearranged to maintain the locations of
        the fields that might be used in glibc getc/putc type macros, while
        shrinking the structure to save some space.
      
      - error cases for fflush, fseek, etc. should be more correct.
      
      - library-internal macros are used for getc_unlocked and putc_unlocked
        now, eliminating some ugly code duplication. __uflow and __overflow
        are no longer used anywhere but these macros. switch to read or
        write mode is also separated so the code can be better shared, e.g.
        with ungetc.
      
      - lots of other small things.
      e3cd6c5c
  26. 25 3月, 2011 1 次提交
  27. 18 3月, 2011 1 次提交
  28. 13 3月, 2011 1 次提交
  29. 12 2月, 2011 1 次提交