[1025] in Coldmud discussion meeting

root meeting help first previous next last

[COLD] binarydb.c patch

daemon@ATHENA.MIT.EDU (Mon Jul 8 16:10:33 1996 )

Date: Sat, 6 Jul 1996 07:52:24 +0200
From: silovic@srce.hr (Miroslav Silovic)
To: coldstuff@tombstone.sunrem.com


This patch should fix the binary database bloat. There could be some
slowdown with VERY many objects in the db (could become noticable on
object creation, with about 25-100 MB database). This code needs some
testing, in other words. :) Good luck!


*** binarydb_old.c	Wed Jul  3 01:01:52 1996
--- binarydb.c	Sat Jul  6 07:33:33 1996
***************
*** 321,358 ****
  
  static int db_alloc(int size)
  {
      int blocks_needed, b, count, starting_block, over_the_top;
  
      b = last_free;
      blocks_needed = NEEDED(size, BLOCK_SIZE);
      over_the_top = 0;
  
      while (1) {
  	if (b >= bitmap_blocks) {
  	    /* Only wrap around once. */
  	    if (!over_the_top) {
  		b = 0;
  		over_the_top = 1;
  	    } else {
  		grow_bitmap(b + DB_BITBLOCK);
  	    }
  	}
  
  	starting_block = b;
  
  	for (count = 0; count < blocks_needed; count++) {
  	    if (bitmap[b >> 3] & (1 << (b & 7)))
  		break;
  	    b++;
  	    if (b >= bitmap_blocks)
! 		grow_bitmap(b + DB_BITBLOCK);
  	}
  
  	if (count == blocks_needed) {
  	    /* Mark these blocks taken and return the starting block. */
  	    for (b = starting_block; b < starting_block + count; b++)
  		bitmap[b >> 3] |= (1 << (b & 7));
  	    last_free = b;
  	    return starting_block;
  	}
  
--- 321,374 ----
  
  static int db_alloc(int size)
  {
      int blocks_needed, b, count, starting_block, over_the_top;
  
      b = last_free;
      blocks_needed = NEEDED(size, BLOCK_SIZE);
      over_the_top = 0;
  
      while (1) {
+ 
+ 	if (b < bitmap_blocks && bitmap[b >> 3] == (char)255) {
+ 	    /* 8 full blocks. Let's run away from this! */
+ 	    b = (b & ~7) + 8;
+ 	    while (b < bitmap_blocks && bitmap[b >> 3] == (char)255) {
+ 		b += 8;
+ 	    }
+ 	}
+ 
  	if (b >= bitmap_blocks) {
  	    /* Only wrap around once. */
  	    if (!over_the_top) {
  		b = 0;
  		over_the_top = 1;
+ 		continue;
  	    } else {
  		grow_bitmap(b + DB_BITBLOCK);
  	    }
  	}
  
  	starting_block = b;
  
  	for (count = 0; count < blocks_needed; count++) {
  	    if (bitmap[b >> 3] & (1 << (b & 7)))
  		break;
  	    b++;
  	    if (b >= bitmap_blocks)
! 		/* time to wrap around if we still haven't */
! 		if (!over_the_top) {
! 		    b=0;
! 		    over_the_top=1;
! 		    break;
! 		} else
! 		    grow_bitmap(b + DB_BITBLOCK);
  	}
  
  	if (count == blocks_needed) {
  	    /* Mark these blocks taken and return the starting block. */
  	    for (b = starting_block; b < starting_block + count; b++)
  		bitmap[b >> 3] |= (1 << (b & 7));
  	    last_free = b;
  	    return starting_block;
  	}