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