2 * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
4 * Copyright (C) 2002-2010 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
7 * Created by Charles Manning <charles@aleph1.co.uk>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License version 2.1 as
11 * published by the Free Software Foundation.
13 * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
17 #include "yaffs_allocator.h"
18 #include "yaffs_guts.h"
19 #include "yaffs_trace.h"
22 #ifdef CONFIG_YAFFS_YMALLOC_ALLOCATOR
24 void yaffs_DeinitialiseRawTnodesAndObjects(yaffs_Device *dev)
29 void yaffs_InitialiseRawTnodesAndObjects(yaffs_Device *dev)
34 yaffs_Tnode *yaffs_AllocateRawTnode(yaffs_Device *dev)
36 return (yaffs_Tnode *)YMALLOC(dev->tnodeSize);
39 void yaffs_FreeRawTnode(yaffs_Device *dev, yaffs_Tnode *tn)
45 void yaffs_InitialiseRawObjects(yaffs_Device *dev)
50 void yaffs_DeinitialiseRawObjects(yaffs_Device *dev)
55 yaffs_Object *yaffs_AllocateRawObject(yaffs_Device *dev)
58 return (yaffs_Object *) YMALLOC(sizeof(yaffs_Object));
62 void yaffs_FreeRawObject(yaffs_Device *dev, yaffs_Object *obj)
71 struct yaffs_TnodeList_struct {
72 struct yaffs_TnodeList_struct *next;
76 typedef struct yaffs_TnodeList_struct yaffs_TnodeList;
78 struct yaffs_ObjectList_struct {
79 yaffs_Object *objects;
80 struct yaffs_ObjectList_struct *next;
83 typedef struct yaffs_ObjectList_struct yaffs_ObjectList;
86 struct yaffs_AllocatorStruct {
88 yaffs_Tnode *freeTnodes;
90 yaffs_TnodeList *allocatedTnodeList;
93 yaffs_Object *freeObjects;
96 yaffs_ObjectList *allocatedObjectList;
99 typedef struct yaffs_AllocatorStruct yaffs_Allocator;
102 static void yaffs_DeinitialiseRawTnodes(yaffs_Device *dev)
105 yaffs_Allocator *allocator = (yaffs_Allocator *)dev->allocator;
107 yaffs_TnodeList *tmp;
114 while (allocator->allocatedTnodeList) {
115 tmp = allocator->allocatedTnodeList->next;
117 YFREE(allocator->allocatedTnodeList->tnodes);
118 YFREE(allocator->allocatedTnodeList);
119 allocator->allocatedTnodeList = tmp;
123 allocator->freeTnodes = NULL;
124 allocator->nFreeTnodes = 0;
125 allocator->nTnodesCreated = 0;
128 static void yaffs_InitialiseRawTnodes(yaffs_Device *dev)
130 yaffs_Allocator *allocator = dev->allocator;
133 allocator->allocatedTnodeList = NULL;
134 allocator->freeTnodes = NULL;
135 allocator->nFreeTnodes = 0;
136 allocator->nTnodesCreated = 0;
141 static int yaffs_CreateTnodes(yaffs_Device *dev, int nTnodes)
143 yaffs_Allocator *allocator = (yaffs_Allocator *)dev->allocator;
145 yaffs_Tnode *newTnodes;
149 yaffs_TnodeList *tnl;
160 /* make these things */
162 newTnodes = YMALLOC(nTnodes * dev->tnodeSize);
163 mem = (__u8 *)newTnodes;
167 (TSTR("yaffs: Could not allocate Tnodes" TENDSTR)));
171 /* Hook them into the free list */
173 for (i = 0; i < nTnodes - 1; i++) {
174 newTnodes[i].internal[0] = &newTnodes[i + 1];
175 #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
176 newTnodes[i].internal[YAFFS_NTNODES_INTERNAL] = (void *)1;
180 newTnodes[nTnodes - 1].internal[0] = allocator->freeTnodes;
181 #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
182 newTnodes[nTnodes - 1].internal[YAFFS_NTNODES_INTERNAL] = (void *)1;
184 allocator->freeTnodes = newTnodes;
186 /* New hookup for wide tnodes */
187 for (i = 0; i < nTnodes - 1; i++) {
188 curr = (yaffs_Tnode *) &mem[i * dev->tnodeSize];
189 next = (yaffs_Tnode *) &mem[(i+1) * dev->tnodeSize];
190 curr->internal[0] = next;
193 curr = (yaffs_Tnode *) &mem[(nTnodes - 1) * dev->tnodeSize];
194 curr->internal[0] = allocator->freeTnodes;
195 allocator->freeTnodes = (yaffs_Tnode *)mem;
200 allocator->nFreeTnodes += nTnodes;
201 allocator->nTnodesCreated += nTnodes;
203 /* Now add this bunch of tnodes to a list for freeing up.
204 * NB If we can't add this to the management list it isn't fatal
205 * but it just means we can't free this bunch of tnodes later.
208 tnl = YMALLOC(sizeof(yaffs_TnodeList));
212 ("yaffs: Could not add tnodes to management list" TENDSTR)));
215 tnl->tnodes = newTnodes;
216 tnl->next = allocator->allocatedTnodeList;
217 allocator->allocatedTnodeList = tnl;
220 T(YAFFS_TRACE_ALLOCATE, (TSTR("yaffs: Tnodes added" TENDSTR)));
226 yaffs_Tnode *yaffs_AllocateRawTnode(yaffs_Device *dev)
228 yaffs_Allocator *allocator = (yaffs_Allocator *)dev->allocator;
229 yaffs_Tnode *tn = NULL;
236 /* If there are none left make more */
237 if (!allocator->freeTnodes)
238 yaffs_CreateTnodes(dev, YAFFS_ALLOCATION_NTNODES);
240 if (allocator->freeTnodes) {
241 tn = allocator->freeTnodes;
242 #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
243 if (tn->internal[YAFFS_NTNODES_INTERNAL] != (void *)1) {
244 /* Hoosterman, this thing looks like it isn't in the list */
245 T(YAFFS_TRACE_ALWAYS,
246 (TSTR("yaffs: Tnode list bug 1" TENDSTR)));
249 allocator->freeTnodes = allocator->freeTnodes->internal[0];
250 allocator->nFreeTnodes--;
256 /* FreeTnode frees up a tnode and puts it back on the free list */
257 void yaffs_FreeRawTnode(yaffs_Device *dev, yaffs_Tnode *tn)
259 yaffs_Allocator *allocator = dev->allocator;
267 #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
268 if (tn->internal[YAFFS_NTNODES_INTERNAL] != 0) {
269 /* Hoosterman, this thing looks like it is already in the list */
270 T(YAFFS_TRACE_ALWAYS,
271 (TSTR("yaffs: Tnode list bug 2" TENDSTR)));
273 tn->internal[YAFFS_NTNODES_INTERNAL] = (void *)1;
275 tn->internal[0] = allocator->freeTnodes;
276 allocator->freeTnodes = tn;
277 allocator->nFreeTnodes++;
279 dev->nCheckpointBlocksRequired = 0; /* force recalculation*/
284 static void yaffs_InitialiseRawObjects(yaffs_Device *dev)
286 yaffs_Allocator *allocator = dev->allocator;
289 allocator->allocatedObjectList = NULL;
290 allocator->freeObjects = NULL;
291 allocator->nFreeObjects = 0;
296 static void yaffs_DeinitialiseRawObjects(yaffs_Device *dev)
298 yaffs_Allocator *allocator = dev->allocator;
299 yaffs_ObjectList *tmp;
306 while (allocator->allocatedObjectList) {
307 tmp = allocator->allocatedObjectList->next;
308 YFREE(allocator->allocatedObjectList->objects);
309 YFREE(allocator->allocatedObjectList);
311 allocator->allocatedObjectList = tmp;
314 allocator->freeObjects = NULL;
315 allocator->nFreeObjects = 0;
316 allocator->nObjectsCreated = 0;
320 static int yaffs_CreateFreeObjects(yaffs_Device *dev, int nObjects)
322 yaffs_Allocator *allocator = dev->allocator;
325 yaffs_Object *newObjects;
326 yaffs_ObjectList *list;
336 /* make these things */
337 newObjects = YMALLOC(nObjects * sizeof(yaffs_Object));
338 list = YMALLOC(sizeof(yaffs_ObjectList));
340 if (!newObjects || !list) {
349 T(YAFFS_TRACE_ALLOCATE,
350 (TSTR("yaffs: Could not allocate more objects" TENDSTR)));
354 /* Hook them into the free list */
355 for (i = 0; i < nObjects - 1; i++) {
356 newObjects[i].siblings.next =
357 (struct ylist_head *)(&newObjects[i + 1]);
360 newObjects[nObjects - 1].siblings.next = (void *)allocator->freeObjects;
361 allocator->freeObjects = newObjects;
362 allocator->nFreeObjects += nObjects;
363 allocator->nObjectsCreated += nObjects;
365 /* Now add this bunch of Objects to a list for freeing up. */
367 list->objects = newObjects;
368 list->next = allocator->allocatedObjectList;
369 allocator->allocatedObjectList = list;
374 yaffs_Object *yaffs_AllocateRawObject(yaffs_Device *dev)
376 yaffs_Object *obj = NULL;
377 yaffs_Allocator *allocator = dev->allocator;
384 /* If there are none left make more */
385 if (!allocator->freeObjects)
386 yaffs_CreateFreeObjects(dev, YAFFS_ALLOCATION_NOBJECTS);
388 if (allocator->freeObjects) {
389 obj = allocator->freeObjects;
390 allocator->freeObjects =
391 (yaffs_Object *) (allocator->freeObjects->siblings.next);
392 allocator->nFreeObjects--;
399 void yaffs_FreeRawObject(yaffs_Device *dev, yaffs_Object *obj)
402 yaffs_Allocator *allocator = dev->allocator;
407 /* Link into the free list. */
408 obj->siblings.next = (struct ylist_head *)(allocator->freeObjects);
409 allocator->freeObjects = obj;
410 allocator->nFreeObjects++;
414 void yaffs_DeinitialiseRawTnodesAndObjects(yaffs_Device *dev)
417 yaffs_DeinitialiseRawTnodes(dev);
418 yaffs_DeinitialiseRawObjects(dev);
420 YFREE(dev->allocator);
426 void yaffs_InitialiseRawTnodesAndObjects(yaffs_Device *dev)
428 yaffs_Allocator *allocator;
431 allocator = YMALLOC(sizeof(yaffs_Allocator));
433 dev->allocator = allocator;
434 yaffs_InitialiseRawTnodes(dev);
435 yaffs_InitialiseRawObjects(dev);