~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to storage/heap/hp_block.c

  • Committer: Monty Taylor
  • Date: 2008-08-02 01:03:15 UTC
  • mto: (236.1.42 codestyle)
  • mto: This revision was merged to the branch mainline in revision 261.
  • Revision ID: monty@inaugust.com-20080802010315-65h5938pymg9d99z
Moved m4 macros to top-level m4 dir, per GNU standards (and where gettext wanted it :)

Show diffs side-by-side

added added

removed removed

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