66ba5a6705fcf2eea2e8a22f8f1dac8c4944d6ac
[yaffs2.git] / yaffs_allocator.c
1 /*
2  * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3  *
4  * Copyright (C) 2002-2010 Aleph One Ltd.
5  *   for Toby Churchill Ltd and Brightstar Engineering
6  *
7  * Created by Charles Manning <charles@aleph1.co.uk>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  */
13
14
15
16 #include "yaffs_allocator.h"
17 #include "yaffs_guts.h"
18 #include "yaffs_trace.h"
19 #include "yportenv.h"
20
21 #ifdef CONFIG_YAFFS_YMALLOC_ALLOCATOR
22
23 void yaffs_deinit_raw_tnodes_and_objs(yaffs_dev_t *dev)
24 {
25         dev = dev;
26 }
27
28 void yaffs_init_raw_tnodes_and_objs(yaffs_dev_t *dev)
29 {
30         dev = dev;
31 }
32
33 yaffs_tnode_t *yaffs_alloc_raw_tnode(yaffs_dev_t *dev)
34 {
35         return (yaffs_tnode_t *)YMALLOC(dev->tnode_size);
36 }
37
38 void yaffs_free_raw_tnode(yaffs_dev_t *dev, yaffs_tnode_t *tn)
39 {
40         dev = dev;
41         YFREE(tn);
42 }
43
44 void yaffs_init_raw_objs(yaffs_dev_t *dev)
45 {
46         dev = dev;
47 }
48
49 void yaffs_deinit_raw_objs(yaffs_dev_t *dev)
50 {
51         dev = dev;
52 }
53
54 yaffs_obj_t *yaffs_alloc_raw_obj(yaffs_dev_t *dev)
55 {
56         dev = dev;
57         return (yaffs_obj_t *) YMALLOC(sizeof(yaffs_obj_t));
58 }
59
60
61 void yaffs_free_raw_obj(yaffs_dev_t *dev, yaffs_obj_t *obj)
62 {
63
64         dev = dev;
65         YFREE(obj);
66 }
67
68 #else
69
70 struct yaffs_tnode_list {
71         struct yaffs_tnode_list *next;
72         yaffs_tnode_t *tnodes;
73 };
74
75 typedef struct yaffs_tnode_list yaffs_tnodelist_t;
76
77 struct yaffs_obj_list_struct {
78         yaffs_obj_t *objects;
79         struct yaffs_obj_list_struct *next;
80 };
81
82 typedef struct yaffs_obj_list_struct yaffs_obj_list;
83
84
85 struct yaffs_allocator {
86         int n_tnodes_created;
87         yaffs_tnode_t *free_tnodes;
88         int n_free_tnodes;
89         yaffs_tnodelist_t *alloc_tnode_list;
90
91         int n_obj_created;
92         yaffs_obj_t *free_objs;
93         int n_free_objects;
94
95         yaffs_obj_list *allocated_obj_list;
96 };
97
98
99 static void yaffs_deinit_raw_tnodes(yaffs_dev_t *dev)
100 {
101
102         struct yaffs_allocator *allocator = (struct yaffs_allocator *)dev->allocator;
103
104         yaffs_tnodelist_t *tmp;
105
106         if(!allocator){
107                 YBUG();
108                 return;
109         }
110
111         while (allocator->alloc_tnode_list) {
112                 tmp = allocator->alloc_tnode_list->next;
113
114                 YFREE(allocator->alloc_tnode_list->tnodes);
115                 YFREE(allocator->alloc_tnode_list);
116                 allocator->alloc_tnode_list = tmp;
117
118         }
119
120         allocator->free_tnodes = NULL;
121         allocator->n_free_tnodes = 0;
122         allocator->n_tnodes_created = 0;
123 }
124
125 static void yaffs_init_raw_tnodes(yaffs_dev_t *dev)
126 {
127         struct yaffs_allocator *allocator = dev->allocator;
128
129         if(allocator){
130                 allocator->alloc_tnode_list = NULL;
131                 allocator->free_tnodes = NULL;
132                 allocator->n_free_tnodes = 0;
133                 allocator->n_tnodes_created = 0;
134         } else
135                 YBUG();
136 }
137
138 static int yaffs_create_tnodes(yaffs_dev_t *dev, int n_tnodes)
139 {
140         struct yaffs_allocator *allocator = (struct yaffs_allocator *)dev->allocator;
141         int i;
142         yaffs_tnode_t *new_tnodes;
143         __u8 *mem;
144         yaffs_tnode_t *curr;
145         yaffs_tnode_t *next;
146         yaffs_tnodelist_t *tnl;
147
148         if(!allocator){
149                 YBUG();
150                 return YAFFS_FAIL;
151         }
152
153         if (n_tnodes < 1)
154                 return YAFFS_OK;
155
156
157         /* make these things */
158
159         new_tnodes = YMALLOC(n_tnodes * dev->tnode_size);
160         mem = (__u8 *)new_tnodes;
161
162         if (!new_tnodes) {
163                 T(YAFFS_TRACE_ERROR,
164                         (TSTR("yaffs: Could not allocate Tnodes" TENDSTR)));
165                 return YAFFS_FAIL;
166         }
167
168         /* New hookup for wide tnodes */
169         for (i = 0; i < n_tnodes - 1; i++) {
170                 curr = (yaffs_tnode_t *) &mem[i * dev->tnode_size];
171                 next = (yaffs_tnode_t *) &mem[(i+1) * dev->tnode_size];
172                 curr->internal[0] = next;
173         }
174
175         curr = (yaffs_tnode_t *) &mem[(n_tnodes - 1) * dev->tnode_size];
176         curr->internal[0] = allocator->free_tnodes;
177         allocator->free_tnodes = (yaffs_tnode_t *)mem;
178
179         allocator->n_free_tnodes += n_tnodes;
180         allocator->n_tnodes_created += n_tnodes;
181
182         /* Now add this bunch of tnodes to a list for freeing up.
183          * NB If we can't add this to the management list it isn't fatal
184          * but it just means we can't free this bunch of tnodes later.
185          */
186
187         tnl = YMALLOC(sizeof(yaffs_tnodelist_t));
188         if (!tnl) {
189                 T(YAFFS_TRACE_ERROR,
190                   (TSTR
191                    ("yaffs: Could not add tnodes to management list" TENDSTR)));
192                    return YAFFS_FAIL;
193         } else {
194                 tnl->tnodes = new_tnodes;
195                 tnl->next = allocator->alloc_tnode_list;
196                 allocator->alloc_tnode_list = tnl;
197         }
198
199         T(YAFFS_TRACE_ALLOCATE, (TSTR("yaffs: Tnodes added" TENDSTR)));
200
201         return YAFFS_OK;
202 }
203
204
205 yaffs_tnode_t *yaffs_alloc_raw_tnode(yaffs_dev_t *dev)
206 {
207         struct yaffs_allocator *allocator = (struct yaffs_allocator *)dev->allocator;
208         yaffs_tnode_t *tn = NULL;
209
210         if(!allocator){
211                 YBUG();
212                 return NULL;
213         }
214
215         /* If there are none left make more */
216         if (!allocator->free_tnodes)
217                 yaffs_create_tnodes(dev, YAFFS_ALLOCATION_NTNODES);
218
219         if (allocator->free_tnodes) {
220                 tn = allocator->free_tnodes;
221                 allocator->free_tnodes = allocator->free_tnodes->internal[0];
222                 allocator->n_free_tnodes--;
223         }
224
225         return tn;
226 }
227
228 /* FreeTnode frees up a tnode and puts it back on the free list */
229 void yaffs_free_raw_tnode(yaffs_dev_t *dev, yaffs_tnode_t *tn)
230 {
231         struct yaffs_allocator *allocator = dev->allocator;
232
233         if(!allocator){
234                 YBUG();
235                 return;
236         }
237
238         if (tn) {
239                 tn->internal[0] = allocator->free_tnodes;
240                 allocator->free_tnodes = tn;
241                 allocator->n_free_tnodes++;
242         }
243         dev->checkpoint_blocks_required = 0; /* force recalculation*/
244 }
245
246
247
248 static void yaffs_init_raw_objs(yaffs_dev_t *dev)
249 {
250         struct yaffs_allocator *allocator = dev->allocator;
251
252         if(allocator) {
253                 allocator->allocated_obj_list = NULL;
254                 allocator->free_objs = NULL;
255                 allocator->n_free_objects = 0;
256         } else
257                 YBUG();
258 }
259
260 static void yaffs_deinit_raw_objs(yaffs_dev_t *dev)
261 {
262         struct yaffs_allocator *allocator = dev->allocator;
263         yaffs_obj_list *tmp;
264
265         if(!allocator){
266                 YBUG();
267                 return;
268         }
269
270         while (allocator->allocated_obj_list) {
271                 tmp = allocator->allocated_obj_list->next;
272                 YFREE(allocator->allocated_obj_list->objects);
273                 YFREE(allocator->allocated_obj_list);
274
275                 allocator->allocated_obj_list = tmp;
276         }
277
278         allocator->free_objs = NULL;
279         allocator->n_free_objects = 0;
280         allocator->n_obj_created = 0;
281 }
282
283
284 static int yaffs_create_free_objs(yaffs_dev_t *dev, int n_obj)
285 {
286         struct yaffs_allocator *allocator = dev->allocator;
287
288         int i;
289         yaffs_obj_t *new_objs;
290         yaffs_obj_list *list;
291
292         if(!allocator){
293                 YBUG();
294                 return YAFFS_FAIL;
295         }
296
297         if (n_obj < 1)
298                 return YAFFS_OK;
299
300         /* make these things */
301         new_objs = YMALLOC(n_obj * sizeof(yaffs_obj_t));
302         list = YMALLOC(sizeof(yaffs_obj_list));
303
304         if (!new_objs || !list) {
305                 if (new_objs){
306                         YFREE(new_objs);
307                         new_objs = NULL;
308                 }
309                 if (list){
310                         YFREE(list);
311                         list = NULL;
312                 }
313                 T(YAFFS_TRACE_ALLOCATE,
314                   (TSTR("yaffs: Could not allocate more objects" TENDSTR)));
315                 return YAFFS_FAIL;
316         }
317
318         /* Hook them into the free list */
319         for (i = 0; i < n_obj - 1; i++) {
320                 new_objs[i].siblings.next =
321                                 (struct ylist_head *)(&new_objs[i + 1]);
322         }
323
324         new_objs[n_obj - 1].siblings.next = (void *)allocator->free_objs;
325         allocator->free_objs = new_objs;
326         allocator->n_free_objects += n_obj;
327         allocator->n_obj_created += n_obj;
328
329         /* Now add this bunch of Objects to a list for freeing up. */
330
331         list->objects = new_objs;
332         list->next = allocator->allocated_obj_list;
333         allocator->allocated_obj_list = list;
334
335         return YAFFS_OK;
336 }
337
338 yaffs_obj_t *yaffs_alloc_raw_obj(yaffs_dev_t *dev)
339 {
340         yaffs_obj_t *obj = NULL;
341         struct yaffs_allocator *allocator = dev->allocator;
342
343         if(!allocator) {
344                 YBUG();
345                 return obj;
346         }
347
348         /* If there are none left make more */
349         if (!allocator->free_objs)
350                 yaffs_create_free_objs(dev, YAFFS_ALLOCATION_NOBJECTS);
351
352         if (allocator->free_objs) {
353                 obj = allocator->free_objs;
354                 allocator->free_objs =
355                         (yaffs_obj_t *) (allocator->free_objs->siblings.next);
356                 allocator->n_free_objects--;
357         }
358
359         return obj;
360 }
361
362
363 void yaffs_free_raw_obj(yaffs_dev_t *dev, yaffs_obj_t *obj)
364 {
365
366         struct yaffs_allocator *allocator = dev->allocator;
367
368         if(!allocator)
369                 YBUG();
370         else {
371                 /* Link into the free list. */
372                 obj->siblings.next = (struct ylist_head *)(allocator->free_objs);
373                 allocator->free_objs = obj;
374                 allocator->n_free_objects++;
375         }
376 }
377
378 void yaffs_deinit_raw_tnodes_and_objs(yaffs_dev_t *dev)
379 {
380         if(dev->allocator){
381                 yaffs_deinit_raw_tnodes(dev);
382                 yaffs_deinit_raw_objs(dev);
383
384                 YFREE(dev->allocator);
385                 dev->allocator=NULL;
386         } else
387                 YBUG();
388 }
389
390 void yaffs_init_raw_tnodes_and_objs(yaffs_dev_t *dev)
391 {
392         struct yaffs_allocator *allocator;
393
394         if(!dev->allocator){
395                 allocator = YMALLOC(sizeof(struct yaffs_allocator));
396                 if(allocator){
397                         dev->allocator = allocator;
398                         yaffs_init_raw_tnodes(dev);
399                         yaffs_init_raw_objs(dev);
400                 }
401         } else
402                 YBUG();
403 }
404
405
406 #endif