home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 1 / FFMCD01.bin / useful / dist / gnu / gdbm / gdbm-1.6-amiga / falloc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-19  |  11.4 KB  |  401 lines

  1. /* falloc.c - The file space management routines for dbm. */
  2.  
  3. /*  This file is part of GDBM, the GNU data base manager, by Philip A. Nelson.
  4.     Copyright (C) 1990, 1991, 1993  Free Software Foundation, Inc.
  5.  
  6.     GDBM is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; either version 2, or (at your option)
  9.     any later version.
  10.  
  11.     GDBM is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with GDBM; see the file COPYING.  If not, write to
  18.     the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20.     You may contact the author by:
  21.        e-mail:  phil@cs.wwu.edu
  22.       us-mail:  Philip A. Nelson
  23.                 Computer Science Department
  24.                 Western Washington University
  25.                 Bellingham, WA 98226
  26.        
  27. *************************************************************************/
  28.  
  29. #include "gdbmdefs.h"
  30.  
  31.  
  32. /* The forward definitions for this file.  See the functions for
  33.    the definition of the function. */
  34.  
  35. static avail_elem get_elem ();
  36. static avail_elem get_block ();
  37. static push_avail_block ();
  38. static pop_avail_block ();
  39. static adjust_bucket_avail ();
  40.  
  41. /* Allocate space in the file DBF for a block NUM_BYTES in length.  Return
  42.    the file address of the start of the block.  
  43.  
  44.    Each hash bucket has a fixed size avail table.  We first check this
  45.    avail table to satisfy the request for space.  In most cases we can
  46.    and this causes changes to be only in the current hash bucket.
  47.    Allocation is done on a first fit basis from the entries.  If a
  48.    request can not be satisfied from the current hash bucket, then it is
  49.    satisfied from the file header avail block.  If nothing is there that
  50.    has enough space, another block at the end of the file is allocated
  51.    and the unused portion is returned to the avail block.  This routine
  52.    "guarantees" that an allocation does not cross a block boundary unless
  53.    the size is larger than a single block.  The avail structure is
  54.    changed by this routine if a change is needed.  If an error occurs,
  55.    the value of 0 will be returned.  */
  56.  
  57. long
  58. _gdbm_alloc (dbf, num_bytes)
  59.      gdbm_file_info *dbf;
  60.      int num_bytes;
  61. {
  62.   long  file_adr;        /* The address of the block. */
  63.   avail_elem av_el;        /* For temporary use. */
  64.  
  65.   /* The current bucket is the first place to look for space. */
  66.   av_el = get_elem (num_bytes, dbf->bucket->bucket_avail,
  67.             &dbf->bucket->av_count);
  68.  
  69.   /* If we did not find some space, we have more work to do. */
  70.   if (av_el.av_size == 0)
  71.     {
  72.      /* Is the header avail block empty and there is something on the stack. */
  73.       if (dbf->header->avail.count == 0 && dbf->header->avail.next_block != 0)
  74.     pop_avail_block (dbf);
  75.       
  76.       /* Get another full block from end of file. */
  77.       av_el = get_block (num_bytes, dbf);
  78.  
  79.       dbf->header_changed = TRUE;
  80.     }
  81.  
  82.   /* We now have the place from which we will allocate the new space. */
  83.   file_adr = av_el.av_adr;
  84.  
  85.   /* Put the unused space back in the avail block. */
  86.   av_el.av_adr += num_bytes;
  87.   av_el.av_size -= num_bytes;
  88.   _gdbm_free (dbf, av_el.av_adr, av_el.av_size);
  89.  
  90.   /* Return the address. */
  91.   return file_adr;
  92.   
  93. }
  94.  
  95.  
  96.  
  97. /* Free space of size NUM_BYTES in the file DBF at file address FILE_ADR.  Make
  98.    it avaliable for reuse through _gdbm_alloc.  This routine changes the
  99.    avail structure.  The value TRUE is returned if there were errors.  If no
  100.    errors occured, the value FALSE is returned. */
  101.  
  102. _gdbm_free (dbf, file_adr, num_bytes)
  103.      gdbm_file_info *dbf;
  104.      long file_adr;
  105.      int num_bytes;
  106. {
  107.   avail_elem temp;
  108.  
  109.   /* Is it too small to worry about? */
  110.   if (num_bytes <= IGNORE_SIZE)
  111.     return;
  112.  
  113.   /* Initialize the avail element. */
  114.   temp.av_size = num_bytes;
  115.   temp.av_adr = file_adr;
  116.  
  117.   /* Is the freed space large or small? */
  118.   if (num_bytes >= dbf->header->block_size)
  119.     {
  120.       if (dbf->header->avail.count == dbf->header->avail.size)
  121.     {
  122.       push_avail_block (dbf);
  123.     }
  124.       _gdbm_put_av_elem (temp, dbf->header->avail.av_table,
  125.              &dbf->header->avail.count);
  126.       dbf->header_changed = TRUE;
  127.     }
  128.   else
  129.     {
  130.       /* Try to put into the current bucket. */
  131.       if (dbf->bucket->av_count < BUCKET_AVAIL)
  132.     _gdbm_put_av_elem (temp, dbf->bucket->bucket_avail,
  133.                &dbf->bucket->av_count);
  134.       else
  135.     {
  136.       if (dbf->header->avail.count == dbf->header->avail.size)
  137.         {
  138.           push_avail_block (dbf);
  139.         }
  140.       _gdbm_put_av_elem (temp, dbf->header->avail.av_table,
  141.                  &dbf->header->avail.count);
  142.       dbf->header_changed = TRUE;
  143.     }
  144.     }
  145.  
  146.   if (dbf->header_changed)
  147.     adjust_bucket_avail (dbf);
  148.  
  149.   /* All work is done. */
  150.   return;
  151. }
  152.  
  153.  
  154.  
  155. /* The following are all utility routines needed by the previous two. */
  156.  
  157.  
  158. /* Gets the avail block at the top of the stack and loads it into the
  159.    active avail block.  It does a "free" for itself! */
  160.  
  161. static
  162. pop_avail_block (dbf)
  163.      gdbm_file_info *dbf;
  164. {
  165.   int  num_bytes;        /* For use with the read system call. */
  166.   long file_pos;        /* For use with the lseek system call. */
  167.   avail_elem temp;
  168.  
  169.   /* Set up variables. */
  170.   temp.av_adr = dbf->header->avail.next_block;
  171.   temp.av_size = ( ( (dbf->header->avail.size * sizeof (avail_elem)) >> 1)
  172.           + sizeof (avail_block));
  173.  
  174.   /* Read the block. */
  175.   file_pos = lseek (dbf->desc, temp.av_adr, L_SET);
  176.   if (file_pos != temp.av_adr)  _gdbm_fatal (dbf, "lseek error");
  177.   num_bytes = read (dbf->desc, &dbf->header->avail, temp.av_size);
  178.   if (num_bytes != temp.av_size) _gdbm_fatal (dbf, "read error");
  179.  
  180.   /* We changed the header. */
  181.   dbf->header_changed = TRUE;
  182.  
  183.   /* Free the previous avail block. */
  184.   _gdbm_put_av_elem (temp, dbf->header->avail.av_table,
  185.              &dbf->header->avail.count);
  186. }
  187.  
  188.  
  189. /* Splits the header avail block and pushes half onto the avail stack. */
  190.  
  191. static
  192. push_avail_block (dbf)
  193.      gdbm_file_info *dbf;
  194. {
  195.   int  num_bytes;
  196.   int  av_size;
  197.   int  av_adr;
  198.   int  index;
  199.   long file_pos;
  200.   avail_block *temp;
  201.   avail_elem  new_loc;
  202.  
  203.  
  204.   /* Caclulate the size of the split block. */
  205.   av_size = ( (dbf->header->avail.size * sizeof (avail_elem)) >> 1)
  206.             + sizeof (avail_block);
  207.  
  208.   /* Get address in file for new av_size bytes. */
  209.   new_loc = get_elem (av_size, dbf->header->avail.av_table,
  210.               &dbf->header->avail.count);
  211.   if (new_loc.av_size == 0)
  212.     new_loc = get_block (av_size, dbf);
  213.   av_adr = new_loc.av_adr;
  214.  
  215.  
  216.   /* Split the header block. */
  217.   temp = (avail_block *) alloca (av_size);
  218.   /* Set the size to be correct AFTER the pop_avail_block. */
  219.   temp->size = dbf->header->avail.size;
  220.   temp->count = 0;
  221.   temp->next_block = dbf->header->avail.next_block;
  222.   dbf->header->avail.next_block = av_adr;
  223.   for (index = 1; index < dbf->header->avail.count; index++)
  224.     if ( (index & 0x1) == 1)    /* Index is odd. */
  225.       temp->av_table[temp->count++] = dbf->header->avail.av_table[index];
  226.     else
  227.       dbf->header->avail.av_table[index>>1]
  228.     = dbf->header->avail.av_table[index];
  229.  
  230.   /* Update the header avail count to previous size divided by 2. */
  231.   dbf->header->avail.count >>= 1;
  232.  
  233.   /* Free the unneeded space. */
  234.   new_loc.av_adr += av_size;
  235.   new_loc.av_size -= av_size;
  236.   _gdbm_free (dbf, new_loc.av_adr, new_loc.av_size);
  237.  
  238.   /* Update the disk. */
  239.   file_pos = lseek (dbf->desc, av_adr, L_SET);
  240.   if (file_pos != av_adr) _gdbm_fatal (dbf, "lseek error");
  241.   num_bytes = write (dbf->desc, temp, av_size);
  242.   if (num_bytes != av_size) _gdbm_fatal (dbf, "write error");
  243.  
  244. }
  245.  
  246.  
  247.  
  248. /* Get_elem returns an element in the AV_TABLE block which is
  249.    larger than SIZE.  AV_COUNT is the number of elements in the
  250.    AV_TABLE.  If an item is found, it extracts it from the AV_TABLE
  251.    and moves the other elements up to fill the space. If no block is 
  252.    found larger than SIZE, get_elem returns a size of zero.  This
  253.    routine does no I/O. */
  254.  
  255. static avail_elem
  256. get_elem (size, av_table, av_count)
  257.      int size;
  258.      avail_elem av_table[];
  259.      int *av_count;
  260. {
  261.   int index;            /* For searching through the avail block. */
  262.   avail_elem val;        /* The default return value. */
  263.  
  264.   /* Initialize default return value. */
  265.   val.av_adr = 0;
  266.   val.av_size = 0;
  267.  
  268.   /* Search for element.  List is sorted by size. */
  269.   index = 0;
  270.   while (index < *av_count && av_table[index].av_size < size)
  271.     {
  272.       index++;
  273.     }
  274.  
  275.   /* Did we find one of the right size? */
  276.   if (index >= *av_count)
  277.     return val;
  278.  
  279.   /* Ok, save that element and move all others up one. */
  280.   val = av_table[index];
  281.   *av_count -= 1;
  282.   while (index < *av_count)
  283.     {
  284.       av_table[index] = av_table[index+1];
  285.       index++;
  286.     }
  287.  
  288.   return val;
  289. }
  290.  
  291.  
  292. /* This routine inserts a single NEW_EL into the AV_TABLE block in
  293.    sorted order. This routine does no I/O. */
  294.  
  295. _gdbm_put_av_elem (new_el, av_table, av_count)
  296.      avail_elem new_el;
  297.      avail_elem av_table[];
  298.      int *av_count;
  299. {
  300.   int index;            /* For searching through the avail block. */
  301.   int index1;
  302.  
  303.   /* Is it too small to deal with? */
  304.   if (new_el.av_size <= IGNORE_SIZE)
  305.     return FALSE; 
  306.  
  307.   /* Search for place to put element.  List is sorted by size. */
  308.   index = 0;
  309.   while (index < *av_count && av_table[index].av_size < new_el.av_size)
  310.     {
  311.       index++;
  312.     }
  313.  
  314.   /* Move all others up one. */
  315.   index1 = *av_count-1;
  316.   while (index1 >= index)
  317.     {
  318.       av_table[index1+1] = av_table[index1];
  319.       index1--;
  320.     }
  321.   
  322.   /* Add the new element. */
  323.   av_table[index] = new_el;
  324.  
  325.   /* Increment the number of elements. */
  326.   *av_count += 1;  
  327.  
  328.   return TRUE;
  329. }
  330.  
  331.  
  332.  
  333. /* Get_block "allocates" new file space and the end of the file.  This is
  334.    done in integral block sizes.  (This helps insure that data smaller than
  335.    one block size is in a single block.)  Enough blocks are allocated to
  336.    make sure the number of bytes allocated in the blocks is larger than SIZE.
  337.    DBF contains the file header that needs updating.  This routine does
  338.    no I/O.  */
  339.  
  340. static avail_elem
  341. get_block (size, dbf)
  342.      int size;
  343.      gdbm_file_info *dbf;
  344. {
  345.   avail_elem val;
  346.  
  347.   /* Need at least one block. */
  348.   val.av_adr  = dbf->header->next_block;
  349.   val.av_size = dbf->header->block_size;
  350.  
  351.   /* Get enough blocks to fit the need. */
  352.   while (val.av_size < size)
  353.     val.av_size += dbf->header->block_size;
  354.  
  355.   /* Update the header and return. */
  356.   dbf->header->next_block += val.av_size;
  357.  
  358.   /* We changed the header. */
  359.   dbf->header_changed = TRUE;
  360.  
  361.   return val;
  362.   
  363. }
  364.  
  365.  
  366. /*  When the header already needs writing, we can make sure the current
  367.     bucket has its avail block as close to 1/2 full as possible. */
  368. static
  369. adjust_bucket_avail (dbf)
  370.      gdbm_file_info *dbf;
  371. {
  372.   int third = BUCKET_AVAIL / 3;
  373.   avail_elem av_el;
  374.  
  375.   /* Can we add more entries to the bucket? */
  376.   if (dbf->bucket->av_count < third)
  377.     {
  378.       if (dbf->header->avail.count > 0)
  379.     {
  380.       dbf->header->avail.count -= 1;
  381.       av_el = dbf->header->avail.av_table[dbf->header->avail.count];
  382.       _gdbm_put_av_elem (av_el, dbf->bucket->bucket_avail,
  383.                  &dbf->bucket->av_count);
  384.       dbf->bucket_changed = TRUE;
  385.     }
  386.       return;
  387.     }
  388.  
  389.   /* Is there too much in the bucket? */
  390.   while (dbf->bucket->av_count > BUCKET_AVAIL-third
  391.      && dbf->header->avail.count < dbf->header->avail.size)
  392.     {
  393.       av_el = get_elem (0, dbf->bucket->bucket_avail, &dbf->bucket->av_count);
  394.       _gdbm_put_av_elem (av_el, dbf->header->avail.av_table,
  395.              &dbf->header->avail.count);
  396.       dbf->bucket_changed = TRUE;
  397.     }
  398. }
  399.  
  400.  
  401.