kmem.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright (c) 2000-2004 Silicon Graphics, Inc. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of version 2 of the GNU General Public License as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it would be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. *
  12. * Further, this software is distributed without any warranty that it is
  13. * free of the rightful claim of any third person regarding infringement
  14. * or the like. Any license provided herein, whether implied or
  15. * otherwise, applies only to this software file. Patent licenses, if
  16. * any, provided herein do not apply to combinations of this program with
  17. * other software, or any other product whatsoever.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write the Free Software Foundation, Inc., 59
  21. * Temple Place - Suite 330, Boston MA 02111-1307, USA.
  22. *
  23. * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
  24. * Mountain View, CA 94043, or:
  25. *
  26. * http://www.sgi.com
  27. *
  28. * For further information regarding this notice, see:
  29. *
  30. * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
  31. */
  32. #include <linux/sched.h>
  33. #include <linux/mm.h>
  34. #include <linux/vmalloc.h>
  35. #include <linux/highmem.h>
  36. #include <linux/swap.h>
  37. #include <linux/blkdev.h>
  38. #include "time.h"
  39. #include "kmem.h"
  40. #define MAX_VMALLOCS 6
  41. #define MAX_SLAB_SIZE 0x20000
  42. void *
  43. kmem_alloc(size_t size, int flags)
  44. {
  45. int retries = 0;
  46. int lflags = kmem_flags_convert(flags);
  47. void *ptr;
  48. do {
  49. if (size < MAX_SLAB_SIZE || retries > MAX_VMALLOCS)
  50. ptr = kmalloc(size, lflags);
  51. else
  52. ptr = __vmalloc(size, lflags, PAGE_KERNEL);
  53. if (ptr || (flags & (KM_MAYFAIL|KM_NOSLEEP)))
  54. return ptr;
  55. if (!(++retries % 100))
  56. printk(KERN_ERR "XFS: possible memory allocation "
  57. "deadlock in %s (mode:0x%x)\n",
  58. __FUNCTION__, lflags);
  59. blk_congestion_wait(WRITE, HZ/50);
  60. } while (1);
  61. }
  62. void *
  63. kmem_zalloc(size_t size, int flags)
  64. {
  65. void *ptr;
  66. ptr = kmem_alloc(size, flags);
  67. if (ptr)
  68. memset((char *)ptr, 0, (int)size);
  69. return ptr;
  70. }
  71. void
  72. kmem_free(void *ptr, size_t size)
  73. {
  74. if (((unsigned long)ptr < VMALLOC_START) ||
  75. ((unsigned long)ptr >= VMALLOC_END)) {
  76. kfree(ptr);
  77. } else {
  78. vfree(ptr);
  79. }
  80. }
  81. void *
  82. kmem_realloc(void *ptr, size_t newsize, size_t oldsize, int flags)
  83. {
  84. void *new;
  85. new = kmem_alloc(newsize, flags);
  86. if (ptr) {
  87. if (new)
  88. memcpy(new, ptr,
  89. ((oldsize < newsize) ? oldsize : newsize));
  90. kmem_free(ptr, oldsize);
  91. }
  92. return new;
  93. }
  94. void *
  95. kmem_zone_alloc(kmem_zone_t *zone, int flags)
  96. {
  97. int retries = 0;
  98. int lflags = kmem_flags_convert(flags);
  99. void *ptr;
  100. do {
  101. ptr = kmem_cache_alloc(zone, lflags);
  102. if (ptr || (flags & (KM_MAYFAIL|KM_NOSLEEP)))
  103. return ptr;
  104. if (!(++retries % 100))
  105. printk(KERN_ERR "XFS: possible memory allocation "
  106. "deadlock in %s (mode:0x%x)\n",
  107. __FUNCTION__, lflags);
  108. blk_congestion_wait(WRITE, HZ/50);
  109. } while (1);
  110. }
  111. void *
  112. kmem_zone_zalloc(kmem_zone_t *zone, int flags)
  113. {
  114. void *ptr;
  115. ptr = kmem_zone_alloc(zone, flags);
  116. if (ptr)
  117. memset((char *)ptr, 0, kmem_cache_size(zone));
  118. return ptr;
  119. }