Skip to content

Commit

Permalink
linux_usbfs: Clean up inconsistencies and optimize memory usage
Browse files Browse the repository at this point in the history
The formatting and coding style varied across the whole file. Adopt the
following consistent style:

  - Align function arguments to the opening parenthesis
  - Do not check for NULL before calling free()
  - Reduce indentation where possible in loops by continuing in the
    success case
  - Remove space between function name and opening parenthesis
  - Remove pointless pointer casts from void *
  - Replace comparisons with NULL or 0 by a negation operator
  - When comparing against rvalues, place the rvalue on the right side
    of the expression
  - Where possible, have the debug message string on the same line as
    the usbi_* call. This makes it easier to grep for specific strings.

Also update the definitions in linux_usbfs.h to exactly match that of
the kernel and remove definitions that are not needed.

A number of functions declared stack buffers of size PATH_MAX. This is
generally 4K, which is very much overkill for a lot of the strings and
is not friendly for embedded environments. Replace many of these buffers
with reasonably-sized ones, in many cases using exactly the size needed.

When reading the descriptors during device enumeration, we were starting
with a 1K buffer and doubling as needed. The vast majority of devices
will not have a large set of descriptors, so change the allocation logic
to grow the buffer in steps of 256 bytes.

Introduce a new parsing function for reading sysfs attributes. Using the
fdopen() function to use fscanf() results in excessive memory
allocation, one for the FILE object and another for the buffer into
which the C library will read the data. The sysfs attributes of interest
are generally just a few characters, so use a small stack buffer and
some rigorous parsing to read these attributes. This also consolidates
error checking (e.g. negative values or larger than expected values).

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
  • Loading branch information
dickens committed Jan 24, 2020
1 parent d5bb64b commit c3864c8
Show file tree
Hide file tree
Showing 3 changed files with 539 additions and 571 deletions.

0 comments on commit c3864c8

Please sign in to comment.