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 
00023 #pragma once
00024 
00025 #include <cstddef>
00026 
00027 #include <drizzled/definitions.h>
00028 
00029 #include <drizzled/visibility.h>
00030 
00031 namespace drizzled
00032 {
00033 
00042 namespace memory
00043 {
00044 
00045 static const int KEEP_PREALLOC= 1;
00046 /* move used to free list and reuse them */
00047 static const int MARK_BLOCKS_FREE= 2;
00048 
00049 namespace internal
00050 {
00051 
00052 class UsedMemory
00053 {        /* struct for once_alloc (block) */
00054 public:
00055   UsedMemory *next;    /* Next block in use */
00056   size_t left;       /* memory left in block  */            
00057   size_t size;       /* size of block */
00058 };
00059 
00060 }
00061 
00062 static const size_t ROOT_MIN_BLOCK_SIZE= (MALLOC_OVERHEAD + sizeof(internal::UsedMemory) + 8);
00063 
00064 
00065 
00066 class DRIZZLED_API Root
00067 {
00068 public:
00069 
00070   Root() :
00071     free(0),
00072     used(0),
00073     pre_alloc(0),
00074     min_malloc(0),
00075     block_size(0),
00076     block_num(0),
00077     first_block_usage(0),
00078     error_handler(0)
00079   { }
00080 
00081   Root(size_t block_size_arg)
00082   {
00083     free= used= pre_alloc= 0;
00084     min_malloc= 32;
00085     block_size= block_size_arg - memory::ROOT_MIN_BLOCK_SIZE;
00086     block_num= 4;     /* We shift this with >>2 */
00087     first_block_usage= 0;
00088     error_handler= 0;
00089   }
00090 
00091   ~Root();
00092 
00096   internal::UsedMemory *free;
00097 
00101   internal::UsedMemory *used;
00102 
00106   internal::UsedMemory *pre_alloc;
00107 
00111   size_t min_malloc;
00112 
00113   size_t block_size;         
00114   unsigned int block_num;    
00115 
00120   unsigned int first_block_usage;
00121 
00122   void (*error_handler)(void);
00123   void reset_root_defaults(size_t block_size, size_t prealloc_size);
00124   void *alloc_root(size_t Size);
00125   inline void *allocate(size_t Size)
00126   {
00127     return alloc_root(Size);
00128   }
00129   void mark_blocks_free();
00130   void *memdup_root(const void *str, size_t len);
00131   char *strdup_root(const char *str);
00132 
00133   char *strmake_root(const char *str,size_t len);
00134   void init_alloc_root(size_t block_size= ROOT_MIN_BLOCK_SIZE);
00135 
00136   inline void *duplicate(const void *str, size_t len)
00137   {
00138     return memdup_root(str, len);
00139   }
00140 
00141   inline bool alloc_root_inited()
00142   {
00143     return min_malloc != 0;
00144   }
00145   void free_root(myf MyFLAGS);
00146   void *multi_alloc_root(int unused, ...);
00147 };
00148 
00149 } /* namespace memory */
00150 } /* namespace drizzled */
00151