9
0
Fork 0

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>
This commit is contained in:
Sascha Hauer 2016-03-29 13:07:09 +02:00
parent 68bd0e5036
commit 3bafe5eac5
10 changed files with 55 additions and 41 deletions

View File

@ -30,7 +30,7 @@
STATIC_RW_DATA unsigned long malloc_ptr;
STATIC_RW_DATA int malloc_count;
static void *malloc(int size)
static __maybe_unused void *simple_malloc(int size)
{
void *p;
@ -51,15 +51,18 @@ static void *malloc(int size)
return p;
}
static void free(void *where)
static __maybe_unused void simple_free(void *where)
{
malloc_count--;
if (!malloc_count)
malloc_ptr = free_mem_ptr;
}
#define large_malloc(a) malloc(a)
#define large_free(a) free(a)
#define large_malloc(a) simple_malloc(a)
#define large_free(a) simple_free(a)
#define MALLOC simple_malloc
#define FREE simple_free
#define INIT

View File

@ -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 */
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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);

View File

@ -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;

View File

@ -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);
}

View File

@ -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);
}
}

View File

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

View File

@ -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 */
}