Drizzled Public API Documentation

sql_alloc.cc

00001 /* Copyright (C) 2000-2001, 2003-2004 MySQL AB
00002 
00003    This program is free software; you can redistribute it and/or modify
00004    it under the terms of the GNU General Public License as published by
00005    the Free Software Foundation; version 2 of the License.
00006 
00007    This program is distributed in the hope that it will be useful,
00008    but WITHOUT ANY WARRANTY; without even the implied warranty of
00009    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00010    GNU General Public License for more details.
00011 
00012    You should have received a copy of the GNU General Public License
00013    along with this program; if not, write to the Free Software
00014    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
00015 
00016 
00017 /* Mallocs for used in threads */
00018 
00019 #include <config.h>
00020 
00021 #include <string.h>
00022 
00023 #include <drizzled/errmsg_print.h>
00024 #include <drizzled/memory/sql_alloc.h>
00025 #include <drizzled/current_session.h>
00026 #include <drizzled/error.h>
00027 #include <drizzled/definitions.h>
00028 
00029 #include <drizzled/internal/my_sys.h>
00030 
00031 namespace drizzled
00032 {
00033 
00034 static void sql_alloc_error_handler(void)
00035 {
00036   errmsg_printf(error::ERROR, "%s",ER(ER_OUT_OF_RESOURCES));
00037 }
00038 
00039 void memory::init_sql_alloc(memory::Root *mem_root, size_t block_size, size_t)
00040 {
00041   mem_root->init_alloc_root(block_size);
00042   mem_root->error_handler= sql_alloc_error_handler;
00043 }
00044 
00045 
00046 void *memory::sql_alloc(size_t Size)
00047 {
00048   memory::Root *root= current_mem_root();
00049   return root->alloc_root(Size);
00050 }
00051 
00052 
00053 void *memory::sql_calloc(size_t size)
00054 {
00055   void *ptr;
00056 
00057   if ((ptr=memory::sql_alloc(size)))
00058     memset(ptr, 0, size);
00059 
00060   return ptr;
00061 }
00062 
00063 
00064 char *memory::sql_strdup(const char *str)
00065 {
00066   size_t len= strlen(str)+1;
00067   char *pos;
00068   if ((pos= (char*) memory::sql_alloc(len)))
00069     memcpy(pos,str,len);
00070   return pos;
00071 }
00072 
00073 
00074 char *memory::sql_strmake(const char *str, size_t len)
00075 {
00076   char *pos;
00077   if ((pos= (char*) memory::sql_alloc(len+1)))
00078   {
00079     memcpy(pos,str,len);
00080     pos[len]=0;
00081   }
00082   return pos;
00083 }
00084 
00085 
00086 void* memory::sql_memdup(const void *ptr, size_t len)
00087 {
00088   void *pos;
00089   if ((pos= memory::sql_alloc(len)))
00090     memcpy(pos,ptr,len);
00091   return pos;
00092 }
00093 
00094 void *memory::SqlAlloc::operator new(size_t size)
00095 {
00096   return memory::sql_alloc(size);
00097 }
00098 
00099 void *memory::SqlAlloc::operator new[](size_t size)
00100 {
00101   return memory::sql_alloc(size);
00102 }
00103 
00104 void *memory::SqlAlloc::operator new[](size_t size, memory::Root *mem_root)
00105 {
00106   return mem_root->alloc_root(size);
00107 }
00108 
00109 void *memory::SqlAlloc::operator new(size_t size, memory::Root *mem_root)
00110 {
00111   return mem_root->alloc_root(size);
00112 }
00113 
00114 } /* namespace drizzled */