Changeset 106 for trunk/lib


Ignore:
Timestamp:
04/18/08 00:16:51 (16 years ago)
Author:
tim
Message:

replaced linked list hbin lookup with range_list based binary search

Location:
trunk/lib
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/range_list.c

    r100 r106  
    148148{
    149149  uint32_t i;
     150
     151  if(rl == NULL)
     152    return;
     153
    150154  for(i=0; i < rl->size; i++)
    151155    free(rl->elements[i]);
  • trunk/lib/regfi.c

    r105 r106  
    413413
    414414/*******************************************************************
    415  Input a random offset and receive the correpsonding HBIN
    416  block for it
    417 *******************************************************************/
    418 static bool hbin_contains_offset( REGF_HBIN *hbin, uint32 offset )
    419 {
    420   if ( !hbin )
     415 * Given an offset and an hbin, is the offset within that hbin?
     416 * The offset is a virtual file offset.
     417 *******************************************************************/
     418static bool regfi_offset_in_hbin(REGF_HBIN* hbin, uint32 offset)
     419{
     420  if(!hbin)
    421421    return false;
    422        
    423   if ( (offset > hbin->first_hbin_off) && (offset < (hbin->first_hbin_off+hbin->block_size)) )
     422
     423  if((offset > hbin->first_hbin_off)
     424     && (offset < (hbin->first_hbin_off + hbin->block_size)))
    424425    return true;
    425426               
     
    428429
    429430
     431
    430432/*******************************************************************
    431  Input a random offset and receive the correpsonding HBIN
    432  block for it
    433 *******************************************************************/
    434 static REGF_HBIN* lookup_hbin_block( REGF_FILE *file, uint32 offset )
    435 {
    436   REGF_HBIN *hbin = NULL;
    437   uint32 block_off;
    438 
    439   /* start with the open list */
    440 
    441   for ( hbin=file->block_list; hbin; hbin=hbin->next ) {
    442     /* DEBUG(10,("lookup_hbin_block: address = 0x%x [0x%x]\n", hbin->file_off, (uint32)hbin ));*/
    443     if ( hbin_contains_offset( hbin, offset ) )
    444       return hbin;
    445   }
    446        
    447   if ( !hbin ) {
    448     /* start at the beginning */
    449 
    450     block_off = REGF_BLOCKSIZE;
    451     do {
    452       hbin = regfi_parse_hbin(file, block_off, true, false);
    453 
    454       if ( hbin )
    455         block_off = hbin->file_off + hbin->block_size;
    456 
    457     } while ( hbin && !hbin_contains_offset( hbin, offset ) );
    458   }
    459 
    460   if ( hbin )
    461     /* XXX: this kind of caching needs to be re-evaluated */
    462     DLIST_ADD( file->block_list, hbin );
    463 
    464   return hbin;
     433 * Given a virtual offset, and receive the correpsonding HBIN
     434 * block for it.  NULL if one doesn't exist.
     435 *******************************************************************/
     436static REGF_HBIN* regfi_lookup_hbin(REGF_FILE* file, uint32 offset)
     437{
     438  return (REGF_HBIN*)range_list_find_data(file->hbins, offset+REGF_BLOCKSIZE);
    465439}
    466440
     
    683657    vk_raw_offset = IVAL(buf, i*4);
    684658   
    685     sub_hbin = lookup_hbin_block(file, vk_raw_offset);
     659    sub_hbin = regfi_lookup_hbin(file, vk_raw_offset);
    686660    if (!sub_hbin)
    687661    {
     
    740714
    741715/*******************************************************************
     716 * TODO: Need to add full key and SK record caching using a
     717 *       custom cache structure.
    742718 *******************************************************************/
    743719REGF_NK_REC* regfi_load_key(REGF_FILE *file, uint32 offset, bool strict)
     
    748724  uint32 max_length, off;
    749725
    750   hbin = lookup_hbin_block(file, offset-REGF_BLOCKSIZE);
     726  hbin = regfi_lookup_hbin(file, offset-REGF_BLOCKSIZE);
    751727  if (hbin == NULL)
    752728    return NULL;
     
    761737  {
    762738    sub_hbin = hbin;
    763     if(!hbin_contains_offset(hbin, nk->values_off))
    764       sub_hbin = lookup_hbin_block(file, nk->values_off);
     739    if(!regfi_offset_in_hbin(hbin, nk->values_off))
     740      sub_hbin = regfi_lookup_hbin(file, nk->values_off);
    765741   
    766742    if(sub_hbin == NULL)
     
    792768  {
    793769    sub_hbin = hbin;
    794     if(!hbin_contains_offset(hbin, nk->subkeys_off))
    795       sub_hbin = lookup_hbin_block(file, nk->subkeys_off);
     770    if(!regfi_offset_in_hbin(hbin, nk->subkeys_off))
     771      sub_hbin = regfi_lookup_hbin(file, nk->subkeys_off);
    796772
    797773    if (sub_hbin == NULL)
     
    825801  {
    826802    sub_hbin = hbin;
    827     if(!hbin_contains_offset(hbin, nk->sk_off))
    828       sub_hbin = lookup_hbin_block(file, nk->sk_off);
     803    if(!regfi_offset_in_hbin(hbin, nk->sk_off))
     804      sub_hbin = regfi_lookup_hbin(file, nk->sk_off);
    829805
    830806    if(sub_hbin == NULL)
     
    910886{
    911887  REGF_FILE* rb;
     888  REGF_HBIN* hbin = NULL;
     889  uint32 hbin_off;
    912890  int fd;
    913891  int flags = O_RDONLY;
     892  bool rla;
    914893
    915894  /* open an existing file */
     
    932911  if((rb->hbins == NULL) || (rb->unalloc_cells == NULL))
    933912  {
     913    range_list_free(rb->hbins);
     914    range_list_free(rb->unalloc_cells);
    934915    close(fd);
    935916    free(rb);
    936917    return NULL;
     918  }
     919
     920  rla = true;
     921  hbin_off = REGF_BLOCKSIZE;
     922  hbin = regfi_parse_hbin(rb, hbin_off, true, false);
     923  while(hbin && rla)
     924  {
     925    hbin_off = hbin->file_off + hbin->block_size;
     926    rla = range_list_add(rb->hbins, hbin->file_off, hbin->block_size, hbin);
     927    hbin = regfi_parse_hbin(rb, hbin_off, true, false);
    937928  }
    938929
     
    947938{
    948939  int fd;
     940  uint32 i;
    949941
    950942  /* nothing to do if there is no open file */
     
    954946  fd = file->fd;
    955947  file->fd = -1;
     948  for(i=0; i < range_list_size(file->hbins); i++)
     949    free(range_list_get(file->hbins, i)->data);
    956950  range_list_free(file->hbins);
     951
     952  for(i=0; i < range_list_size(file->unalloc_cells); i++)
     953    free(range_list_get(file->unalloc_cells, i)->data);
    957954  range_list_free(file->unalloc_cells);
     955
    958956  free(file);
    959957
    960   return close( fd );
     958  return close(fd);
    961959}
    962960
     
    13211319}
    13221320
    1323 
    1324 
    1325 /****************/
    1326 /* Experimental */
    1327 /****************/
    1328 /*
    1329 typedef struct {
    1330   uint32 offset;
    1331   uint32 size;
    1332 } REGFI_CELL_INFO;
    1333 
    1334 typedef struct {
    1335   uint32 count
    1336   REGFI_CELL_INFO** cells;
    1337 } REGFI_CELL_LIST;
    1338 */
    13391321
    13401322
Note: See TracChangeset for help on using the changeset viewer.