summaryrefslogtreecommitdiff
path: root/sql/sql_string.cc
diff options
context:
space:
mode:
Diffstat (limited to 'sql/sql_string.cc')
-rw-r--r--sql/sql_string.cc54
1 files changed, 27 insertions, 27 deletions
diff --git a/sql/sql_string.cc b/sql/sql_string.cc
index f2a0f55aec8..aca9ede9000 100644
--- a/sql/sql_string.cc
+++ b/sql/sql_string.cc
@@ -41,7 +41,7 @@ bool Binary_string::real_alloc(size_t length)
if (Alloced_length < arg_length)
{
free();
- if (!(Ptr=(char*) my_malloc(PSI_INSTRUMENT_ME,
+ if (!(Ptr=(char*) my_malloc(STRING_PSI_MEMORY_KEY,
arg_length,MYF(MY_WME | (thread_specific ?
MY_THREAD_SPECIFIC : 0)))))
return TRUE;
@@ -55,7 +55,8 @@ bool Binary_string::real_alloc(size_t length)
/**
- Allocates a new buffer on the heap for this String.
+ Allocates a new buffer on the heap for this String if current buffer is
+ smaller.
- If the String's internal buffer is privately owned and heap allocated,
one of the following is performed.
@@ -70,7 +71,8 @@ bool Binary_string::real_alloc(size_t length)
will be allocated and the string copied accoring to its length, as found
in String::length().
- For C compatibility, the new string buffer is null terminated.
+ For C compatibility, the new string buffer is null terminated if it was
+ allocated.
@param alloc_length The requested string size in characters, excluding any
null terminator.
@@ -81,9 +83,10 @@ bool Binary_string::real_alloc(size_t length)
@retval true An error occurred when attempting to allocate memory.
*/
+
bool Binary_string::realloc_raw(size_t alloc_length)
{
- if (Alloced_length <= alloc_length)
+ if (Alloced_length < alloc_length)
{
char *new_ptr;
uint32 len= ALIGN_SIZE(alloc_length+1);
@@ -92,13 +95,13 @@ bool Binary_string::realloc_raw(size_t alloc_length)
return TRUE; /* Overflow */
if (alloced)
{
- if (!(new_ptr= (char*) my_realloc(PSI_INSTRUMENT_ME, Ptr,len,
+ if (!(new_ptr= (char*) my_realloc(STRING_PSI_MEMORY_KEY, Ptr,len,
MYF(MY_WME |
(thread_specific ?
MY_THREAD_SPECIFIC : 0)))))
return TRUE; // Signal error
}
- else if ((new_ptr= (char*) my_malloc(PSI_INSTRUMENT_ME, len,
+ else if ((new_ptr= (char*) my_malloc(STRING_PSI_MEMORY_KEY, len,
MYF(MY_WME |
(thread_specific ?
MY_THREAD_SPECIFIC : 0)))))
@@ -118,9 +121,14 @@ bool Binary_string::realloc_raw(size_t alloc_length)
return FALSE;
}
+
bool String::set_int(longlong num, bool unsigned_flag, CHARSET_INFO *cs)
{
- uint l=20*cs->mbmaxlen+1;
+ /*
+ This allocates a few bytes extra in the unlikely case that cs->mb_maxlen
+ > 1, but we can live with that
+ */
+ uint l= LONGLONG_BUFFER_SIZE * cs->mbmaxlen;
int base= unsigned_flag ? 10 : -10;
if (alloc(l))
@@ -235,7 +243,7 @@ bool Binary_string::copy()
*/
bool Binary_string::copy(const Binary_string &str)
{
- if (alloc(str.str_length))
+ if (alloc(str.str_length+1))
return TRUE;
if ((str_length=str.str_length))
bmove(Ptr,str.Ptr,str_length); // May be overlapping
@@ -246,7 +254,7 @@ bool Binary_string::copy(const Binary_string &str)
bool Binary_string::copy(const char *str, size_t arg_length)
{
DBUG_ASSERT(arg_length < UINT_MAX32);
- if (alloc(arg_length))
+ if (alloc(arg_length+1))
return TRUE;
if (Ptr == str && arg_length == uint32(str_length))
{
@@ -272,7 +280,7 @@ bool Binary_string::copy(const char *str, size_t arg_length)
bool Binary_string::copy_or_move(const char *str, size_t arg_length)
{
DBUG_ASSERT(arg_length < UINT_MAX32);
- if (alloc(arg_length))
+ if (alloc(arg_length+1))
return TRUE;
if ((str_length=uint32(arg_length)))
memmove(Ptr,str,arg_length);
@@ -1251,24 +1259,16 @@ bool String::append_semi_hex(const char *s, uint len, CHARSET_INFO *cs)
return false;
}
+
// Shrink the buffer, but only if it is allocated on the heap.
void Binary_string::shrink(size_t arg_length)
{
- if (!is_alloced())
- return;
- if (ALIGN_SIZE(arg_length + 1) < Alloced_length)
- {
- char* new_ptr;
- if (!(new_ptr = (char*)my_realloc(STRING_PSI_MEMORY_KEY, Ptr, arg_length,
- MYF(thread_specific ? MY_THREAD_SPECIFIC : 0))))
- {
- Alloced_length = 0;
- real_alloc(arg_length);
- }
- else
- {
- Ptr = new_ptr;
- Alloced_length = (uint32)arg_length;
- }
- }
+ if (is_alloced() && ALIGN_SIZE(arg_length + 1) < Alloced_length)
+ {
+ /* my_realloc() can't fail as new buffer is less than the original one */
+ Ptr= (char*) my_realloc(STRING_PSI_MEMORY_KEY, Ptr, arg_length,
+ MYF(thread_specific ?
+ MY_THREAD_SPECIFIC : 0));
+ Alloced_length= (uint32) arg_length;
+ }
}