mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <s.hauer@pengutronix.de>
To: Barebox List <barebox@lists.infradead.org>
Cc: Johannes Zink <j.zink@pengutronix.de>
Subject: [PATCH 3/4] crypto: crc32: allocate crc_table statically
Date: Mon,  4 Sep 2023 10:18:24 +0200	[thread overview]
Message-ID: <20230904081825.1997618-3-s.hauer@pengutronix.de> (raw)
In-Reply-To: <20230904081825.1997618-1-s.hauer@pengutronix.de>

Allocate crc_table statically. This makes the crc32 implementation
usable in PBL where we have no memory allocation function.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 crypto/crc32.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/crypto/crc32.c b/crypto/crc32.c
index 39572ff225..7cfc779078 100644
--- a/crypto/crc32.c
+++ b/crypto/crc32.c
@@ -22,7 +22,7 @@
 #define STATIC static inline
 #endif
 
-static uint32_t *crc_table;
+static uint32_t crc_table[sizeof(uint32_t) * 256];
 
 /*
   Generate a table for a byte-wise 32-bit CRC calculation on the polynomial:
@@ -56,13 +56,14 @@ static void make_crc_table(void)
 	/* terms of polynomial defining this crc (except x^32): */
 	static const char p[] = { 0, 1, 2, 4, 5, 7, 8, 10, 11, 12, 16, 22, 23, 26 };
 
+	if (crc_table[0])
+		return;
+
 	/* make exclusive-or pattern from polynomial (0xedb88320L) */
 	poly = 0;
 	for (n = 0; n < sizeof(p) / sizeof(char); n++)
 		poly |= 1U << (31 - p[n]);
 
-	crc_table = xmalloc(sizeof(uint32_t) * 256);
-
 	for (n = 0; n < 256; n++) {
 		c = (uint32_t) n;
 		for (k = 0; k < 8; k++)
@@ -80,8 +81,8 @@ STATIC uint32_t crc32(uint32_t crc, const void *_buf, unsigned int len)
 {
 	const unsigned char *buf = _buf;
 
-	if (!crc_table)
-		make_crc_table();
+	make_crc_table();
+
 	crc = crc ^ 0xffffffffL;
 	while (len >= 8) {
 		DO8(buf);
@@ -105,8 +106,8 @@ STATIC uint32_t crc32_no_comp(uint32_t crc, const void *_buf, unsigned int len)
 {
 	const unsigned char *buf = _buf;
 
-	if (!crc_table)
-		make_crc_table();
+	make_crc_table();
+
 	while (len >= 8) {
 		DO8(buf);
 		len -= 8;
-- 
2.39.2




  parent reply	other threads:[~2023-09-04  8:20 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-04  8:18 [PATCH 1/4] crypto: crc32: Drop CONFIG_DYNAMIC_CRC_TABLE option Sascha Hauer
2023-09-04  8:18 ` [PATCH 2/4] crypto: crc32: Lindent Sascha Hauer
2023-09-04  8:18 ` Sascha Hauer [this message]
2023-09-04  8:18 ` [PATCH 4/4] crypto: crc32: enable for PBL Sascha Hauer
2023-09-07  5:48 ` [PATCH 1/4] crypto: crc32: Drop CONFIG_DYNAMIC_CRC_TABLE option Johannes Zink

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=20230904081825.1997618-3-s.hauer@pengutronix.de \
    --to=s.hauer@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    --cc=j.zink@pengutronix.de \
    /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