~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to storage/heap/hp_block.c

  • Committer: Monty Taylor
  • Date: 2008-11-16 06:29:53 UTC
  • mto: (584.1.9 devel)
  • mto: This revision was merged to the branch mainline in revision 589.
  • Revision ID: monty@inaugust.com-20081116062953-ivdltjmfe009b5fr
Moved stuff into item/

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
 
16
16
/* functions on blocks; Keys and records are saved in blocks */
17
17
 
18
 
#include "heap_priv.h"
19
 
 
20
 
#include <cstdlib>
 
18
#include "heapdef.h"
21
19
 
22
20
/*
23
21
  Find record according to record-position.
24
 
 
25
 
  The record is located by factoring position number pos into (p_0, p_1, ...)
 
22
      
 
23
  The record is located by factoring position number pos into (p_0, p_1, ...) 
26
24
  such that
27
25
     pos = SUM_i(block->level_info[i].records_under_level * p_i)
28
26
  {p_0, p_1, ...} serve as indexes to descend the blocks tree.
30
28
 
31
29
unsigned char *hp_find_block(HP_BLOCK *block, uint32_t pos)
32
30
{
33
 
  int i;
34
 
  HP_PTRS *ptr; /* block base ptr */
 
31
  register int i;
 
32
  register HP_PTRS *ptr; /* block base ptr */
35
33
 
36
 
  for (i= block->levels-1, ptr=block->root ; i > 0 ; i--)
 
34
  for (i=block->levels-1, ptr=block->root ; i > 0 ; i--)
37
35
  {
38
36
    ptr=(HP_PTRS*)ptr->blocks[pos/block->level_info[i].records_under_level];
39
37
    pos%=block->level_info[i].records_under_level;
49
47
    hp_get_new_block()
50
48
      block             HP_BLOCK tree-like block
51
49
      alloc_length OUT  Amount of memory allocated from the heap
52
 
 
53
 
  Interrupts are stopped to allow ha_panic in interrupts
 
50
      
 
51
  Interrupts are stopped to allow ha_panic in interrupts 
54
52
  RETURN
55
53
    0  OK
56
54
    1  Out of memory
58
56
 
59
57
int hp_get_new_block(HP_BLOCK *block, size_t *alloc_length)
60
58
{
61
 
  uint32_t i;
 
59
  register uint32_t i,j;
62
60
  HP_PTRS *root;
63
61
 
64
62
  for (i=0 ; i < block->levels ; i++)
67
65
 
68
66
  /*
69
67
    Allocate space for leaf block plus space for upper level blocks up to
70
 
    first level that has a free slot to put the pointer.
 
68
    first level that has a free slot to put the pointer. 
71
69
    In some cases we actually allocate more then we need:
72
70
    Consider e.g. a situation where we have one level 1 block and one level 0
73
 
    block, the level 0 block is full and this function is called. We only
74
 
    need a leaf block in this case. Nevertheless, we will get here with i=1
75
 
    and will also allocate sizeof(HP_PTRS) for non-leaf block and will never
 
71
    block, the level 0 block is full and this function is called. We only 
 
72
    need a leaf block in this case. Nevertheless, we will get here with i=1 
 
73
    and will also allocate sizeof(HP_PTRS) for non-leaf block and will never 
76
74
    use this space.
77
 
    This doesn't add much overhead - with current values of sizeof(HP_PTRS)
 
75
    This doesn't add much overhead - with current values of sizeof(HP_PTRS) 
78
76
    and my_default_record_cache_size we get about 1/128 unused memory.
79
77
   */
80
78
  *alloc_length=sizeof(HP_PTRS)*i+block->records_in_block* block->recbuffer;
81
 
  if (!(root=(HP_PTRS*) malloc(*alloc_length)))
 
79
  if (!(root=(HP_PTRS*) my_malloc(*alloc_length,MYF(MY_WME))))
82
80
    return 1;
83
81
 
84
82
  if (i == 0)
104
102
    block->level_info[i].last_blocks->
105
103
      blocks[HP_PTRS_IN_NOD - block->level_info[i].free_ptrs_in_block--]=
106
104
        (unsigned char*) root;
107
 
 
 
105
    
108
106
    /* Add a block subtree with each node having one left-most child */
109
 
    for (uint32_t j= i-1 ; j >0 ; j--)
 
107
    for (j=i-1 ; j >0 ; j--)
110
108
    {
111
109
      block->level_info[j].last_blocks= root++;
112
110
      block->level_info[j].last_blocks->blocks[0]=(unsigned char*) root;
113
111
      block->level_info[j].free_ptrs_in_block=HP_PTRS_IN_NOD-1;
114
112
    }
115
 
 
116
 
    /*
 
113
    
 
114
    /* 
117
115
      root now points to last (block->records_in_block* block->recbuffer)
118
116
      allocated bytes. Use it as a leaf block.
119
117
    */
127
125
 
128
126
unsigned char *hp_free_level(HP_BLOCK *block, uint32_t level, HP_PTRS *pos, unsigned char *last_pos)
129
127
{
130
 
  int max_pos;
 
128
  int i,max_pos;
131
129
  unsigned char *next_ptr;
132
130
 
133
131
  if (level == 1)
134
 
  {
135
132
    next_ptr=(unsigned char*) pos+block->recbuffer;
136
 
  }
137
133
  else
138
134
  {
139
135
    max_pos= (block->level_info[level-1].last_blocks == pos) ?
141
137
    HP_PTRS_IN_NOD;
142
138
 
143
139
    next_ptr=(unsigned char*) (pos+1);
144
 
    for (int i= 0; i < max_pos ; i++)
 
140
    for (i=0 ; i < max_pos ; i++)
145
141
      next_ptr=hp_free_level(block,level-1,
146
142
                              (HP_PTRS*) pos->blocks[i],next_ptr);
147
143
  }
148
 
 
149
144
  if ((unsigned char*) pos != last_pos)
150
145
  {
151
146
    free((unsigned char*) pos);