diff options
author | Nicholas Piggin <npiggin@gmail.com> | 2023-05-13 21:31:34 +1000 |
---|---|---|
committer | Reza Arbab <arbab@linux.ibm.com> | 2023-06-06 13:32:12 -0500 |
commit | c6d9b3d454cfb077e32fe257d8d3dc3f6f78d10f (patch) | |
tree | c02ea3e80a122a3866bb834c8002e37b1e2061eb | |
parent | 17bbda085f3bc12dd94198706c7199f10e15fce9 (diff) | |
download | skiboot-c6d9b3d454cfb077e32fe257d8d3dc3f6f78d10f.zip skiboot-c6d9b3d454cfb077e32fe257d8d3dc3f6f78d10f.tar.gz skiboot-c6d9b3d454cfb077e32fe257d8d3dc3f6f78d10f.tar.bz2 |
mem_region: Add a local_free function
This allows memory to be allocated with local_alloc() to be freed.
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
Signed-off-by: Reza Arbab <arbab@linux.ibm.com>
-rw-r--r-- | core/mem_region.c | 34 | ||||
-rw-r--r-- | include/mem_region-malloc.h | 4 |
2 files changed, 38 insertions, 0 deletions
diff --git a/core/mem_region.c b/core/mem_region.c index e78d0a9..0ed00f3 100644 --- a/core/mem_region.c +++ b/core/mem_region.c @@ -920,6 +920,40 @@ restart: return p; } +static struct mem_region *mem_to_region(void *mem) +{ + struct mem_region *region; + + list_for_each(®ions, region, list) { + if (mem < region_start(region)) + continue; + if (mem >= region_start(region) + region->len) + continue; + return region; + } + return NULL; +} + +void __local_free(void *mem, const char *location) +{ + struct mem_region *region; + + lock(&mem_region_lock); + + region = mem_to_region(mem); + if (!region) { + prerror("MEM: local_free mem=%p no matching region.\n", mem); + unlock(&mem_region_lock); + return; + } + + lock(®ion->free_list_lock); + mem_free(region, mem, location); + unlock(®ion->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 271311b..b2e4337 100644 --- a/include/mem_region-malloc.h +++ b/include/mem_region-malloc.h @@ -28,4 +28,8 @@ 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 *mem, const char *location); +#define local_free(mem) \ + __local_free((mem), __location__) + #endif /* __MEM_REGION_MALLOC_H */ |