xref: /OK3568_Linux_fs/kernel/include/linux/decompress/mm.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * linux/compr_mm.h
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Memory management for pre-boot and ramdisk uncompressors
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Authors: Alain Knaff <alain@knaff.lu>
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  */
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun #ifndef DECOMPR_MM_H
12*4882a593Smuzhiyun #define DECOMPR_MM_H
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun #ifdef STATIC
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun /* Code active when included from pre-boot environment: */
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun /*
19*4882a593Smuzhiyun  * Some architectures want to ensure there is no local data in their
20*4882a593Smuzhiyun  * pre-boot environment, so that data can arbitrarily relocated (via
21*4882a593Smuzhiyun  * GOT references).  This is achieved by defining STATIC_RW_DATA to
22*4882a593Smuzhiyun  * be null.
23*4882a593Smuzhiyun  */
24*4882a593Smuzhiyun #ifndef STATIC_RW_DATA
25*4882a593Smuzhiyun #define STATIC_RW_DATA static
26*4882a593Smuzhiyun #endif
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun /* A trivial malloc implementation, adapted from
29*4882a593Smuzhiyun  *  malloc by Hannu Savolainen 1993 and Matthias Urlichs 1994
30*4882a593Smuzhiyun  */
31*4882a593Smuzhiyun STATIC_RW_DATA unsigned long malloc_ptr;
32*4882a593Smuzhiyun STATIC_RW_DATA int malloc_count;
33*4882a593Smuzhiyun 
malloc(int size)34*4882a593Smuzhiyun static void *malloc(int size)
35*4882a593Smuzhiyun {
36*4882a593Smuzhiyun 	void *p;
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun 	if (size < 0)
39*4882a593Smuzhiyun 		return NULL;
40*4882a593Smuzhiyun 	if (!malloc_ptr)
41*4882a593Smuzhiyun 		malloc_ptr = free_mem_ptr;
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun 	malloc_ptr = (malloc_ptr + 3) & ~3;     /* Align */
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun 	p = (void *)malloc_ptr;
46*4882a593Smuzhiyun 	malloc_ptr += size;
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun 	if (free_mem_end_ptr && malloc_ptr >= free_mem_end_ptr)
49*4882a593Smuzhiyun 		return NULL;
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun 	malloc_count++;
52*4882a593Smuzhiyun 	return p;
53*4882a593Smuzhiyun }
54*4882a593Smuzhiyun 
free(void * where)55*4882a593Smuzhiyun static void free(void *where)
56*4882a593Smuzhiyun {
57*4882a593Smuzhiyun 	malloc_count--;
58*4882a593Smuzhiyun 	if (!malloc_count)
59*4882a593Smuzhiyun 		malloc_ptr = free_mem_ptr;
60*4882a593Smuzhiyun }
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun #define large_malloc(a) malloc(a)
63*4882a593Smuzhiyun #define large_free(a) free(a)
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun #define INIT
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun #else /* STATIC */
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun /* Code active when compiled standalone for use when loading ramdisk: */
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun #include <linux/kernel.h>
72*4882a593Smuzhiyun #include <linux/fs.h>
73*4882a593Smuzhiyun #include <linux/string.h>
74*4882a593Smuzhiyun #include <linux/slab.h>
75*4882a593Smuzhiyun #include <linux/vmalloc.h>
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun /* Use defines rather than static inline in order to avoid spurious
78*4882a593Smuzhiyun  * warnings when not needed (indeed large_malloc / large_free are not
79*4882a593Smuzhiyun  * needed by inflate */
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun #define malloc(a) kmalloc(a, GFP_KERNEL)
82*4882a593Smuzhiyun #define free(a) kfree(a)
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun #define large_malloc(a) vmalloc(a)
85*4882a593Smuzhiyun #define large_free(a) vfree(a)
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun #define INIT __init
88*4882a593Smuzhiyun #define STATIC
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun #include <linux/init.h>
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun #endif /* STATIC */
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun #endif /* DECOMPR_MM_H */
95