[Skiboot] [RFC PATCH v7 09/12] core/mem_region.c: Implement local free

Ryan Grimm grimm at linux.ibm.com
Tue Jul 7 02:14:36 AEST 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 36de2d09..aefea8d2 100644
--- a/core/mem_region.c
+++ b/core/mem_region.c
@@ -920,6 +920,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 271311b2..cf64eb25 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