thr_malloc.cc
上传用户:tsgydb
上传日期:2007-04-14
资源大小:10674k
文件大小:2k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. /* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult 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
  5.    the Free Software Foundation; either version 2 of the License, or
  6.    (at your option) any later version.
  7.    
  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.
  12.    
  13.    You should have received a copy of the GNU General Public License
  14.    along with this program; if not, write to the Free Software
  15.    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
  16. /* Mallocs for used in threads */
  17. #include "mysql_priv.h"
  18. extern "C" {
  19.   void sql_alloc_error_handler(void)
  20.   {
  21.     current_thd->fatal_error=1; /* purecov: inspected */
  22.     sql_print_error(ER(ER_OUT_OF_RESOURCES));
  23.   }
  24. }
  25. void init_sql_alloc(MEM_ROOT *mem_root, uint block_size, uint pre_alloc)
  26. {
  27.   init_alloc_root(mem_root, block_size, pre_alloc);
  28.   mem_root->error_handler=sql_alloc_error_handler;
  29. }
  30. gptr sql_alloc(uint Size)
  31. {
  32.   MEM_ROOT *root=my_pthread_getspecific_ptr(MEM_ROOT*,THR_MALLOC);
  33.   char *ptr= (char*) alloc_root(root,Size);
  34.   return ptr;
  35. }
  36. gptr sql_calloc(uint size)
  37. {
  38.   gptr ptr;
  39.   if ((ptr=sql_alloc(size)))
  40.     bzero((char*) ptr,size);
  41.   return ptr;
  42. }
  43. char *sql_strdup(const char *str)
  44. {
  45.   uint len=(uint) strlen(str)+1;
  46.   char *pos;
  47.   if ((pos= (char*) sql_alloc(len)))
  48.     memcpy(pos,str,len);
  49.   return pos;
  50. }
  51. char *sql_strmake(const char *str,uint len)
  52. {
  53.   char *pos;
  54.   if ((pos= (char*) sql_alloc(len+1)))
  55.   {
  56.     memcpy(pos,str,len);
  57.     pos[len]=0;
  58.   }
  59.   return pos;
  60. }
  61. gptr sql_memdup(const void *ptr,uint len)
  62. {
  63.   char *pos;
  64.   if ((pos= (char*) sql_alloc(len)))
  65.     memcpy(pos,ptr,len);
  66.   return pos;
  67. }
  68. void sql_element_free(void *ptr __attribute__((unused)))
  69. {} /* purecov: deadcode */