This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
[PATCH v2 1/2] Add nmalloc and nrealloc
- From: OndÅej BÃlka <neleai at seznam dot cz>
- To: Roland McGrath <roland at hack dot frob dot com>
- Cc: libc-alpha at sourceware dot org
- Date: Sun, 3 Nov 2013 17:54:11 +0100
- Subject: [PATCH v2 1/2] Add nmalloc and nrealloc
- Authentication-results: sourceware.org; auth=none
- References: <20131031164614 dot GA28117 at domone dot podge> <20131031202932 dot 8942074699 at topped-with-meat dot com>
Hi,
This patches cover more cases than previous patch. As a first part we
define nmalloc macros.
I tried a inline function but gcc decided not to inline some of these.
As this would cause a avoidable division that cost much more than few
bites that not inlining could save I go back to macros.
* include/nmalloc.h: New file.
---
include/nmalloc.h | 41 +++++++++++++++++++++++++++++++++++++++++
1 file changed, 41 insertions(+)
create mode 100644 include/nmalloc.h
diff --git a/include/nmalloc.h b/include/nmalloc.h
new file mode 100644
index 0000000..48deeaf
--- /dev/null
+++ b/include/nmalloc.h
@@ -0,0 +1,41 @@
+/* malloc macros with overflow checking.
+ Copyright (C) 2013 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ <http://www.gnu.org/licenses/>. */
+
+
+#include <stdint.h>
+#ifndef _NMALLOC_H
+#define _NMALLOC_H
+
+#define NMALLOC(x, t) ((t *) nmalloc (x, sizeof (t)))
+#define NREALLOC(p, x, t) ((t *) nrealloc (p, x, sizeof (t)))
+
+#define nmalloc(n, s) \
+({ \
+ size_t __n = (n), __s = (s); \
+ (__s == 0 || SIZE_MAX / __s < __n) ? NULL : malloc (__n * __s); \
+})
+
+void *realloc(void *ptr, size_t size);
+
+#define nrealloc(p, n, s) \
+({ \
+ size_t __n = (n), __s = (s); \
+ (__n != 0 && SIZE_MAX / __s < __n) ? NULL : realloc (p, __n * __s); \
+})
+
+#endif /* _NMALLOC_H */
--
1.8.4.rc3