malloc.c 4.9 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9
/*
 * JFFS2 -- Journalling Flash File System, Version 2.
 *
 * Copyright (C) 2001-2003 Red Hat, Inc.
 *
 * Created by David Woodhouse <dwmw2@infradead.org>
 *
 * For licensing information, see the file 'LICENCE' in this directory.
 *
10
 * $Id: malloc.c,v 1.31 2005/11/07 11:14:40 gleixner Exp $
L
Linus Torvalds 已提交
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
 *
 */

#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/jffs2.h>
#include "nodelist.h"

/* These are initialised to NULL in the kernel startup code.
   If you're porting to other operating systems, beware */
static kmem_cache_t *full_dnode_slab;
static kmem_cache_t *raw_dirent_slab;
static kmem_cache_t *raw_inode_slab;
static kmem_cache_t *tmp_dnode_info_slab;
static kmem_cache_t *raw_node_ref_slab;
static kmem_cache_t *node_frag_slab;
static kmem_cache_t *inode_cache_slab;

int __init jffs2_create_slab_caches(void)
{
32
	full_dnode_slab = kmem_cache_create("jffs2_full_dnode",
L
Linus Torvalds 已提交
33
					    sizeof(struct jffs2_full_dnode),
34
					    0, 0, NULL, NULL);
L
Linus Torvalds 已提交
35 36 37 38 39
	if (!full_dnode_slab)
		goto err;

	raw_dirent_slab = kmem_cache_create("jffs2_raw_dirent",
					    sizeof(struct jffs2_raw_dirent),
40
					    0, 0, NULL, NULL);
L
Linus Torvalds 已提交
41 42 43 44 45
	if (!raw_dirent_slab)
		goto err;

	raw_inode_slab = kmem_cache_create("jffs2_raw_inode",
					   sizeof(struct jffs2_raw_inode),
46
					   0, 0, NULL, NULL);
L
Linus Torvalds 已提交
47 48 49 50 51
	if (!raw_inode_slab)
		goto err;

	tmp_dnode_info_slab = kmem_cache_create("jffs2_tmp_dnode",
						sizeof(struct jffs2_tmp_dnode_info),
52
						0, 0, NULL, NULL);
L
Linus Torvalds 已提交
53 54 55 56 57
	if (!tmp_dnode_info_slab)
		goto err;

	raw_node_ref_slab = kmem_cache_create("jffs2_raw_node_ref",
					      sizeof(struct jffs2_raw_node_ref),
58
					      0, 0, NULL, NULL);
L
Linus Torvalds 已提交
59 60 61 62 63
	if (!raw_node_ref_slab)
		goto err;

	node_frag_slab = kmem_cache_create("jffs2_node_frag",
					   sizeof(struct jffs2_node_frag),
64
					   0, 0, NULL, NULL);
L
Linus Torvalds 已提交
65 66 67 68 69
	if (!node_frag_slab)
		goto err;

	inode_cache_slab = kmem_cache_create("jffs2_inode_cache",
					     sizeof(struct jffs2_inode_cache),
70
					     0, 0, NULL, NULL);
L
Linus Torvalds 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
	if (inode_cache_slab)
		return 0;
 err:
	jffs2_destroy_slab_caches();
	return -ENOMEM;
}

void jffs2_destroy_slab_caches(void)
{
	if(full_dnode_slab)
		kmem_cache_destroy(full_dnode_slab);
	if(raw_dirent_slab)
		kmem_cache_destroy(raw_dirent_slab);
	if(raw_inode_slab)
		kmem_cache_destroy(raw_inode_slab);
	if(tmp_dnode_info_slab)
		kmem_cache_destroy(tmp_dnode_info_slab);
	if(raw_node_ref_slab)
		kmem_cache_destroy(raw_node_ref_slab);
	if(node_frag_slab)
		kmem_cache_destroy(node_frag_slab);
	if(inode_cache_slab)
		kmem_cache_destroy(inode_cache_slab);
}

struct jffs2_full_dirent *jffs2_alloc_full_dirent(int namesize)
{
98 99
	struct jffs2_full_dirent *ret;
	ret = kmalloc(sizeof(struct jffs2_full_dirent) + namesize, GFP_KERNEL);
100
	dbg_memalloc("%p\n", ret);
101
	return ret;
L
Linus Torvalds 已提交
102 103 104 105
}

void jffs2_free_full_dirent(struct jffs2_full_dirent *x)
{
106
	dbg_memalloc("%p\n", x);
L
Linus Torvalds 已提交
107 108 109 110 111
	kfree(x);
}

struct jffs2_full_dnode *jffs2_alloc_full_dnode(void)
{
112 113
	struct jffs2_full_dnode *ret;
	ret = kmem_cache_alloc(full_dnode_slab, GFP_KERNEL);
114
	dbg_memalloc("%p\n", ret);
L
Linus Torvalds 已提交
115 116 117 118 119
	return ret;
}

void jffs2_free_full_dnode(struct jffs2_full_dnode *x)
{
120
	dbg_memalloc("%p\n", x);
L
Linus Torvalds 已提交
121 122 123 124 125
	kmem_cache_free(full_dnode_slab, x);
}

struct jffs2_raw_dirent *jffs2_alloc_raw_dirent(void)
{
126 127
	struct jffs2_raw_dirent *ret;
	ret = kmem_cache_alloc(raw_dirent_slab, GFP_KERNEL);
128
	dbg_memalloc("%p\n", ret);
L
Linus Torvalds 已提交
129 130 131 132 133
	return ret;
}

void jffs2_free_raw_dirent(struct jffs2_raw_dirent *x)
{
134
	dbg_memalloc("%p\n", x);
L
Linus Torvalds 已提交
135 136 137 138 139
	kmem_cache_free(raw_dirent_slab, x);
}

struct jffs2_raw_inode *jffs2_alloc_raw_inode(void)
{
140 141
	struct jffs2_raw_inode *ret;
	ret = kmem_cache_alloc(raw_inode_slab, GFP_KERNEL);
142
	dbg_memalloc("%p\n", ret);
L
Linus Torvalds 已提交
143 144 145 146 147
	return ret;
}

void jffs2_free_raw_inode(struct jffs2_raw_inode *x)
{
148
	dbg_memalloc("%p\n", x);
L
Linus Torvalds 已提交
149 150 151 152 153
	kmem_cache_free(raw_inode_slab, x);
}

struct jffs2_tmp_dnode_info *jffs2_alloc_tmp_dnode_info(void)
{
154 155
	struct jffs2_tmp_dnode_info *ret;
	ret = kmem_cache_alloc(tmp_dnode_info_slab, GFP_KERNEL);
156
	dbg_memalloc("%p\n",
157
		ret);
L
Linus Torvalds 已提交
158 159 160 161 162
	return ret;
}

void jffs2_free_tmp_dnode_info(struct jffs2_tmp_dnode_info *x)
{
163
	dbg_memalloc("%p\n", x);
L
Linus Torvalds 已提交
164 165 166 167 168
	kmem_cache_free(tmp_dnode_info_slab, x);
}

struct jffs2_raw_node_ref *jffs2_alloc_raw_node_ref(void)
{
169 170
	struct jffs2_raw_node_ref *ret;
	ret = kmem_cache_alloc(raw_node_ref_slab, GFP_KERNEL);
171
	dbg_memalloc("%p\n", ret);
L
Linus Torvalds 已提交
172 173 174 175 176
	return ret;
}

void jffs2_free_raw_node_ref(struct jffs2_raw_node_ref *x)
{
177
	dbg_memalloc("%p\n", x);
L
Linus Torvalds 已提交
178 179 180 181 182
	kmem_cache_free(raw_node_ref_slab, x);
}

struct jffs2_node_frag *jffs2_alloc_node_frag(void)
{
183 184
	struct jffs2_node_frag *ret;
	ret = kmem_cache_alloc(node_frag_slab, GFP_KERNEL);
185
	dbg_memalloc("%p\n", ret);
L
Linus Torvalds 已提交
186 187 188 189 190
	return ret;
}

void jffs2_free_node_frag(struct jffs2_node_frag *x)
{
191
	dbg_memalloc("%p\n", x);
L
Linus Torvalds 已提交
192 193 194 195 196
	kmem_cache_free(node_frag_slab, x);
}

struct jffs2_inode_cache *jffs2_alloc_inode_cache(void)
{
197 198
	struct jffs2_inode_cache *ret;
	ret = kmem_cache_alloc(inode_cache_slab, GFP_KERNEL);
199
	dbg_memalloc("%p\n", ret);
L
Linus Torvalds 已提交
200 201 202 203 204
	return ret;
}

void jffs2_free_inode_cache(struct jffs2_inode_cache *x)
{
205
	dbg_memalloc("%p\n", x);
L
Linus Torvalds 已提交
206 207
	kmem_cache_free(inode_cache_slab, x);
}