summaryrefslogtreecommitdiff
path: root/heap/hp_block.c
blob: 510d89d6d1e25e9b142da85ec406013741d6cdae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

/* functions on blocks; Keys and records are saved in blocks */

#include "heapdef.h"

	/* Find record according to record-position */

byte *_hp_find_block(HP_BLOCK *block, ulong pos)
{
  reg1 int i;
  reg3 HP_PTRS *ptr;

  for (i=block->levels-1, ptr=block->root ; i > 0 ; i--)
  {
    ptr=(HP_PTRS*)ptr->blocks[pos/block->level_info[i].records_under_level];
    pos%=block->level_info[i].records_under_level;
  }
  return (byte*) ptr+ pos*block->recbuffer;
}


	/* get one new block-of-records. Alloc ptr to block if neaded */
	/* Interrupts are stopped to allow ha_panic in interrupts */

int _hp_get_new_block(HP_BLOCK *block, ulong *alloc_length)
{
  reg1 uint i,j;
  HP_PTRS *root;

  for (i=0 ; i < block->levels ; i++)
    if (block->level_info[i].free_ptrs_in_block)
      break;

  *alloc_length=sizeof(HP_PTRS)*i+block->records_in_block* block->recbuffer;
  if (!(root=(HP_PTRS*) my_malloc(*alloc_length,MYF(0))))
    return 1;

  if (i == 0)
  {
    block->levels=1;
    block->root=block->level_info[0].last_blocks=root;
  }
  else
  {
    dont_break();		/* Dont allow SIGHUP or SIGINT */
    if ((uint) i == block->levels)
    {
      block->levels=i+1;
      block->level_info[i].free_ptrs_in_block=HP_PTRS_IN_NOD-1;
      ((HP_PTRS**) root)[0]= block->root;
      block->root=block->level_info[i].last_blocks= root++;
    }
    block->level_info[i].last_blocks->
      blocks[HP_PTRS_IN_NOD - block->level_info[i].free_ptrs_in_block--]=
	(byte*) root;

    for (j=i-1 ; j >0 ; j--)
    {
      block->level_info[j].last_blocks= root++;
      block->level_info[j].last_blocks->blocks[0]=(byte*) root;
      block->level_info[j].free_ptrs_in_block=HP_PTRS_IN_NOD-1;
    }
    block->level_info[0].last_blocks= root;
    allow_break();		/* Allow SIGHUP & SIGINT */
  }
  return 0;
}


	/* free all blocks under level */

byte *_hp_free_level(HP_BLOCK *block, uint level, HP_PTRS *pos, byte *last_pos)
{
  int i,max_pos;
  byte *next_ptr;

  if (level == 1)
    next_ptr=(byte*) pos+block->recbuffer;
  else
  {
    max_pos= (block->level_info[level-1].last_blocks == pos) ?
      HP_PTRS_IN_NOD - block->level_info[level-1].free_ptrs_in_block :
    HP_PTRS_IN_NOD;

    next_ptr=(byte*) (pos+1);
    for (i=0 ; i < max_pos ; i++)
      next_ptr=_hp_free_level(block,level-1,
			      (HP_PTRS*) pos->blocks[i],next_ptr);
  }
  if ((byte*) pos != last_pos)
  {
    my_free((gptr) pos,MYF(0));
    return last_pos;
  }
  return next_ptr;			/* next memory position */
}