~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/my_alloc.cc

  • Committer: Brian Aker
  • Date: 2009-05-11 17:50:22 UTC
  • Revision ID: brian@gaz-20090511175022-y35q9ky6uh9ldcjt
Replacing Sun employee copyright headers (aka... anything done by a Sun
employee is copyright by Sun).

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
 
23
21
 
24
22
/*
43
41
*/
44
42
 
45
43
void init_alloc_root(MEM_ROOT *mem_root, size_t block_size,
46
 
                     size_t pre_alloc_size __attribute__((unused)))
 
44
                     size_t )
47
45
{
48
46
  mem_root->free= mem_root->used= mem_root->pre_alloc= 0;
49
47
  mem_root->min_malloc= 32;
52
50
  mem_root->block_num= 4;                       /* We shift this with >>2 */
53
51
  mem_root->first_block_usage= 0;
54
52
 
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
53
  return;
69
54
}
70
55
 
87
72
*/
88
73
 
89
74
void reset_root_defaults(MEM_ROOT *mem_root, size_t block_size,
90
 
                         size_t pre_alloc_size __attribute__((unused)))
 
75
                         size_t pre_alloc_size)
91
76
{
92
77
  assert(alloc_root_inited(mem_root));
93
78
 
94
79
  mem_root->block_size= block_size - ALLOC_ROOT_MIN_BLOCK_SIZE;
95
 
#if !(defined(HAVE_purify) && defined(EXTRA_DEBUG))
96
80
  if (pre_alloc_size)
97
81
  {
98
82
    size_t size= pre_alloc_size + ALIGN_SIZE(sizeof(USED_MEM));
122
106
          prev= &mem->next;
123
107
      }
124
108
      /* Allocate new prealloc block and add it to the end of free list */
125
 
      if ((mem= (USED_MEM *) my_malloc(size, MYF(0))))
 
109
      if ((mem= (USED_MEM *) malloc(size)))
126
110
      {
127
 
        mem->size= size; 
 
111
        mem->size= size;
128
112
        mem->left= pre_alloc_size;
129
113
        mem->next= *prev;
130
 
        *prev= mem_root->pre_alloc= mem; 
 
114
        *prev= mem_root->pre_alloc= mem;
131
115
      }
132
116
      else
133
117
      {
136
120
    }
137
121
  }
138
122
  else
139
 
#endif
 
123
  {
140
124
    mem_root->pre_alloc= 0;
 
125
  }
141
126
}
142
127
 
143
128
 
144
129
void *alloc_root(MEM_ROOT *mem_root, size_t length)
145
130
{
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((unsigned char*) 0);                 /* purecov: inspected */
157
 
  }
158
 
  next->next= mem_root->used;
159
 
  next->size= length;
160
 
  mem_root->used= next;
161
 
  return((unsigned char*) (((char*) next)+ALIGN_SIZE(sizeof(USED_MEM))));
162
 
#else
163
131
  size_t get_size, block_size;
164
132
  unsigned char* point;
165
133
  register USED_MEM *next= 0;
188
156
    get_size= length+ALIGN_SIZE(sizeof(USED_MEM));
189
157
    get_size= cmax(get_size, block_size);
190
158
 
191
 
    if (!(next = (USED_MEM*) my_malloc(get_size,MYF(MY_WME | ME_FATALERROR))))
 
159
    if (!(next = (USED_MEM*) malloc(get_size)))
192
160
    {
193
161
      if (mem_root->error_handler)
194
162
        (*mem_root->error_handler)();
211
179
    mem_root->first_block_usage= 0;
212
180
  }
213
181
  return((void*) point);
214
 
#endif
215
182
}
216
183
 
217
184
 
388
355
char *strmake_root(MEM_ROOT *root, const char *str, size_t len)
389
356
{
390
357
  char *pos;
391
 
  if ((pos=alloc_root(root,len+1)))
 
358
  if ((pos=(char *)alloc_root(root,len+1)))
392
359
  {
393
360
    memcpy(pos,str,len);
394
361
    pos[len]=0;
399
366
 
400
367
void *memdup_root(MEM_ROOT *root, const void *str, size_t len)
401
368
{
402
 
  char *pos;
 
369
  void *pos;
403
370
  if ((pos=alloc_root(root,len)))
404
371
    memcpy(pos,str,len);
405
372
  return pos;