kmem.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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/sched.h>
  19. #include <linux/mm.h>
  20. #include <linux/vmalloc.h>
  21. #include <linux/highmem.h>
  22. #include <linux/swap.h>
  23. #include <linux/blkdev.h>
  24. #include "time.h"
  25. #include "kmem.h"
  26. #define MAX_VMALLOCS 6
  27. #define MAX_SLAB_SIZE 0x20000
  28. void *
  29. kmem_alloc(size_t size, unsigned int __nocast flags)
  30. {
  31. int retries = 0;
  32. gfp_t lflags = kmem_flags_convert(flags);
  33. void *ptr;
  34. do {
  35. if (size < MAX_SLAB_SIZE || retries > MAX_VMALLOCS)
  36. ptr = kmalloc(size, lflags);
  37. else
  38. ptr = __vmalloc(size, lflags, PAGE_KERNEL);
  39. if (ptr || (flags & (KM_MAYFAIL|KM_NOSLEEP)))
  40. return ptr;
  41. if (!(++retries % 100))
  42. printk(KERN_ERR "XFS: possible memory allocation "
  43. "deadlock in %s (mode:0x%x)\n",
  44. __FUNCTION__, lflags);
  45. blk_congestion_wait(WRITE, HZ/50);
  46. } while (1);
  47. }
  48. void *
  49. kmem_zalloc(size_t size, unsigned int __nocast flags)
  50. {
  51. void *ptr;
  52. ptr = kmem_alloc(size, flags);
  53. if (ptr)
  54. memset((char *)ptr, 0, (int)size);
  55. return ptr;
  56. }
  57. void
  58. kmem_free(void *ptr, size_t size)
  59. {
  60. if (((unsigned long)ptr < VMALLOC_START) ||
  61. ((unsigned long)ptr >= VMALLOC_END)) {
  62. kfree(ptr);
  63. } else {
  64. vfree(ptr);
  65. }
  66. }
  67. void *
  68. kmem_realloc(void *ptr, size_t newsize, size_t oldsize,
  69. unsigned int __nocast flags)
  70. {
  71. void *new;
  72. new = kmem_alloc(newsize, flags);
  73. if (ptr) {
  74. if (new)
  75. memcpy(new, ptr,
  76. ((oldsize < newsize) ? oldsize : newsize));
  77. kmem_free(ptr, oldsize);
  78. }
  79. return new;
  80. }
  81. void *
  82. kmem_zone_alloc(kmem_zone_t *zone, unsigned int __nocast flags)
  83. {
  84. int retries = 0;
  85. gfp_t lflags = kmem_flags_convert(flags);
  86. void *ptr;
  87. do {
  88. ptr = kmem_cache_alloc(zone, lflags);
  89. if (ptr || (flags & (KM_MAYFAIL|KM_NOSLEEP)))
  90. return ptr;
  91. if (!(++retries % 100))
  92. printk(KERN_ERR "XFS: possible memory allocation "
  93. "deadlock in %s (mode:0x%x)\n",
  94. __FUNCTION__, lflags);
  95. blk_congestion_wait(WRITE, HZ/50);
  96. } while (1);
  97. }
  98. void *
  99. kmem_zone_zalloc(kmem_zone_t *zone, unsigned int __nocast flags)
  100. {
  101. void *ptr;
  102. ptr = kmem_zone_alloc(zone, flags);
  103. if (ptr)
  104. memset((char *)ptr, 0, kmem_cache_size(zone));
  105. return ptr;
  106. }