Changeset 651 for soft


Ignore:
Timestamp:
Jul 22, 2015, 2:31:33 PM (9 years ago)
Author:
guerin
Message:

fat32: don't keep tree structure when releasing cache

This fixes a bunch of memleaks.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • soft/giet_vm/giet_fat32/fat32.c

    r650 r651  
    407407
    408408//////////////////////////////////////////////////////////////////////////////////
    409 // This recursive function scan one File-Cache (or Fat-Cache) from root to leaves.
    410 // The cache 64-Tree infrastructure is kept, but all memory allocated for 4 Kbytes
    411 // buffers, and for buffer descriptors (in leaves) is released.
    412 // The cache is identified by the "root" an "levels" arguments.
     409// This recursive function scans one File-Cache (or Fat-Cache) from root to
     410// leaves. All memory allocated for 4KB buffers, and buffer descriptors (in
     411// leaves) is released, along with the 64-Tree structure (root node is kept).
     412// The cache is identified by the "root" and "levels" arguments.
    413413// It should not contain any dirty clusters.
    414 // It returns 0 if success.
    415 // It returns 1 if error.
    416 //////////////////////////////////////////////////////////////////////////////////
    417 
    418 static unsigned int _release_cache_memory( fat_cache_node_t*  root,
    419                                            unsigned int       levels );
     414//////////////////////////////////////////////////////////////////////////////////
     415
     416static void _release_cache_memory( fat_cache_node_t*  root,
     417                                   unsigned int       levels );
    420418
    421419//////////////////////////////////////////////////////////////////////////////////
     
    13971395
    13981396///////////////////////////////////////////////////////////////////
    1399 static unsigned int _release_cache_memory( fat_cache_node_t*  root,
    1400                                            unsigned int       levels )
    1401 {
    1402     unsigned int index;
    1403     unsigned int ret = 0;
     1397static void _release_cache_memory( fat_cache_node_t*  root,
     1398                                   unsigned int       levels )
     1399{
     1400    unsigned int i;
    14041401
    14051402    if ( levels == 1 )  // last level => children are cluster descriptors
    14061403    {
    1407         for( index = 0 ; index < 64 ; index++ )
     1404        for( i = 0 ; i < 64 ; i++ )
    14081405        {
    1409             fat_cache_desc_t* pdesc = root->children[index];
     1406            fat_cache_desc_t* pdesc = root->children[i];
    14101407
    14111408            if ( pdesc != NULL )
     
    14131410                // check dirty
    14141411                if ( pdesc->dirty )
    1415                 {
    1416                     _printf("\n[FAT_ERROR] in _release_cache_memory() : dirty cluster\n");
    1417                     ret = 1;
    1418                 }
    1419                 else
    1420                 {
    1421                     _free( pdesc->buffer );
    1422                     _free( pdesc );
    1423                     root->children[index] = NULL;
    1424                 }
     1412                    _printf("\n[FAT ERROR] in _release_cache_memory() : dirty cluster\n");
     1413
     1414                _free( pdesc->buffer );
     1415                _free( pdesc );
     1416                root->children[i] = NULL;
    14251417            }
    14261418        }
     
    14281420    else               // not the last level = recursive call on each children
    14291421    {
    1430         for( index = 0 ; index < 64 ; index++ )
     1422        for( i = 0 ; i < 64 ; i++ )
    14311423        {
    1432             fat_cache_node_t*    cnode = root->children[index];
    1433 
    1434             if ( cnode != NULL ) ret   = _release_cache_memory( root->children[index],
    1435                                                                 levels - 1 );
    1436         }
    1437     }
    1438     return ret;
     1424            fat_cache_node_t* cnode = root->children[i];
     1425
     1426            if ( cnode != NULL )
     1427            {
     1428                _release_cache_memory( cnode, levels - 1 );
     1429                _free( cnode );
     1430                root->children[i] = NULL;
     1431            }
     1432        }
     1433    }
    14391434}  // end _release_cache_memory()
    14401435
     
    24002395
    24012396    // release File-Cache
    2402     if ( _release_cache_memory( inode->cache,
    2403                                 inode->levels ) ) return 1;
     2397    _release_cache_memory( inode->cache, inode->levels );
     2398    _free ( inode->cache );
    24042399
    24052400    // remove inode from Inode-Tree
     
    30053000// in the file descriptors array.
    30063001// If the reference count is zero, it writes all dirty clusters on block device,
    3007 // and releases the memory allocated to the file_cache: The cache 64-Tree
    3008 // infrastructure (depending on file size) is kept, but all buffers and all
    3009 // buffer descriptors are released.
     3002// and releases the memory allocated to the File_Cache.
    30103003/////////////////////////////////////////////////////////////////////////////////
    30113004// Returns 0 on success.
     
    30153008//  -3  : "File not open"
    30163009//  -4  : "Cannot update DATA regions"
    3017 //  -5  : "Cannot release memory"
    30183010/////////////////////////////////////////////////////////////////////////////////
    30193011int _fat_close( unsigned int fd_id )
     
    30913083#endif
    30923084
    3093         // release memory allocated to File-Cache
    3094         if ( _release_cache_memory( inode->cache,
    3095                                     inode-> levels ) )
    3096         {
    3097             _spin_lock_release( &_fat.fat_lock );
    3098             _printf("\n[FAT ERROR] in _fat_close() : cannot release cache memory "
    3099                     "for file <%s>\n", _fat.fd[fd_id].inode->name );
    3100             return -5;
    3101         }
     3085        // release memory allocated to File-Cache (keep cache root node)
     3086        _release_cache_memory( inode->cache, inode->levels );
    31023087
    31033088#if GIET_DEBUG_FAT
Note: See TracChangeset for help on using the changeset viewer.