Drizzled Public API Documentation

root.h
Go to the documentation of this file.
1 /* Copyright (C) 2000 MySQL 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; version 2 of the License.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  GNU General Public License for more details.
11 
12  You should have received a copy of the GNU General Public License
13  along with this program; if not, write to the Free Software
14  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15 
21 #pragma once
22 
23 #include <drizzled/common_fwd.h>
24 #include <drizzled/definitions.h>
25 #include <drizzled/util/data_ref.h>
26 #include <drizzled/visibility.h>
27 
28 namespace drizzled {
29 
38 namespace memory {
39 
40 static const int KEEP_PREALLOC= 1;
41 /* move used to free list and reuse them */
42 static const int MARK_BLOCKS_FREE= 2;
43 
44 namespace internal
45 {
46  class UsedMemory
47  { /* struct for once_alloc (block) */
48  public:
49  UsedMemory *next; /* Next block in use */
50  size_t left; /* memory left in block */
51  size_t size; /* size of block */
52  };
53 }
54 
55 static const size_t ROOT_MIN_BLOCK_SIZE= (MALLOC_OVERHEAD + sizeof(internal::UsedMemory) + 8);
56 
58 {
59 public:
60  Root() :
61  free(0),
62  used(0),
63  pre_alloc(0),
64  min_malloc(0),
65  block_size(0),
66  block_num(0),
67  first_block_usage(0)
68  { }
69 
70  Root(size_t block_size_arg)
71  {
72  free= used= pre_alloc= 0;
73  min_malloc= 32;
74  block_size= block_size_arg - memory::ROOT_MIN_BLOCK_SIZE;
75  block_num= 4; /* We shift this with >>2 */
76  first_block_usage= 0;
77  }
78 
83 
88 
93 
97  size_t min_malloc;
98 
99  size_t block_size;
100  unsigned int block_num;
101 
106  unsigned int first_block_usage;
107 
108  void reset_defaults(size_t block_size, size_t prealloc_size);
109  unsigned char* alloc(size_t Size);
110  void mark_blocks_free();
111  void* memdup(const void*, size_t);
112  char* strdup(const char*);
113 
114  char* strdup(const char*, size_t);
115  char* strdup(str_ref);
116  void init(size_t block_size= ROOT_MIN_BLOCK_SIZE);
117 
118  bool alloc_root_inited() const
119  {
120  return min_malloc != 0;
121  }
122  void free_root(myf MyFLAGS);
123  void* multi_alloc(int unused, ...);
124 
125  void* calloc(size_t size)
126  {
127  void* ptr= alloc(size);
128  memset(ptr, 0, size);
129  return ptr;
130  }
131 };
132 
133 } /* namespace memory */
134 } /* namespace drizzled */
135 
136 inline void* operator new(size_t size, drizzled::memory::Root& root)
137 {
138  return root.alloc(size);
139 }
140 
141 inline void* operator new[](size_t size, drizzled::memory::Root& root)
142 {
143  return root.alloc(size);
144 }
145 
146 inline void operator delete(void*, drizzled::memory::Root&)
147 {
148 }
149 
150 inline void operator delete[](void*, drizzled::memory::Root&)
151 {
152 }
internal::UsedMemory * free
Definition: root.h:82
internal::UsedMemory * used
Definition: root.h:87
unsigned int block_num
allocated blocks counter
Definition: root.h:100
#define DRIZZLED_API
Definition: visibility.h:62
unsigned int first_block_usage
Definition: root.h:106
Visibility Control Macros.
size_t min_malloc
Definition: root.h:97
internal::UsedMemory * pre_alloc
Definition: root.h:92
size_t block_size
initial block size
Definition: root.h:99