drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
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
20
21#pragma once
22
23#include <drizzled/common_fwd.h>
25#include <drizzled/util/data_ref.h>
26#include <drizzled/visibility.h>
27
28namespace drizzled {
29
38namespace memory {
39
40static const int KEEP_PREALLOC= 1;
41/* move used to free list and reuse them */
42static const int MARK_BLOCKS_FREE= 2;
43
44namespace internal
45{
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
55static const size_t ROOT_MIN_BLOCK_SIZE= (MALLOC_OVERHEAD + sizeof(internal::UsedMemory) + 8);
56
57class DRIZZLED_API Root
58{
59public:
60 Root() :
61 free(0),
62 used(0),
63 pre_alloc(0),
64 min_malloc(0),
65 block_size(0),
66 block_num(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 */
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
136inline void* operator new(size_t size, drizzled::memory::Root& root)
137{
138 return root.alloc(size);
139}
140
141inline void* operator new[](size_t size, drizzled::memory::Root& root)
142{
143 return root.alloc(size);
144}
145
146inline void operator delete(void*, drizzled::memory::Root&)
147{
148}
149
150inline void operator delete[](void*, drizzled::memory::Root&)
151{
152}
unsigned char * alloc(size_t Size)
Allocate a chunk of memory from the Root structure provided, obtaining more memory from the heap if n...
Definition root.cc:140
void * memdup(const void *, size_t)
Duplicate the provided block into memory allocated from within the specified Root.
Definition root.cc:365
unsigned int block_num
allocated blocks counter
Definition root.h:100
unsigned int first_block_usage
Definition root.h:106
void mark_blocks_free()
Mark all data in blocks free for reusage.
Definition root.cc:245
size_t block_size
initial block size
Definition root.h:99
char * strdup(const char *)
Duplicate a null-terminated string into memory allocated from within the specified Root.
Definition root.cc:327
internal::UsedMemory * free
Definition root.h:82
void reset_defaults(size_t block_size, size_t prealloc_size)
Definition root.cc:81
internal::UsedMemory * used
Definition root.h:87
internal::UsedMemory * pre_alloc
Definition root.h:92
void init(size_t block_size=ROOT_MIN_BLOCK_SIZE)
Initialize memory root.
Definition root.cc:57
TODO: Rename this file - func.h is stupid.
Visibility Control Macros.
#define DRIZZLED_API
Definition visibility.h:38