Coding: Refactoring (Slideshow)#
What’s Bad?#
We’re putting everything into a single file:
struct my_eventstruct 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
Makefilefor amodule.kothat is built from multiple files looks like so,obj-m += module.o module-y += file1.o file2.o file3.o
Todo
jjj draw a sketch
Factor macros out into
misc.h(and include that in the userspace programs)Add the warning options
-Wswitch-enumand-Werror(and-Wall) to the compiler commandline. This is done by adding the following line to theMakefile,ccflags-y += -Werror -Wall -Wswitch-enum
Encapsulate global variables into
struct my_deviceWe currently only have a single device node, implemented by multiple distinct entities which are global variables in the driver code,
struct my_event_liststruct cdevstruct device*…
Create a separate file pair
device.{h,c}that containsstruct my_deviceto 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()
Footnotes