~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/memory/root.cc

  • Committer: Monty Taylor
  • Date: 2009-12-25 08:08:25 UTC
  • mto: This revision was merged to the branch mainline in revision 1255.
  • Revision ID: mordred@inaugust.com-20091225080825-r7qmhm2mswl0c4li
We have init_sql_alloc and init_alloc_root - and I can't tell the difference.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
using namespace std;
24
24
using namespace drizzled;
25
25
 
 
26
static const unsigned int MAX_BLOCK_TO_DROP= 4096;
 
27
static const unsigned int MAX_BLOCK_USAGE_BEFORE_DROP= 10;
 
28
 
26
29
/*
27
30
  Initialize memory root
28
31
 
32
35
      block_size     - size of chunks (blocks) used for memory allocation
33
36
                       (It is external size of chunk i.e. it should include
34
37
                        memory required for internal structures, thus it
35
 
                        should be no less than ALLOC_ROOT_MIN_BLOCK_SIZE)
36
 
      pre_alloc_size - if non-0, then size of block that should be
37
 
                       pre-allocated during memory root initialization.
 
38
                        should be no less than memory::ROOT_MIN_BLOCK_SIZE)
38
39
 
39
40
  DESCRIPTION
40
41
    This function prepares memory root for further use, sets initial size of
44
45
    reported as error in first alloc_root() on this memory root.
45
46
*/
46
47
 
47
 
void init_alloc_root(MEM_ROOT *mem_root, size_t block_size,
48
 
                     size_t )
 
48
void init_alloc_root(MEM_ROOT *mem_root, size_t block_size)
49
49
{
50
50
  mem_root->free= mem_root->used= mem_root->pre_alloc= 0;
51
51
  mem_root->min_malloc= 32;
52
 
  mem_root->block_size= block_size - ALLOC_ROOT_MIN_BLOCK_SIZE;
 
52
  mem_root->block_size= block_size - memory::ROOT_MIN_BLOCK_SIZE;
53
53
  mem_root->error_handler= 0;
54
54
  mem_root->block_num= 4;                       /* We shift this with >>2 */
55
55
  mem_root->first_block_usage= 0;
80
80
{
81
81
  assert(alloc_root_inited(mem_root));
82
82
 
83
 
  mem_root->block_size= block_size - ALLOC_ROOT_MIN_BLOCK_SIZE;
 
83
  mem_root->block_size= block_size - memory::ROOT_MIN_BLOCK_SIZE;
84
84
  if (pre_alloc_size)
85
85
  {
86
 
    size_t size= pre_alloc_size + ALIGN_SIZE(sizeof(USED_MEM));
 
86
    size_t size= pre_alloc_size + ALIGN_SIZE(sizeof(memory::internal::UsedMemory));
87
87
    if (!mem_root->pre_alloc || mem_root->pre_alloc->size != size)
88
88
    {
89
 
      USED_MEM *mem, **prev= &mem_root->free;
 
89
      memory::internal::UsedMemory *mem, **prev= &mem_root->free;
90
90
      /*
91
91
        Free unused blocks, so that consequent calls
92
92
        to reset_root_defaults won't eat away memory.
100
100
          mem_root->pre_alloc= mem;
101
101
          return;
102
102
        }
103
 
        if (mem->left + ALIGN_SIZE(sizeof(USED_MEM)) == mem->size)
 
103
        if (mem->left + ALIGN_SIZE(sizeof(memory::internal::UsedMemory)) == mem->size)
104
104
        {
105
105
          /* remove block from the list and free it */
106
106
          *prev= mem->next;
110
110
          prev= &mem->next;
111
111
      }
112
112
      /* Allocate new prealloc block and add it to the end of free list */
113
 
      if ((mem= (USED_MEM *) malloc(size)))
 
113
      if ((mem= static_cast<memory::internal::UsedMemory *>(malloc(size))))
114
114
      {
115
115
        mem->size= size;
116
116
        mem->left= pre_alloc_size;
134
134
{
135
135
  size_t get_size, block_size;
136
136
  unsigned char* point;
137
 
  register USED_MEM *next= 0;
138
 
  register USED_MEM **prev;
 
137
  memory::internal::UsedMemory *next= NULL;
 
138
  memory::internal::UsedMemory **prev;
139
139
  assert(alloc_root_inited(mem_root));
140
140
 
141
141
  length= ALIGN_SIZE(length);
142
142
  if ((*(prev= &mem_root->free)) != NULL)
143
143
  {
144
144
    if ((*prev)->left < length &&
145
 
        mem_root->first_block_usage++ >= ALLOC_MAX_BLOCK_USAGE_BEFORE_DROP &&
146
 
        (*prev)->left < ALLOC_MAX_BLOCK_TO_DROP)
 
145
        mem_root->first_block_usage++ >= MAX_BLOCK_USAGE_BEFORE_DROP &&
 
146
        (*prev)->left < MAX_BLOCK_TO_DROP)
147
147
    {
148
148
      next= *prev;
149
149
      *prev= next->next;                        /* Remove block from list */
157
157
  if (! next)
158
158
  {                                             /* Time to alloc new block */
159
159
    block_size= mem_root->block_size * (mem_root->block_num >> 2);
160
 
    get_size= length+ALIGN_SIZE(sizeof(USED_MEM));
 
160
    get_size= length+ALIGN_SIZE(sizeof(memory::internal::UsedMemory));
161
161
    get_size= max(get_size, block_size);
162
162
 
163
 
    if (!(next = (USED_MEM*) malloc(get_size)))
 
163
    if (!(next = static_cast<memory::internal::UsedMemory *>(malloc(get_size))))
164
164
    {
165
165
      if (mem_root->error_handler)
166
166
        (*mem_root->error_handler)();
167
 
      return((void*) 0);
 
167
      return NULL;
168
168
    }
169
169
    mem_root->block_num++;
170
170
    next->next= *prev;
171
171
    next->size= get_size;
172
 
    next->left= get_size-ALIGN_SIZE(sizeof(USED_MEM));
 
172
    next->left= get_size-ALIGN_SIZE(sizeof(memory::internal::UsedMemory));
173
173
    *prev=next;
174
174
  }
175
175
 
241
241
 
242
242
static inline void mark_blocks_free(MEM_ROOT* root)
243
243
{
244
 
  register USED_MEM *next;
245
 
  register USED_MEM **last;
 
244
  memory::internal::UsedMemory *next;
 
245
  memory::internal::UsedMemory **last;
246
246
 
247
247
  /* iterate through (partially) free blocks, mark them free */
248
248
  last= &root->free;
249
249
  for (next= root->free; next; next= *(last= &next->next))
250
250
  {
251
 
    next->left= next->size - ALIGN_SIZE(sizeof(USED_MEM));
 
251
    next->left= next->size - ALIGN_SIZE(sizeof(memory::internal::UsedMemory));
252
252
    TRASH_MEM(next);
253
253
  }
254
254
 
258
258
  /* now go through the used blocks and mark them free */
259
259
  for (; next; next= next->next)
260
260
  {
261
 
    next->left= next->size - ALIGN_SIZE(sizeof(USED_MEM));
 
261
    next->left= next->size - ALIGN_SIZE(sizeof(memory::internal::UsedMemory));
262
262
    TRASH_MEM(next);
263
263
  }
264
264
 
289
289
 
290
290
void free_root(MEM_ROOT *root, myf MyFlags)
291
291
{
292
 
  register USED_MEM *next,*old;
 
292
  memory::internal::UsedMemory *next,*old;
293
293
 
294
294
  if (MyFlags & memory::MARK_BLOCKS_FREE)
295
295
  {
315
315
  if (root->pre_alloc)
316
316
  {
317
317
    root->free=root->pre_alloc;
318
 
    root->free->left=root->pre_alloc->size-ALIGN_SIZE(sizeof(USED_MEM));
 
318
    root->free->left=root->pre_alloc->size-ALIGN_SIZE(sizeof(memory::internal::UsedMemory));
319
319
    TRASH_MEM(root->pre_alloc);
320
320
    root->free->next=0;
321
321
  }
330
330
 
331
331
void set_prealloc_root(MEM_ROOT *root, char *ptr)
332
332
{
333
 
  USED_MEM *next;
 
333
  memory::internal::UsedMemory *next;
334
334
  for (next=root->used; next ; next=next->next)
335
335
  {
336
336
    if ((char*) next <= ptr && (char*) next + next->size > ptr)