From: Oleksandr Natalenko <oleksandr@natalenko.name>
To: netfilter-devel@vger.kernel.org
Subject: [PATCH v2 1/2] utils: provide array allocation wrapper
Date: Mon, 2 Jan 2017 20:54:08 +0100 [thread overview]
Message-ID: <20170102195409.14669-2-oleksandr@natalenko.name> (raw)
In-Reply-To: <20170102195409.14669-1-oleksandr@natalenko.name>
This will be used for allocating memory for arrays
in heap instead of keeping them on stack.
Signed-off-by: Oleksandr Natalenko <oleksandr@natalenko.name>
---
include/utils.h | 1 +
src/utils.c | 10 ++++++++++
2 files changed, 11 insertions(+)
diff --git a/include/utils.h b/include/utils.h
index bb58ba4..3199388 100644
--- a/include/utils.h
+++ b/include/utils.h
@@ -138,6 +138,7 @@ extern void __memory_allocation_error(const char *filename, uint32_t line) __nor
extern void xfree(const void *ptr);
extern void *xmalloc(size_t size);
+extern void *xmalloc_array(size_t nmemb, size_t size);
extern void *xrealloc(void *ptr, size_t size);
extern void *xzalloc(size_t size);
extern char *xstrdup(const char *s);
diff --git a/src/utils.c b/src/utils.c
index 65dabf4..16bc9e2 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -39,6 +39,16 @@ void *xmalloc(size_t size)
return ptr;
}
+void *xmalloc_array(size_t nmemb, size_t size)
+{
+ assert(size != 0);
+
+ if (nmemb > SIZE_MAX / size)
+ memory_allocation_error();
+
+ return xmalloc(nmemb * size);
+}
+
void *xrealloc(void *ptr, size_t size)
{
ptr = realloc(ptr, size);
--
2.11.0
next prev parent reply other threads:[~2017-01-02 19:54 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-01-02 19:54 [PATCH v2 0/2] segtree: move huge arrays to heap Oleksandr Natalenko
2017-01-02 19:54 ` Oleksandr Natalenko [this message]
2017-01-02 19:54 ` [PATCH v2 2/2] segtree: allocate memory for arrays on heap Oleksandr Natalenko
2017-01-03 1:54 ` [PATCH v2 0/2] segtree: move huge arrays to heap Florian Westphal
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20170102195409.14669-2-oleksandr@natalenko.name \
--to=oleksandr@natalenko.name \
--cc=netfilter-devel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).