blob: 868e9eacd69e5e2f81804766b4d61d9b7eb21f87 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Alain Knaffbc22c172009-01-04 22:46:16 +01002/*
3 * linux/compr_mm.h
4 *
5 * Memory management for pre-boot and ramdisk uncompressors
6 *
7 * Authors: Alain Knaff <alain@knaff.lu>
8 *
9 */
10
11#ifndef DECOMPR_MM_H
12#define DECOMPR_MM_H
13
14#ifdef STATIC
15
16/* Code active when included from pre-boot environment: */
17
Russell King5ceaa2f2010-03-10 15:23:53 -080018/*
19 * Some architectures want to ensure there is no local data in their
Lucas De Marchi25985ed2011-03-30 22:57:33 -030020 * pre-boot environment, so that data can arbitrarily relocated (via
Russell King5ceaa2f2010-03-10 15:23:53 -080021 * GOT references). This is achieved by defining STATIC_RW_DATA to
22 * be null.
23 */
24#ifndef STATIC_RW_DATA
25#define STATIC_RW_DATA static
26#endif
27
Alain Knaffbc22c172009-01-04 22:46:16 +010028/* A trivial malloc implementation, adapted from
29 * malloc by Hannu Savolainen 1993 and Matthias Urlichs 1994
30 */
Russell King5ceaa2f2010-03-10 15:23:53 -080031STATIC_RW_DATA unsigned long malloc_ptr;
32STATIC_RW_DATA int malloc_count;
Alain Knaffbc22c172009-01-04 22:46:16 +010033
34static void *malloc(int size)
35{
36 void *p;
37
38 if (size < 0)
Phillip Lougherc1e7c3ae2009-12-14 21:45:19 +000039 return NULL;
Alain Knaffbc22c172009-01-04 22:46:16 +010040 if (!malloc_ptr)
41 malloc_ptr = free_mem_ptr;
42
43 malloc_ptr = (malloc_ptr + 3) & ~3; /* Align */
44
45 p = (void *)malloc_ptr;
46 malloc_ptr += size;
47
48 if (free_mem_end_ptr && malloc_ptr >= free_mem_end_ptr)
Phillip Lougherc1e7c3ae2009-12-14 21:45:19 +000049 return NULL;
Alain Knaffbc22c172009-01-04 22:46:16 +010050
51 malloc_count++;
52 return p;
53}
54
55static void free(void *where)
56{
57 malloc_count--;
58 if (!malloc_count)
59 malloc_ptr = free_mem_ptr;
60}
61
62#define large_malloc(a) malloc(a)
63#define large_free(a) free(a)
64
Alain Knaffbc22c172009-01-04 22:46:16 +010065#define INIT
66
67#else /* STATIC */
68
69/* Code active when compiled standalone for use when loading ramdisk: */
70
71#include <linux/kernel.h>
72#include <linux/fs.h>
73#include <linux/string.h>
Lasse Collin2b6b5ca2011-01-12 17:01:15 -080074#include <linux/slab.h>
Alain Knaffbc22c172009-01-04 22:46:16 +010075#include <linux/vmalloc.h>
76
77/* Use defines rather than static inline in order to avoid spurious
78 * warnings when not needed (indeed large_malloc / large_free are not
79 * needed by inflate */
80
81#define malloc(a) kmalloc(a, GFP_KERNEL)
82#define free(a) kfree(a)
83
84#define large_malloc(a) vmalloc(a)
85#define large_free(a) vfree(a)
86
Alain Knaffbc22c172009-01-04 22:46:16 +010087#define INIT __init
88#define STATIC
89
90#include <linux/init.h>
91
92#endif /* STATIC */
93
94#endif /* DECOMPR_MM_H */