diff options
-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 */ |