Kernel: Use vector<array<Module*>> instead of flat array
authorBen Gamari <bgamari.foss@gmail.com>
Tue, 22 Jan 2013 04:39:44 +0000 (23:39 -0500)
committerBen Gamari <bgamari.foss@gmail.com>
Wed, 23 Jan 2013 05:15:45 +0000 (00:15 -0500)
As it turns out ON_CONFIG_RELOAD filled the previous array size of 32
entries. Using vector<array<...>> here does incur a memory indirection,
which Arthur rightly points out will have a performance penalty in a hot
path. That being said, we'll use this for now. Eventually it might make
sense to move to array<array<...>> with a sufficiently large size.

src/libs/Kernel.cpp
src/libs/Kernel.h

index 8578ea0..b3aafaf 100644 (file)
@@ -32,9 +32,6 @@
 
 // The kernel is the central point in Smoothie : it stores modules, and handles event calls
 Kernel::Kernel(){
-    // Value init for the arrays
-    bzero(hooks, sizeof(hooks));
-
     // Config first, because we need the baud_rate setting before we start serial
     this->config         = new Config();
 
@@ -116,31 +113,17 @@ void Kernel::add_module(Module* module){
 }
 
 void Kernel::register_for_event(_EVENT_ENUM id_event, Module* module){
-    uint8_t current_id = 0;
-    Module* current = this->hooks[id_event][0];
-    while(current != NULL ){
-        if( current == module ){ return; }
-        current_id++;
-        current = this->hooks[id_event][current_id];
-    }
-    this->hooks[id_event][current_id] = module;
-    this->hooks[id_event][current_id+1] = NULL;
+    this->hooks[id_event].push_back(module);
 }
 
 void Kernel::call_event(_EVENT_ENUM id_event){
-    uint8_t current_id = 0; Module* current = this->hooks[id_event][0];
-    while(current != NULL ){   // For each active stepper
+    for (Module* current : hooks[id_event]) {
         (current->*kernel_callback_functions[id_event])(this);
-        current_id++;
-        current = this->hooks[id_event][current_id];
     }
 }
 
 void Kernel::call_event(_EVENT_ENUM id_event, void * argument){
-    uint8_t current_id = 0; Module* current = this->hooks[id_event][0];
-    while(current != NULL ){   // For each active stepper
+    for (Module* current : hooks[id_event]) {
         (current->*kernel_callback_functions[id_event])(argument);
-        current_id++;
-        current = this->hooks[id_event][current_id];
     }
 }
index 38ef2e6..e861538 100644 (file)
@@ -20,6 +20,7 @@
 #include "modules/robot/Planner.h"
 #include "modules/robot/Robot.h"
 #include "modules/robot/Stepper.h"
+#include <array>
 
 //Module manager
 class Module;
@@ -52,7 +53,7 @@ class Kernel {
         Digipot*          digipot;
 
     private:
-        Module* hooks[NUMBER_OF_DEFINED_EVENTS][32]; // When a module asks to be called for a specific event ( a hook ), this is where that request is remembered
+        std::array<std::vector<Module*>, NUMBER_OF_DEFINED_EVENTS> hooks; // When a module asks to be called for a specific event ( a hook ), this is where that request is remembered
 
 };