libpayload: Add wrappers for malloc which check its return value.
The xmalloc wrapper checks whether the malloc succeeded, and if not stops execution and prints a message. xmalloc always returns a valid pointer. The xzalloc wrapper does the same thing, but also zeroes the memory before returning it. BUG=None TEST=Used this function in nyan, built and booted on it. BRANCH=None Change-Id: I00e7de04a5c368ab3603530b98bd3e3596e10632 Signed-off-by: Gabe Black <gabeblack@google.com> Reviewed-on: https://chromium-review.googlesource.com/178001 Reviewed-by: Julius Werner <jwerner@chromium.org> Reviewed-by: David Hendricks <dhendrix@chromium.org> Commit-Queue: Gabe Black <gabeblack@chromium.org> Tested-by: Gabe Black <gabeblack@chromium.org>
This commit is contained in:
parent
af36079987
commit
4029796d4f
1 changed files with 24 additions and 0 deletions
|
|
@ -2,6 +2,7 @@
|
|||
* This file is part of the libpayload project.
|
||||
*
|
||||
* Copyright (C) 2008 Advanced Micro Devices, Inc.
|
||||
* Copyright 2013 Google Inc.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
|
|
@ -30,7 +31,9 @@
|
|||
#ifndef _STDLIB_H
|
||||
#define _STDLIB_H
|
||||
|
||||
#include <die.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
/**
|
||||
* @defgroup malloc Memory allocation functions
|
||||
|
|
@ -145,6 +148,27 @@ void *dma_memalign(size_t align, size_t size);
|
|||
void init_dma_memory(void *start, u32 size);
|
||||
int dma_initialized(void);
|
||||
int dma_coherent(void *ptr);
|
||||
|
||||
static inline void *xmalloc_work(size_t size, const char *file,
|
||||
const char *func, int line)
|
||||
{
|
||||
void *ret = malloc(size);
|
||||
if (!ret) {
|
||||
die_work(file, func, line, "Failed to malloc %zd bytes.\n",
|
||||
size);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
#define xmalloc(size) xmalloc_work((size), __FILE__, __FUNCTION__, __LINE__)
|
||||
|
||||
static inline void *xzalloc_work(size_t size, const char *file,
|
||||
const char *func, int line)
|
||||
{
|
||||
void *ret = xmalloc_work(size, file, func, line);
|
||||
memset(ret, 0, size);
|
||||
return ret;
|
||||
}
|
||||
#define xzalloc(size) xzalloc_work((size), __FILE__, __FUNCTION__, __LINE__)
|
||||
/** @} */
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue