mm.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * linux/compr_mm.h
  4. *
  5. * Memory management for pre-boot and ramdisk uncompressors
  6. *
  7. * Authors: Alain Knaff <[email protected]>
  8. *
  9. */
  10. #ifndef DECOMPR_MM_H
  11. #define DECOMPR_MM_H
  12. #ifdef STATIC
  13. /* Code active when included from pre-boot environment: */
  14. /*
  15. * Some architectures want to ensure there is no local data in their
  16. * pre-boot environment, so that data can arbitrarily relocated (via
  17. * GOT references). This is achieved by defining STATIC_RW_DATA to
  18. * be null.
  19. */
  20. #ifndef STATIC_RW_DATA
  21. #define STATIC_RW_DATA static
  22. #endif
  23. /*
  24. * When an architecture needs to share the malloc()/free() implementation
  25. * between compilation units, it needs to have non-local visibility.
  26. */
  27. #ifndef MALLOC_VISIBLE
  28. #define MALLOC_VISIBLE static
  29. #endif
  30. /* A trivial malloc implementation, adapted from
  31. * malloc by Hannu Savolainen 1993 and Matthias Urlichs 1994
  32. */
  33. STATIC_RW_DATA unsigned long malloc_ptr;
  34. STATIC_RW_DATA int malloc_count;
  35. MALLOC_VISIBLE void *malloc(int size)
  36. {
  37. void *p;
  38. if (size < 0)
  39. return NULL;
  40. if (!malloc_ptr)
  41. malloc_ptr = free_mem_ptr;
  42. malloc_ptr = (malloc_ptr + 3) & ~3; /* Align */
  43. p = (void *)malloc_ptr;
  44. malloc_ptr += size;
  45. if (free_mem_end_ptr && malloc_ptr >= free_mem_end_ptr)
  46. return NULL;
  47. malloc_count++;
  48. return p;
  49. }
  50. MALLOC_VISIBLE void free(void *where)
  51. {
  52. malloc_count--;
  53. if (!malloc_count)
  54. malloc_ptr = free_mem_ptr;
  55. }
  56. #define large_malloc(a) malloc(a)
  57. #define large_free(a) free(a)
  58. #define INIT
  59. #else /* STATIC */
  60. /* Code active when compiled standalone for use when loading ramdisk: */
  61. #include <linux/kernel.h>
  62. #include <linux/fs.h>
  63. #include <linux/string.h>
  64. #include <linux/slab.h>
  65. #include <linux/vmalloc.h>
  66. /* Use defines rather than static inline in order to avoid spurious
  67. * warnings when not needed (indeed large_malloc / large_free are not
  68. * needed by inflate */
  69. #define malloc(a) kmalloc(a, GFP_KERNEL)
  70. #define free(a) kfree(a)
  71. #define large_malloc(a) vmalloc(a)
  72. #define large_free(a) vfree(a)
  73. #define INIT __init
  74. #define STATIC
  75. #include <linux/init.h>
  76. #endif /* STATIC */
  77. #endif /* DECOMPR_MM_H */