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 /* New hookup for wide tnodes */
172 for (i = 0; i < nTnodes - 1; i++) {
173 curr = (yaffs_Tnode *) &mem[i * dev->tnodeSize];
174 next = (yaffs_Tnode *) &mem[(i+1) * dev->tnodeSize];
175 curr->internal[0] = next;
178 curr = (yaffs_Tnode *) &mem[(nTnodes - 1) * dev->tnodeSize];
179 curr->internal[0] = allocator->freeTnodes;
180 allocator->freeTnodes = (yaffs_Tnode *)mem;
182 allocator->nFreeTnodes += nTnodes;
183 allocator->nTnodesCreated += nTnodes;
185 /* Now add this bunch of tnodes to a list for freeing up.
186 * NB If we can't add this to the management list it isn't fatal
187 * but it just means we can't free this bunch of tnodes later.
190 tnl = YMALLOC(sizeof(yaffs_TnodeList));
194 ("yaffs: Could not add tnodes to management list" TENDSTR)));
197 tnl->tnodes = newTnodes;
198 tnl->next = allocator->allocatedTnodeList;
199 allocator->allocatedTnodeList = tnl;
202 T(YAFFS_TRACE_ALLOCATE, (TSTR("yaffs: Tnodes added" TENDSTR)));
208 yaffs_Tnode *yaffs_AllocateRawTnode(yaffs_Device *dev)
210 yaffs_Allocator *allocator = (yaffs_Allocator *)dev->allocator;
211 yaffs_Tnode *tn = NULL;
218 /* If there are none left make more */
219 if (!allocator->freeTnodes)
220 yaffs_CreateTnodes(dev, YAFFS_ALLOCATION_NTNODES);
222 if (allocator->freeTnodes) {
223 tn = allocator->freeTnodes;
224 allocator->freeTnodes = allocator->freeTnodes->internal[0];
225 allocator->nFreeTnodes--;
231 /* FreeTnode frees up a tnode and puts it back on the free list */
232 void yaffs_FreeRawTnode(yaffs_Device *dev, yaffs_Tnode *tn)
234 yaffs_Allocator *allocator = dev->allocator;
242 tn->internal[0] = allocator->freeTnodes;
243 allocator->freeTnodes = tn;
244 allocator->nFreeTnodes++;
246 dev->nCheckpointBlocksRequired = 0; /* force recalculation*/
251 static void yaffs_InitialiseRawObjects(yaffs_Device *dev)
253 yaffs_Allocator *allocator = dev->allocator;
256 allocator->allocatedObjectList = NULL;
257 allocator->freeObjects = NULL;
258 allocator->nFreeObjects = 0;
263 static void yaffs_DeinitialiseRawObjects(yaffs_Device *dev)
265 yaffs_Allocator *allocator = dev->allocator;
266 yaffs_ObjectList *tmp;
273 while (allocator->allocatedObjectList) {
274 tmp = allocator->allocatedObjectList->next;
275 YFREE(allocator->allocatedObjectList->objects);
276 YFREE(allocator->allocatedObjectList);
278 allocator->allocatedObjectList = tmp;
281 allocator->freeObjects = NULL;
282 allocator->nFreeObjects = 0;
283 allocator->nObjectsCreated = 0;
287 static int yaffs_CreateFreeObjects(yaffs_Device *dev, int nObjects)
289 yaffs_Allocator *allocator = dev->allocator;
292 yaffs_Object *newObjects;
293 yaffs_ObjectList *list;
303 /* make these things */
304 newObjects = YMALLOC(nObjects * sizeof(yaffs_Object));
305 list = YMALLOC(sizeof(yaffs_ObjectList));
307 if (!newObjects || !list) {
316 T(YAFFS_TRACE_ALLOCATE,
317 (TSTR("yaffs: Could not allocate more objects" TENDSTR)));
321 /* Hook them into the free list */
322 for (i = 0; i < nObjects - 1; i++) {
323 newObjects[i].siblings.next =
324 (struct ylist_head *)(&newObjects[i + 1]);
327 newObjects[nObjects - 1].siblings.next = (void *)allocator->freeObjects;
328 allocator->freeObjects = newObjects;
329 allocator->nFreeObjects += nObjects;
330 allocator->nObjectsCreated += nObjects;
332 /* Now add this bunch of Objects to a list for freeing up. */
334 list->objects = newObjects;
335 list->next = allocator->allocatedObjectList;
336 allocator->allocatedObjectList = list;
341 yaffs_Object *yaffs_AllocateRawObject(yaffs_Device *dev)
343 yaffs_Object *obj = NULL;
344 yaffs_Allocator *allocator = dev->allocator;
351 /* If there are none left make more */
352 if (!allocator->freeObjects)
353 yaffs_CreateFreeObjects(dev, YAFFS_ALLOCATION_NOBJECTS);
355 if (allocator->freeObjects) {
356 obj = allocator->freeObjects;
357 allocator->freeObjects =
358 (yaffs_Object *) (allocator->freeObjects->siblings.next);
359 allocator->nFreeObjects--;
366 void yaffs_FreeRawObject(yaffs_Device *dev, yaffs_Object *obj)
369 yaffs_Allocator *allocator = dev->allocator;
374 /* Link into the free list. */
375 obj->siblings.next = (struct ylist_head *)(allocator->freeObjects);
376 allocator->freeObjects = obj;
377 allocator->nFreeObjects++;
381 void yaffs_DeinitialiseRawTnodesAndObjects(yaffs_Device *dev)
384 yaffs_DeinitialiseRawTnodes(dev);
385 yaffs_DeinitialiseRawObjects(dev);
387 YFREE(dev->allocator);
393 void yaffs_InitialiseRawTnodesAndObjects(yaffs_Device *dev)
395 yaffs_Allocator *allocator;
398 allocator = YMALLOC(sizeof(yaffs_Allocator));
400 dev->allocator = allocator;
401 yaffs_InitialiseRawTnodes(dev);
402 yaffs_InitialiseRawObjects(dev);