hp_block.c
Upload User: tsgydb
Upload Date: 2007-04-14
Package Size: 10674k
Code Size: 3k
Category:

MySQL

Development Platform:

Visual C++

  1. /* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
  2.    
  3.    This program is free software; you can redistribute it and/or modify
  4.    it under the terms of the GNU General Public License as published by
  5.    the Free Software Foundation; either version 2 of the License, or
  6.    (at your option) any later version.
  7.    
  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.
  12.    
  13.    You should have received a copy of the GNU General Public License
  14.    along with this program; if not, write to the Free Software
  15.    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
  16. /* functions on blocks; Keys and records are saved in blocks */
  17. #include "heapdef.h"
  18. /* Find record according to record-position */
  19. byte *_hp_find_block(HP_BLOCK *block, ulong pos)
  20. {
  21.   reg1 int i;
  22.   reg3 HP_PTRS *ptr;
  23.   for (i=block->levels-1, ptr=block->root ; i > 0 ; i--)
  24.   {
  25.     ptr=(HP_PTRS*)ptr->blocks[pos/block->level_info[i].records_under_level];
  26.     pos%=block->level_info[i].records_under_level;
  27.   }
  28.   return (byte*) ptr+ pos*block->recbuffer;
  29. }
  30. /* get one new block-of-records. Alloc ptr to block if neaded */
  31. /* Interrupts are stopped to allow ha_panic in interrupts */
  32. int _hp_get_new_block(HP_BLOCK *block, ulong *alloc_length)
  33. {
  34.   reg1 uint i,j;
  35.   HP_PTRS *root;
  36.   for (i=0 ; i < block->levels ; i++)
  37.     if (block->level_info[i].free_ptrs_in_block)
  38.       break;
  39.   *alloc_length=sizeof(HP_PTRS)*i+block->records_in_block* block->recbuffer;
  40.   if (!(root=(HP_PTRS*) my_malloc(*alloc_length,MYF(0))))
  41.     return 1;
  42.   if (i == 0)
  43.   {
  44.     block->levels=1;
  45.     block->root=block->level_info[0].last_blocks=root;
  46.   }
  47.   else
  48.   {
  49.     dont_break(); /* Dont allow SIGHUP or SIGINT */
  50.     if ((uint) i == block->levels)
  51.     {
  52.       block->levels=i+1;
  53.       block->level_info[i].free_ptrs_in_block=HP_PTRS_IN_NOD-1;
  54.       ((HP_PTRS**) root)[0]= block->root;
  55.       block->root=block->level_info[i].last_blocks= root++;
  56.     }
  57.     block->level_info[i].last_blocks->
  58.       blocks[HP_PTRS_IN_NOD - block->level_info[i].free_ptrs_in_block--]=
  59. (byte*) root;
  60.     for (j=i-1 ; j >0 ; j--)
  61.     {
  62.       block->level_info[j].last_blocks= root++;
  63.       block->level_info[j].last_blocks->blocks[0]=(byte*) root;
  64.       block->level_info[j].free_ptrs_in_block=HP_PTRS_IN_NOD-1;
  65.     }
  66.     block->level_info[0].last_blocks= root;
  67.     allow_break(); /* Allow SIGHUP & SIGINT */
  68.   }
  69.   return 0;
  70. }
  71. /* free all blocks under level */
  72. byte *_hp_free_level(HP_BLOCK *block, uint level, HP_PTRS *pos, byte *last_pos)
  73. {
  74.   int i,max_pos;
  75.   byte *next_ptr;
  76.   if (level == 1)
  77.     next_ptr=(byte*) pos+block->recbuffer;
  78.   else
  79.   {
  80.     max_pos= (block->level_info[level-1].last_blocks == pos) ?
  81.       HP_PTRS_IN_NOD - block->level_info[level-1].free_ptrs_in_block :
  82.     HP_PTRS_IN_NOD;
  83.     next_ptr=(byte*) (pos+1);
  84.     for (i=0 ; i < max_pos ; i++)
  85.       next_ptr=_hp_free_level(block,level-1,
  86.       (HP_PTRS*) pos->blocks[i],next_ptr);
  87.   }
  88.   if ((byte*) pos != last_pos)
  89.   {
  90.     my_free((gptr) pos,MYF(0));
  91.     return last_pos;
  92.   }
  93.   return next_ptr; /* next memory position */
  94. }