inflate: refactor inflate malloc code
Subsystems (6)
PATCH 1/1 inflate: refactor inflate malloc code
Inflate requires some dynamic memory allocation very early in the boot process and this is provided with a set of four functions:
malloc/free/gzip_mark/gzip_release.
The old inflate code used a mark/release strategy rather than implement free. This new version instead keeps a count on the number of outstanding allocations and when it hits zero, it resets the malloc arena.
This allows removing all the mark and release implementations and unifying all the malloc/free implementations.
It applies on top of the previously sent patch entitled "Use directly kmalloc() and kfree() in init/initramfs.c".
Work initially done by Matt Mackall <[email protected]>, updated to a recent version of the kernel by me.
--- arch/alpha/boot/misc.c | 35 ------------------- arch/arm/boot/compressed/misc.c | 53 ++---------------------------- arch/cris/arch-v10/boot/compressed/misc.c | 34 ------------------- arch/cris/arch-v32/boot/compressed/misc.c | 34 ------------------- arch/h8300/boot/compressed/misc.c | 38 --------------------- arch/m32r/boot/compressed/misc.c | 37 -------------------- arch/mn10300/boot/compressed/misc.c | 37 -------------------- arch/sh/boot/compressed/misc_32.c | 38 --------------------- arch/sh/boot/compressed/misc_64.c | 40 ---------------------- arch/x86/boot/compressed/misc.c | 37 -------------------- init/do_mounts_rd.c | 25 +------------- init/initramfs.c | 22 +----------- lib/inflate.c | 49 +++++++++++++++++++++++---- 13 files changed, 50 insertions(+), 429 deletions(-)
arch/alpha/boot/misc.c +0 -35
@@ -78,8 +78,6 @@static int fill_inbuf (void );static void flush_window (void );static void error (char *m); -static void gzip_mark (void **); -static void gzip_release (void **);static char *input_data;static int input_data_size;
@@ -88,8 +86,6 @@static ulg output_ptr;static ulg bytes_out; -static void *malloc (int size); -static void free (void *where);static void error (char *m);static void gzip_mark (void **);static void gzip_release (void **);
@@ -102,37 +98,6 @@#include "../../../lib/inflate.c" -static void *malloc (int size) -{ -void *p; - -if (size <0 )error ("Malloc error" ); -if (free_mem_ptr <=0 )error ("Memory error" ); - - free_mem_ptr = (free_mem_ptr +3 ) & ~3 ;/* Align */ - - p = (void *)free_mem_ptr; - free_mem_ptr += size; - -if (free_mem_ptr >= free_mem_ptr_end) -error ("Out of memory" ); -return p; -} - -static void free (void *where) -{/* gzip_mark & gzip_release do the free */ -} - -static void gzip_mark (void **ptr) -{ - *ptr = (void *) free_mem_ptr; -} - -static void gzip_release (void **ptr) -{ - free_mem_ptr = (long ) *ptr; -} -/* =========================================================================== * Fill the input buffer. This is called only when the buffer is empty * and at least one byte is really needed.
arch/arm/boot/compressed/misc.c +4 -49
74 lines not on this page · raw message
arch/cris/arch-v10/boot/compressed/misc.c +0 -34
@@ -84,8 +84,6 @@static int fill_inbuf (void );static void flush_window (void );static void error (char *m); -static void gzip_mark (void **); -static void gzip_release (void **);extern char *input_data;/* lives in head.S */
@@ -93,11 +91,7 @@static uch *output_data;static unsigned long output_ptr =0 ; -static void *malloc (int size); -static void free (void *where);static void error (char *m); -static void gzip_mark (void **); -static void gzip_release (void **);static void puts (const char *);
@@ -108,34 +102,6 @@#include "../../../../../lib/inflate.c" -static void *malloc (int size) -{ -void *p; - -if (size <0 )error ("Malloc error" ); - - free_mem_ptr = (free_mem_ptr +3 ) & ~3 ;/* Align */ - - p = (void *)free_mem_ptr; - free_mem_ptr += size; - -return p; -} - -static void free (void *where) -{/* Don't care */ -} - -static void gzip_mark (void **ptr) -{ - *ptr = (void *) free_mem_ptr; -} - -static void gzip_release (void **ptr) -{ - free_mem_ptr = (long ) *ptr; -} -/* decompressor info and error messages to serial console */ static void
arch/cris/arch-v32/boot/compressed/misc.c +0 -34
@@ -89,8 +89,6 @@static void flush_window (void );static void error (char *m); -static void gzip_mark (void **); -static void gzip_release (void **);extern char *input_data;/* lives in head.S */
@@ -98,11 +96,7 @@static uch *output_data;static unsigned long output_ptr =0 ; -static void *malloc (int size); -static void free (void *where);static void error (char *m); -static void gzip_mark (void **); -static void gzip_release (void **);static void puts (const char *);
@@ -113,34 +107,6 @@#include "../../../../../lib/inflate.c" -static void *malloc (int size) -{ -void *p; - -if (size <0 )error ("Malloc error" ); - - free_mem_ptr = (free_mem_ptr +3 ) & ~3 ;/* Align */ - - p = (void *)free_mem_ptr; - free_mem_ptr += size; - -return p; -} - -static void free (void *where) -{/* Don't care */ -} - -static void gzip_mark (void **ptr) -{ - *ptr = (void *) free_mem_ptr; -} - -static void gzip_release (void **ptr) -{ - free_mem_ptr = (long ) *ptr; -} -/* decompressor info and error messages to serial console */ static inline void
arch/h8300/boot/compressed/misc.c +0 -38
@@ -67,8 +67,6 @@static int fill_inbuf (void );static void flush_window (void );static void error (char *m); -static void gzip_mark (void **); -static void gzip_release (void **);extern char input_data[];extern int input_len;
@@ -77,11 +75,7 @@static uch *output_data;static unsigned long output_ptr =0 ; -static void *malloc (int size); -static void free (void *where);static void error (char *m); -static void gzip_mark (void **); -static void gzip_release (void **);int puts (const char *);
@@ -98,38 +92,6 @@#define TDR *((volatile unsigned char *)0xffff8b)#define SSR *((volatile unsigned char *)0xffff8c) -static void *malloc (int size) -{ -void *p; - -if (size <0 )error ("Malloc error" ); -if (free_mem_ptr ==0 )error ("Memory error" ); - - free_mem_ptr = (free_mem_ptr +3 ) & ~3 ;/* Align */ - - p = (void *)free_mem_ptr; - free_mem_ptr += size; - -if (free_mem_ptr >= free_mem_end_ptr) -error ("Out of memory" ); - -return p; -} - -static void free (void *where) -{/* Don't care */ -} - -static void gzip_mark (void **ptr) -{ - *ptr = (void *) free_mem_ptr; -} - -static void gzip_release (void **ptr) -{ - free_mem_ptr = (long ) *ptr; -} -int puts (const char *s) {return 0 ;
arch/m32r/boot/compressed/misc.c +0 -37
@@ -70,8 +70,6 @@static int fill_inbuf (void );static void flush_window (void );static void error (char *m); -static void gzip_mark (void **); -static void gzip_release (void **);static unsigned char *input_data;static int input_len;
@@ -82,9 +80,6 @@#include "m32r_sio.c" -static void *malloc (int size); -static void free (void *where); -static unsigned long free_mem_ptr;static unsigned long free_mem_end_ptr;
@@ -92,38 +87,6 @@#include "../../../../lib/inflate.c" -static void *malloc (int size) -{ -void *p; - -if (size <0 )error ("Malloc error" ); -if (free_mem_ptr ==0 )error ("Memory error" ); - - free_mem_ptr = (free_mem_ptr +3 ) & ~3 ;/* Align */ - - p = (void *)free_mem_ptr; - free_mem_ptr += size; - -if (free_mem_ptr >= free_mem_end_ptr) -error ("Out of memory" ); - -return p; -} - -static void free (void *where) -{/* Don't care */ -} - -static void gzip_mark (void **ptr) -{ - *ptr = (void *) free_mem_ptr; -} - -static void gzip_release (void **ptr) -{ - free_mem_ptr = (long ) *ptr; -} -void *memset (void * s,int c,size_t n) {int i;
arch/mn10300/boot/compressed/misc.c +0 -37
@@ -153,26 +153,9 @@static unsigned long output_ptr; -static void *malloc (int size); - -static inline void free (void *where) -{/* Don't care */ -} -static unsigned long free_mem_ptr = (unsigned long ) &end;static unsigned long free_mem_end_ptr = (unsigned long ) &end +0x90000 ; -static inline void gzip_mark (void **ptr) -{ -kputs ("." ); - *ptr = (void *) free_mem_ptr; -} - -static inline void gzip_release (void **ptr) -{ - free_mem_ptr = (unsigned long ) *ptr; -} -#define INPLACE_MOVE_ROUTINE 0x1000#define LOW_BUFFER_START 0x2000#define LOW_BUFFER_END 0x90000
@@ -186,26 +169,6 @@#include "../../../../lib/inflate.c" -static void *malloc (int size) -{ -void *p; - -if (size <0 ) -error ("Malloc error\n" ); -if (!free_mem_ptr) -error ("Memory error\n" ); - - free_mem_ptr = (free_mem_ptr +3 ) & ~3 ;/* Align */ - - p = (void *) free_mem_ptr; - free_mem_ptr += size; - -if (free_mem_ptr >= free_mem_end_ptr) -error ("\nOut of memory\n" ); - -return p; -} -static inline void scroll (void ) {int i;
arch/sh/boot/compressed/misc_32.c +0 -38
@@ -74,8 +74,6 @@static int fill_inbuf (void );static void flush_window (void );static void error (char *m); -static void gzip_mark (void **); -static void gzip_release (void **);extern char input_data[];extern int input_len;
@@ -84,11 +82,7 @@static uch *output_data;static unsigned long output_ptr =0 ; -static void *malloc (int size); -static void free (void *where);static void error (char *m); -static void gzip_mark (void **); -static void gzip_release (void **);int puts (const char *);
@@ -101,38 +95,6 @@#include "../../../../lib/inflate.c" -static void *malloc (int size) -{ -void *p; - -if (size <0 )error ("Malloc error" ); -if (free_mem_ptr ==0 )error ("Memory error" ); - - free_mem_ptr = (free_mem_ptr +3 ) & ~3 ;/* Align */ - - p = (void *)free_mem_ptr; - free_mem_ptr += size; - -if (free_mem_ptr >= free_mem_end_ptr) -error ("Out of memory" ); - -return p; -} - -static void free (void *where) -{/* Don't care */ -} - -static void gzip_mark (void **ptr) -{ - *ptr = (void *) free_mem_ptr; -} - -static void gzip_release (void **ptr) -{ - free_mem_ptr = (long ) *ptr; -} -#ifdef CONFIG_SH_STANDARD_BIOS size_t strlen (const char *s) {
arch/sh/boot/compressed/misc_64.c +0 -40
@@ -72,8 +72,6 @@static int fill_inbuf (void );static void flush_window (void );static void error (char *m); -static void gzip_mark (void **); -static void gzip_release (void **);extern char input_data[];extern int input_len;
@@ -82,11 +80,7 @@static uch *output_data;static unsigned long output_ptr =0 ; -static void *malloc (int size); -static void free (void *where);static void error (char *m); -static void gzip_mark (void **); -static void gzip_release (void **);static void puts (const char *);
@@ -99,40 +93,6 @@#include "../../../../lib/inflate.c" -static void *malloc (int size) -{ -void *p; - -if (size <0 ) -error ("Malloc error\n" ); -if (free_mem_ptr ==0 ) -error ("Memory error\n" ); - - free_mem_ptr = (free_mem_ptr +3 ) & ~3 ;/* Align */ - - p = (void *) free_mem_ptr; - free_mem_ptr += size; - -if (free_mem_ptr >= free_mem_end_ptr) -error ("\nOut of memory\n" ); - -return p; -} - -static void free (void *where) -{/* Don't care */ -} - -static void gzip_mark (void **ptr) -{ - *ptr = (void *) free_mem_ptr; -} - -static void gzip_release (void **ptr) -{ - free_mem_ptr = (long ) *ptr; -} -void puts (const char *s) { }
arch/x86/boot/compressed/misc.c +0 -37
@@ -168,8 +168,6 @@static int fill_inbuf (void );static void flush_window (void );static void error (char *m); -static void gzip_mark (void **); -static void gzip_release (void **);/* * This is set up by the setup-routine at boot-time
@@ -187,9 +185,6 @@static long bytes_out =0 ; -static void *malloc (int size); -static void free (void *where); -static void *memset (void *s,int c,unsigned n);static void *memcpy (void *dest,const void *src,unsigned n);
@@ -220,38 +215,6 @@#include "../../../../lib/inflate.c" -static void *malloc (int size) -{ -void *p; - -if (size <0 )error ("Malloc error" ); -if (free_mem_ptr <=0 )error ("Memory error" ); - - free_mem_ptr = (free_mem_ptr +3 ) & ~3 ;/* Align */ - - p = (void *)free_mem_ptr; - free_mem_ptr += size; - -if (free_mem_ptr >= free_mem_end_ptr) -error ("Out of memory" ); - -return p; -} - -static void free (void *where) -{/* Don't care */ -} - -static void gzip_mark (void **ptr) -{ - *ptr = (void *) free_mem_ptr; -} - -static void gzip_release (void **ptr) -{ - free_mem_ptr = (memptr) *ptr; -} -static void scroll (void ) {int i;
init/do_mounts_rd.c +2 -23
@@ -313,32 +313,11 @@static int __init fill_inbuf (void );static void __init flush_window (void ); -static void __init *malloc (size_t size); -static void __init free (void *where);static void __init error (char *m); -static void __init gzip_mark (void **); -static void __init gzip_release (void **); -#include "../lib/inflate.c" - -static void __init *malloc (size_t size) -{ -return kmalloc (size,GFP_KERNEL ); -} - -static void __init free (void *where) -{ -kfree (where); -} - -static void __init gzip_mark (void **ptr) -{ -} - -static void __init gzip_release (void **ptr) -{ -} +#define NO_INFLATE_MALLOC +#include "../lib/inflate.c" /* =========================================================================== * Fill the input buffer. This is called only when the buffer is empty
init/initramfs.c +2 -20
@@ -14,16 +14,6 @@ message = x; } -static void __init *malloc (size_t size) -{ -return kmalloc (size,GFP_KERNEL ); -} - -static void __init free (void *where) -{ -kfree (where); -} -/* link hash */ #define N_ALIGN (len) ((((len) + 1) & ~3) + 2)
@@ -407,18 +397,10 @@static void __init flush_window (void );static void __init error (char *m); -static void __init gzip_mark (void **); -static void __init gzip_release (void **); -#include "../lib/inflate.c" +#define NO_INFLATE_MALLOC -static void __init gzip_mark (void **ptr) -{ -} - -static void __init gzip_release (void **ptr) -{ -} +#include "../lib/inflate.c" /* =========================================================================== * Write the output window window[0..outcnt-1] and update crc and bytes_out.
lib/inflate.c +42 -7
67 lines not on this page · raw message
Signature · 4 lines
Thomas Petazzoni, Free Electrons Free Embedded Linux Training Materials on http://free-electrons.com/training (More than 1500 pages!)