Nico Huber has uploaded this change for review.

View Change

layout: Add interface to create a dynamically growing layout

Another interim solution until we have a better internal layout
structure (i.e. linked lists).

Change-Id: I0093de58bc7eadc5cd52355937df23c4a6e934ed
Signed-off-by: Nico Huber <nico.h@gmx.de>
---
M layout.c
M layout.h
2 files changed, 60 insertions(+), 0 deletions(-)

git pull ssh://review.coreboot.org:29418/flashrom refs/changes/14/31014/1
diff --git a/layout.c b/layout.c
index c589d17..420a3d9 100644
--- a/layout.c
+++ b/layout.c
@@ -243,3 +243,54 @@
l->entries[i].start, l->entries[i].end, l->entries[i].name);
}
}
+
+static size_t dynlayout_size(unsigned int num_entries)
+{
+ return sizeof(struct dynamic_layout) + num_entries * sizeof(struct romentry);
+}
+
+struct flashrom_layout *new_layout(unsigned int num_entries)
+{
+ struct dynamic_layout *const dyn = malloc(dynlayout_size(num_entries));
+ if (!dyn) {
+ msg_gerr("Out of memory!\n");
+ return NULL;
+ }
+
+ memset(dyn, 0x00, sizeof(*dyn));
+ dyn->base.entries = (void *)((uint8_t *)dyn + sizeof(*dyn));
+ dyn->allocated = num_entries;
+
+ return &dyn->base;
+}
+
+int add_layout_entry(struct flashrom_layout **const l, const char *const name,
+ const size_t start, const size_t end)
+{
+ struct dynamic_layout *dyn;
+
+ if (!*l)
+ *l = new_layout(8);
+ if (!*l)
+ return -1;
+
+ dyn = (void *)*l;
+
+ if (dyn->base.num_entries >= dyn->allocated) {
+ const unsigned int allocate = (dyn->allocated ? : 1) * 2;
+ dyn = realloc(dyn, dynlayout_size(allocate));
+ if (!dyn)
+ return -1;
+ dyn->base.entries = (void *)((uint8_t *)dyn + sizeof(*dyn));
+ dyn->allocated = allocate;
+ *l = (void *)dyn;
+ }
+
+ const unsigned int i = dyn->base.num_entries++;
+ memset(&dyn->base.entries[i], 0x00, sizeof(dyn->base.entries[i]));
+ dyn->base.entries[i].start = start;
+ dyn->base.entries[i].end = end;
+ snprintf(dyn->base.entries[i].name, sizeof(dyn->base.entries[i].name), "%s", name);
+
+ return 0;
+}
diff --git a/layout.h b/layout.h
index a10cbb4..4227e92 100644
--- a/layout.h
+++ b/layout.h
@@ -54,6 +54,12 @@
struct romentry entry;
};

+struct dynamic_layout {
+ struct flashrom_layout base;
+ /* number of (pre)allocated entries */
+ unsigned int allocated;
+};
+
struct flashrom_layout *get_global_layout(void);
struct flashrom_flashctx;
const struct flashrom_layout *get_layout(const struct flashrom_flashctx *const flashctx);
@@ -62,4 +68,7 @@

void report_layout(const struct flashrom_layout *);

+struct flashrom_layout *new_layout(unsigned int num_entries);
+int add_layout_entry(struct flashrom_layout **, const char *name, size_t start, size_t end);
+
#endif /* !__LAYOUT_H__ */

To view, visit change 31014. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: flashrom
Gerrit-Branch: master
Gerrit-Change-Id: I0093de58bc7eadc5cd52355937df23c4a6e934ed
Gerrit-Change-Number: 31014
Gerrit-PatchSet: 1
Gerrit-Owner: Nico Huber <nico.h@gmx.de>
Gerrit-MessageType: newchange