lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [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 | /* Code active when included from pre-boot environment: */ |
| 14 | |
| 15 | /* |
| 16 | * Some architectures want to ensure there is no local data in their |
| 17 | * pre-boot environment, so that data can arbitrarily relocated (via |
| 18 | * GOT references). This is achieved by defining STATIC_RW_DATA to |
| 19 | * be null. |
| 20 | */ |
| 21 | #ifndef STATIC_RW_DATA |
| 22 | #define STATIC_RW_DATA static |
| 23 | #endif |
| 24 | |
| 25 | /* A trivial malloc implementation, adapted from |
| 26 | * malloc by Hannu Savolainen 1993 and Matthias Urlichs 1994 |
| 27 | */ |
| 28 | STATIC_RW_DATA unsigned long malloc_ptr; |
| 29 | STATIC_RW_DATA int malloc_count; |
| 30 | extern unsigned long free_mem_ptr; |
| 31 | extern unsigned long free_mem_end_ptr; |
| 32 | |
| 33 | static void *malloc(int size) |
| 34 | { |
| 35 | void *p; |
| 36 | |
| 37 | if (size < 0) |
| 38 | return NULL; |
| 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) |
| 48 | return NULL; |
| 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 | |
| 64 | #define INIT |
| 65 | |
| 66 | #endif /* DECOMPR_MM_H */ |