Code Review: Tolerate NULL device functions

3 views
Skip to first unread message

Davide Libenzi

unread,
Dec 15, 2015, 5:45:30 PM12/15/15
to Akaros
It makes life easier if certain simple devices (ex, things like #version), would not have to declare empty stubs for functions it does not need to have.




The following changes since commit c8a6943551e4eb433c058e258bca0a99e713d581:

  Rename backtrace_kframe -> backtrace_hwtf [2/2] (2015-12-10 11:26:40 -0500)

are available in the git repository at:

  g...@github.com:dlibenzi/akaros tollerate_missing_devfuncs

for you to fetch changes up to b2b238bfd135d435a390d36067eb472aaa4c239b:

  Allow for certain device APIs to be NULL (2015-12-15 14:42:17 -0800)

----------------------------------------------------------------
Davide Libenzi (1):
      Allow for certain device APIs to be NULL

 kern/src/ns/chan.c   | 18 ++++++++++++------
 kern/src/ns/devtab.c | 15 ++++++++++-----
 2 files changed, 22 insertions(+), 11 deletions(-)

diff --git a/kern/src/ns/chan.c b/kern/src/ns/chan.c
index 918f289..ca2c965 100644
--- a/kern/src/ns/chan.c
+++ b/kern/src/ns/chan.c
@@ -116,16 +116,20 @@ void chandevreset(void)
 {
  int i;
 
- for (i = 0; &devtab[i] < __devtabend; i++)
- devtab[i].reset();
+ for (i = 0; &devtab[i] < __devtabend; i++) {
+ if (devtab[i].reset)
+ devtab[i].reset();
+ }
 }
 
 void chandevinit(void)
 {
  int i;
 
- for (i = 0; &devtab[i] < __devtabend; i++)
- devtab[i].init();
+ for (i = 0; &devtab[i] < __devtabend; i++) {
+ if (devtab[i].init)
+ devtab[i].init();
+ }
 }
 
 void chandevshutdown(void)
@@ -134,8 +138,10 @@ void chandevshutdown(void)
 
  /* shutdown in reverse order */
  for (i = 0; &devtab[i] < __devtabend; i++) ;
- for (i--; i >= 0; i--)
- devtab[i].shutdown();
+ for (i--; i >= 0; i--) {
+ if (devtab[i].shutdown)
+ devtab[i].shutdown();
+ }
 }
 
 static void chan_release(struct kref *kref)
diff --git a/kern/src/ns/devtab.c b/kern/src/ns/devtab.c
index 40ef1e3..7f1c5ab 100644
--- a/kern/src/ns/devtab.c
+++ b/kern/src/ns/devtab.c
@@ -20,8 +20,10 @@ void devtabreset()
 {
  int i;
 
- for (i = 0; &devtab[i] < __devtabend; i++)
- devtab[i].reset();
+ for (i = 0; &devtab[i] < __devtabend; i++) {
+ if (devtab[i].reset)
+ devtab[i].reset();
+ }
 }
 
 void devtabinit()
@@ -32,7 +34,8 @@ void devtabinit()
  /* if we have errors, check the align of struct dev and objdump */
  printd("i %d, '%s', dev %p, init %p\n", i, devtab[i].name,
  &devtab[i], devtab[i].init);
- devtab[i].init();
+ if (devtab[i].init)
+ devtab[i].init();
  }
 }
 
@@ -44,8 +47,10 @@ void devtabshutdown()
  * Shutdown in reverse order.
  */
  for (i = 0; &devtab[i] < __devtabend; i++) ;
- for (i--; i >= 0; i--)
- devtab[i].shutdown();
+ for (i--; i >= 0; i--) {
+ if (devtab[i].shutdown)
+ devtab[i].shutdown();
+ }
 }
 
 struct dev *devtabget(const char *name, int user)

Barret Rhoden

unread,
Dec 21, 2015, 12:41:54 PM12/21/15
to aka...@googlegroups.com
On 2015-12-15 at 14:45 "'Davide Libenzi' via Akaros"
<aka...@googlegroups.com> wrote:
> It makes life easier if certain simple devices (ex, things like
> #version), would not have to declare empty stubs for functions it
> does not need to have.

Under the old system (right before this patch), #version didn't need
to have stubs (like ver_init and ver_shutdown). It can just use
devinit, devshutdown, and devreset. But I prefer allowing them to be
0, since it was a source of confusion.

Merged to master at 345158006a10..f945c3bf7870 (from, to]

You can see the entire diff with 'git diff' or at
https://github.com/brho/akaros/compare/345158006a10...f945c3bf7870

Barret

Davide Libenzi

unread,
Dec 21, 2015, 1:32:46 PM12/21/15
to Akaros
In Linux, the file_ops, can just be initialized with only the callbacks you want to override.
If we want to always have the devtab struct to hold valid callbacks, we could have a DEVTAB_INIT macro, which initializes to the defaults, followed by device specific initializations.
As this is allowed in GCC:

struc a {
  .close = dummy_close,
  ...
  .close = my_device_close
};



Barret

--
You received this message because you are subscribed to the Google Groups "Akaros" group.
To unsubscribe from this group and stop receiving emails from it, send an email to akaros+un...@googlegroups.com.
To post to this group, send email to aka...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages