~drizzle-trunk/drizzle/development

1 by brian
clean slate
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 <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
  NOTES
32
    No DBUG_ENTER... here to get smaller dbug-startup 
33
*/
34
35
void* my_once_alloc(size_t Size, myf MyFlags)
36
{
37
  size_t get_size, max_left;
38
  uchar* point;
39
  register USED_MEM *next;
40
  register USED_MEM **prev;
41
42
  Size= ALIGN_SIZE(Size);
43
  prev= &my_once_root_block;
44
  max_left=0;
45
  for (next=my_once_root_block ; next && next->left < Size ; next= next->next)
46
  {
47
    if (next->left > max_left)
48
      max_left=next->left;
49
    prev= &next->next;
50
  }
51
  if (! next)
52
  {						/* Time to alloc new block */
53
    get_size= Size+ALIGN_SIZE(sizeof(USED_MEM));
54
    if (max_left*4 < my_once_extra && get_size < my_once_extra)
55
      get_size=my_once_extra;			/* Normal alloc */
56
57
    if ((next = (USED_MEM*) malloc(get_size)) == 0)
58
    {
59
      my_errno=errno;
60
      if (MyFlags & (MY_FAE+MY_WME))
61
	my_error(EE_OUTOFMEMORY, MYF(ME_BELL+ME_WAITTANG),get_size);
62
      return((uchar*) 0);
63
    }
64
    DBUG_PRINT("test",("my_once_malloc %lu byte malloced", (ulong) get_size));
65
    next->next= 0;
66
    next->size= get_size;
67
    next->left= get_size-ALIGN_SIZE(sizeof(USED_MEM));
68
    *prev=next;
69
  }
70
  point= (uchar*) ((char*) next+ (next->size-next->left));
71
  next->left-= Size;
72
73
  if (MyFlags & MY_ZEROFILL)
74
    bzero(point, Size);
75
  return((void*) point);
76
} /* my_once_alloc */
77
78
79
char *my_once_strdup(const char *src,myf myflags)
80
{
81
  size_t len= strlen(src)+1;
82
  uchar *dst= my_once_alloc(len, myflags);
83
  if (dst)
84
    memcpy(dst, src, len);
85
  return (char*) dst;
86
}
87
88
89
void *my_once_memdup(const void *src, size_t len, myf myflags)
90
{
91
  uchar *dst= my_once_alloc(len, myflags);
92
  if (dst)
93
    memcpy(dst, src, len);
94
  return dst;
95
}
96
97
98
/*
99
  Deallocate everything used by my_once_alloc
100
101
  SYNOPSIS
102
    my_once_free()
103
*/
104
105
void my_once_free(void)
106
{
107
  register USED_MEM *next,*old;
108
  DBUG_ENTER("my_once_free");
109
110
  for (next=my_once_root_block ; next ; )
111
  {
112
    old=next; next= next->next ;
113
    free((uchar*) old);
114
  }
115
  my_once_root_block=0;
116
117
  DBUG_VOID_RETURN;
118
} /* my_once_free */