kmem.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright (c) 2000-2005 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include <linux/mm.h>
  19. #include <linux/highmem.h>
  20. #include <linux/slab.h>
  21. #include <linux/swap.h>
  22. #include <linux/blkdev.h>
  23. #include <linux/backing-dev.h>
  24. #include "time.h"
  25. #include "kmem.h"
  26. /*
  27. * Greedy allocation. May fail and may return vmalloced memory.
  28. *
  29. * Must be freed using kmem_free_large.
  30. */
  31. void *
  32. kmem_zalloc_greedy(size_t *size, size_t minsize, size_t maxsize)
  33. {
  34. void *ptr;
  35. size_t kmsize = maxsize;
  36. while (!(ptr = kmem_zalloc_large(kmsize))) {
  37. if ((kmsize >>= 1) <= minsize)
  38. kmsize = minsize;
  39. }
  40. if (ptr)
  41. *size = kmsize;
  42. return ptr;
  43. }
  44. void *
  45. kmem_alloc(size_t size, unsigned int __nocast flags)
  46. {
  47. int retries = 0;
  48. gfp_t lflags = kmem_flags_convert(flags);
  49. void *ptr;
  50. do {
  51. ptr = kmalloc(size, lflags);
  52. if (ptr || (flags & (KM_MAYFAIL|KM_NOSLEEP)))
  53. return ptr;
  54. if (!(++retries % 100))
  55. printk(KERN_ERR "XFS: possible memory allocation "
  56. "deadlock in %s (mode:0x%x)\n",
  57. __func__, lflags);
  58. congestion_wait(BLK_RW_ASYNC, HZ/50);
  59. } while (1);
  60. }
  61. void *
  62. kmem_zalloc(size_t size, unsigned int __nocast flags)
  63. {
  64. void *ptr;
  65. ptr = kmem_alloc(size, flags);
  66. if (ptr)
  67. memset((char *)ptr, 0, (int)size);
  68. return ptr;
  69. }
  70. void
  71. kmem_free(const void *ptr)
  72. {
  73. if (!is_vmalloc_addr(ptr)) {
  74. kfree(ptr);
  75. } else {
  76. vfree(ptr);
  77. }
  78. }
  79. void *
  80. kmem_realloc(const void *ptr, size_t newsize, size_t oldsize,
  81. unsigned int __nocast flags)
  82. {
  83. void *new;
  84. new = kmem_alloc(newsize, flags);
  85. if (ptr) {
  86. if (new)
  87. memcpy(new, ptr,
  88. ((oldsize < newsize) ? oldsize : newsize));
  89. kmem_free(ptr);
  90. }
  91. return new;
  92. }
  93. void *
  94. kmem_zone_alloc(kmem_zone_t *zone, unsigned int __nocast flags)
  95. {
  96. int retries = 0;
  97. gfp_t lflags = kmem_flags_convert(flags);
  98. void *ptr;
  99. do {
  100. ptr = kmem_cache_alloc(zone, lflags);
  101. if (ptr || (flags & (KM_MAYFAIL|KM_NOSLEEP)))
  102. return ptr;
  103. if (!(++retries % 100))
  104. printk(KERN_ERR "XFS: possible memory allocation "
  105. "deadlock in %s (mode:0x%x)\n",
  106. __func__, lflags);
  107. congestion_wait(BLK_RW_ASYNC, HZ/50);
  108. } while (1);
  109. }
  110. void *
  111. kmem_zone_zalloc(kmem_zone_t *zone, unsigned int __nocast flags)
  112. {
  113. void *ptr;
  114. ptr = kmem_zone_alloc(zone, flags);
  115. if (ptr)
  116. memset((char *)ptr, 0, kmem_cache_size(zone));
  117. return ptr;
  118. }