~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/my_alloc.c

Removed DBUG symbols and fixed TRUE/FALSE

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
 
16
16
/* Routines to handle mallocing of results which will be freed the same time */
17
17
 
18
 
#include <mystrings/m_string.h>
 
18
#include <my_global.h>
19
19
#include <my_sys.h>
 
20
#include <m_string.h>
20
21
#undef EXTRA_DEBUG
21
22
#define EXTRA_DEBUG
22
23
 
45
46
void init_alloc_root(MEM_ROOT *mem_root, size_t block_size,
46
47
                     size_t pre_alloc_size __attribute__((unused)))
47
48
{
 
49
  DBUG_ENTER("init_alloc_root");
 
50
  DBUG_PRINT("enter",("root: 0x%lx", (long) mem_root));
 
51
 
48
52
  mem_root->free= mem_root->used= mem_root->pre_alloc= 0;
49
53
  mem_root->min_malloc= 32;
50
54
  mem_root->block_size= block_size - ALLOC_ROOT_MIN_BLOCK_SIZE;
65
69
    }
66
70
  }
67
71
#endif
68
 
  return;
 
72
  DBUG_VOID_RETURN;
69
73
}
70
74
 
71
75
 
89
93
void reset_root_defaults(MEM_ROOT *mem_root, size_t block_size,
90
94
                         size_t pre_alloc_size __attribute__((unused)))
91
95
{
92
 
  assert(alloc_root_inited(mem_root));
 
96
  DBUG_ASSERT(alloc_root_inited(mem_root));
93
97
 
94
98
  mem_root->block_size= block_size - ALLOC_ROOT_MIN_BLOCK_SIZE;
95
99
#if !(defined(HAVE_purify) && defined(EXTRA_DEBUG))
116
120
        {
117
121
          /* remove block from the list and free it */
118
122
          *prev= mem->next;
119
 
          free(mem);
 
123
          my_free(mem, MYF(0));
120
124
        }
121
125
        else
122
126
          prev= &mem->next;
145
149
{
146
150
#if defined(HAVE_purify) && defined(EXTRA_DEBUG)
147
151
  register USED_MEM *next;
 
152
  DBUG_ENTER("alloc_root");
 
153
  DBUG_PRINT("enter",("root: 0x%lx", (long) mem_root));
148
154
 
149
 
  assert(alloc_root_inited(mem_root));
 
155
  DBUG_ASSERT(alloc_root_inited(mem_root));
150
156
 
151
157
  length+=ALIGN_SIZE(sizeof(USED_MEM));
152
158
  if (!(next = (USED_MEM*) my_malloc(length,MYF(MY_WME | ME_FATALERROR))))
153
159
  {
154
160
    if (mem_root->error_handler)
155
161
      (*mem_root->error_handler)();
156
 
    return((unsigned char*) 0);                 /* purecov: inspected */
 
162
    DBUG_RETURN((uchar*) 0);                    /* purecov: inspected */
157
163
  }
158
164
  next->next= mem_root->used;
159
165
  next->size= length;
160
166
  mem_root->used= next;
161
 
  return((unsigned char*) (((char*) next)+ALIGN_SIZE(sizeof(USED_MEM))));
 
167
  DBUG_PRINT("exit",("ptr: 0x%lx", (long) (((char*) next)+
 
168
                                           ALIGN_SIZE(sizeof(USED_MEM)))));
 
169
  DBUG_RETURN((uchar*) (((char*) next)+ALIGN_SIZE(sizeof(USED_MEM))));
162
170
#else
163
171
  size_t get_size, block_size;
164
 
  unsigned char* point;
 
172
  uchar* point;
165
173
  register USED_MEM *next= 0;
166
174
  register USED_MEM **prev;
167
 
  assert(alloc_root_inited(mem_root));
 
175
  DBUG_ENTER("alloc_root");
 
176
  DBUG_PRINT("enter",("root: 0x%lx", (long) mem_root));
 
177
  DBUG_ASSERT(alloc_root_inited(mem_root));
168
178
 
169
179
  length= ALIGN_SIZE(length);
170
180
  if ((*(prev= &mem_root->free)) != NULL)
186
196
  {                                             /* Time to alloc new block */
187
197
    block_size= mem_root->block_size * (mem_root->block_num >> 2);
188
198
    get_size= length+ALIGN_SIZE(sizeof(USED_MEM));
189
 
    get_size= cmax(get_size, block_size);
 
199
    get_size= max(get_size, block_size);
190
200
 
191
201
    if (!(next = (USED_MEM*) my_malloc(get_size,MYF(MY_WME | ME_FATALERROR))))
192
202
    {
193
203
      if (mem_root->error_handler)
194
204
        (*mem_root->error_handler)();
195
 
      return((void*) 0);                      /* purecov: inspected */
 
205
      DBUG_RETURN((void*) 0);                      /* purecov: inspected */
196
206
    }
197
207
    mem_root->block_num++;
198
208
    next->next= *prev;
201
211
    *prev=next;
202
212
  }
203
213
 
204
 
  point= (unsigned char*) ((char*) next+ (next->size-next->left));
 
214
  point= (uchar*) ((char*) next+ (next->size-next->left));
205
215
  /*TODO: next part may be unneded due to mem_root->first_block_usage counter*/
206
216
  if ((next->left-= length) < mem_root->min_malloc)
207
217
  {                                             /* Full block */
210
220
    mem_root->used= next;
211
221
    mem_root->first_block_usage= 0;
212
222
  }
213
 
  return((void*) point);
 
223
  DBUG_PRINT("exit",("ptr: 0x%lx", (ulong) point));
 
224
  DBUG_RETURN((void*) point);
214
225
#endif
215
226
}
216
227
 
239
250
  va_list args;
240
251
  char **ptr, *start, *res;
241
252
  size_t tot_length, length;
 
253
  DBUG_ENTER("multi_alloc_root");
242
254
 
243
255
  va_start(args, root);
244
256
  tot_length= 0;
250
262
  va_end(args);
251
263
 
252
264
  if (!(start= (char*) alloc_root(root, tot_length)))
253
 
    return(0);                            /* purecov: inspected */
 
265
    DBUG_RETURN(0);                            /* purecov: inspected */
254
266
 
255
267
  va_start(args, root);
256
268
  res= start;
261
273
    res+= ALIGN_SIZE(length);
262
274
  }
263
275
  va_end(args);
264
 
  return((void*) start);
 
276
  DBUG_RETURN((void*) start);
265
277
}
266
278
 
267
279
#define TRASH_MEM(X) TRASH(((char*)(X) + ((X)->size-(X)->left)), (X)->left)
312
324
 
313
325
  NOTES
314
326
    One can call this function either with root block initialised with
315
 
    init_alloc_root() or with a zero:ed block.
 
327
    init_alloc_root() or with a bzero()-ed block.
316
328
    It's also safe to call this multiple times with the same mem_root.
317
329
*/
318
330
 
319
331
void free_root(MEM_ROOT *root, myf MyFlags)
320
332
{
321
333
  register USED_MEM *next,*old;
 
334
  DBUG_ENTER("free_root");
 
335
  DBUG_PRINT("enter",("root: 0x%lx  flags: %u", (long) root, (uint) MyFlags));
322
336
 
323
337
  if (MyFlags & MY_MARK_BLOCKS_FREE)
324
338
  {
325
339
    mark_blocks_free(root);
326
 
    return;
 
340
    DBUG_VOID_RETURN;
327
341
  }
328
342
  if (!(MyFlags & MY_KEEP_PREALLOC))
329
343
    root->pre_alloc=0;
332
346
  {
333
347
    old=next; next= next->next ;
334
348
    if (old != root->pre_alloc)
335
 
      free(old);
 
349
      my_free(old,MYF(0));
336
350
  }
337
351
  for (next=root->free ; next ;)
338
352
  {
339
353
    old=next; next= next->next;
340
354
    if (old != root->pre_alloc)
341
 
      free(old);
 
355
      my_free(old,MYF(0));
342
356
  }
343
357
  root->used=root->free=0;
344
358
  if (root->pre_alloc)
350
364
  }
351
365
  root->block_num= 4;
352
366
  root->first_block_usage= 0;
353
 
  return;
 
367
  DBUG_VOID_RETURN;
354
368
}
355
369
 
356
370
/*