mm.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. #ifndef DECOMPR_MM_H
  10. #define DECOMPR_MM_H
  11. #ifdef STATIC
  12. /* Code active when included from pre-boot environment: */
  13. /*
  14. * Some architectures want to ensure there is no local data in their
  15. * pre-boot environment, so that data can arbitrarily relocated (via
  16. * GOT references). This is achieved by defining STATIC_RW_DATA to
  17. * be null.
  18. */
  19. #ifndef STATIC_RW_DATA
  20. #define STATIC_RW_DATA static
  21. #endif
  22. /* A trivial malloc implementation, adapted from
  23. * malloc by Hannu Savolainen 1993 and Matthias Urlichs 1994
  24. */
  25. STATIC_RW_DATA unsigned long malloc_ptr;
  26. STATIC_RW_DATA int malloc_count;
  27. static void *malloc(int size)
  28. {
  29. void *p;
  30. if (size < 0)
  31. return NULL;
  32. if (!malloc_ptr)
  33. malloc_ptr = free_mem_ptr;
  34. malloc_ptr = (malloc_ptr + 3) & ~3; /* Align */
  35. p = (void *)malloc_ptr;
  36. malloc_ptr += size;
  37. if (free_mem_end_ptr && malloc_ptr >= free_mem_end_ptr)
  38. return NULL;
  39. malloc_count++;
  40. return p;
  41. }
  42. static void free(void *where)
  43. {
  44. malloc_count--;
  45. if (!malloc_count)
  46. malloc_ptr = free_mem_ptr;
  47. }
  48. #define large_malloc(a) malloc(a)
  49. #define large_free(a) free(a)
  50. #define INIT
  51. #else /* STATIC */
  52. /* Code active when compiled standalone for use when loading ramdisk: */
  53. #include <linux/kernel.h>
  54. #include <linux/fs.h>
  55. #include <linux/string.h>
  56. #include <linux/slab.h>
  57. #include <linux/vmalloc.h>
  58. /* Use defines rather than static inline in order to avoid spurious
  59. * warnings when not needed (indeed large_malloc / large_free are not
  60. * needed by inflate */
  61. #define malloc(a) kmalloc(a, GFP_KERNEL)
  62. #define free(a) kfree(a)
  63. #define large_malloc(a) vmalloc(a)
  64. #define large_free(a) vfree(a)
  65. #define INIT __init
  66. #define STATIC
  67. #include <linux/init.h>
  68. #endif /* STATIC */
  69. #endif /* DECOMPR_MM_H */