mirror of
				https://xff.cz/git/u-boot/
				synced 2025-10-31 10:26:10 +01:00 
			
		
		
		
	malloc_simple: Add logging of allocations
It is sometimes useful to see what memory is being allocated early during boot. Add logging to support this, using a new LOGC_ALLOC category. Signed-off-by: Simon Glass <sjg@chromium.org>
This commit is contained in:
		| @@ -5,6 +5,8 @@ | |||||||
|  * Copyright (c) 2014 Google, Inc |  * Copyright (c) 2014 Google, Inc | ||||||
|  */ |  */ | ||||||
|  |  | ||||||
|  | #define LOG_CATEGORY LOGC_ALLOC | ||||||
|  |  | ||||||
| #include <common.h> | #include <common.h> | ||||||
| #include <malloc.h> | #include <malloc.h> | ||||||
| #include <mapmem.h> | #include <mapmem.h> | ||||||
| @@ -12,40 +14,47 @@ | |||||||
|  |  | ||||||
| DECLARE_GLOBAL_DATA_PTR; | DECLARE_GLOBAL_DATA_PTR; | ||||||
|  |  | ||||||
| void *malloc_simple(size_t bytes) | static void *alloc_simple(size_t bytes, int align) | ||||||
| { |  | ||||||
| 	ulong new_ptr; |  | ||||||
| 	void *ptr; |  | ||||||
|  |  | ||||||
| 	new_ptr = gd->malloc_ptr + bytes; |  | ||||||
| 	debug("%s: size=%zx, ptr=%lx, limit=%lx: ", __func__, bytes, new_ptr, |  | ||||||
| 	      gd->malloc_limit); |  | ||||||
| 	if (new_ptr > gd->malloc_limit) { |  | ||||||
| 		debug("space exhausted\n"); |  | ||||||
| 		return NULL; |  | ||||||
| 	} |  | ||||||
| 	ptr = map_sysmem(gd->malloc_base + gd->malloc_ptr, bytes); |  | ||||||
| 	gd->malloc_ptr = ALIGN(new_ptr, sizeof(new_ptr)); |  | ||||||
| 	debug("%lx\n", (ulong)ptr); |  | ||||||
|  |  | ||||||
| 	return ptr; |  | ||||||
| } |  | ||||||
|  |  | ||||||
| void *memalign_simple(size_t align, size_t bytes) |  | ||||||
| { | { | ||||||
| 	ulong addr, new_ptr; | 	ulong addr, new_ptr; | ||||||
| 	void *ptr; | 	void *ptr; | ||||||
|  |  | ||||||
| 	addr = ALIGN(gd->malloc_base + gd->malloc_ptr, align); | 	addr = ALIGN(gd->malloc_base + gd->malloc_ptr, align); | ||||||
| 	new_ptr = addr + bytes - gd->malloc_base; | 	new_ptr = addr + bytes - gd->malloc_base; | ||||||
|  | 	log_debug("size=%zx, ptr=%lx, limit=%lx: ", bytes, new_ptr, | ||||||
|  | 		  gd->malloc_limit); | ||||||
| 	if (new_ptr > gd->malloc_limit) { | 	if (new_ptr > gd->malloc_limit) { | ||||||
| 		debug("space exhausted\n"); | 		log_err("alloc space exhausted\n"); | ||||||
| 		return NULL; | 		return NULL; | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	ptr = map_sysmem(addr, bytes); | 	ptr = map_sysmem(addr, bytes); | ||||||
| 	gd->malloc_ptr = ALIGN(new_ptr, sizeof(new_ptr)); | 	gd->malloc_ptr = ALIGN(new_ptr, sizeof(new_ptr)); | ||||||
| 	debug("%lx\n", (ulong)ptr); |  | ||||||
|  | 	return ptr; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | void *malloc_simple(size_t bytes) | ||||||
|  | { | ||||||
|  | 	void *ptr; | ||||||
|  |  | ||||||
|  | 	ptr = alloc_simple(bytes, 1); | ||||||
|  | 	if (!ptr) | ||||||
|  | 		return ptr; | ||||||
|  |  | ||||||
|  | 	log_debug("%lx\n", (ulong)ptr); | ||||||
|  |  | ||||||
|  | 	return ptr; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | void *memalign_simple(size_t align, size_t bytes) | ||||||
|  | { | ||||||
|  | 	void *ptr; | ||||||
|  |  | ||||||
|  | 	ptr = alloc_simple(bytes, align); | ||||||
|  | 	if (!ptr) | ||||||
|  | 		return ptr; | ||||||
|  | 	log_debug("aligned to %lx\n", (ulong)ptr); | ||||||
|  |  | ||||||
| 	return ptr; | 	return ptr; | ||||||
| } | } | ||||||
| @@ -57,9 +66,16 @@ void *calloc(size_t nmemb, size_t elem_size) | |||||||
| 	void *ptr; | 	void *ptr; | ||||||
|  |  | ||||||
| 	ptr = malloc(size); | 	ptr = malloc(size); | ||||||
| 	if (ptr) | 	if (!ptr) | ||||||
| 		memset(ptr, '\0', size); | 		return ptr; | ||||||
|  | 	memset(ptr, '\0', size); | ||||||
|  |  | ||||||
| 	return ptr; | 	return ptr; | ||||||
| } | } | ||||||
| #endif | #endif | ||||||
|  |  | ||||||
|  | void malloc_simple_info(void) | ||||||
|  | { | ||||||
|  | 	log_info("malloc_simple: %lx bytes used, %lx remain\n", gd->malloc_ptr, | ||||||
|  | 		 CONFIG_VAL(SYS_MALLOC_F_LEN) - gd->malloc_ptr); | ||||||
|  | } | ||||||
|   | |||||||
| @@ -114,7 +114,7 @@ int _log(enum log_category_t cat, enum log_level_t level, const char *file, | |||||||
| /* Emit a log record if the level is less that the maximum */ | /* Emit a log record if the level is less that the maximum */ | ||||||
| #define log(_cat, _level, _fmt, _args...) ({ \ | #define log(_cat, _level, _fmt, _args...) ({ \ | ||||||
| 	int _l = _level; \ | 	int _l = _level; \ | ||||||
| 	if (_l <= _LOG_MAX_LEVEL) \ | 	if (CONFIG_IS_ENABLED(LOG) && _l <= _LOG_MAX_LEVEL) \ | ||||||
| 		_log((enum log_category_t)(_cat), _l, __FILE__, __LINE__, \ | 		_log((enum log_category_t)(_cat), _l, __FILE__, __LINE__, \ | ||||||
| 		      __func__, \ | 		      __func__, \ | ||||||
| 		      pr_fmt(_fmt), ##_args); \ | 		      pr_fmt(_fmt), ##_args); \ | ||||||
|   | |||||||
| @@ -880,6 +880,7 @@ static inline void free(void *ptr) {} | |||||||
| void *calloc(size_t nmemb, size_t size); | void *calloc(size_t nmemb, size_t size); | ||||||
| void *memalign_simple(size_t alignment, size_t bytes); | void *memalign_simple(size_t alignment, size_t bytes); | ||||||
| void *realloc_simple(void *ptr, size_t size); | void *realloc_simple(void *ptr, size_t size); | ||||||
|  | void malloc_simple_info(void); | ||||||
| #else | #else | ||||||
|  |  | ||||||
| # ifdef USE_DL_PREFIX | # ifdef USE_DL_PREFIX | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user