Subversion Repositories HelenOS

Rev

Rev 2890 | Rev 2945 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2890 Rev 2944
Line 66... Line 66...
66
 
66
 
67
    /** Sorted list of intervals of freed indices. */
67
    /** Sorted list of intervals of freed indices. */
68
    link_t      freed_head;
68
    link_t      freed_head;
69
} unused_t;
69
} unused_t;
70
 
70
 
-
 
71
/** Futex protecting the list of unused structures. */
-
 
72
static futex_t unused_futex = FUTEX_INITIALIZER;
-
 
73
 
-
 
74
/** List of unused structures. */
71
static LIST_INITIALIZE(unused_head);
75
static LIST_INITIALIZE(unused_head);
72
 
76
 
73
/**
77
/**
74
 * Global hash table of all used fat_idx_t structures.
78
 * Global hash table of all used fat_idx_t structures.
75
 * The index structures are hashed by the dev_handle, parent node's first
79
 * The index structures are hashed by the dev_handle, parent node's first
Line 185... Line 189...
185
{
189
{
186
    link_t *l;
190
    link_t *l;
187
    unused_t *u;
191
    unused_t *u;
188
   
192
   
189
    assert(index);
193
    assert(index);
-
 
194
    futex_down(&unused_futex);
190
    for (l = unused_head.next; l != &unused_head; l = l->next) {
195
    for (l = unused_head.next; l != &unused_head; l = l->next) {
191
        u = list_get_instance(l, unused_t, link);
196
        u = list_get_instance(l, unused_t, link);
192
        if (u->dev_handle == dev_handle)
197
        if (u->dev_handle == dev_handle)
193
            goto hit;
198
            goto hit;
194
    }
199
    }
-
 
200
    futex_up(&unused_futex);
195
 
201
   
196
    /* dev_handle not found */
202
    /* dev_handle not found */
197
    return false;  
203
    return false;  
198
 
204
 
199
hit:
205
hit:
200
    if (list_empty(&u->freed_head)) {
206
    if (list_empty(&u->freed_head)) {
Line 203... Line 209...
203
             * There are no freed indices, allocate one directly
209
             * There are no freed indices, allocate one directly
204
             * from the counter.
210
             * from the counter.
205
             */
211
             */
206
            *index = u->next++;
212
            *index = u->next++;
207
            --u->remaining;
213
            --u->remaining;
-
 
214
            futex_up(&unused_futex);
208
            return true;
215
            return true;
209
        }
216
        }
210
    } else {
217
    } else {
211
        /* There are some freed indices which we can reuse. */
218
        /* There are some freed indices which we can reuse. */
212
        freed_t *f = list_get_instance(u->freed_head.next, freed_t,
219
        freed_t *f = list_get_instance(u->freed_head.next, freed_t,
Line 215... Line 222...
215
        if (f->first++ == f->last) {
222
        if (f->first++ == f->last) {
216
            /* Destroy the interval. */
223
            /* Destroy the interval. */
217
            list_remove(&f->link);
224
            list_remove(&f->link);
218
            free(f);
225
            free(f);
219
        }
226
        }
-
 
227
        futex_up(&unused_futex);
220
        return true;
228
        return true;
221
    }
229
    }
222
    /*
230
    /*
223
     * We ran out of indices, which is extremely unlikely with FAT16, but
231
     * We ran out of indices, which is extremely unlikely with FAT16, but
224
     * theoretically still possible (e.g. too many open unlinked nodes or
232
     * theoretically still possible (e.g. too many open unlinked nodes or
225
     * too many zero-sized nodes).
233
     * too many zero-sized nodes).
226
     */
234
     */
-
 
235
    futex_up(&unused_futex);
227
    return false;
236
    return false;
228
}
237
}
229
 
238
 
230
/** If possible, coalesce two intervals of freed indices. */
239
/** If possible, coalesce two intervals of freed indices. */
231
static void try_coalesce_intervals(link_t *l, link_t *r, link_t *cur)
240
static void try_coalesce_intervals(link_t *l, link_t *r, link_t *cur)
Line 250... Line 259...
250
static void fat_idx_free(dev_handle_t dev_handle, fs_index_t index)
259
static void fat_idx_free(dev_handle_t dev_handle, fs_index_t index)
251
{
260
{
252
    link_t *l;
261
    link_t *l;
253
    unused_t *u;
262
    unused_t *u;
254
 
263
 
-
 
264
    futex_down(&unused_futex);
255
    for (l = unused_head.next; l != &unused_head; l = l->next) {
265
    for (l = unused_head.next; l != &unused_head; l = l->next) {
256
        u = list_get_instance(l, unused_t, link);
266
        u = list_get_instance(l, unused_t, link);
257
        if (u->dev_handle == dev_handle)
267
        if (u->dev_handle == dev_handle)
258
            goto hit;
268
            goto hit;
259
    }
269
    }
-
 
270
    futex_up(&unused_futex);
260
 
271
 
261
    /* should not happen */
272
    /* should not happen */
262
    assert(0);
273
    assert(0);
263
 
274
 
264
hit:
275
hit:
265
    if (u->next == index + 1) {
276
    if (u->next == index + 1) {
266
        /* The index can be returned directly to the counter. */
277
        /* The index can be returned directly to the counter. */
267
        u->next--;
278
        u->next--;
268
        u->remaining++;
279
        u->remaining++;
269
        return;
-
 
270
    } else {
280
    } else {
271
        /*
281
        /*
272
         * The index must be returned either to an existing freed
282
         * The index must be returned either to an existing freed
273
         * interval or a new interval must be created.
283
         * interval or a new interval must be created.
274
         */
284
         */
Line 280... Line 290...
280
            if (f->first == index + 1) {
290
            if (f->first == index + 1) {
281
                f->first--;
291
                f->first--;
282
                if (lnk->prev != &u->freed_head)
292
                if (lnk->prev != &u->freed_head)
283
                    try_coalesce_intervals(lnk->prev, lnk,
293
                    try_coalesce_intervals(lnk->prev, lnk,
284
                        lnk);
294
                        lnk);
-
 
295
                futex_up(&unused_futex);
285
                return;
296
                return;
286
            }
297
            }
287
            if (f->last == index - 1) {
298
            if (f->last == index - 1) {
288
                f->last++;
299
                f->last++;
289
                if (lnk->next != &u->freed_head)
300
                if (lnk->next != &u->freed_head)
290
                    try_coalesce_intervals(lnk, lnk->next,
301
                    try_coalesce_intervals(lnk, lnk->next,
291
                        lnk);
302
                        lnk);
-
 
303
                futex_up(&unused_futex);
292
                return;
304
                return;
293
            }
305
            }
294
            if (index > f->first) {
306
            if (index > f->first) {
295
                n = malloc(sizeof(freed_t));
307
                n = malloc(sizeof(freed_t));
296
                /* TODO: sleep until allocation succeeds */
308
                /* TODO: sleep until allocation succeeds */
297
                assert(n);
309
                assert(n);
298
                link_initialize(&n->link);
310
                link_initialize(&n->link);
299
                n->first = index;
311
                n->first = index;
300
                n->last = index;
312
                n->last = index;
301
                list_insert_before(&n->link, lnk);
313
                list_insert_before(&n->link, lnk);
-
 
314
                futex_up(&unused_futex);
302
                return;
315
                return;
303
            }
316
            }
304
 
317
 
305
        }
318
        }
306
        /* The index will form the last interval. */
319
        /* The index will form the last interval. */
Line 310... Line 323...
310
        link_initialize(&n->link);
323
        link_initialize(&n->link);
311
        n->first = index;
324
        n->first = index;
312
        n->last = index;
325
        n->last = index;
313
        list_append(&n->link, &u->freed_head);
326
        list_append(&n->link, &u->freed_head);
314
    }
327
    }
-
 
328
    futex_up(&unused_futex);
315
}
329
}
316
 
330
 
317
fat_idx_t *
331
fat_idx_t *
318
fat_idx_get_by_pos(dev_handle_t dev_handle, fat_cluster_t pfc, unsigned pdi)
332
fat_idx_get_by_pos(dev_handle_t dev_handle, fat_cluster_t pfc, unsigned pdi)
319
{
333
{