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