~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/memory/root.h

  • Committer: Olaf van der Spek
  • Date: 2011-06-23 11:44:30 UTC
  • mto: This revision was merged to the branch mainline in revision 2348.
  • Revision ID: olafvdspek@gmail.com-20110623114430-no355yypk4y3icqb
Refactor

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 * @brief Memory root declarations
19
19
 */
20
20
 
21
 
#ifndef DRIZZLED_MEMORY_ROOT_H
22
 
#define DRIZZLED_MEMORY_ROOT_H
23
 
 
24
 
#include <cstddef>
25
 
 
 
21
#pragma once
 
22
 
 
23
#include <drizzled/common_fwd.h>
26
24
#include <drizzled/definitions.h>
 
25
#include <drizzled/visibility.h>
27
26
 
28
 
namespace drizzled
29
 
{
 
27
namespace drizzled {
30
28
 
31
29
/**
32
30
 * @namespace drizzled::memory
36
34
 * comment on the namespace Doxygen won't extract any documentation for
37
35
 * namespace members.
38
36
 */
39
 
namespace memory
40
 
{
 
37
namespace memory {
41
38
 
42
39
static const int KEEP_PREALLOC= 1;
43
40
/* move used to free list and reuse them */
44
41
static const int MARK_BLOCKS_FREE= 2;
45
42
 
46
 
namespace internal
 
43
namespace internal 
47
44
{
48
 
 
49
 
class UsedMemory
50
 
{                          /* struct for once_alloc (block) */
51
 
public:
52
 
  UsedMemory *next;        /* Next block in use */
53
 
  size_t left;             /* memory left in block  */            
54
 
  size_t size;             /* size of block */
55
 
};
56
 
 
 
45
  class UsedMemory
 
46
  {                        /* struct for once_alloc (block) */
 
47
  public:
 
48
    UsedMemory *next;      /* Next block in use */
 
49
    size_t left;                   /* memory left in block  */            
 
50
    size_t size;                   /* size of block */
 
51
  };
57
52
}
58
53
 
59
54
static const size_t ROOT_MIN_BLOCK_SIZE= (MALLOC_OVERHEAD + sizeof(internal::UsedMemory) + 8);
60
55
 
61
 
 
62
 
 
63
 
class Root
 
56
class DRIZZLED_API Root
64
57
{
65
58
public:
66
 
 
67
59
  Root() :
68
60
    free(0),
69
61
    used(0),
71
63
    min_malloc(0),
72
64
    block_size(0),
73
65
    block_num(0),
74
 
    first_block_usage(0),
75
 
    error_handler(0)
 
66
    first_block_usage(0)
76
67
  { }
77
68
 
78
69
  Root(size_t block_size_arg)
82
73
    block_size= block_size_arg - memory::ROOT_MIN_BLOCK_SIZE;
83
74
    block_num= 4;                       /* We shift this with >>2 */
84
75
    first_block_usage= 0;
85
 
    error_handler= 0;
86
76
  }
87
77
 
88
 
  ~Root();
89
 
 
90
78
  /**
91
79
   * blocks with free memory in it 
92
80
   */
116
104
   */
117
105
  unsigned int first_block_usage;
118
106
 
119
 
  void (*error_handler)(void);
120
 
  void reset_root_defaults(size_t block_size, size_t prealloc_size);
121
 
  void *alloc_root(size_t Size);
 
107
  void reset_defaults(size_t block_size, size_t prealloc_size);
 
108
  unsigned char* alloc(size_t Size);
122
109
  void mark_blocks_free();
123
 
  void *memdup_root(const void *str, size_t len);
124
 
  char *strdup_root(const char *str);
125
 
  char *strmake_root(const char *str,size_t len);
126
 
  void init_alloc_root(size_t block_size= ROOT_MIN_BLOCK_SIZE);
 
110
  void* memdup(const void*, size_t);
 
111
  char* strdup(const char*);
 
112
 
 
113
  char* strmake(const char*, size_t);
 
114
  char* strmake(const std::string&);
 
115
  char* strmake(const String&);
 
116
  void init(size_t block_size= ROOT_MIN_BLOCK_SIZE);
127
117
 
128
118
  inline bool alloc_root_inited()
129
119
  {
130
120
    return min_malloc != 0;
131
121
  }
132
122
  void free_root(myf MyFLAGS);
133
 
  void *multi_alloc_root(int unused, ...);
 
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
  }
134
131
};
135
132
 
136
133
} /* namespace memory */
137
134
} /* namespace drizzled */
138
135
 
139
 
#endif /* DRIZZLED_MEMORY_ROOT_H */
 
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
}