Linux 2.6.14.1
[linux/fpc-iii.git] / fs / jffs2 / malloc.c
blob5abb431c2a00fcf1ccfa24d84474f20e9045a5a8
1 /*
2 * JFFS2 -- Journalling Flash File System, Version 2.
4 * Copyright (C) 2001-2003 Red Hat, Inc.
6 * Created by David Woodhouse <dwmw2@infradead.org>
8 * For licensing information, see the file 'LICENCE' in this directory.
10 * $Id: malloc.c,v 1.28 2004/11/16 20:36:11 dwmw2 Exp $
14 #include <linux/kernel.h>
15 #include <linux/slab.h>
16 #include <linux/init.h>
17 #include <linux/jffs2.h>
18 #include "nodelist.h"
20 #if 0
21 #define JFFS2_SLAB_POISON SLAB_POISON
22 #else
23 #define JFFS2_SLAB_POISON 0
24 #endif
26 // replace this by #define D3 (x) x for cache debugging
27 #define D3(x)
29 /* These are initialised to NULL in the kernel startup code.
30 If you're porting to other operating systems, beware */
31 static kmem_cache_t *full_dnode_slab;
32 static kmem_cache_t *raw_dirent_slab;
33 static kmem_cache_t *raw_inode_slab;
34 static kmem_cache_t *tmp_dnode_info_slab;
35 static kmem_cache_t *raw_node_ref_slab;
36 static kmem_cache_t *node_frag_slab;
37 static kmem_cache_t *inode_cache_slab;
39 int __init jffs2_create_slab_caches(void)
41 full_dnode_slab = kmem_cache_create("jffs2_full_dnode",
42 sizeof(struct jffs2_full_dnode),
43 0, JFFS2_SLAB_POISON, NULL, NULL);
44 if (!full_dnode_slab)
45 goto err;
47 raw_dirent_slab = kmem_cache_create("jffs2_raw_dirent",
48 sizeof(struct jffs2_raw_dirent),
49 0, JFFS2_SLAB_POISON, NULL, NULL);
50 if (!raw_dirent_slab)
51 goto err;
53 raw_inode_slab = kmem_cache_create("jffs2_raw_inode",
54 sizeof(struct jffs2_raw_inode),
55 0, JFFS2_SLAB_POISON, NULL, NULL);
56 if (!raw_inode_slab)
57 goto err;
59 tmp_dnode_info_slab = kmem_cache_create("jffs2_tmp_dnode",
60 sizeof(struct jffs2_tmp_dnode_info),
61 0, JFFS2_SLAB_POISON, NULL, NULL);
62 if (!tmp_dnode_info_slab)
63 goto err;
65 raw_node_ref_slab = kmem_cache_create("jffs2_raw_node_ref",
66 sizeof(struct jffs2_raw_node_ref),
67 0, JFFS2_SLAB_POISON, NULL, NULL);
68 if (!raw_node_ref_slab)
69 goto err;
71 node_frag_slab = kmem_cache_create("jffs2_node_frag",
72 sizeof(struct jffs2_node_frag),
73 0, JFFS2_SLAB_POISON, NULL, NULL);
74 if (!node_frag_slab)
75 goto err;
77 inode_cache_slab = kmem_cache_create("jffs2_inode_cache",
78 sizeof(struct jffs2_inode_cache),
79 0, JFFS2_SLAB_POISON, NULL, NULL);
80 if (inode_cache_slab)
81 return 0;
82 err:
83 jffs2_destroy_slab_caches();
84 return -ENOMEM;
87 void jffs2_destroy_slab_caches(void)
89 if(full_dnode_slab)
90 kmem_cache_destroy(full_dnode_slab);
91 if(raw_dirent_slab)
92 kmem_cache_destroy(raw_dirent_slab);
93 if(raw_inode_slab)
94 kmem_cache_destroy(raw_inode_slab);
95 if(tmp_dnode_info_slab)
96 kmem_cache_destroy(tmp_dnode_info_slab);
97 if(raw_node_ref_slab)
98 kmem_cache_destroy(raw_node_ref_slab);
99 if(node_frag_slab)
100 kmem_cache_destroy(node_frag_slab);
101 if(inode_cache_slab)
102 kmem_cache_destroy(inode_cache_slab);
105 struct jffs2_full_dirent *jffs2_alloc_full_dirent(int namesize)
107 return kmalloc(sizeof(struct jffs2_full_dirent) + namesize, GFP_KERNEL);
110 void jffs2_free_full_dirent(struct jffs2_full_dirent *x)
112 kfree(x);
115 struct jffs2_full_dnode *jffs2_alloc_full_dnode(void)
117 struct jffs2_full_dnode *ret = kmem_cache_alloc(full_dnode_slab, GFP_KERNEL);
118 D3 (printk (KERN_DEBUG "alloc_full_dnode at %p\n", ret));
119 return ret;
122 void jffs2_free_full_dnode(struct jffs2_full_dnode *x)
124 D3 (printk (KERN_DEBUG "free full_dnode at %p\n", x));
125 kmem_cache_free(full_dnode_slab, x);
128 struct jffs2_raw_dirent *jffs2_alloc_raw_dirent(void)
130 struct jffs2_raw_dirent *ret = kmem_cache_alloc(raw_dirent_slab, GFP_KERNEL);
131 D3 (printk (KERN_DEBUG "alloc_raw_dirent\n", ret));
132 return ret;
135 void jffs2_free_raw_dirent(struct jffs2_raw_dirent *x)
137 D3 (printk (KERN_DEBUG "free_raw_dirent at %p\n", x));
138 kmem_cache_free(raw_dirent_slab, x);
141 struct jffs2_raw_inode *jffs2_alloc_raw_inode(void)
143 struct jffs2_raw_inode *ret = kmem_cache_alloc(raw_inode_slab, GFP_KERNEL);
144 D3 (printk (KERN_DEBUG "alloc_raw_inode at %p\n", ret));
145 return ret;
148 void jffs2_free_raw_inode(struct jffs2_raw_inode *x)
150 D3 (printk (KERN_DEBUG "free_raw_inode at %p\n", x));
151 kmem_cache_free(raw_inode_slab, x);
154 struct jffs2_tmp_dnode_info *jffs2_alloc_tmp_dnode_info(void)
156 struct jffs2_tmp_dnode_info *ret = kmem_cache_alloc(tmp_dnode_info_slab, GFP_KERNEL);
157 D3 (printk (KERN_DEBUG "alloc_tmp_dnode_info at %p\n", ret));
158 return ret;
161 void jffs2_free_tmp_dnode_info(struct jffs2_tmp_dnode_info *x)
163 D3 (printk (KERN_DEBUG "free_tmp_dnode_info at %p\n", x));
164 kmem_cache_free(tmp_dnode_info_slab, x);
167 struct jffs2_raw_node_ref *jffs2_alloc_raw_node_ref(void)
169 struct jffs2_raw_node_ref *ret = kmem_cache_alloc(raw_node_ref_slab, GFP_KERNEL);
170 D3 (printk (KERN_DEBUG "alloc_raw_node_ref at %p\n", ret));
171 return ret;
174 void jffs2_free_raw_node_ref(struct jffs2_raw_node_ref *x)
176 D3 (printk (KERN_DEBUG "free_raw_node_ref at %p\n", x));
177 kmem_cache_free(raw_node_ref_slab, x);
180 struct jffs2_node_frag *jffs2_alloc_node_frag(void)
182 struct jffs2_node_frag *ret = kmem_cache_alloc(node_frag_slab, GFP_KERNEL);
183 D3 (printk (KERN_DEBUG "alloc_node_frag at %p\n", ret));
184 return ret;
187 void jffs2_free_node_frag(struct jffs2_node_frag *x)
189 D3 (printk (KERN_DEBUG "free_node_frag at %p\n", x));
190 kmem_cache_free(node_frag_slab, x);
193 struct jffs2_inode_cache *jffs2_alloc_inode_cache(void)
195 struct jffs2_inode_cache *ret = kmem_cache_alloc(inode_cache_slab, GFP_KERNEL);
196 D3 (printk(KERN_DEBUG "Allocated inocache at %p\n", ret));
197 return ret;
200 void jffs2_free_inode_cache(struct jffs2_inode_cache *x)
202 D3 (printk(KERN_DEBUG "Freeing inocache at %p\n", x));
203 kmem_cache_free(inode_cache_slab, x);