~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/my_alloc.cc

pandora-build v0.71. Added check for avahi.

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 "mysys/mysys_priv.h"
18
19
#include <mystrings/m_string.h>
19
 
#include <my_sys.h>
20
 
#undef EXTRA_DEBUG
21
 
#define EXTRA_DEBUG
22
 
 
 
20
 
 
21
#include <algorithm>
 
22
 
 
23
using namespace std;
23
24
 
24
25
/*
25
26
  Initialize memory root
43
44
*/
44
45
 
45
46
void init_alloc_root(MEM_ROOT *mem_root, size_t block_size,
46
 
                     size_t pre_alloc_size __attribute__((unused)))
 
47
                     size_t )
47
48
{
48
49
  mem_root->free= mem_root->used= mem_root->pre_alloc= 0;
49
50
  mem_root->min_malloc= 32;
52
53
  mem_root->block_num= 4;                       /* We shift this with >>2 */
53
54
  mem_root->first_block_usage= 0;
54
55
 
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
56
  return;
69
57
}
70
58
 
87
75
*/
88
76
 
89
77
void reset_root_defaults(MEM_ROOT *mem_root, size_t block_size,
90
 
                         size_t pre_alloc_size __attribute__((unused)))
 
78
                         size_t pre_alloc_size)
91
79
{
92
80
  assert(alloc_root_inited(mem_root));
93
81
 
94
82
  mem_root->block_size= block_size - ALLOC_ROOT_MIN_BLOCK_SIZE;
95
 
#if !(defined(HAVE_purify) && defined(EXTRA_DEBUG))
96
83
  if (pre_alloc_size)
97
84
  {
98
85
    size_t size= pre_alloc_size + ALIGN_SIZE(sizeof(USED_MEM));
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
      {
136
123
    }
137
124
  }
138
125
  else
139
 
#endif
 
126
  {
140
127
    mem_root->pre_alloc= 0;
 
128
  }
141
129
}
142
130
 
143
131
 
144
132
void *alloc_root(MEM_ROOT *mem_root, size_t length)
145
133
{
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
134
  size_t get_size, block_size;
164
 
  uchar* point;
 
135
  unsigned char* point;
165
136
  register USED_MEM *next= 0;
166
137
  register USED_MEM **prev;
167
138
  assert(alloc_root_inited(mem_root));
188
159
    get_size= length+ALIGN_SIZE(sizeof(USED_MEM));
189
160
    get_size= max(get_size, block_size);
190
161
 
191
 
    if (!(next = (USED_MEM*) my_malloc(get_size,MYF(MY_WME | ME_FATALERROR))))
 
162
    if (!(next = (USED_MEM*) malloc(get_size)))
192
163
    {
193
164
      if (mem_root->error_handler)
194
165
        (*mem_root->error_handler)();
195
 
      return((void*) 0);                      /* purecov: inspected */
 
166
      return((void*) 0);
196
167
    }
197
168
    mem_root->block_num++;
198
169
    next->next= *prev;
201
172
    *prev=next;
202
173
  }
203
174
 
204
 
  point= (uchar*) ((char*) next+ (next->size-next->left));
 
175
  point= (unsigned char*) ((char*) next+ (next->size-next->left));
205
176
  /*TODO: next part may be unneded due to mem_root->first_block_usage counter*/
206
177
  if ((next->left-= length) < mem_root->min_malloc)
207
178
  {                                             /* Full block */
211
182
    mem_root->first_block_usage= 0;
212
183
  }
213
184
  return((void*) point);
214
 
#endif
215
185
}
216
186
 
217
187
 
250
220
  va_end(args);
251
221
 
252
222
  if (!(start= (char*) alloc_root(root, tot_length)))
253
 
    return(0);                            /* purecov: inspected */
 
223
    return(0);
254
224
 
255
225
  va_start(args, root);
256
226
  res= start;
332
302
  {
333
303
    old=next; next= next->next ;
334
304
    if (old != root->pre_alloc)
335
 
      my_free(old,MYF(0));
 
305
      free(old);
336
306
  }
337
307
  for (next=root->free ; next ;)
338
308
  {
339
309
    old=next; next= next->next;
340
310
    if (old != root->pre_alloc)
341
 
      my_free(old,MYF(0));
 
311
      free(old);
342
312
  }
343
313
  root->used=root->free=0;
344
314
  if (root->pre_alloc)
388
358
char *strmake_root(MEM_ROOT *root, const char *str, size_t len)
389
359
{
390
360
  char *pos;
391
 
  if ((pos=alloc_root(root,len+1)))
 
361
  if ((pos=(char *)alloc_root(root,len+1)))
392
362
  {
393
363
    memcpy(pos,str,len);
394
364
    pos[len]=0;
399
369
 
400
370
void *memdup_root(MEM_ROOT *root, const void *str, size_t len)
401
371
{
402
 
  char *pos;
 
372
  void *pos;
403
373
  if ((pos=alloc_root(root,len)))
404
374
    memcpy(pos,str,len);
405
375
  return pos;