import of upstream 2.4.34.4 from kernel.org
[linux-2.4.git] / fs / jffs2 / malloc.c
1 /*
2  * JFFS2 -- Journalling Flash File System, Version 2.
3  *
4  * Copyright (C) 2001 Red Hat, Inc.
5  *
6  * Created by David Woodhouse <dwmw2@cambridge.redhat.com>
7  *
8  * The original JFFS, from which the design for JFFS2 was derived,
9  * was designed and implemented by Axis Communications AB.
10  *
11  * The contents of this file are subject to the Red Hat eCos Public
12  * License Version 1.1 (the "Licence"); you may not use this file
13  * except in compliance with the Licence.  You may obtain a copy of
14  * the Licence at http://www.redhat.com/
15  *
16  * Software distributed under the Licence is distributed on an "AS IS"
17  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
18  * See the Licence for the specific language governing rights and
19  * limitations under the Licence.
20  *
21  * The Original Code is JFFS2 - Journalling Flash File System, version 2
22  *
23  * Alternatively, the contents of this file may be used under the
24  * terms of the GNU General Public License version 2 (the "GPL"), in
25  * which case the provisions of the GPL are applicable instead of the
26  * above.  If you wish to allow the use of your version of this file
27  * only under the terms of the GPL and not to allow others to use your
28  * version of this file under the RHEPL, indicate your decision by
29  * deleting the provisions above and replace them with the notice and
30  * other provisions required by the GPL.  If you do not delete the
31  * provisions above, a recipient may use your version of this file
32  * under either the RHEPL or the GPL.
33  *
34  * $Id: malloc.c,v 1.16 2001/03/15 15:38:24 dwmw2 Exp $
35  *
36  */
37
38 #include <linux/kernel.h>
39 #include <linux/slab.h>
40 #include <linux/init.h>
41 #include <linux/jffs2.h>
42 #include "nodelist.h"
43
44 #if 0
45 #define JFFS2_SLAB_POISON SLAB_POISON
46 #else
47 #define JFFS2_SLAB_POISON 0
48 #endif
49
50 /* These are initialised to NULL in the kernel startup code.
51    If you're porting to other operating systems, beware */
52 static kmem_cache_t *full_dnode_slab;
53 static kmem_cache_t *raw_dirent_slab;
54 static kmem_cache_t *raw_inode_slab;
55 static kmem_cache_t *tmp_dnode_info_slab;
56 static kmem_cache_t *raw_node_ref_slab;
57 static kmem_cache_t *node_frag_slab;
58 static kmem_cache_t *inode_cache_slab;
59
60 void jffs2_free_tmp_dnode_info_list(struct jffs2_tmp_dnode_info *tn)
61 {
62         struct jffs2_tmp_dnode_info *next;
63
64         while (tn) {
65                 next = tn;
66                 tn = tn->next;
67                 jffs2_free_full_dnode(next->fn);
68                 jffs2_free_tmp_dnode_info(next);
69         }
70 }
71
72 void jffs2_free_full_dirent_list(struct jffs2_full_dirent *fd)
73 {
74         struct jffs2_full_dirent *next;
75
76         while (fd) {
77                 next = fd->next;
78                 jffs2_free_full_dirent(fd);
79                 fd = next;
80         }
81 }
82
83 int __init jffs2_create_slab_caches(void)
84 {
85         full_dnode_slab = kmem_cache_create("jffs2_full_dnode", sizeof(struct jffs2_full_dnode), 0, JFFS2_SLAB_POISON, NULL, NULL);
86         if (!full_dnode_slab)
87                 goto err;
88
89         raw_dirent_slab = kmem_cache_create("jffs2_raw_dirent", sizeof(struct jffs2_raw_dirent), 0, JFFS2_SLAB_POISON, NULL, NULL);
90         if (!raw_dirent_slab)
91                 goto err;
92
93         raw_inode_slab = kmem_cache_create("jffs2_raw_inode", sizeof(struct jffs2_raw_inode), 0, JFFS2_SLAB_POISON, NULL, NULL);
94         if (!raw_inode_slab)
95                 goto err;
96
97         tmp_dnode_info_slab = kmem_cache_create("jffs2_tmp_dnode", sizeof(struct jffs2_tmp_dnode_info), 0, JFFS2_SLAB_POISON, NULL, NULL);
98         if (!tmp_dnode_info_slab)
99                 goto err;
100
101         raw_node_ref_slab = kmem_cache_create("jffs2_raw_node_ref", sizeof(struct jffs2_raw_node_ref), 0, JFFS2_SLAB_POISON, NULL, NULL);
102         if (!raw_node_ref_slab)
103                 goto err;
104
105         node_frag_slab = kmem_cache_create("jffs2_node_frag", sizeof(struct jffs2_node_frag), 0, JFFS2_SLAB_POISON, NULL, NULL);
106         if (!node_frag_slab)
107                 goto err;
108
109         inode_cache_slab = kmem_cache_create("jffs2_inode_cache", sizeof(struct jffs2_inode_cache), 0, JFFS2_SLAB_POISON, NULL, NULL);
110
111         if (inode_cache_slab)
112                 return 0;
113  err:
114         jffs2_destroy_slab_caches();
115         return -ENOMEM;
116 }
117
118 void jffs2_destroy_slab_caches(void)
119 {
120         if(full_dnode_slab)
121                 kmem_cache_destroy(full_dnode_slab);
122         if(raw_dirent_slab)
123                 kmem_cache_destroy(raw_dirent_slab);
124         if(raw_inode_slab)
125                 kmem_cache_destroy(raw_inode_slab);
126         if(tmp_dnode_info_slab)
127                 kmem_cache_destroy(tmp_dnode_info_slab);
128         if(raw_node_ref_slab)
129                 kmem_cache_destroy(raw_node_ref_slab);
130         if(node_frag_slab)
131                 kmem_cache_destroy(node_frag_slab);
132         if(inode_cache_slab)
133                 kmem_cache_destroy(inode_cache_slab);
134
135 }
136
137 struct jffs2_full_dirent *jffs2_alloc_full_dirent(int namesize)
138 {
139         return kmalloc(sizeof(struct jffs2_full_dirent) + namesize, GFP_KERNEL);
140 }
141
142 void jffs2_free_full_dirent(struct jffs2_full_dirent *x)
143 {
144         kfree(x);
145 }
146
147 struct jffs2_full_dnode *jffs2_alloc_full_dnode(void)
148 {
149         void *ret = kmem_cache_alloc(full_dnode_slab, GFP_KERNEL);
150         return ret;
151 }
152
153 void jffs2_free_full_dnode(struct jffs2_full_dnode *x)
154 {
155         kmem_cache_free(full_dnode_slab, x);
156 }
157
158 struct jffs2_raw_dirent *jffs2_alloc_raw_dirent(void)
159 {
160         return kmem_cache_alloc(raw_dirent_slab, GFP_KERNEL);
161 }
162
163 void jffs2_free_raw_dirent(struct jffs2_raw_dirent *x)
164 {
165         kmem_cache_free(raw_dirent_slab, x);
166 }
167
168 struct jffs2_raw_inode *jffs2_alloc_raw_inode(void)
169 {
170         return kmem_cache_alloc(raw_inode_slab, GFP_KERNEL);
171 }
172
173 void jffs2_free_raw_inode(struct jffs2_raw_inode *x)
174 {
175         kmem_cache_free(raw_inode_slab, x);
176 }
177
178 struct jffs2_tmp_dnode_info *jffs2_alloc_tmp_dnode_info(void)
179 {
180         return kmem_cache_alloc(tmp_dnode_info_slab, GFP_KERNEL);
181 }
182
183 void jffs2_free_tmp_dnode_info(struct jffs2_tmp_dnode_info *x)
184 {
185         kmem_cache_free(tmp_dnode_info_slab, x);
186 }
187
188 struct jffs2_raw_node_ref *jffs2_alloc_raw_node_ref(void)
189 {
190         return kmem_cache_alloc(raw_node_ref_slab, GFP_KERNEL);
191 }
192
193 void jffs2_free_raw_node_ref(struct jffs2_raw_node_ref *x)
194 {
195         kmem_cache_free(raw_node_ref_slab, x);
196 }
197
198 struct jffs2_node_frag *jffs2_alloc_node_frag(void)
199 {
200         return kmem_cache_alloc(node_frag_slab, GFP_KERNEL);
201 }
202
203 void jffs2_free_node_frag(struct jffs2_node_frag *x)
204 {
205         kmem_cache_free(node_frag_slab, x);
206 }
207
208 struct jffs2_inode_cache *jffs2_alloc_inode_cache(void)
209 {
210         struct jffs2_inode_cache *ret = kmem_cache_alloc(inode_cache_slab, GFP_KERNEL);
211         D1(printk(KERN_DEBUG "Allocated inocache at %p\n", ret));
212         return ret;
213 }
214
215 void jffs2_free_inode_cache(struct jffs2_inode_cache *x)
216 {
217         D1(printk(KERN_DEBUG "Freeing inocache at %p\n", x));
218         kmem_cache_free(inode_cache_slab, x);
219 }
220