[PATCH 5/7] sandbox: Avoid using malloc() for system state

Heinrich Schuchardt xypron.glpk at gmx.de
Sun Jan 17 05:35:06 CET 2021


On 1/16/21 10:54 PM, Simon Glass wrote:
> This state is not accessible to the running U-Boot but at present it is
> allocated in the emulated SDRAM. This doesn't seem very useful. Adjust
> it to allocate from the OS instead.
>
> Signed-off-by: Simon Glass <sjg at chromium.org>
> ---
>
>   arch/sandbox/cpu/state.c | 16 ++++++++--------
>   1 file changed, 8 insertions(+), 8 deletions(-)
>
> diff --git a/arch/sandbox/cpu/state.c b/arch/sandbox/cpu/state.c
> index b2901b7a8ca..f9b4b4c045e 100644
> --- a/arch/sandbox/cpu/state.c
> +++ b/arch/sandbox/cpu/state.c
> @@ -29,17 +29,17 @@ static int state_ensure_space(int extra_size)
>   		return 0;
>
>   	size = used + extra_size;
> -	buf = malloc(size);
> +	buf = os_malloc(size);
>   	if (!buf)
>   		return -ENOMEM;
>
>   	ret = fdt_open_into(blob, buf, size);
>   	if (ret) {
> -		free(buf);
> +		os_free(buf);
>   		return -EIO;
>   	}
>
> -	free(blob);
> +	os_free(blob);
>   	state->state_fdt = buf;
>   	return 0;
>   }
> @@ -55,7 +55,7 @@ static int state_read_file(struct sandbox_state *state, const char *fname)
>   		printf("Cannot find sandbox state file '%s'\n", fname);
>   		return -ENOENT;
>   	}
> -	state->state_fdt = malloc(size);
> +	state->state_fdt = os_malloc(size);
>   	if (!state->state_fdt) {
>   		puts("No memory to read sandbox state\n");
>   		return -ENOMEM;
> @@ -77,7 +77,7 @@ static int state_read_file(struct sandbox_state *state, const char *fname)
>   err_read:
>   	os_close(fd);
>   err_open:
> -	free(state->state_fdt);
> +	os_free(state->state_fdt);
>   	state->state_fdt = NULL;
>
>   	return ret;
> @@ -244,7 +244,7 @@ int sandbox_write_state(struct sandbox_state *state, const char *fname)
>   	/* Create a state FDT if we don't have one */
>   	if (!state->state_fdt) {
>   		size = 0x4000;
> -		state->state_fdt = malloc(size);
> +		state->state_fdt = os_malloc(size);
>   		if (!state->state_fdt) {
>   			puts("No memory to create FDT\n");
>   			return -ENOMEM;
> @@ -302,7 +302,7 @@ int sandbox_write_state(struct sandbox_state *state, const char *fname)
>   err_write:
>   	os_close(fd);
>   err_create:
> -	free(state->state_fdt);
> +	os_free(state->state_fdt);
>
>   	return ret;
>   }
> @@ -420,7 +420,7 @@ int state_uninit(void)
>   		os_unlink(state->jumped_fname);
>
>   	if (state->state_fdt)

This if is superfluous. os_free() checks for NULL.

     os_free(state->ram_buf);

is missing here.

Best regards

Heinrich

> -		free(state->state_fdt);
> +		os_free(state->state_fdt);
>   	memset(state, '\0', sizeof(*state));
>
>   	return 0;
>



More information about the U-Boot mailing list