[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] Re: [Xen-devel] [PATCH 1/2] xen: move perform_gunzip to common
On Thu, 13 Aug 2015, Jan Beulich wrote: > >>> On 12.08.15 at 18:15, <stefano.stabellini@xxxxxxxxxxxxx> wrote: > > On Wed, 12 Aug 2015, Jan Beulich wrote: > >> >>> On 12.08.15 at 16:47, <stefano.stabellini@xxxxxxxxxxxxx> wrote: > >> > @@ -31,8 +33,15 @@ typedef int decompress_fn(unsigned char *inbuf, > >> > unsigned int len, > >> > * dependent). > >> > */ > >> > > >> > -decompress_fn bunzip2, unxz, unlzma, unlzo, unlz4; > >> > +decompress_fn perform_gunzip, bunzip2, unxz, unlzma, unlzo, unlz4; > >> > > >> > int decompress(void *inbuf, unsigned int len, void *outbuf); > >> > > >> > +static inline unsigned long output_length(char *image, unsigned long > >> > image_len) > >> > >> Neither of the callers gets moved out of bzimage.c - why does this > >> function need to move? > > > > We'll use it on arm. > > Hmm, the way it is used on x86 makes it quite architecture specific > (namely because of the assumption that the size is also in said > place for non-gz compression methods). I'd therefore prefer code > duplication over code sharing here. Actually after seeing the size and quality of the resulting patches, I am starting to feel the same way. In terms of code changes, I was thinking that the best result would be moving the "boilerplate" code from xen/arch/x86/bzimage.c to xen/common/inflate.c, see below, then the interface would become just perform_gunzip and gzip_check. But I guess you wouldn't want inflate.c to be modified, right? Alternatively we could move it to a new file, let's call it gunzip.h, that would #include "inflate.c", so: bzimage.c -- #include --> gunzip.h -- #include --> inflate.c And again we just leave the perform_gunzip and gzip_check calls in bzimage.c. What do you think? --- diff --git a/xen/arch/x86/bzimage.c b/xen/arch/x86/bzimage.c index c86c39e..cfd34d6 100644 --- a/xen/arch/x86/bzimage.c +++ b/xen/arch/x86/bzimage.c @@ -8,144 +8,8 @@ #include <xen/libelf.h> #include <asm/bzimage.h> -#define HEAPORDER 3 - -static unsigned char *__initdata window; -#define memptr long -static memptr __initdata free_mem_ptr; -static memptr __initdata free_mem_end_ptr; - -#define WSIZE 0x80000000 - -static unsigned char *__initdata inbuf; -static unsigned __initdata insize; - -/* Index of next byte to be processed in inbuf: */ -static unsigned __initdata inptr; - -/* Bytes in output buffer: */ -static unsigned __initdata outcnt; - -#define OF(args) args -#define STATIC static - -#define memzero(s, n) memset((s), 0, (n)) - -typedef unsigned char uch; -typedef unsigned short ush; -typedef unsigned long ulg; - -#define INIT __init -#define INITDATA __initdata - -#define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf()) - -/* Diagnostic functions */ -#ifdef DEBUG -# define Assert(cond, msg) do { if (!(cond)) error(msg); } while (0) -# define Trace(x) do { fprintf x; } while (0) -# define Tracev(x) do { if (verbose) fprintf x ; } while (0) -# define Tracevv(x) do { if (verbose > 1) fprintf x ; } while (0) -# define Tracec(c, x) do { if (verbose && (c)) fprintf x ; } while (0) -# define Tracecv(c, x) do { if (verbose > 1 && (c)) fprintf x ; } while (0) -#else -# define Assert(cond, msg) -# define Trace(x) -# define Tracev(x) -# define Tracevv(x) -# define Tracec(c, x) -# define Tracecv(c, x) -#endif - -static long __initdata bytes_out; -static void flush_window(void); - -static __init void error(char *x) -{ - panic("%s", x); -} - -static __init int fill_inbuf(void) -{ - error("ran out of input data"); - return 0; -} - - #include "../../common/inflate.c" -static __init void flush_window(void) -{ - /* - * The window is equal to the output buffer therefore only need to - * compute the crc. - */ - unsigned long c = crc; - unsigned n; - unsigned char *in, ch; - - in = window; - for ( n = 0; n < outcnt; n++ ) - { - ch = *in++; - c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8); - } - crc = c; - - bytes_out += (unsigned long)outcnt; - outcnt = 0; -} - -static __init unsigned long output_length(char *image, unsigned long image_len) -{ - return *(uint32_t *)&image[image_len - 4]; -} - -static __init int gzip_check(char *image, unsigned long image_len) -{ - unsigned char magic0, magic1; - - if ( image_len < 2 ) - return 0; - - magic0 = (unsigned char)image[0]; - magic1 = (unsigned char)image[1]; - - return (magic0 == 0x1f) && ((magic1 == 0x8b) || (magic1 == 0x9e)); -} - -static __init int perform_gunzip(char *output, char *image, unsigned long image_len) -{ - int rc; - - if ( !gzip_check(image, image_len) ) - return 1; - - window = (unsigned char *)output; - - free_mem_ptr = (unsigned long)alloc_xenheap_pages(HEAPORDER, 0); - free_mem_end_ptr = free_mem_ptr + (PAGE_SIZE << HEAPORDER); - - inbuf = (unsigned char *)image; - insize = image_len; - inptr = 0; - - makecrc(); - - if ( gunzip() < 0 ) - { - rc = -EINVAL; - } - else - { - rc = 0; - } - - free_xenheap_pages((void *)free_mem_ptr, HEAPORDER); - - return rc; -} - struct __packed setup_header { uint8_t _pad0[0x1f1]; /* skip uninteresting stuff */ uint8_t setup_sects; diff --git a/xen/common/inflate.c b/xen/common/inflate.c index f99c985..10c236a 100644 --- a/xen/common/inflate.c +++ b/xen/common/inflate.c @@ -103,6 +103,68 @@ the two sets of lengths. */ +#define HEAPORDER 3 + +static unsigned char *__initdata window; +#define memptr long +static memptr __initdata free_mem_ptr; +static memptr __initdata free_mem_end_ptr; + +#define WSIZE 0x80000000 + +static unsigned char *__initdata inbuf; +static unsigned __initdata insize; + +/* Index of next byte to be processed in inbuf: */ +static unsigned __initdata inptr; + +/* Bytes in output buffer: */ +static unsigned __initdata outcnt; + +#define OF(args) args +#define STATIC static + +#define memzero(s, n) memset((s), 0, (n)) + +typedef unsigned char uch; +typedef unsigned short ush; +typedef unsigned long ulg; + +#define INIT __init +#define INITDATA __initdata + +#define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf()) + +#ifdef DEBUG +# define Assert(cond, msg) do { if (!(cond)) error(msg); } while (0) +# define Trace(x) do { fprintf x; } while (0) +# define Tracev(x) do { if (verbose) fprintf x ; } while (0) +# define Tracevv(x) do { if (verbose > 1) fprintf x ; } while (0) +# define Tracec(c, x) do { if (verbose && (c)) fprintf x ; } while (0) +# define Tracecv(c, x) do { if (verbose > 1 && (c)) fprintf x ; } while (0) +#else +# define Assert(cond, msg) +# define Trace(x) +# define Tracev(x) +# define Tracevv(x) +# define Tracec(c, x) +# define Tracecv(c, x) +#endif + +static long __initdata bytes_out; +static void flush_window(void); + +static __init void error(char *x) +{ + panic("%s", x); +} + +static __init int fill_inbuf(void) +{ + error("ran out of input data"); + return 0; +} + #ifdef RCSID static char rcsid[] = "#Id: inflate.c,v 0.14 1993/06/10 13:27:04 jloup Exp #"; #endif @@ -1302,3 +1364,54 @@ static int INIT gunzip(void) error("out of input data"); return -1; } + +static __init void flush_window(void) +{ + /* + * The window is equal to the output buffer therefore only need to + * compute the crc. + */ + unsigned long c = crc; + unsigned n; + unsigned char *in, ch; + + in = window; + for ( n = 0; n < outcnt; n++ ) + { + ch = *in++; + c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8); + } + crc = c; + + bytes_out += (unsigned long)outcnt; + outcnt = 0; +} + +static __init int perform_gunzip(char *output, char *image, unsigned long image_len) +{ + int rc; + + window = (unsigned char *)output; + + free_mem_ptr = (unsigned long)alloc_xenheap_pages(HEAPORDER, 0); + free_mem_end_ptr = free_mem_ptr + (PAGE_SIZE << HEAPORDER); + + inbuf = (unsigned char *)image; + insize = image_len; + inptr = 0; + + makecrc(); + + if ( gunzip() < 0 ) + { + rc = -EINVAL; + } + else + { + rc = 0; + } + + free_xenheap_pages((void *)free_mem_ptr, HEAPORDER); + + return rc; +} _______________________________________________ Xen-devel mailing list Xen-devel@xxxxxxxxxxxxx http://lists.xen.org/xen-devel
|
Lists.xenproject.org is hosted with RackSpace, monitoring our |