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