mySQL 5.0.11 sources for tomato
[tomato.git] / release / src / router / mysql / server-tools / instance-manager / buffer.cc
blob4859634830cc2011ed10372e71544ed8a7ac1b2e
1 /*
2 Copyright (c) 2004-2007 MySQL AB, 2009 Sun Microsystems, Inc.
3 Use is subject to license terms.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; version 2 of the License.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 #if defined(__GNUC__) && defined(USE_PRAGMA_IMPLEMENTATION)
20 #pragma implementation
21 #endif
23 #include "buffer.h"
24 #include <m_string.h>
26 const uint Buffer::BUFFER_INITIAL_SIZE= 4096;
27 const uint Buffer::MAX_BUFFER_SIZE= 16777216;
30 Puts the given string to the buffer.
32 SYNOPSIS
33 append()
34 position start position in the buffer
35 string string to be put in the buffer
36 len_arg the length of the string. This way we can avoid some
37 strlens.
39 DESCRIPTION
41 The method puts a string into the buffer, starting from position .
42 In the case when the buffer is too small it reallocs the buffer. The
43 total size of the buffer is restricted with 16.
45 RETURN
46 0 - ok
47 1 - got an error in reserve()
50 int Buffer::append(size_t position, const char *string, size_t len_arg)
52 if (reserve(position, len_arg))
53 return 1;
55 strnmov((char*) buffer + position, string, len_arg);
56 return 0;
61 Checks whether the current buffer size is ok to put a string of the length
62 "len_arg" starting from "position" and reallocs it if no.
64 SYNOPSIS
65 reserve()
66 position the number starting byte on the buffer to store a buffer
67 len_arg the length of the string.
69 DESCRIPTION
71 The method checks whether it is possible to put a string of the "len_arg"
72 length into the buffer, starting from "position" byte. In the case when the
73 buffer is too small it reallocs the buffer. The total size of the buffer is
74 restricted with 16 Mb.
76 RETURN
77 0 - ok
78 1 - realloc error or we have come to the 16Mb barrier
81 int Buffer::reserve(size_t position, size_t len_arg)
83 if (position + len_arg >= MAX_BUFFER_SIZE)
84 goto err;
86 if (position + len_arg >= buffer_size)
88 buffer= (uchar*) my_realloc(buffer,
89 min(MAX_BUFFER_SIZE,
90 max((uint) (buffer_size*1.5),
91 position + len_arg)), MYF(0));
92 if (!(buffer))
93 goto err;
94 buffer_size= (size_t) (buffer_size*1.5);
96 return 0;
98 err:
99 error= 1;
100 return 1;
104 int Buffer::get_size()
106 return (uint) buffer_size;
110 int Buffer::is_error()
112 return error;