summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorSascha Hauer <s.hauer@pengutronix.de>2016-03-29 13:07:09 +0200
committerSascha Hauer <s.hauer@pengutronix.de>2016-04-08 13:33:18 +0200
commit3bafe5eac5b48193607c4617f32d9e5e2075189f (patch)
treea9a524dae075f47b4b5b9ac8896fec4fe34410a1 /lib
parent68bd0e50360fd97914e0716f4c9d81ceb980e3ba (diff)
downloadbarebox-3bafe5eac5b48193607c4617f32d9e5e2075189f.tar.gz
barebox-3bafe5eac5b48193607c4617f32d9e5e2075189f.tar.xz
decompressors: Use malloc/free wrappers
The decompressors are used both in a regular image and also for image decompression. Both need different malloc implementations. Using malloc/free directly in the decompressor code easily leads to include file conflicts, so use MALLOC/FREE which can be defined correctly for the two different usecases. Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'lib')
-rw-r--r--lib/decompress_inflate.c19
-rw-r--r--lib/decompress_unlz4.c10
-rw-r--r--lib/decompress_unlzo.c10
-rw-r--r--lib/decompress_unxz.c12
-rw-r--r--lib/xz/xz_dec_bcj.c2
-rw-r--r--lib/xz/xz_dec_lzma2.c14
-rw-r--r--lib/xz/xz_dec_stream.c6
-rw-r--r--lib/xz/xz_private.h4
-rw-r--r--lib/zlib_inflate/infutil.c8
9 files changed, 48 insertions, 37 deletions
diff --git a/lib/decompress_inflate.c b/lib/decompress_inflate.c
index d5ea01ad56..47bd3db131 100644
--- a/lib/decompress_inflate.c
+++ b/lib/decompress_inflate.c
@@ -13,6 +13,9 @@
#else /* STATIC */
/* initramfs et al: linked */
+#define MALLOC malloc
+#define FREE free
+
#include <linux/zutil.h>
#include <common.h>
#include <malloc.h>
@@ -49,7 +52,7 @@ int gunzip(unsigned char *buf, int len,
rc = -1;
if (flush) {
out_len = 0x8000; /* 32 K */
- out_buf = malloc(out_len);
+ out_buf = MALLOC(out_len);
} else {
out_len = 0x7fffffff; /* no limit */
}
@@ -61,7 +64,7 @@ int gunzip(unsigned char *buf, int len,
if (buf)
zbuf = buf;
else {
- zbuf = malloc(GZIP_IOBUF_SIZE);
+ zbuf = MALLOC(GZIP_IOBUF_SIZE);
len = 0;
}
if (!zbuf) {
@@ -69,13 +72,13 @@ int gunzip(unsigned char *buf, int len,
goto gunzip_nomem2;
}
- strm = malloc(sizeof(*strm));
+ strm = MALLOC(sizeof(*strm));
if (strm == NULL) {
error("Out of memory while allocating z_stream");
goto gunzip_nomem3;
}
- strm->workspace = malloc(flush ? zlib_inflate_workspacesize() :
+ strm->workspace = MALLOC(flush ? zlib_inflate_workspacesize() :
sizeof(struct inflate_state));
if (strm->workspace == NULL) {
error("Out of memory while allocating workspace");
@@ -170,15 +173,15 @@ int gunzip(unsigned char *buf, int len,
*pos = strm->next_in - zbuf+8;
gunzip_5:
- free(strm->workspace);
+ FREE(strm->workspace);
gunzip_nomem4:
- free(strm);
+ FREE(strm);
gunzip_nomem3:
if (!buf)
- free(zbuf);
+ FREE(zbuf);
gunzip_nomem2:
if (flush)
- free(out_buf);
+ FREE(out_buf);
gunzip_nomem1:
return rc; /* returns Z_OK (0) if successful */
}
diff --git a/lib/decompress_unlz4.c b/lib/decompress_unlz4.c
index 0dfb08c7d7..46a010ad4c 100644
--- a/lib/decompress_unlz4.c
+++ b/lib/decompress_unlz4.c
@@ -15,6 +15,8 @@
#else
#include <linux/decompress/unlz4.h>
#include <malloc.h>
+#define MALLOC malloc
+#define FREE free
#endif
#include <linux/types.h>
#include <linux/lz4.h>
@@ -62,7 +64,7 @@ static inline int unlz4(u8 *input, int in_len,
error("NULL output pointer and no flush function provided");
goto exit_0;
} else {
- outp = malloc(uncomp_chunksize);
+ outp = MALLOC(uncomp_chunksize);
if (!outp) {
error("Could not allocate output buffer");
goto exit_0;
@@ -78,7 +80,7 @@ static inline int unlz4(u8 *input, int in_len,
error("NULL input pointer and missing fill function");
goto exit_1;
} else {
- inp = malloc(lz4_compressbound(uncomp_chunksize));
+ inp = MALLOC(lz4_compressbound(uncomp_chunksize));
if (!inp) {
error("Could not allocate input buffer");
goto exit_1;
@@ -171,10 +173,10 @@ static inline int unlz4(u8 *input, int in_len,
ret = 0;
exit_2:
if (!input)
- free(inp_start);
+ FREE(inp_start);
exit_1:
if (!output)
- free(outp);
+ FREE(outp);
exit_0:
return ret;
}
diff --git a/lib/decompress_unlzo.c b/lib/decompress_unlzo.c
index 10ad42ab14..ad7f977280 100644
--- a/lib/decompress_unlzo.c
+++ b/lib/decompress_unlzo.c
@@ -36,6 +36,8 @@
#include "lzo/lzo1x_decompress_safe.c"
#else
#include <malloc.h>
+#define MALLOC malloc
+#define FREE free
#endif
#include <lzo.h>
@@ -126,7 +128,7 @@ int decompress_unlzo(u8 *input, int in_len,
error("NULL output pointer and no flush function provided");
goto exit;
} else {
- out_buf = malloc(LZO_BLOCK_SIZE);
+ out_buf = MALLOC(LZO_BLOCK_SIZE);
if (!out_buf) {
error("Could not allocate output buffer");
goto exit;
@@ -142,7 +144,7 @@ int decompress_unlzo(u8 *input, int in_len,
error("NULL input pointer and missing fill function");
goto exit_1;
} else {
- in_buf = malloc(lzo1x_worst_compress(LZO_BLOCK_SIZE));
+ in_buf = MALLOC(lzo1x_worst_compress(LZO_BLOCK_SIZE));
if (!in_buf) {
error("Could not allocate input buffer");
goto exit_1;
@@ -278,10 +280,10 @@ int decompress_unlzo(u8 *input, int in_len,
ret = 0;
exit_2:
if (!input)
- free(in_buf_save);
+ FREE(in_buf_save);
exit_1:
if (!output)
- free(out_buf);
+ FREE(out_buf);
exit:
return ret;
}
diff --git a/lib/decompress_unxz.c b/lib/decompress_unxz.c
index 1ddcee38ee..a7e2d331ab 100644
--- a/lib/decompress_unxz.c
+++ b/lib/decompress_unxz.c
@@ -109,6 +109,8 @@
#define XZ_EXTERN STATIC
#ifndef XZ_PREBOOT
+#define FREE free
+#define MALLOC malloc
# include <malloc.h>
# include <linux/xz.h>
#else
@@ -258,14 +260,14 @@ STATIC int decompress_unxz(unsigned char *in, int in_size,
b.out_size = (size_t)-1;
} else {
b.out_size = XZ_IOBUF_SIZE;
- b.out = malloc(XZ_IOBUF_SIZE);
+ b.out = MALLOC(XZ_IOBUF_SIZE);
if (b.out == NULL)
goto error_alloc_out;
}
if (in == NULL) {
must_free_in = true;
- in = malloc(XZ_IOBUF_SIZE);
+ in = MALLOC(XZ_IOBUF_SIZE);
if (in == NULL)
goto error_alloc_in;
}
@@ -316,10 +318,10 @@ STATIC int decompress_unxz(unsigned char *in, int in_size,
} while (ret == XZ_OK);
if (must_free_in)
- free(in);
+ FREE(in);
if (flush != NULL)
- free(b.out);
+ FREE(b.out);
}
if (in_used != NULL)
@@ -359,7 +361,7 @@ STATIC int decompress_unxz(unsigned char *in, int in_size,
error_alloc_in:
if (flush != NULL)
- free(b.out);
+ FREE(b.out);
error_alloc_out:
xz_dec_end(s);
diff --git a/lib/xz/xz_dec_bcj.c b/lib/xz/xz_dec_bcj.c
index a768e6d28b..d268adbc65 100644
--- a/lib/xz/xz_dec_bcj.c
+++ b/lib/xz/xz_dec_bcj.c
@@ -526,7 +526,7 @@ XZ_EXTERN enum xz_ret xz_dec_bcj_run(struct xz_dec_bcj *s,
XZ_EXTERN struct xz_dec_bcj *xz_dec_bcj_create(bool single_call)
{
- struct xz_dec_bcj *s = kmalloc(sizeof(*s), GFP_KERNEL);
+ struct xz_dec_bcj *s = MALLOC(sizeof(*s));
if (s != NULL)
s->single_call = single_call;
diff --git a/lib/xz/xz_dec_lzma2.c b/lib/xz/xz_dec_lzma2.c
index 08c3c80499..87ffd95f2e 100644
--- a/lib/xz/xz_dec_lzma2.c
+++ b/lib/xz/xz_dec_lzma2.c
@@ -1108,7 +1108,7 @@ XZ_EXTERN enum xz_ret xz_dec_lzma2_run(struct xz_dec_lzma2 *s,
XZ_EXTERN struct xz_dec_lzma2 *xz_dec_lzma2_create(enum xz_mode mode,
uint32_t dict_max)
{
- struct xz_dec_lzma2 *s = kmalloc(sizeof(*s), GFP_KERNEL);
+ struct xz_dec_lzma2 *s = MALLOC(sizeof(*s));
if (s == NULL)
return NULL;
@@ -1116,9 +1116,9 @@ XZ_EXTERN struct xz_dec_lzma2 *xz_dec_lzma2_create(enum xz_mode mode,
s->dict.size_max = dict_max;
if (DEC_IS_PREALLOC(mode)) {
- s->dict.buf = vmalloc(dict_max);
+ s->dict.buf = MALLOC(dict_max);
if (s->dict.buf == NULL) {
- kfree(s);
+ FREE(s);
return NULL;
}
} else if (DEC_IS_DYNALLOC(mode)) {
@@ -1146,8 +1146,8 @@ XZ_EXTERN enum xz_ret xz_dec_lzma2_reset(struct xz_dec_lzma2 *s, uint8_t props)
if (DEC_IS_DYNALLOC(s->dict.mode)) {
if (s->dict.allocated < s->dict.size) {
- vfree(s->dict.buf);
- s->dict.buf = vmalloc(s->dict.size);
+ FREE(s->dict.buf);
+ s->dict.buf = MALLOC(s->dict.size);
if (s->dict.buf == NULL) {
s->dict.allocated = 0;
return XZ_MEM_ERROR;
@@ -1169,7 +1169,7 @@ XZ_EXTERN enum xz_ret xz_dec_lzma2_reset(struct xz_dec_lzma2 *s, uint8_t props)
XZ_EXTERN void xz_dec_lzma2_end(struct xz_dec_lzma2 *s)
{
if (DEC_IS_MULTI(s->dict.mode))
- vfree(s->dict.buf);
+ FREE(s->dict.buf);
- kfree(s);
+ FREE(s);
}
diff --git a/lib/xz/xz_dec_stream.c b/lib/xz/xz_dec_stream.c
index ac809b1e64..fa1751dfca 100644
--- a/lib/xz/xz_dec_stream.c
+++ b/lib/xz/xz_dec_stream.c
@@ -769,7 +769,7 @@ XZ_EXTERN enum xz_ret xz_dec_run(struct xz_dec *s, struct xz_buf *b)
XZ_EXTERN struct xz_dec *xz_dec_init(enum xz_mode mode, uint32_t dict_max)
{
- struct xz_dec *s = kmalloc(sizeof(*s), GFP_KERNEL);
+ struct xz_dec *s = MALLOC(sizeof(*s));
if (s == NULL)
return NULL;
@@ -793,7 +793,7 @@ error_lzma2:
xz_dec_bcj_end(s->bcj);
error_bcj:
#endif
- kfree(s);
+ FREE(s);
return NULL;
}
@@ -816,6 +816,6 @@ XZ_EXTERN void xz_dec_end(struct xz_dec *s)
#ifdef XZ_DEC_BCJ
xz_dec_bcj_end(s->bcj);
#endif
- kfree(s);
+ FREE(s);
}
}
diff --git a/lib/xz/xz_private.h b/lib/xz/xz_private.h
index 99b8ade6cf..85f79635f0 100644
--- a/lib/xz/xz_private.h
+++ b/lib/xz/xz_private.h
@@ -39,6 +39,8 @@
# endif
# define memeq(a, b, size) (memcmp(a, b, size) == 0)
# define memzero(buf, size) memset(buf, 0, size)
+# define FREE free
+# define MALLOC malloc
# endif
# define get_le32(p) le32_to_cpup((const uint32_t *)(p))
#else
@@ -150,7 +152,7 @@ XZ_EXTERN enum xz_ret xz_dec_bcj_run(struct xz_dec_bcj *s,
struct xz_buf *b);
/* Free the memory allocated for the BCJ filters. */
-#define xz_dec_bcj_end(s) kfree(s)
+#define xz_dec_bcj_end(s) FREE(s)
#endif
#endif
diff --git a/lib/zlib_inflate/infutil.c b/lib/zlib_inflate/infutil.c
index f452ba6040..d9765523a9 100644
--- a/lib/zlib_inflate/infutil.c
+++ b/lib/zlib_inflate/infutil.c
@@ -12,10 +12,10 @@ int zlib_inflate_blob(void *gunzip_buf, unsigned int sz,
int rc;
rc = -ENOMEM;
- strm = kmalloc(sizeof(*strm), GFP_KERNEL);
+ strm = MALLOC(sizeof(*strm));
if (strm == NULL)
goto gunzip_nomem1;
- strm->workspace = kmalloc(zlib_inflate_workspacesize(), GFP_KERNEL);
+ strm->workspace = MALLOC(zlib_inflate_workspacesize());
if (strm->workspace == NULL)
goto gunzip_nomem2;
@@ -39,9 +39,9 @@ int zlib_inflate_blob(void *gunzip_buf, unsigned int sz,
} else
rc = -EINVAL;
- kfree(strm->workspace);
+ FREE(strm->workspace);
gunzip_nomem2:
- kfree(strm);
+ FREE(strm);
gunzip_nomem1:
return rc; /* returns Z_OK (0) if successful */
}