~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/heap/hp_block.cc

  • Committer: Brian Aker
  • Date: 2010-02-10 18:04:24 UTC
  • mfrom: (1286.1.5 build)
  • Revision ID: brian@gaz-20100210180424-03ypoyifmlc2lgcp
Merge of Brian/Padraig

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