Return a string which has the same value with "from" and which is safe to modify, trying to avoid unnecessary allocation and copying when possible. @param to Buffer. Must not be a constant string. @param from Some existing value. We'll try to reuse it. Can be a constant or a variable string. @param from_length The total size that will be possibly
| 906 | from "from" to "to", then "to" is returned. |
| 907 | */ |
| 908 | String *copy_if_not_alloced(String *to,String *from,uint32 from_length) |
| 909 | { |
| 910 | DBUG_ASSERT(to); |
| 911 | /* |
| 912 | If "from" is a constant string, e.g.: |
| 913 | SELECT INSERT('', <pos>, <length>, <replacement>); |
| 914 | we should not return it. See MDEV-9332. |
| 915 | |
| 916 | The code below detects different string types: |
| 917 | |
| 918 | a. All constant strings have Alloced_length==0 and alloced==false. |
| 919 | They point to a static memory array, or a mem_root memory, |
| 920 | and should stay untouched until the end of their life cycle. |
| 921 | Not safe to reuse. |
| 922 | |
| 923 | b. Some variable string have Alloced_length==0 and alloced==false initially, |
| 924 | they are not bound to any char array and allocate space on the first use |
| 925 | (and become #d). A typical example of such String is Item::str_value. |
| 926 | This type of string could be reused, but there is no a way to distinguish |
| 927 | them from the true constant strings (#a). |
| 928 | Not safe to reuse. |
| 929 | |
| 930 | c. Some variable strings have Alloced_length>0 and alloced==false. |
| 931 | They point to a fixed size writtable char array (typically on stack) |
| 932 | initially but can later allocate more space on the heap when the |
| 933 | fixed size array is too small (these strings become #d after allocation). |
| 934 | Safe to reuse. |
| 935 | |
| 936 | d. Some variable strings have Alloced_length>0 and alloced==true. |
| 937 | They already store data on the heap. |
| 938 | Safe to reuse. |
| 939 | |
| 940 | e. Some strings can have Alloced_length==0 and alloced==true. |
| 941 | This type of strings allocate space on the heap, but then are marked |
| 942 | as constant strings using String::mark_as_const(). |
| 943 | A typical example - the result of a character set conversion |
| 944 | of a constant string. |
| 945 | Not safe to reuse. |
| 946 | */ |
| 947 | if (from->alloced_length() > 0) // "from" is #c or #d (not a constant) |
| 948 | { |
| 949 | if (from->alloced_length() >= from_length) |
| 950 | return from; // #c or #d (large enough to store from_length bytes) |
| 951 | |
| 952 | if (from->is_alloced()) |
| 953 | { |
| 954 | (void) from->realloc(from_length); |
| 955 | return from; // #d (reallocated to fit from_length bytes) |
| 956 | } |
| 957 | /* |
| 958 | "from" is of type #c. It currently points to a writtable char array |
| 959 | (typically on stack), but is too small for "from_length" bytes. |
| 960 | We need to reallocate either "from" or "to". |
| 961 | |
| 962 | "from" typically points to a temporary buffer inside Item_xxx::val_str(), |
| 963 | or to Item::str_value, and thus is "less permanent" than "to". |
| 964 | |
| 965 | Reallocating "to" may give more benefits: |
no test coverage detected