[MDEV-31950] Cleanup: Move MEM_ROOT allocation methods from THD to Query_arena Created: 2023-08-18  Updated: 2023-09-17  Resolved: 2023-08-18

Status: Closed
Project: MariaDB Server
Component/s: OTHER
Fix Version/s: 11.3.0

Type: Task Priority: Critical
Reporter: Alexander Barkov Assignee: Alexander Barkov
Resolution: Fixed Votes: 0
Labels: None

Issue Links:
Blocks
blocks MDEV-31606 Refactor check_db_name() to get a con... Closed

 Description   

The class Query_arena has a number of methods allocating data on Query_arena::mem_root:

  inline void* alloc(size_t size) const { return alloc_root(mem_root,size); }
  inline void* calloc(size_t size) const
  {
    void *ptr;
    if (likely((ptr=alloc_root(mem_root,size))))
      bzero(ptr, size);
    return ptr;
  }
  inline char *strdup(const char *str) const
  { return strdup_root(mem_root,str); }
  inline char *strmake(const char *str, size_t size) const
  { return strmake_root(mem_root,str,size); }
  inline LEX_CSTRING strcat(const LEX_CSTRING &a, const LEX_CSTRING &b) const
  {
    char *buf= (char*)alloc(a.length + b.length + 1);
    if (unlikely(!buf))
      return null_clex_str;
    memcpy(buf, a.str, a.length);
    memcpy(buf + a.length, b.str, b.length);
    buf[a.length + b.length] = 0;
    return {buf, a.length + b.length};
  }
  inline void *memdup(const void *str, size_t size) const
  { return memdup_root(mem_root,str,size); }
  inline void *memdup_w_gap(const void *str, size_t size, size_t gap) const
  {
    void *ptr;
    if (likely((ptr= alloc_root(mem_root,size+gap))))
      memcpy(ptr,str,size);
    return ptr;
  }

Additionally, {{class THD} has more methods allocating data on Query_arena::mem_root:

  LEX_CSTRING strmake_lex_cstring(const char *str, size_t length) const
  {
    const char *tmp= strmake_root(mem_root, str, length);
    if (!tmp)
      return {0,0};
    return {tmp, length};
  }
  LEX_CSTRING strmake_lex_cstring(const LEX_CSTRING &from) const
  {
    return strmake_lex_cstring(from.str, from.length);
  }
 
  LEX_STRING *make_lex_string(LEX_STRING *lex_str, const char* str,
                              size_t length) const
  {
    if (!(lex_str->str= strmake_root(mem_root, str, length)))
    {
      lex_str->length= 0;
      return 0;
    }
    lex_str->length= length;
    return lex_str;
  }
  LEX_CSTRING *make_lex_string(LEX_CSTRING *lex_str, const char* str,
                               size_t length) const
  {
    if (!(lex_str->str= strmake_root(mem_root, str, length)))
    {
      lex_str->length= 0;
      return 0;
    }
    lex_str->length= length;
    return lex_str;
  }
};

These methods use nothing of THD except Query_arena::mem_root.

Obviously, these methods are in a wrong place. Let's move them to Query_arena, so all MEM_ROOT allocating methods reside in the same place.

Note, MDEV-31606 will add some more MEM_ROOT allocating methods to Query_arena. It's better to collect all allocating methods into one place.


Generated at Thu Feb 08 10:27:41 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.