zftape-buffers.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright (C) 1995-1997 Claus-Justus Heine.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; see the file COPYING. If not, write to
  13. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  14. *
  15. * $Source: /homes/cvs/ftape-stacked/ftape/zftape/zftape-buffers.c,v $
  16. * $Revision: 1.2 $
  17. * $Date: 1997/10/05 19:18:59 $
  18. *
  19. * This file contains the dynamic buffer allocation routines
  20. * of zftape
  21. */
  22. #include <linux/errno.h>
  23. #include <linux/mm.h>
  24. #include <linux/slab.h>
  25. #include <linux/delay.h>
  26. #include <linux/zftape.h>
  27. #include <linux/vmalloc.h>
  28. #include "../zftape/zftape-init.h"
  29. #include "../zftape/zftape-eof.h"
  30. #include "../zftape/zftape-ctl.h"
  31. #include "../zftape/zftape-write.h"
  32. #include "../zftape/zftape-read.h"
  33. #include "../zftape/zftape-rw.h"
  34. #include "../zftape/zftape-vtbl.h"
  35. /* global variables
  36. */
  37. /* local varibales
  38. */
  39. static unsigned int used_memory;
  40. static unsigned int peak_memory;
  41. void zft_memory_stats(void)
  42. {
  43. TRACE_FUN(ft_t_flow);
  44. TRACE(ft_t_noise, "Memory usage (vmalloc allocations):\n"
  45. KERN_INFO "total allocated: %d\n"
  46. KERN_INFO "peak allocation: %d",
  47. used_memory, peak_memory);
  48. peak_memory = used_memory;
  49. TRACE_EXIT;
  50. }
  51. int zft_vcalloc_once(void *new, size_t size)
  52. {
  53. TRACE_FUN(ft_t_flow);
  54. if (zft_vmalloc_once(new, size) < 0) {
  55. TRACE_EXIT -ENOMEM;
  56. }
  57. memset(*(void **)new, '\0', size);
  58. TRACE_EXIT 0;
  59. }
  60. int zft_vmalloc_once(void *new, size_t size)
  61. {
  62. TRACE_FUN(ft_t_flow);
  63. if (*(void **)new != NULL || size == 0) {
  64. TRACE_EXIT 0;
  65. }
  66. if ((*(void **)new = vmalloc(size)) == NULL) {
  67. TRACE_EXIT -ENOMEM;
  68. }
  69. used_memory += size;
  70. if (peak_memory < used_memory) {
  71. peak_memory = used_memory;
  72. }
  73. TRACE_ABORT(0, ft_t_noise,
  74. "allocated buffer @ %p, %d bytes", *(void **)new, size);
  75. }
  76. int zft_vmalloc_always(void *new, size_t size)
  77. {
  78. TRACE_FUN(ft_t_flow);
  79. zft_vfree(new, size);
  80. TRACE_EXIT zft_vmalloc_once(new, size);
  81. }
  82. void zft_vfree(void *old, size_t size)
  83. {
  84. TRACE_FUN(ft_t_flow);
  85. if (*(void **)old) {
  86. vfree(*(void **)old);
  87. used_memory -= size;
  88. TRACE(ft_t_noise, "released buffer @ %p, %d bytes",
  89. *(void **)old, size);
  90. *(void **)old = NULL;
  91. }
  92. TRACE_EXIT;
  93. }
  94. void *zft_kmalloc(size_t size)
  95. {
  96. void *new;
  97. while ((new = kmalloc(size, GFP_KERNEL)) == NULL) {
  98. msleep_interruptible(100);
  99. }
  100. memset(new, 0, size);
  101. used_memory += size;
  102. if (peak_memory < used_memory) {
  103. peak_memory = used_memory;
  104. }
  105. return new;
  106. }
  107. void zft_kfree(void *old, size_t size)
  108. {
  109. kfree(old);
  110. used_memory -= size;
  111. }
  112. /* there are some more buffers that are allocated on demand.
  113. * cleanup_module() calles this function to be sure to have released
  114. * them
  115. */
  116. void zft_uninit_mem(void)
  117. {
  118. TRACE_FUN(ft_t_flow);
  119. zft_vfree(&zft_hseg_buf, FT_SEGMENT_SIZE);
  120. zft_vfree(&zft_deblock_buf, FT_SEGMENT_SIZE); zft_deblock_segment = -1;
  121. zft_free_vtbl();
  122. if (zft_cmpr_lock(0 /* don't load */) == 0) {
  123. (*zft_cmpr_ops->cleanup)();
  124. (*zft_cmpr_ops->reset)(); /* unlock it again */
  125. }
  126. zft_memory_stats();
  127. TRACE_EXIT;
  128. }