ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / char / ftape / zftape / zftape-buffers.c
1 /*
2  *      Copyright (C) 1995-1997 Claus-Justus Heine.
3
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2, or (at your option)
7  any later version.
8
9  This program is distributed in the hope that it will 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; see the file COPYING.  If not, write to
16  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
17
18  *
19  * $Source: /homes/cvs/ftape-stacked/ftape/zftape/zftape-buffers.c,v $
20  * $Revision: 1.2 $
21  * $Date: 1997/10/05 19:18:59 $
22  *
23  *      This file contains the dynamic buffer allocation routines 
24  *      of zftape
25  */
26
27 #include <linux/errno.h>
28 #include <linux/mm.h>
29 #include <linux/slab.h>
30
31 #include <linux/zftape.h>
32
33 #include <linux/vmalloc.h>
34
35 #include "../zftape/zftape-init.h"
36 #include "../zftape/zftape-eof.h"
37 #include "../zftape/zftape-ctl.h"
38 #include "../zftape/zftape-write.h"
39 #include "../zftape/zftape-read.h"
40 #include "../zftape/zftape-rw.h"
41 #include "../zftape/zftape-vtbl.h"
42
43 /*  global variables
44  */
45
46 /*  local varibales
47  */
48 static unsigned int used_memory;
49 static unsigned int peak_memory;
50
51 void zft_memory_stats(void)
52 {
53         TRACE_FUN(ft_t_flow);
54
55         TRACE(ft_t_noise, "Memory usage (vmalloc allocations):\n"
56               KERN_INFO "total allocated: %d\n"
57               KERN_INFO "peak allocation: %d",
58               used_memory, peak_memory);
59         peak_memory = used_memory;
60         TRACE_EXIT;
61 }
62
63 int zft_vcalloc_once(void *new, size_t size)
64 {
65         TRACE_FUN(ft_t_flow);
66         if (zft_vmalloc_once(new, size) < 0) {
67                 TRACE_EXIT -ENOMEM;
68         }
69         memset(*(void **)new, '\0', size);
70         TRACE_EXIT 0;
71 }
72 int zft_vmalloc_once(void *new, size_t size)
73 {
74         TRACE_FUN(ft_t_flow);
75
76         if (*(void **)new != NULL || size == 0) {
77                 TRACE_EXIT 0;
78         }
79         if ((*(void **)new = vmalloc(size)) == NULL) {
80                 TRACE_EXIT -ENOMEM;
81         }
82         used_memory += size;
83         if (peak_memory < used_memory) {
84                 peak_memory = used_memory;
85         }
86         TRACE_ABORT(0, ft_t_noise,
87                     "allocated buffer @ %p, %d bytes", *(void **)new, size);
88 }
89 int zft_vcalloc_always(void *new, size_t size)
90 {
91         TRACE_FUN(ft_t_flow);
92
93         zft_vfree(new, size);
94         TRACE_EXIT zft_vcalloc_once(new, size);
95 }
96 int zft_vmalloc_always(void *new, size_t size)
97 {
98         TRACE_FUN(ft_t_flow);
99
100         zft_vfree(new, size);
101         TRACE_EXIT zft_vmalloc_once(new, size);
102 }
103 void zft_vfree(void *old, size_t size)
104 {
105         TRACE_FUN(ft_t_flow);
106
107         if (*(void **)old) {
108                 vfree(*(void **)old);
109                 used_memory -= size;
110                 TRACE(ft_t_noise, "released buffer @ %p, %d bytes",
111                       *(void **)old, size);
112                 *(void **)old = NULL;
113         }
114         TRACE_EXIT;
115 }
116
117 void *zft_kmalloc(size_t size)
118 {
119         void *new;
120
121         while ((new = kmalloc(size, GFP_KERNEL)) == NULL) {
122                 current->state   = TASK_INTERRUPTIBLE;
123                 schedule_timeout(HZ/10);
124         }
125         memset(new, 0, size);
126         used_memory += size;
127         if (peak_memory < used_memory) {
128                 peak_memory = used_memory;
129         }
130         return new;
131 }
132
133 void zft_kfree(void *old, size_t size)
134 {
135         kfree(old);
136         used_memory -= size;
137 }
138
139 /* there are some more buffers that are allocated on demand.
140  * cleanup_module() calles this function to be sure to have released
141  * them 
142  */
143 void zft_uninit_mem(void)
144 {
145         TRACE_FUN(ft_t_flow);
146
147         zft_vfree(&zft_hseg_buf, FT_SEGMENT_SIZE);
148         zft_vfree(&zft_deblock_buf, FT_SEGMENT_SIZE); zft_deblock_segment = -1;
149         zft_free_vtbl();
150         if (zft_cmpr_lock(0 /* don't load */) == 0) {
151                 (*zft_cmpr_ops->cleanup)();
152                 (*zft_cmpr_ops->reset)(); /* unlock it again */
153         }
154         zft_memory_stats();
155         TRACE_EXIT;
156 }