[SeaBIOS] [PATCH v5 2/4] malloc: support looking up a given pattern in FSEG

Michael S. Tsirkin mst at redhat.com
Wed Sep 25 11:54:42 CEST 2013


Will be used to find RSDP there.

Signed-off-by: Michael S. Tsirkin <mst at redhat.com>
---
 src/malloc.h |  1 +
 src/malloc.c | 19 ++++++++++++++++++-
 2 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/src/malloc.h b/src/malloc.h
index af8a21d..feb8938 100644
--- a/src/malloc.h
+++ b/src/malloc.h
@@ -19,6 +19,7 @@ void *_malloc(struct zone_s *zone, u32 handle, u32 size, u32 align);
 int _free(void *data);
 u32 malloc_getspace(struct zone_s *zone);
 void *malloc_find(u32 handle);
+void *malloc_find_fseg_pattern(void *pattern, unsigned pattern_size);
 
 #define MALLOC_DEFAULT_HANDLE 0xFFFFFFFF
 // Minimum alignment of malloc'd memory
diff --git a/src/malloc.c b/src/malloc.c
index 281f41e..0f5fae7 100644
--- a/src/malloc.c
+++ b/src/malloc.c
@@ -12,7 +12,7 @@
 #include "output.h" // dprintf
 #include "stacks.h" // wait_preempt
 #include "std/optionrom.h" // OPTION_ROM_ALIGN
-#include "string.h" // memset
+#include "string.h" // memset, memcmp
 
 // Information on a reserved area.
 struct allocinfo_s {
@@ -273,6 +273,23 @@ _free(void *data)
     return 0;
 }
 
+// Find the data block in zone matching a given pattern.
+void *malloc_find_fseg_pattern(void *pattern, unsigned pattern_size)
+{
+    extern u8 zonefseg_start[], zonefseg_end[];
+    unsigned space = zonefseg_end - zonefseg_start;
+    int off;
+
+    if (space < pattern_size)
+        return NULL;
+
+    for (off = 0; off < space - pattern_size; ++off) {
+        if (!memcmp(zonefseg_start + off, pattern, pattern_size))
+            return zonefseg_start + off;
+    }
+    return NULL;
+}
+
 // Find the amount of free space in a given zone.
 u32
 malloc_getspace(struct zone_s *zone)
-- 
MST




More information about the SeaBIOS mailing list