Browse Source

Remember the valid configuration location

Until now, the valid configuration object's location was not stored, so
it had to be looked up every time pdb_config_flash_read was called.  Now
the valid object's location is stored in config_cur, which is updated
every time the configuration's location changes.  This means that
pdb_config_flash_read only has to do the full routine the first time
it's called, or after pdb_config_flash_erase is run.  Now other parts of
the configuration won't have to cache the location themselves anymore.
Clara Hobbs 7 years ago
parent
commit
cf1375fe29
1 changed files with 20 additions and 1 deletions
  1. 20
    1
      src/storage.c

+ 20
- 1
src/storage.c View File

@@ -27,6 +27,10 @@
27 27
  * in the Makefile. */
28 28
 struct pdb_config *pdb_config_array = (struct pdb_config *) PDB_CONFIG_BASE;
29 29
 
30
+/* The location of the current configuration object.  NULL if not known or
31
+ * there is no current configuration. */
32
+struct pdb_config *config_cur = NULL;
33
+
30 34
 
31 35
 void pdb_config_print(BaseSequentialStream *chp, const struct pdb_config *cfg)
32 36
 {
@@ -170,6 +174,9 @@ void pdb_config_flash_erase(void)
170 174
 
171 175
     flash_lock();
172 176
 
177
+    /* There is no configuration now, so update config_cur to reflect this */
178
+    config_cur = NULL;
179
+
173 180
     /* Exit the critical zone */
174 181
     chSysUnlock();
175 182
 }
@@ -213,12 +220,23 @@ void pdb_config_flash_update(const struct pdb_config *cfg)
213 220
 
214 221
     flash_lock();
215 222
 
223
+    /* Update config_cur for the new configuration */
224
+    config_cur = empty;
225
+
216 226
     /* Exit the critical zone */
217 227
     chSysUnlock();
218 228
 }
219 229
 
220 230
 struct pdb_config *pdb_config_flash_read(void)
221 231
 {
232
+    /* If we already know where the configuration is, return its location */
233
+    if (config_cur != NULL) {
234
+        return config_cur;
235
+    }
236
+
237
+    /* We don't know where the configuration is (config_cur == NULL), so we
238
+     * need to find it and store its location if applicable. */
239
+
222 240
     /* If the first element is empty, there is no valid structure. */
223 241
     if (pdb_config_array[0].status == PDB_CONFIG_STATUS_EMPTY) {
224 242
         return NULL;
@@ -228,7 +246,8 @@ struct pdb_config *pdb_config_flash_read(void)
228 246
     for (int i = 0; i < PDB_CONFIG_ARRAY_LEN; i++) {
229 247
         /* If we've found it, return it. */
230 248
         if (pdb_config_array[i].status == PDB_CONFIG_STATUS_VALID) {
231
-            return &pdb_config_array[i];
249
+            config_cur = &pdb_config_array[i];
250
+            return config_cur;
232 251
         }
233 252
     }
234 253
 

Loading…
Cancel
Save