~ubuntu-branches/ubuntu/precise/mysql-5.1/precise

« back to all changes in this revision

Viewing changes to mysys/my_once.c

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2000 MySQL AB
 
2
 
 
3
   This program is free software; you can redistribute it and/or modify
 
4
   it under the terms of the GNU General Public License as published by
 
5
   the Free Software Foundation; version 2 of the License.
 
6
 
 
7
   This program is distributed in the hope that it will be useful,
 
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
   GNU General Public License for more details.
 
11
 
 
12
   You should have received a copy of the GNU General Public License
 
13
   along with this program; if not, write to the Free Software
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
15
 
 
16
/* Not MT-SAFE */
 
17
 
 
18
#ifdef SAFEMALLOC                       /* We don't need SAFEMALLOC here */
 
19
#undef SAFEMALLOC
 
20
#endif
 
21
 
 
22
#include "mysys_priv.h"
 
23
#include "my_static.h"
 
24
#include "mysys_err.h"
 
25
#include <m_string.h>
 
26
 
 
27
/*
 
28
  Alloc for things we don't nead to free
 
29
 
 
30
  SYNOPSIS
 
31
    my_once_alloc()
 
32
      Size
 
33
      MyFlags
 
34
 
 
35
  NOTES
 
36
    No DBUG_ENTER... here to get smaller dbug-startup 
 
37
*/
 
38
 
 
39
void* my_once_alloc(size_t Size, myf MyFlags)
 
40
{
 
41
  size_t get_size, max_left;
 
42
  uchar* point;
 
43
  reg1 USED_MEM *next;
 
44
  reg2 USED_MEM **prev;
 
45
 
 
46
  Size= ALIGN_SIZE(Size);
 
47
  prev= &my_once_root_block;
 
48
  max_left=0;
 
49
  for (next=my_once_root_block ; next && next->left < Size ; next= next->next)
 
50
  {
 
51
    if (next->left > max_left)
 
52
      max_left=next->left;
 
53
    prev= &next->next;
 
54
  }
 
55
  if (! next)
 
56
  {                                             /* Time to alloc new block */
 
57
    get_size= Size+ALIGN_SIZE(sizeof(USED_MEM));
 
58
    if (max_left*4 < my_once_extra && get_size < my_once_extra)
 
59
      get_size=my_once_extra;                   /* Normal alloc */
 
60
 
 
61
    if ((next = (USED_MEM*) malloc(get_size)) == 0)
 
62
    {
 
63
      my_errno=errno;
 
64
      if (MyFlags & (MY_FAE+MY_WME))
 
65
        my_error(EE_OUTOFMEMORY, MYF(ME_BELL+ME_WAITTANG),get_size);
 
66
      return((uchar*) 0);
 
67
    }
 
68
    DBUG_PRINT("test",("my_once_malloc %lu byte malloced", (ulong) get_size));
 
69
    next->next= 0;
 
70
    next->size= get_size;
 
71
    next->left= get_size-ALIGN_SIZE(sizeof(USED_MEM));
 
72
    *prev=next;
 
73
  }
 
74
  point= (uchar*) ((char*) next+ (next->size-next->left));
 
75
  next->left-= Size;
 
76
 
 
77
  if (MyFlags & MY_ZEROFILL)
 
78
    bzero(point, Size);
 
79
  return((void*) point);
 
80
} /* my_once_alloc */
 
81
 
 
82
 
 
83
char *my_once_strdup(const char *src,myf myflags)
 
84
{
 
85
  size_t len= strlen(src)+1;
 
86
  uchar *dst= my_once_alloc(len, myflags);
 
87
  if (dst)
 
88
    memcpy(dst, src, len);
 
89
  return (char*) dst;
 
90
}
 
91
 
 
92
 
 
93
void *my_once_memdup(const void *src, size_t len, myf myflags)
 
94
{
 
95
  uchar *dst= my_once_alloc(len, myflags);
 
96
  if (dst)
 
97
    memcpy(dst, src, len);
 
98
  return dst;
 
99
}
 
100
 
 
101
 
 
102
/*
 
103
  Deallocate everything used by my_once_alloc
 
104
 
 
105
  SYNOPSIS
 
106
    my_once_free()
 
107
*/
 
108
 
 
109
void my_once_free(void)
 
110
{
 
111
  reg1 USED_MEM *next,*old;
 
112
  DBUG_ENTER("my_once_free");
 
113
 
 
114
  for (next=my_once_root_block ; next ; )
 
115
  {
 
116
    old=next; next= next->next ;
 
117
    free((uchar*) old);
 
118
  }
 
119
  my_once_root_block=0;
 
120
 
 
121
  DBUG_VOID_RETURN;
 
122
} /* my_once_free */