Coding: Refactoring (Slideshow)

What’s Bad?

  1. We’re putting everything into a single file:

    • struct my_event

    • struct my_event_list

    Factor these out into separate files, event.{h,c}, and use those from the main driver file, my_driver.c [1].

    By the way, the Makefile for a module.ko that is built from multiple files looks like so,

    obj-m += module.o
    module-y += file1.o file2.o file3.o


    jjj draw a sketch

  2. Factor macros out into misc.h (and include that in the userspace programs)

  3. Add the warning options -Wswitch-enum and -Werror (and -Wall) to the compiler commandline. This is done by adding the following line to the Makefile,

    ccflags-y += -Werror -Wall -Wswitch-enum
  4. Encapsulate global variables into struct my_device

    We currently only have a single device node, implemented by multiple distinct entities which are global variables in the driver code,

    • struct my_event_list

    • struct cdev

    • struct device*

    Create a separate file pair device.{h,c} that contains struct my_device to contain these variables, and make an instance of that type a single global device instead.

    Lets move the build-up and tear-down code into methods

    • my_device_init()

    • my_device_destroy()