home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / gnu / gdbm-1.7.1.tar.gz / gdbm-1.7.1.tar / gdbm-1.7.1 / falloc.c < prev    next >
C/C++ Source or Header  |  1993-11-13  |  12KB  |  412 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.  
  30. /* AIX demands this be the very first thing in the file. */
  31. #if !defined(__GNUC__) && defined(_AIX)
  32.  #pragma alloca
  33. #endif
  34.  
  35. /* include system configuration before all else. */
  36. #include "autoconf.h"
  37.  
  38. #include "gdbmdefs.h"
  39.  
  40.  
  41. /* The forward definitions for this file.  See the functions for
  42.    the definition of the function. */
  43.  
  44. static avail_elem get_elem _ARGS((int, avail_elem [], int *));
  45. static avail_elem get_block _ARGS((int, gdbm_file_info *));
  46. static void push_avail_block _ARGS((gdbm_file_info *));
  47. static void pop_avail_block _ARGS((gdbm_file_info *));
  48. static void adjust_bucket_avail _ARGS((gdbm_file_info *));
  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. off_t
  67. _gdbm_alloc (dbf, num_bytes)
  68.      gdbm_file_info *dbf;
  69.      int num_bytes;
  70. {
  71.   off_t 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. void
  112. _gdbm_free (dbf, file_adr, num_bytes)
  113.      gdbm_file_info *dbf;
  114.      off_t 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;
  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;
  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.   off_t 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.   off_t av_adr;
  208.   int  index;
  209.   off_t 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.   /* Set the size to be correct AFTER the pop_avail_block. */
  229.   temp->size = dbf->header->avail.size;
  230.   temp->count = 0;
  231.   temp->next_block = dbf->header->avail.next_block;
  232.   dbf->header->avail.next_block = av_adr;
  233.   for (index = 1; index < dbf->header->avail.count; index++)
  234.     if ( (index & 0x1) == 1)    /* Index is odd. */
  235.       temp->av_table[temp->count++] = dbf->header->avail.av_table[index];
  236.     else
  237.       dbf->header->avail.av_table[index>>1]
  238.     = dbf->header->avail.av_table[index];
  239.  
  240.   /* Update the header avail count to previous size divided by 2. */
  241.   dbf->header->avail.count >>= 1;
  242.  
  243.   /* Free the unneeded space. */
  244.   new_loc.av_adr += av_size;
  245.   new_loc.av_size -= av_size;
  246.   _gdbm_free (dbf, new_loc.av_adr, new_loc.av_size);
  247.  
  248.   /* Update the disk. */
  249.   file_pos = lseek (dbf->desc, av_adr, L_SET);
  250.   if (file_pos != av_adr) _gdbm_fatal (dbf, "lseek error");
  251.   num_bytes = write (dbf->desc, temp, av_size);
  252.   if (num_bytes != av_size) _gdbm_fatal (dbf, "write error");
  253.  
  254. }
  255.  
  256.  
  257.  
  258. /* Get_elem returns an element in the AV_TABLE block which is
  259.    larger than SIZE.  AV_COUNT is the number of elements in the
  260.    AV_TABLE.  If an item is found, it extracts it from the AV_TABLE
  261.    and moves the other elements up to fill the space. If no block is 
  262.    found larger than SIZE, get_elem returns a size of zero.  This
  263.    routine does no I/O. */
  264.  
  265. static avail_elem
  266. get_elem (size, av_table, av_count)
  267.      int size;
  268.      avail_elem av_table[];
  269.      int *av_count;
  270. {
  271.   int index;            /* For searching through the avail block. */
  272.   avail_elem val;        /* The default return value. */
  273.  
  274.   /* Initialize default return value. */
  275.   val.av_adr = 0;
  276.   val.av_size = 0;
  277.  
  278.   /* Search for element.  List is sorted by size. */
  279.   index = 0;
  280.   while (index < *av_count && av_table[index].av_size < size)
  281.     {
  282.       index++;
  283.     }
  284.  
  285.   /* Did we find one of the right size? */
  286.   if (index >= *av_count)
  287.     return val;
  288.  
  289.   /* Ok, save that element and move all others up one. */
  290.   val = av_table[index];
  291.   *av_count -= 1;
  292.   while (index < *av_count)
  293.     {
  294.       av_table[index] = av_table[index+1];
  295.       index++;
  296.     }
  297.  
  298.   return val;
  299. }
  300.  
  301.  
  302. /* This routine inserts a single NEW_EL into the AV_TABLE block in
  303.    sorted order. This routine does no I/O. */
  304.  
  305. int
  306. _gdbm_put_av_elem (new_el, av_table, av_count)
  307.      avail_elem new_el;
  308.      avail_elem av_table[];
  309.      int *av_count;
  310. {
  311.   int index;            /* For searching through the avail block. */
  312.   int index1;
  313.  
  314.   /* Is it too small to deal with? */
  315.   if (new_el.av_size <= IGNORE_SIZE)
  316.     return FALSE; 
  317.  
  318.   /* Search for place to put element.  List is sorted by size. */
  319.   index = 0;
  320.   while (index < *av_count && av_table[index].av_size < new_el.av_size)
  321.     {
  322.       index++;
  323.     }
  324.  
  325.   /* Move all others up one. */
  326.   index1 = *av_count-1;
  327.   while (index1 >= index)
  328.     {
  329.       av_table[index1+1] = av_table[index1];
  330.       index1--;
  331.     }
  332.   
  333.   /* Add the new element. */
  334.   av_table[index] = new_el;
  335.  
  336.   /* Increment the number of elements. */
  337.   *av_count += 1;  
  338.  
  339.   return TRUE;
  340. }
  341.  
  342.  
  343.  
  344. /* Get_block "allocates" new file space and the end of the file.  This is
  345.    done in integral block sizes.  (This helps insure that data smaller than
  346.    one block size is in a single block.)  Enough blocks are allocated to
  347.    make sure the number of bytes allocated in the blocks is larger than SIZE.
  348.    DBF contains the file header that needs updating.  This routine does
  349.    no I/O.  */
  350.  
  351. static avail_elem
  352. get_block (size, dbf)
  353.      int size;
  354.      gdbm_file_info *dbf;
  355. {
  356.   avail_elem val;
  357.  
  358.   /* Need at least one block. */
  359.   val.av_adr  = dbf->header->next_block;
  360.   val.av_size = dbf->header->block_size;
  361.  
  362.   /* Get enough blocks to fit the need. */
  363.   while (val.av_size < size)
  364.     val.av_size += dbf->header->block_size;
  365.  
  366.   /* Update the header and return. */
  367.   dbf->header->next_block += val.av_size;
  368.  
  369.   /* We changed the header. */
  370.   dbf->header_changed = TRUE;
  371.  
  372.   return val;
  373.   
  374. }
  375.  
  376.  
  377. /*  When the header already needs writing, we can make sure the current
  378.     bucket has its avail block as close to 1/2 full as possible. */
  379. static void
  380. adjust_bucket_avail (dbf)
  381.      gdbm_file_info *dbf;
  382. {
  383.   int third = BUCKET_AVAIL / 3;
  384.   avail_elem av_el;
  385.  
  386.   /* Can we add more entries to the bucket? */
  387.   if (dbf->bucket->av_count < third)
  388.     {
  389.       if (dbf->header->avail.count > 0)
  390.     {
  391.       dbf->header->avail.count -= 1;
  392.       av_el = dbf->header->avail.av_table[dbf->header->avail.count];
  393.       _gdbm_put_av_elem (av_el, dbf->bucket->bucket_avail,
  394.                  &dbf->bucket->av_count);
  395.       dbf->bucket_changed = TRUE;
  396.     }
  397.       return;
  398.     }
  399.  
  400.   /* Is there too much in the bucket? */
  401.   while (dbf->bucket->av_count > BUCKET_AVAIL-third
  402.      && dbf->header->avail.count < dbf->header->avail.size)
  403.     {
  404.       av_el = get_elem (0, dbf->bucket->bucket_avail, &dbf->bucket->av_count);
  405.       _gdbm_put_av_elem (av_el, dbf->header->avail.av_table,
  406.              &dbf->header->avail.count);
  407.       dbf->bucket_changed = TRUE;
  408.     }
  409. }
  410.  
  411.  
  412.