Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * linux/compr_mm.h
0004  *
0005  * Memory management for pre-boot and ramdisk uncompressors
0006  *
0007  * Authors: Alain Knaff <alain@knaff.lu>
0008  *
0009  */
0010 
0011 #ifndef DECOMPR_MM_H
0012 #define DECOMPR_MM_H
0013 
0014 #ifdef STATIC
0015 
0016 /* Code active when included from pre-boot environment: */
0017 
0018 /*
0019  * Some architectures want to ensure there is no local data in their
0020  * pre-boot environment, so that data can arbitrarily relocated (via
0021  * GOT references).  This is achieved by defining STATIC_RW_DATA to
0022  * be null.
0023  */
0024 #ifndef STATIC_RW_DATA
0025 #define STATIC_RW_DATA static
0026 #endif
0027 
0028 /*
0029  * When an architecture needs to share the malloc()/free() implementation
0030  * between compilation units, it needs to have non-local visibility.
0031  */
0032 #ifndef MALLOC_VISIBLE
0033 #define MALLOC_VISIBLE static
0034 #endif
0035 
0036 /* A trivial malloc implementation, adapted from
0037  *  malloc by Hannu Savolainen 1993 and Matthias Urlichs 1994
0038  */
0039 STATIC_RW_DATA unsigned long malloc_ptr;
0040 STATIC_RW_DATA int malloc_count;
0041 
0042 MALLOC_VISIBLE void *malloc(int size)
0043 {
0044     void *p;
0045 
0046     if (size < 0)
0047         return NULL;
0048     if (!malloc_ptr)
0049         malloc_ptr = free_mem_ptr;
0050 
0051     malloc_ptr = (malloc_ptr + 3) & ~3;     /* Align */
0052 
0053     p = (void *)malloc_ptr;
0054     malloc_ptr += size;
0055 
0056     if (free_mem_end_ptr && malloc_ptr >= free_mem_end_ptr)
0057         return NULL;
0058 
0059     malloc_count++;
0060     return p;
0061 }
0062 
0063 MALLOC_VISIBLE void free(void *where)
0064 {
0065     malloc_count--;
0066     if (!malloc_count)
0067         malloc_ptr = free_mem_ptr;
0068 }
0069 
0070 #define large_malloc(a) malloc(a)
0071 #define large_free(a) free(a)
0072 
0073 #define INIT
0074 
0075 #else /* STATIC */
0076 
0077 /* Code active when compiled standalone for use when loading ramdisk: */
0078 
0079 #include <linux/kernel.h>
0080 #include <linux/fs.h>
0081 #include <linux/string.h>
0082 #include <linux/slab.h>
0083 #include <linux/vmalloc.h>
0084 
0085 /* Use defines rather than static inline in order to avoid spurious
0086  * warnings when not needed (indeed large_malloc / large_free are not
0087  * needed by inflate */
0088 
0089 #define malloc(a) kmalloc(a, GFP_KERNEL)
0090 #define free(a) kfree(a)
0091 
0092 #define large_malloc(a) vmalloc(a)
0093 #define large_free(a) vfree(a)
0094 
0095 #define INIT __init
0096 #define STATIC
0097 
0098 #include <linux/init.h>
0099 
0100 #endif /* STATIC */
0101 
0102 #endif /* DECOMPR_MM_H */