[Skiboot] [RFC PATCH v4 11/16] core/mem_region.c: Implement local free

Ryan Grimm grimm at linux.ibm.com
Thu Feb 27 23:20:37 AEDT 2020


Signed-off-by: Ryan Grimm <grimm at us.ibm.com>
---
 core/mem_region.c           | 32 ++++++++++++++++++++++++++++++++
 include/mem_region-malloc.h |  3 +++
 2 files changed, 35 insertions(+)

diff --git a/core/mem_region.c b/core/mem_region.c
index eb48a1a1..6fe315bd 100644
--- a/core/mem_region.c
+++ b/core/mem_region.c
@@ -906,6 +906,38 @@ restart:
 	return p;
 }
 
+void __local_free(void *p, const char *location)
+{
+	struct mem_region *region;
+	struct alloc_hdr *hdr;
+
+	if (!p)
+		return;
+
+	lock(&mem_region_lock);
+
+	list_for_each(&regions, region, list) {
+		/* local_alloc doesn't use heap */
+		if (region == &skiboot_heap)
+			continue;
+
+		if (p >= region_start(region) &&
+		   (p < (region_start(region) + region->len))) {
+			hdr = p - sizeof(*hdr);
+
+			if (hdr->free)
+				bad_header(region, hdr, "re-freed", location);
+
+			lock(&region->free_list_lock);
+			make_free(region, (struct free_hdr *)hdr, location, false);
+			unlock(&region->free_list_lock);
+		}
+
+	}
+
+	unlock(&mem_region_lock);
+}
+
 struct mem_region *find_mem_region(const char *name)
 {
 	struct mem_region *region;
diff --git a/include/mem_region-malloc.h b/include/mem_region-malloc.h
index 4350c564..c1a6b886 100644
--- a/include/mem_region-malloc.h
+++ b/include/mem_region-malloc.h
@@ -28,4 +28,7 @@ void *__local_alloc(unsigned int chip, size_t size, size_t align,
 #define local_alloc(chip_id, size, align)	\
 	__local_alloc((chip_id), (size), (align), __location__)
 
+void __local_free(void *ptr, const char *location);
+#define local_free(ptr) __local_free(ptr, __location__);
+
 #endif /* __MEM_REGION_MALLOC_H */
-- 
2.21.0



More information about the Skiboot mailing list