File: /usr/src/linux/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     #include <asm/segment.h>
31     
32     #include <linux/zftape.h>
33     
34     #if LINUX_VERSION_CODE >= KERNEL_VER(2,1,0)
35     #include <linux/vmalloc.h>
36     #endif
37     
38     #include "../zftape/zftape-init.h"
39     #include "../zftape/zftape-eof.h"
40     #include "../zftape/zftape-ctl.h"
41     #include "../zftape/zftape-write.h"
42     #include "../zftape/zftape-read.h"
43     #include "../zftape/zftape-rw.h"
44     #include "../zftape/zftape-vtbl.h"
45     
46     /*  global variables
47      */
48     
49     /*  local varibales
50      */
51     static unsigned int used_memory;
52     static unsigned int peak_memory;
53     
54     void zft_memory_stats(void)
55     {
56     	TRACE_FUN(ft_t_flow);
57     
58     	TRACE(ft_t_noise, "Memory usage (vmalloc allocations):\n"
59     	      KERN_INFO "total allocated: %d\n"
60     	      KERN_INFO "peak allocation: %d",
61     	      used_memory, peak_memory);
62     	peak_memory = used_memory;
63     	TRACE_EXIT;
64     }
65     
66     int zft_vcalloc_once(void *new, size_t size)
67     {
68     	TRACE_FUN(ft_t_flow);
69     	if (zft_vmalloc_once(new, size) < 0) {
70     		TRACE_EXIT -ENOMEM;
71     	}
72     	memset(*(void **)new, '\0', size);
73     	TRACE_EXIT 0;
74     }
75     int zft_vmalloc_once(void *new, size_t size)
76     {
77     	TRACE_FUN(ft_t_flow);
78     
79     	if (*(void **)new != NULL || size == 0) {
80     		TRACE_EXIT 0;
81     	}
82     	if ((*(void **)new = vmalloc(size)) == NULL) {
83     		TRACE_EXIT -ENOMEM;
84     	}
85     	used_memory += size;
86     	if (peak_memory < used_memory) {
87     		peak_memory = used_memory;
88     	}
89     	TRACE_ABORT(0, ft_t_noise,
90     		    "allocated buffer @ %p, %d bytes", *(void **)new, size);
91     }
92     int zft_vcalloc_always(void *new, size_t size)
93     {
94     	TRACE_FUN(ft_t_flow);
95     
96     	zft_vfree(new, size);
97     	TRACE_EXIT zft_vcalloc_once(new, size);
98     }
99     int zft_vmalloc_always(void *new, size_t size)
100     {
101     	TRACE_FUN(ft_t_flow);
102     
103     	zft_vfree(new, size);
104     	TRACE_EXIT zft_vmalloc_once(new, size);
105     }
106     void zft_vfree(void *old, size_t size)
107     {
108     	TRACE_FUN(ft_t_flow);
109     
110     	if (*(void **)old) {
111     		vfree(*(void **)old);
112     		used_memory -= size;
113     		TRACE(ft_t_noise, "released buffer @ %p, %d bytes",
114     		      *(void **)old, size);
115     		*(void **)old = NULL;
116     	}
117     	TRACE_EXIT;
118     }
119     
120     void *zft_kmalloc(size_t size)
121     {
122     	void *new;
123     
124     	while ((new = kmalloc(size, GFP_KERNEL)) == NULL) {
125     		current->state   = TASK_INTERRUPTIBLE;
126     		schedule_timeout(HZ/10);
127     	}
128     	memset(new, 0, size);
129     	used_memory += size;
130     	if (peak_memory < used_memory) {
131     		peak_memory = used_memory;
132     	}
133     	return new;
134     }
135     
136     void zft_kfree(void *old, size_t size)
137     {
138     	kfree(old);
139     	used_memory -= size;
140     }
141     
142     /* there are some more buffers that are allocated on demand.
143      * cleanup_module() calles this function to be sure to have released
144      * them 
145      */
146     void zft_uninit_mem(void)
147     {
148     	TRACE_FUN(ft_t_flow);
149     
150     	zft_vfree(&zft_hseg_buf, FT_SEGMENT_SIZE);
151     	zft_vfree(&zft_deblock_buf, FT_SEGMENT_SIZE); zft_deblock_segment = -1;
152     	zft_free_vtbl();
153     	if (zft_cmpr_lock(0 /* don't load */) == 0) {
154     		(*zft_cmpr_ops->cleanup)();
155     		(*zft_cmpr_ops->reset)(); /* unlock it again */
156     	}
157     	zft_memory_stats();
158     	TRACE_EXIT;
159     }
160