Drizzled Public API Documentation

root.h
Go to the documentation of this file.
00001 /* Copyright (C) 2000 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 
00021 #pragma once
00022 
00023 #include <drizzled/common_fwd.h>
00024 #include <drizzled/definitions.h>
00025 #include <drizzled/util/data_ref.h>
00026 #include <drizzled/visibility.h>
00027 
00028 namespace drizzled {
00029 
00038 namespace memory {
00039 
00040 static const int KEEP_PREALLOC= 1;
00041 /* move used to free list and reuse them */
00042 static const int MARK_BLOCKS_FREE= 2;
00043 
00044 namespace internal 
00045 {
00046   class UsedMemory
00047   {        /* struct for once_alloc (block) */
00048   public:
00049     UsedMemory *next;    /* Next block in use */
00050     size_t left;       /* memory left in block  */            
00051     size_t size;       /* size of block */
00052   };
00053 }
00054 
00055 static const size_t ROOT_MIN_BLOCK_SIZE= (MALLOC_OVERHEAD + sizeof(internal::UsedMemory) + 8);
00056 
00057 class DRIZZLED_API Root
00058 {
00059 public:
00060   Root() :
00061     free(0),
00062     used(0),
00063     pre_alloc(0),
00064     min_malloc(0),
00065     block_size(0),
00066     block_num(0),
00067     first_block_usage(0)
00068   { }
00069 
00070   Root(size_t block_size_arg)
00071   {
00072     free= used= pre_alloc= 0;
00073     min_malloc= 32;
00074     block_size= block_size_arg - memory::ROOT_MIN_BLOCK_SIZE;
00075     block_num= 4;     /* We shift this with >>2 */
00076     first_block_usage= 0;
00077   }
00078 
00082   internal::UsedMemory *free;
00083 
00087   internal::UsedMemory *used;
00088 
00092   internal::UsedMemory *pre_alloc;
00093 
00097   size_t min_malloc;
00098 
00099   size_t block_size;         
00100   unsigned int block_num;    
00101 
00106   unsigned int first_block_usage;
00107 
00108   void reset_defaults(size_t block_size, size_t prealloc_size);
00109   unsigned char* alloc(size_t Size);
00110   void mark_blocks_free();
00111   void* memdup(const void*, size_t);
00112   char* strdup(const char*);
00113 
00114   char* strdup(const char*, size_t);
00115   char* strdup(str_ref);
00116   void init(size_t block_size= ROOT_MIN_BLOCK_SIZE);
00117 
00118   bool alloc_root_inited() const
00119   {
00120     return min_malloc != 0;
00121   }
00122   void free_root(myf MyFLAGS);
00123   void* multi_alloc(int unused, ...);
00124 
00125   void* calloc(size_t size)
00126   {
00127     void* ptr= alloc(size);
00128     memset(ptr, 0, size);
00129     return ptr;
00130   }
00131 };
00132 
00133 } /* namespace memory */
00134 } /* namespace drizzled */
00135 
00136 inline void* operator new(size_t size, drizzled::memory::Root& root)
00137 {
00138   return root.alloc(size);
00139 }
00140 
00141 inline void* operator new[](size_t size, drizzled::memory::Root& root)
00142 {
00143   return root.alloc(size);
00144 }
00145 
00146 inline void operator delete(void*, drizzled::memory::Root&)
00147 {
00148 }
00149 
00150 inline void operator delete[](void*, drizzled::memory::Root&)
00151 {
00152 }