~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/my_alloc.cc

  • Committer: Brian Aker
  • Date: 2008-12-16 07:07:50 UTC
  • Revision ID: brian@tangent.org-20081216070750-o5ykltxxqvn2awrx
Fixed errors test.

Show diffs side-by-side

added added

removed removed

Lines of Context:
52
52
  mem_root->block_num= 4;                       /* We shift this with >>2 */
53
53
  mem_root->first_block_usage= 0;
54
54
 
55
 
#if !(defined(HAVE_purify) && defined(EXTRA_DEBUG))
56
 
  if (pre_alloc_size)
57
 
  {
58
 
    if ((mem_root->free= mem_root->pre_alloc=
59
 
         (USED_MEM*) my_malloc(pre_alloc_size+ ALIGN_SIZE(sizeof(USED_MEM)),
60
 
                               MYF(0))))
61
 
    {
62
 
      mem_root->free->size= pre_alloc_size+ALIGN_SIZE(sizeof(USED_MEM));
63
 
      mem_root->free->left= pre_alloc_size;
64
 
      mem_root->free->next= 0;
65
 
    }
66
 
  }
67
 
#endif
68
55
  return;
69
56
}
70
57
 
116
103
        {
117
104
          /* remove block from the list and free it */
118
105
          *prev= mem->next;
119
 
          my_free(mem, MYF(0));
 
106
          free(mem);
120
107
        }
121
108
        else
122
109
          prev= &mem->next;
123
110
      }
124
111
      /* Allocate new prealloc block and add it to the end of free list */
125
 
      if ((mem= (USED_MEM *) my_malloc(size, MYF(0))))
 
112
      if ((mem= (USED_MEM *) malloc(size)))
126
113
      {
127
 
        mem->size= size; 
 
114
        mem->size= size;
128
115
        mem->left= pre_alloc_size;
129
116
        mem->next= *prev;
130
 
        *prev= mem_root->pre_alloc= mem; 
 
117
        *prev= mem_root->pre_alloc= mem;
131
118
      }
132
119
      else
133
120
      {
143
130
 
144
131
void *alloc_root(MEM_ROOT *mem_root, size_t length)
145
132
{
146
 
#if defined(HAVE_purify) && defined(EXTRA_DEBUG)
147
 
  register USED_MEM *next;
148
 
 
149
 
  assert(alloc_root_inited(mem_root));
150
 
 
151
 
  length+=ALIGN_SIZE(sizeof(USED_MEM));
152
 
  if (!(next = (USED_MEM*) my_malloc(length,MYF(MY_WME | ME_FATALERROR))))
153
 
  {
154
 
    if (mem_root->error_handler)
155
 
      (*mem_root->error_handler)();
156
 
    return((uchar*) 0);                 /* purecov: inspected */
157
 
  }
158
 
  next->next= mem_root->used;
159
 
  next->size= length;
160
 
  mem_root->used= next;
161
 
  return((uchar*) (((char*) next)+ALIGN_SIZE(sizeof(USED_MEM))));
162
 
#else
163
133
  size_t get_size, block_size;
164
 
  uchar* point;
 
134
  unsigned char* point;
165
135
  register USED_MEM *next= 0;
166
136
  register USED_MEM **prev;
167
137
  assert(alloc_root_inited(mem_root));
186
156
  {                                             /* Time to alloc new block */
187
157
    block_size= mem_root->block_size * (mem_root->block_num >> 2);
188
158
    get_size= length+ALIGN_SIZE(sizeof(USED_MEM));
189
 
    get_size= max(get_size, block_size);
 
159
    get_size= cmax(get_size, block_size);
190
160
 
191
 
    if (!(next = (USED_MEM*) my_malloc(get_size,MYF(MY_WME | ME_FATALERROR))))
 
161
    if (!(next = (USED_MEM*) malloc(get_size)))
192
162
    {
193
163
      if (mem_root->error_handler)
194
164
        (*mem_root->error_handler)();
201
171
    *prev=next;
202
172
  }
203
173
 
204
 
  point= (uchar*) ((char*) next+ (next->size-next->left));
 
174
  point= (unsigned char*) ((char*) next+ (next->size-next->left));
205
175
  /*TODO: next part may be unneded due to mem_root->first_block_usage counter*/
206
176
  if ((next->left-= length) < mem_root->min_malloc)
207
177
  {                                             /* Full block */
211
181
    mem_root->first_block_usage= 0;
212
182
  }
213
183
  return((void*) point);
214
 
#endif
215
184
}
216
185
 
217
186
 
332
301
  {
333
302
    old=next; next= next->next ;
334
303
    if (old != root->pre_alloc)
335
 
      my_free(old,MYF(0));
 
304
      free(old);
336
305
  }
337
306
  for (next=root->free ; next ;)
338
307
  {
339
308
    old=next; next= next->next;
340
309
    if (old != root->pre_alloc)
341
 
      my_free(old,MYF(0));
 
310
      free(old);
342
311
  }
343
312
  root->used=root->free=0;
344
313
  if (root->pre_alloc)
388
357
char *strmake_root(MEM_ROOT *root, const char *str, size_t len)
389
358
{
390
359
  char *pos;
391
 
  if ((pos=alloc_root(root,len+1)))
 
360
  if ((pos=(char *)alloc_root(root,len+1)))
392
361
  {
393
362
    memcpy(pos,str,len);
394
363
    pos[len]=0;
399
368
 
400
369
void *memdup_root(MEM_ROOT *root, const void *str, size_t len)
401
370
{
402
 
  char *pos;
 
371
  void *pos;
403
372
  if ((pos=alloc_root(root,len)))
404
373
    memcpy(pos,str,len);
405
374
  return pos;