[PATCH 3/4] crypto: crc32: allocate crc_table statically

Sascha Hauer s.hauer at pengutronix.de
Mon Sep 4 01:18:24 PDT 2023


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 at 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




More information about the barebox mailing list