summaryrefslogtreecommitdiff
path: root/patches/yaffs_linux_allocator.c
blob: 9cbb5892ba2ec87adbf465653be57cd7324b8c06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
 * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
 *
 * Copyright (C) 2002-2011 Aleph One Ltd.
 *   for Toby Churchill Ltd and Brightstar Engineering
 *
 * Created by Charles Manning <charles@aleph1.co.uk>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 2.1 as
 * published by the Free Software Foundation.
 *
 * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
 */

/*
 * Note: Tis code is currently unused. Being checked in in case it becomes useful.
 */


#include "yaffs_allocator.h"
#include "yaffs_guts.h"
#include "yaffs_trace.h"
#include "yportenv.h"
#include "yaffs_linux.h"
/*
 * Start out with the same allocator as yaffs direct.
 * Todo: Change to Linux slab allocator.
 */



#define NAMELEN  20
struct yaffs_AllocatorStruct {
	char tnode_name[NAMELEN+1];
	char object_name[NAMELEN+1];
	struct kmem_cache *tnode_cache;
	struct kmem_cache *object_cache;
};

typedef struct yaffs_AllocatorStruct yaffs_Allocator;

int mount_id;

void yaffs_deinit_raw_tnodes_and_objs(struct yaffs_dev *dev)
{
	yaffs_Allocator *allocator = (yaffs_Allocator *)dev->allocator;

	T(YAFFS_TRACE_ALLOCATE,(TSTR("Deinitialising yaffs allocator\n")));

	if(allocator){
		if(allocator->tnode_cache){
			kmem_cache_destroy(allocator->tnode_cache);
			allocator->tnode_cache = NULL;
		} else {
			T(YAFFS_TRACE_ALWAYS,
				(TSTR("NULL tnode cache\n")));
			BUG();
		}

		if(allocator->object_cache){
			kmem_cache_destroy(allocator->object_cache);
			allocator->object_cache = NULL;
		} else {
			T(YAFFS_TRACE_ALWAYS,
				(TSTR("NULL object cache\n")));
			BUG();
		}

		kfree(allocator);

	} else {
		T(YAFFS_TRACE_ALWAYS,
			(TSTR("Deinitialising NULL allocator\n")));
		BUG();
	}
	dev->allocator = NULL;
}


static void fake_ctor0(void *data){data = data;}
static void fake_ctor1(void *data){data = data;}
static void fake_ctor2(void *data){data = data;}
static void fake_ctor3(void *data){data = data;}
static void fake_ctor4(void *data){data = data;}
static void fake_ctor5(void *data){data = data;}
static void fake_ctor6(void *data){data = data;}
static void fake_ctor7(void *data){data = data;}
static void fake_ctor8(void *data){data = data;}
static void fake_ctor9(void *data){data = data;}

static void (*fake_ctor_list[10]) (void *) = {
	fake_ctor0,
	fake_ctor1,
	fake_ctor2,
	fake_ctor3,
	fake_ctor4,
	fake_ctor5,
	fake_ctor6,
	fake_ctor7,
	fake_ctor8,
	fake_ctor9,
};

void yaffs_init_raw_tnodes_and_objs(struct yaffs_dev *dev)
{
	yaffs_Allocator *allocator;
	unsigned mount_id = struct yaffs_devo_lc(dev)->mount_id;

	T(YAFFS_TRACE_ALLOCATE,(TSTR("Initialising yaffs allocator\n")));

	if(dev->allocator)
		BUG();
	else if(mount_id >= 10){
		T(YAFFS_TRACE_ALWAYS,(TSTR("Bad mount_id %u\n"),mount_id));
	} else {
		 allocator = kmalloc(sizeof(yaffs_Allocator));
		 memset(allocator,0,sizeof(yaffs_Allocator));
		 dev->allocator = allocator;

		if(!dev->allocator){
			T(YAFFS_TRACE_ALWAYS,
				(TSTR("yaffs allocator creation failed\n")));
			BUG();
			return;

		}

		sprintf(allocator->tnode_name,"yaffs_t_%u",mount_id);
		sprintf(allocator->object_name,"yaffs_o_%u",mount_id);

		allocator->tnode_cache =
			kmem_cache_create(allocator->tnode_name,
				dev->tnode_size,
				0, 0,
				fake_ctor_list[mount_id]);
		if(allocator->tnode_cache)
			T(YAFFS_TRACE_ALLOCATE,
				(TSTR("tnode cache \"%s\" %p\n"),
				allocator->tnode_name,allocator->tnode_cache));
		else {
			T(YAFFS_TRACE_ALWAYS,
				(TSTR("yaffs cache creation failed\n")));
			BUG();
		}


		allocator->object_cache = 
			kmem_cache_create(allocator->object_name,
				sizeof(struct yaffs_obj),
				0, 0,
				fake_ctor_list[mount_id]);

		if(allocator->object_cache)
			T(YAFFS_TRACE_ALLOCATE,
				(TSTR("object cache \"%s\" %p\n"),
				allocator->object_name,allocator->object_cache));

		else {
			T(YAFFS_TRACE_ALWAYS,
				(TSTR("yaffs cache creation failed\n")));
			BUG();
		}
	} 
}


struct yaffs_tnode *yaffs_alloc_raw_tnode(struct yaffs_dev *dev)
{
	yaffs_Allocator *allocator = dev->allocator;
	if(!allocator || !allocator->tnode_cache){
		BUG();
		return NULL;
	}
	return kmem_cache_alloc(allocator->tnode_cache, GFP_NOFS);
}

void yaffs_free_raw_tnode(struct yaffs_dev *dev, struct yaffs_tnode *tn)
{
	yaffs_Allocator *allocator = dev->allocator;
	kmem_cache_free(allocator->tnode_cache,tn);
}

struct yaffs_obj *yaffs_alloc_raw_obj(struct yaffs_dev *dev)
{
	yaffs_Allocator *allocator = dev->allocator;
	if(!allocator){
		BUG();
		return NULL;
	}
	if(!allocator->object_cache){
		BUG();
		return NULL;
	}
	return kmem_cache_alloc(allocator->object_cache, GFP_NOFS);
}

void yaffs_free_raw_obj(struct yaffs_dev *dev, struct yaffs_obj *obj)
{
	yaffs_Allocator *allocator = dev->allocator;
	kmem_cache_free(allocator->object_cache,obj);
}