summaryrefslogtreecommitdiffstats
path: root/target/linux/generic/files/fs/yaffs2/yaffs_allocator.c
blob: c0513d54ec5d13cbcde6260e32893d07732cebf6 (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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*
 * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
 *
 * Copyright (C) 2002-2010 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.
 */


#include "yaffs_allocator.h"
#include "yaffs_guts.h"
#include "yaffs_trace.h"
#include "yportenv.h"

#ifdef CONFIG_YAFFS_YMALLOC_ALLOCATOR

void yaffs_deinit_raw_tnodes_and_objs(yaffs_dev_t *dev)
{
	dev = dev;
}

void yaffs_init_raw_tnodes_and_objs(yaffs_dev_t *dev)
{
	dev = dev;
}

yaffs_tnode_t *yaffs_alloc_raw_tnode(yaffs_dev_t *dev)
{
	return (yaffs_tnode_t *)YMALLOC(dev->tnode_size);
}

void yaffs_free_raw_tnode(yaffs_dev_t *dev, yaffs_tnode_t *tn)
{
	dev = dev;
	YFREE(tn);
}

void yaffs_init_raw_objs(yaffs_dev_t *dev)
{
	dev = dev;
}

void yaffs_deinit_raw_objs(yaffs_dev_t *dev)
{
	dev = dev;
}

yaffs_obj_t *yaffs_alloc_raw_obj(yaffs_dev_t *dev)
{
	dev = dev;
	return (yaffs_obj_t *) YMALLOC(sizeof(yaffs_obj_t));
}


void yaffs_free_raw_obj(yaffs_dev_t *dev, yaffs_obj_t *obj)
{

	dev = dev;
	YFREE(obj);
}

#else

struct yaffs_tnode_list {
	struct yaffs_tnode_list *next;
	yaffs_tnode_t *tnodes;
};

typedef struct yaffs_tnode_list yaffs_tnodelist_t;

struct yaffs_obj_tList_struct {
	yaffs_obj_t *objects;
	struct yaffs_obj_tList_struct *next;
};

typedef struct yaffs_obj_tList_struct yaffs_obj_tList;


struct yaffs_AllocatorStruct {
	int n_tnodesCreated;
	yaffs_tnode_t *freeTnodes;
	int nFreeTnodes;
	yaffs_tnodelist_t *allocatedTnodeList;

	int n_objCreated;
	yaffs_obj_t *freeObjects;
	int nFreeObjects;

	yaffs_obj_tList *allocatedObjectList;
};

typedef struct yaffs_AllocatorStruct yaffs_Allocator;


static void yaffs_deinit_raw_tnodes(yaffs_dev_t *dev)
{

	yaffs_Allocator *allocator = (yaffs_Allocator *)dev->allocator;

	yaffs_tnodelist_t *tmp;

	if(!allocator){
		YBUG();
		return;
	}

	while (allocator->allocatedTnodeList) {
		tmp = allocator->allocatedTnodeList->next;

		YFREE(allocator->allocatedTnodeList->tnodes);
		YFREE(allocator->allocatedTnodeList);
		allocator->allocatedTnodeList = tmp;

	}

	allocator->freeTnodes = NULL;
	allocator->nFreeTnodes = 0;
	allocator->n_tnodesCreated = 0;
}

static void yaffs_init_raw_tnodes(yaffs_dev_t *dev)
{
	yaffs_Allocator *allocator = dev->allocator;

	if(allocator){
		allocator->allocatedTnodeList = NULL;
		allocator->freeTnodes = NULL;
		allocator->nFreeTnodes = 0;
		allocator->n_tnodesCreated = 0;
	} else
		YBUG();
}

static int yaffs_create_tnodes(yaffs_dev_t *dev, int n_tnodes)
{
	yaffs_Allocator *allocator = (yaffs_Allocator *)dev->allocator;
	int i;
	yaffs_tnode_t *newTnodes;
	__u8 *mem;
	yaffs_tnode_t *curr;
	yaffs_tnode_t *next;
	yaffs_tnodelist_t *tnl;

	if(!allocator){
		YBUG();
		return YAFFS_FAIL;
	}

	if (n_tnodes < 1)
		return YAFFS_OK;


	/* make these things */

	newTnodes = YMALLOC(n_tnodes * dev->tnode_size);
	mem = (__u8 *)newTnodes;

	if (!newTnodes) {
		T(YAFFS_TRACE_ERROR,
			(TSTR("yaffs: Could not allocate Tnodes" TENDSTR)));
		return YAFFS_FAIL;
	}

	/* New hookup for wide tnodes */
	for (i = 0; i < n_tnodes - 1; i++) {
		curr = (yaffs_tnode_t *) &mem[i * dev->tnode_size];
		next = (yaffs_tnode_t *) &mem[(i+1) * dev->tnode_size];
		curr->internal[0] = next;
	}

	curr = (yaffs_tnode_t *) &mem[(n_tnodes - 1) * dev->tnode_size];
	curr->internal[0] = allocator->freeTnodes;
	allocator->freeTnodes = (yaffs_tnode_t *)mem;

	allocator->nFreeTnodes += n_tnodes;
	allocator->n_tnodesCreated += n_tnodes;

	/* Now add this bunch of tnodes to a list for freeing up.
	 * NB If we can't add this to the management list it isn't fatal
	 * but it just means we can't free this bunch of tnodes later.
	 */

	tnl = YMALLOC(sizeof(yaffs_tnodelist_t));
	if (!tnl) {
		T(YAFFS_TRACE_ERROR,
		  (TSTR
		   ("yaffs: Could not add tnodes to management list" TENDSTR)));
		   return YAFFS_FAIL;
	} else {
		tnl->tnodes = newTnodes;
		tnl->next = allocator->allocatedTnodeList;
		allocator->allocatedTnodeList = tnl;
	}

	T(YAFFS_TRACE_ALLOCATE, (TSTR("yaffs: Tnodes added" TENDSTR)));

	return YAFFS_OK;
}


yaffs_tnode_t *yaffs_alloc_raw_tnode(yaffs_dev_t *dev)
{
	yaffs_Allocator *allocator = (yaffs_Allocator *)dev->allocator;
	yaffs_tnode_t *tn = NULL;

	if(!allocator){
		YBUG();
		return NULL;
	}

	/* If there are none left make more */
	if (!allocator->freeTnodes)
		yaffs_create_tnodes(dev, YAFFS_ALLOCATION_NTNODES);

	if (allocator->freeTnodes) {
		tn = allocator->freeTnodes;
		allocator->freeTnodes = allocator->freeTnodes->internal[0];
		allocator->nFreeTnodes--;
	}

	return tn;
}

/* FreeTnode frees up a tnode and puts it back on the free list */
void yaffs_free_raw_tnode(yaffs_dev_t *dev, yaffs_tnode_t *tn)
{
	yaffs_Allocator *allocator = dev->allocator;

	if(!allocator){
		YBUG();
		return;
	}

	if (tn) {
		tn->internal[0] = allocator->freeTnodes;
		allocator->freeTnodes = tn;
		allocator->nFreeTnodes++;
	}
	dev->checkpoint_blocks_required = 0; /* force recalculation*/
}



static void yaffs_init_raw_objs(yaffs_dev_t *dev)
{
	yaffs_Allocator *allocator = dev->allocator;

	if(allocator) {
		allocator->allocatedObjectList = NULL;
		allocator->freeObjects = NULL;
		allocator->nFreeObjects = 0;
	} else
		YBUG();
}

static void yaffs_deinit_raw_objs(yaffs_dev_t *dev)
{
	yaffs_Allocator *allocator = dev->allocator;
	yaffs_obj_tList *tmp;

	if(!allocator){
		YBUG();
		return;
	}

	while (allocator->allocatedObjectList) {
		tmp = allocator->allocatedObjectList->next;
		YFREE(allocator->allocatedObjectList->objects);
		YFREE(allocator->allocatedObjectList);

		allocator->allocatedObjectList = tmp;
	}

	allocator->freeObjects = NULL;
	allocator->nFreeObjects = 0;
	allocator->n_objCreated = 0;
}


static int yaffs_create_free_objs(yaffs_dev_t *dev, int n_obj)
{
	yaffs_Allocator *allocator = dev->allocator;

	int i;
	yaffs_obj_t *newObjects;
	yaffs_obj_tList *list;

	if(!allocator){
		YBUG();
		return YAFFS_FAIL;
	}

	if (n_obj < 1)
		return YAFFS_OK;

	/* make these things */
	newObjects = YMALLOC(n_obj * sizeof(yaffs_obj_t));
	list = YMALLOC(sizeof(yaffs_obj_tList));

	if (!newObjects || !list) {
		if (newObjects){
			YFREE(newObjects);
			newObjects = NULL;
		}
		if (list){
			YFREE(list);
			list = NULL;
		}
		T(YAFFS_TRACE_ALLOCATE,
		  (TSTR("yaffs: Could not allocate more objects" TENDSTR)));
		return YAFFS_FAIL;
	}

	/* Hook them into the free list */
	for (i = 0; i < n_obj - 1; i++) {
		newObjects[i].siblings.next =
				(struct ylist_head *)(&newObjects[i + 1]);
	}

	newObjects[n_obj - 1].siblings.next = (void *)allocator->freeObjects;
	allocator->freeObjects = newObjects;
	allocator->nFreeObjects += n_obj;
	allocator->n_objCreated += n_obj;

	/* Now add this bunch of Objects to a list for freeing up. */

	list->objects = newObjects;
	list->next = allocator->allocatedObjectList;
	allocator->allocatedObjectList = list;

	return YAFFS_OK;
}

yaffs_obj_t *yaffs_alloc_raw_obj(yaffs_dev_t *dev)
{
	yaffs_obj_t *obj = NULL;
	yaffs_Allocator *allocator = dev->allocator;

	if(!allocator) {
		YBUG();
		return obj;
	}

	/* If there are none left make more */
	if (!allocator->freeObjects)
		yaffs_create_free_objs(dev, YAFFS_ALLOCATION_NOBJECTS);

	if (allocator->freeObjects) {
		obj = allocator->freeObjects;
		allocator->freeObjects =
			(yaffs_obj_t *) (allocator->freeObjects->siblings.next);
		allocator->nFreeObjects--;
	}

	return obj;
}


void yaffs_free_raw_obj(yaffs_dev_t *dev, yaffs_obj_t *obj)
{

	yaffs_Allocator *allocator = dev->allocator;

	if(!allocator)
		YBUG();
	else {
		/* Link into the free list. */
		obj->siblings.next = (struct ylist_head *)(allocator->freeObjects);
		allocator->freeObjects = obj;
		allocator->nFreeObjects++;
	}
}

void yaffs_deinit_raw_tnodes_and_objs(yaffs_dev_t *dev)
{
	if(dev->allocator){
		yaffs_deinit_raw_tnodes(dev);
		yaffs_deinit_raw_objs(dev);

		YFREE(dev->allocator);
		dev->allocator=NULL;
	} else
		YBUG();
}

void yaffs_init_raw_tnodes_and_objs(yaffs_dev_t *dev)
{
	yaffs_Allocator *allocator;

	if(!dev->allocator){
		allocator = YMALLOC(sizeof(yaffs_Allocator));
		if(allocator){
			dev->allocator = allocator;
			yaffs_init_raw_tnodes(dev);
			yaffs_init_raw_objs(dev);
		}
	} else
		YBUG();
}


#endif