~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to storage/heap/hp_block.c

  • Committer: Stewart Smith
  • Date: 2008-07-13 06:56:15 UTC
  • mto: (210.1.1 drizzle)
  • mto: This revision was merged to the branch mainline in revision 211.
  • Revision ID: stewart@flamingspork.com-20080713065615-vzok75kgnnviokl9
Move MD5() into a UDF

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.
29
27
*/
30
28
 
31
 
unsigned char *hp_find_block(HP_BLOCK *block, uint32_t pos)
 
29
uchar *hp_find_block(HP_BLOCK *block, ulong pos)
32
30
{
33
31
  register int i;
34
32
  register HP_PTRS *ptr; /* block base ptr */
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;
40
38
  }
41
 
  return (unsigned char*) ptr+ pos*block->recbuffer;
 
39
  return (uchar*) ptr+ pos*block->recbuffer;
42
40
}
43
41
 
44
42
 
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
 
  register uint32_t i,j;
 
59
  register uint 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)
103
101
    /* Occupy the free slot we've found at level i */
104
102
    block->level_info[i].last_blocks->
105
103
      blocks[HP_PTRS_IN_NOD - block->level_info[i].free_ptrs_in_block--]=
106
 
        (unsigned char*) root;
107
 
 
 
104
        (uchar*) root;
 
105
    
108
106
    /* Add a block subtree with each node having one left-most child */
109
107
    for (j=i-1 ; j >0 ; j--)
110
108
    {
111
109
      block->level_info[j].last_blocks= root++;
112
 
      block->level_info[j].last_blocks->blocks[0]=(unsigned char*) root;
 
110
      block->level_info[j].last_blocks->blocks[0]=(uchar*) 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
    */
125
123
 
126
124
        /* free all blocks under level */
127
125
 
128
 
unsigned char *hp_free_level(HP_BLOCK *block, uint32_t level, HP_PTRS *pos, unsigned char *last_pos)
 
126
uchar *hp_free_level(HP_BLOCK *block, uint level, HP_PTRS *pos, uchar *last_pos)
129
127
{
130
128
  int i,max_pos;
131
 
  unsigned char *next_ptr;
 
129
  uchar *next_ptr;
132
130
 
133
131
  if (level == 1)
134
 
    next_ptr=(unsigned char*) pos+block->recbuffer;
 
132
    next_ptr=(uchar*) pos+block->recbuffer;
135
133
  else
136
134
  {
137
135
    max_pos= (block->level_info[level-1].last_blocks == pos) ?
138
136
      HP_PTRS_IN_NOD - block->level_info[level-1].free_ptrs_in_block :
139
137
    HP_PTRS_IN_NOD;
140
138
 
141
 
    next_ptr=(unsigned char*) (pos+1);
 
139
    next_ptr=(uchar*) (pos+1);
142
140
    for (i=0 ; i < max_pos ; i++)
143
141
      next_ptr=hp_free_level(block,level-1,
144
142
                              (HP_PTRS*) pos->blocks[i],next_ptr);
145
143
  }
146
 
  if ((unsigned char*) pos != last_pos)
 
144
  if ((uchar*) pos != last_pos)
147
145
  {
148
 
    free((unsigned char*) pos);
 
146
    my_free((uchar*) pos,MYF(0));
149
147
    return last_pos;
150
148
  }
151
149
  return next_ptr;                      /* next memory position */