~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/my_once.cc

  • Committer: Brian Aker
  • Date: 2008-12-09 17:33:02 UTC
  • mfrom: (656.1.54 devel)
  • Revision ID: brian@tangent.org-20081209173302-aptngvc7efxnatnt
Merge from Monty.

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
 
#include "mysys_priv.h"
19
 
#include "my_static.h"
20
 
#include "mysys_err.h"
21
 
#include <mystrings/m_string.h>
22
 
 
23
 
/*
24
 
  Alloc for things we don't nead to free
25
 
 
26
 
  SYNOPSIS
27
 
    my_once_alloc()
28
 
      Size
29
 
      MyFlags
30
 
*/
31
 
 
32
 
void* my_once_alloc(size_t Size, myf MyFlags)
33
 
{
34
 
  size_t get_size, max_left;
35
 
  unsigned char* point;
36
 
  register USED_MEM *next;
37
 
  register USED_MEM **prev;
38
 
 
39
 
  Size= ALIGN_SIZE(Size);
40
 
  prev= &my_once_root_block;
41
 
  max_left=0;
42
 
  for (next=my_once_root_block ; next && next->left < Size ; next= next->next)
43
 
  {
44
 
    if (next->left > max_left)
45
 
      max_left=next->left;
46
 
    prev= &next->next;
47
 
  }
48
 
  if (! next)
49
 
  {                                             /* Time to alloc new block */
50
 
    get_size= Size+ALIGN_SIZE(sizeof(USED_MEM));
51
 
    if (max_left*4 < my_once_extra && get_size < my_once_extra)
52
 
      get_size=my_once_extra;                   /* Normal alloc */
53
 
 
54
 
    if ((next = (USED_MEM*) malloc(get_size)) == 0)
55
 
    {
56
 
      my_errno=errno;
57
 
      if (MyFlags & (MY_FAE+MY_WME))
58
 
        my_error(EE_OUTOFMEMORY, MYF(ME_BELL+ME_WAITTANG),get_size);
59
 
      return((unsigned char*) 0);
60
 
    }
61
 
    next->next= 0;
62
 
    next->size= get_size;
63
 
    next->left= get_size-ALIGN_SIZE(sizeof(USED_MEM));
64
 
    *prev=next;
65
 
  }
66
 
  point= (unsigned char*) ((char*) next+ (next->size-next->left));
67
 
  next->left-= Size;
68
 
 
69
 
  if (MyFlags & MY_ZEROFILL)
70
 
    memset(point, 0, Size);
71
 
  return((void*) point);
72
 
} /* my_once_alloc */
73
 
 
74
 
 
75
 
char *my_once_strdup(const char *src,myf myflags)
76
 
{
77
 
  size_t len= strlen(src)+1;
78
 
  void *dst= my_once_alloc(len, myflags);
79
 
  if (dst)
80
 
    memcpy(dst, src, len);
81
 
  return (char*) dst;
82
 
}
83
 
 
84
 
 
85
 
void *my_once_memdup(const void *src, size_t len, myf myflags)
86
 
{
87
 
  void *dst= my_once_alloc(len, myflags);
88
 
  if (dst)
89
 
    memcpy(dst, src, len);
90
 
  return dst;
91
 
}
92
 
 
93
 
 
94
 
/*
95
 
  Deallocate everything used by my_once_alloc
96
 
 
97
 
  SYNOPSIS
98
 
    my_once_free()
99
 
*/
100
 
 
101
 
void my_once_free(void)
102
 
{
103
 
  register USED_MEM *next,*old;
104
 
 
105
 
  for (next=my_once_root_block ; next ; )
106
 
  {
107
 
    old=next; next= next->next ;
108
 
    free((unsigned char*) old);
109
 
  }
110
 
  my_once_root_block=0;
111
 
 
112
 
  return;
113
 
} /* my_once_free */