[glib: 1/8] galloca: Add new API g_alloca0 and g_newa0
- From: Philip Withnall <pwithnall src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glib: 1/8] galloca: Add new API g_alloca0 and g_newa0
- Date: Fri, 26 Nov 2021 12:38:47 +0000 (UTC)
commit b4631c44ad49574b30bdd71ac50ce2c9ddb1262b
Author: Marc-André Lureau <marcandre lureau redhat com>
Date: Thu Nov 25 13:38:11 2021 +0530
galloca: Add new API g_alloca0 and g_newa0
Added `g_alloca0()` which wraps `g_alloca()` and initializes
allocated memory to zeroes.
Added `g_newa0()` which wraps `g_alloca0()` in a typesafe manner.
Refreshed and tweaked by Nishal Kulkarni.
docs/reference/glib/glib-sections.txt | 2 ++
glib/galloca.h | 31 +++++++++++++++++++++++++++++++
2 files changed, 33 insertions(+)
---
diff --git a/docs/reference/glib/glib-sections.txt b/docs/reference/glib/glib-sections.txt
index 340e29dfa..c563a0bcd 100644
--- a/docs/reference/glib/glib-sections.txt
+++ b/docs/reference/glib/glib-sections.txt
@@ -1389,7 +1389,9 @@ g_mem_gc_friendly
<SUBSECTION>
g_alloca
+g_alloca0
g_newa
+g_newa0
<SUBSECTION>
g_memmove
diff --git a/glib/galloca.h b/glib/galloca.h
index 014a0efcb..86f0d7665 100644
--- a/glib/galloca.h
+++ b/glib/galloca.h
@@ -30,6 +30,7 @@
#endif
#include <glib/gtypes.h>
+#include <string.h>
#if defined(__BIONIC__) && defined (GLIB_HAVE_ALLOCA_H)
# include <alloca.h>
@@ -94,6 +95,22 @@ G_END_DECLS
* Returns: space for @size bytes, allocated on the stack
*/
#define g_alloca(size) alloca (size)
+
+/**
+ * g_alloca0:
+ * @size: number of bytes to allocate.
+ *
+ * Wraps g_alloca() and initializes allocated memory to zeroes.
+ * If @size is `0` it returns %NULL.
+ *
+ * Note that the @size argument will be evaluated multiple times.
+ *
+ * Returns: (nullable) (transfer full): space for @size bytes, allocated on the stack
+ *
+ * Since: 2.72
+ */
+#define g_alloca0(size) ((size) == 0 ? NULL : memset (g_alloca (size), 0, (size)))
+
/**
* g_newa:
* @struct_type: Type of memory chunks to be allocated
@@ -111,4 +128,18 @@ G_END_DECLS
*/
#define g_newa(struct_type, n_structs) ((struct_type*) g_alloca (sizeof (struct_type) * (gsize) (n_structs)))
+/**
+ * g_newa0:
+ * @struct_type: the type of the elements to allocate.
+ * @n_structs: the number of elements to allocate.
+ *
+ * Wraps g_alloca0() in a more typesafe manner.
+ *
+ * Returns: (nullable) (transfer full): Pointer to stack space for @n_structs
+ * chunks of type @struct_type
+ *
+ * Since: 2.72
+ */
+#define g_newa0(struct_type, n_structs) ((struct_type*) g_alloca0 (sizeof (struct_type) * (gsize)
(n_structs)))
+
#endif /* __G_ALLOCA_H__ */
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]