~mysql/mysql-server/mysql-5.1

1627.33.1 by petr at mysql
Intermediate commit - just to make new files visible to bk in the new
1
/* Copyright (C) 2004 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
1810.2403.4 by kent at mysql
Many files:
5
   the Free Software Foundation; version 2 of the License.
1627.33.1 by petr at mysql
Intermediate commit - just to make new files visible to bk in the new
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
1810.816.2 by jani at elisa-laajakaista
Some minor fixes revealed as warnings by
16
#if defined(__GNUC__) && defined(USE_PRAGMA_IMPLEMENTATION)
1627.33.1 by petr at mysql
Intermediate commit - just to make new files visible to bk in the new
17
#pragma implementation
18
#endif
19
20
#include "buffer.h"
21
#include <m_string.h>
22
1810.739.1 by petr at mysql
fix warnings
23
const uint Buffer::BUFFER_INITIAL_SIZE= 4096;
24
const uint Buffer::MAX_BUFFER_SIZE= 16777216;
1627.33.1 by petr at mysql
Intermediate commit - just to make new files visible to bk in the new
25
26
/*
27
  Puts the given string to the buffer.
28
2303.99.14 by kostja at bodhi
Port cleanups, trivial refactoring and code rearrangements from
29
  SYNOPSIS
1627.33.2 by petr at mysql
minor post review fixes
30
    append()
1627.33.3 by petr at mysql
Various post-review fixes
31
    position          start position in the buffer
1627.33.1 by petr at mysql
Intermediate commit - just to make new files visible to bk in the new
32
    string            string to be put in the buffer
33
    len_arg           the length of the string. This way we can avoid some
34
                      strlens.
35
36
  DESCRIPTION
37
1627.33.2 by petr at mysql
minor post review fixes
38
    The method puts a string into the buffer, starting from position .
1627.33.1 by petr at mysql
Intermediate commit - just to make new files visible to bk in the new
39
    In the case when the buffer is too small it reallocs the buffer. The
40
    total size of the buffer is restricted with 16.
41
42
  RETURN
43
    0 - ok
1700.334.7 by petr at mysql
Post-review fixes + some bugs fixed + several minor features
44
    1 - got an error in reserve()
1627.33.1 by petr at mysql
Intermediate commit - just to make new files visible to bk in the new
45
*/
46
2475.68.2 by monty at mysql
WL#3817: Simplify string / memory area types and make things more consistent (first part)
47
int Buffer::append(size_t position, const char *string, size_t len_arg)
1627.33.1 by petr at mysql
Intermediate commit - just to make new files visible to bk in the new
48
{
1627.33.3 by petr at mysql
Various post-review fixes
49
  if (reserve(position, len_arg))
1627.33.1 by petr at mysql
Intermediate commit - just to make new files visible to bk in the new
50
    return 1;
51
2475.68.2 by monty at mysql
WL#3817: Simplify string / memory area types and make things more consistent (first part)
52
  strnmov((char*) buffer + position, string, len_arg);
1627.33.1 by petr at mysql
Intermediate commit - just to make new files visible to bk in the new
53
  return 0;
54
}
55
56
57
/*
58
  Checks whether the current buffer size is ok to put a string of the length
59
  "len_arg" starting from "position" and reallocs it if no.
60
2303.99.14 by kostja at bodhi
Port cleanups, trivial refactoring and code rearrangements from
61
  SYNOPSIS
1627.33.2 by petr at mysql
minor post review fixes
62
    reserve()
1627.33.1 by petr at mysql
Intermediate commit - just to make new files visible to bk in the new
63
    position          the number starting byte on the buffer to store a buffer
64
    len_arg           the length of the string.
65
66
  DESCRIPTION
67
1773.303.2 by petr at mysql
post-review fixes
68
    The method checks whether it is possible to put a string of the "len_arg"
1627.33.1 by petr at mysql
Intermediate commit - just to make new files visible to bk in the new
69
    length into the buffer, starting from "position" byte. In the case when the
70
    buffer is too small it reallocs the buffer. The total size of the buffer is
71
    restricted with 16 Mb.
72
73
  RETURN
74
    0 - ok
1700.334.7 by petr at mysql
Post-review fixes + some bugs fixed + several minor features
75
    1 - realloc error or we have come to the 16Mb barrier
1627.33.1 by petr at mysql
Intermediate commit - just to make new files visible to bk in the new
76
*/
77
2475.68.2 by monty at mysql
WL#3817: Simplify string / memory area types and make things more consistent (first part)
78
int Buffer::reserve(size_t position, size_t len_arg)
1627.33.1 by petr at mysql
Intermediate commit - just to make new files visible to bk in the new
79
{
80
  if (position + len_arg >= MAX_BUFFER_SIZE)
1627.34.3 by petr at mysql
post-review fixes
81
    goto err;
1627.33.1 by petr at mysql
Intermediate commit - just to make new files visible to bk in the new
82
1700.336.4 by petr at mysql
post-review fixes + cleanup + some minor fixes
83
  if (position + len_arg >= buffer_size)
1627.33.1 by petr at mysql
Intermediate commit - just to make new files visible to bk in the new
84
  {
2475.68.2 by monty at mysql
WL#3817: Simplify string / memory area types and make things more consistent (first part)
85
    buffer= (uchar*) my_realloc(buffer,
1700.334.8 by petr at mysql
various fixes
86
                                min(MAX_BUFFER_SIZE,
87
                                    max((uint) (buffer_size*1.5),
88
                                        position + len_arg)), MYF(0));
1700.336.4 by petr at mysql
post-review fixes + cleanup + some minor fixes
89
    if (!(buffer))
1627.34.3 by petr at mysql
post-review fixes
90
      goto err;
2475.68.2 by monty at mysql
WL#3817: Simplify string / memory area types and make things more consistent (first part)
91
    buffer_size= (size_t) (buffer_size*1.5);
1627.33.1 by petr at mysql
Intermediate commit - just to make new files visible to bk in the new
92
  }
93
  return 0;
1627.34.3 by petr at mysql
post-review fixes
94
95
err:
1700.334.7 by petr at mysql
Post-review fixes + some bugs fixed + several minor features
96
  error= 1;
1627.34.3 by petr at mysql
post-review fixes
97
  return 1;
1627.33.1 by petr at mysql
Intermediate commit - just to make new files visible to bk in the new
98
}
1627.33.3 by petr at mysql
Various post-review fixes
99
1700.334.7 by petr at mysql
Post-review fixes + some bugs fixed + several minor features
100
101
int Buffer::get_size()
102
{
103
  return buffer_size;
104
}
105
106
107
int Buffer::is_error()
108
{
109
  return error;
110
}