[coreboot-gerrit] New patch to review for coreboot: 49f6a52 libpayload: Add wrappers for malloc which check its return value.

Isaac Christensen (isaac.christensen@se-eng.com) gerrit at coreboot.org
Fri Sep 12 19:27:37 CEST 2014


Isaac Christensen (isaac.christensen at se-eng.com) just uploaded a new patch set to gerrit, which you can find at http://review.coreboot.org/6890

-gerrit

commit 49f6a522608193b9b29d220d43bbc4a5fb7c7150
Author: Gabe Black <gabeblack at google.com>
Date:   Sat Nov 23 00:54:40 2013 -0800

    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.
    
    Old-Change-Id: I00e7de04a5c368ab3603530b98bd3e3596e10632
    Signed-off-by: Gabe Black <gabeblack at google.com>
    Reviewed-on: https://chromium-review.googlesource.com/178001
    Reviewed-by: Julius Werner <jwerner at chromium.org>
    Reviewed-by: David Hendricks <dhendrix at chromium.org>
    Commit-Queue: Gabe Black <gabeblack at chromium.org>
    Tested-by: Gabe Black <gabeblack at chromium.org>
    (cherry picked from commit 4029796d4f66601e33ae3038dbfc3299f56baf89)
    
    libpayload: malloc: Fix xmalloc() for zero byte allocations
    
    The C standard considers it legal to return a NULL pointer for zero
    length memory allocations, and our malloc implementation does in fact
    make use of that. xmalloc() and xzmalloc() should therefore not consider
    this case a failure.
    
    Also fixed a minor formatting issue.
    
    Old-Change-Id: Ib9b75df9458ce2ba75fd0bc0af9814a3323298eb
    Signed-off-by: Julius Werner <jwerner at chromium.org>
    Reviewed-on: https://chromium-review.googlesource.com/178725
    Reviewed-by: Mike Frysinger <vapier at chromium.org>
    Reviewed-by: David Hendricks <dhendrix at chromium.org>
    (cherry picked from commit 3033437e9d89c6072464860ea50ea27dcb76fe54)
    
    Squashed 2 libpayload malloc related commits.
    
    Change-Id: I682ef5f4aad58c93ae2be40e2edc1fd29e5d0438
    Signed-off-by: Isaac Christensen <isaac.christensen at se-eng.com>
---
 payloads/libpayload/include/stdlib.h | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)

diff --git a/payloads/libpayload/include/stdlib.h b/payloads/libpayload/include/stdlib.h
index 1ed92d5..7113b6f 100644
--- a/payloads/libpayload/include/stdlib.h
+++ b/payloads/libpayload/include/stdlib.h
@@ -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 && size) {
+		die_work(file, func, line, "Failed to malloc %zu 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__)
 /** @} */
 
 /**



More information about the coreboot-gerrit mailing list