~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/my_alloc.h

  • Committer: Brian Aker
  • Date: 2009-02-10 00:14:40 UTC
  • Revision ID: brian@tangent.org-20090210001440-qjg8eofh3h93064b
Adding Multi-threaded Scheduler into the system.

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 
12
12
   You should have received a copy of the GNU General Public License
13
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
 
 
16
 
/**
17
 
 * @file
18
 
 * @brief Memory root declarations
19
 
 */
20
 
 
21
 
 
22
 
 
23
 
#ifndef DRIZZLED_MEMORY_ROOT_H
24
 
#define DRIZZLED_MEMORY_ROOT_H
25
 
 
26
 
#include <cstddef>
27
 
 
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
15
 
 
16
/*
 
17
   Data structures for mysys/my_alloc.c (root memory allocator)
 
18
*/
 
19
 
 
20
#ifndef _my_alloc_h
 
21
#define _my_alloc_h
 
22
 
 
23
#include <stddef.h>
28
24
#include <drizzled/definitions.h>
29
25
 
30
 
#include "drizzled/visibility.h"
31
 
 
32
 
namespace drizzled
33
 
{
34
 
 
35
 
/**
36
 
 * @namespace drizzled::memory
37
 
 * Memory allocation utils
38
 
 *
39
 
 * NB: This namespace documentation may not seem very useful, but without a
40
 
 * comment on the namespace Doxygen won't extract any documentation for
41
 
 * namespace members.
42
 
 */
43
 
namespace memory
44
 
{
45
 
 
46
 
static const int KEEP_PREALLOC= 1;
47
 
/* move used to free list and reuse them */
48
 
static const int MARK_BLOCKS_FREE= 2;
49
 
 
50
 
namespace internal
51
 
{
52
 
 
53
 
class UsedMemory
54
 
{                          /* struct for once_alloc (block) */
55
 
public:
56
 
  UsedMemory *next;        /* Next block in use */
57
 
  size_t left;             /* memory left in block  */            
58
 
  size_t size;             /* size of block */
59
 
};
60
 
 
61
 
}
62
 
 
63
 
static const size_t ROOT_MIN_BLOCK_SIZE= (MALLOC_OVERHEAD + sizeof(internal::UsedMemory) + 8);
64
 
 
65
 
 
66
 
 
67
 
class DRIZZLED_API Root
68
 
{
69
 
public:
70
 
 
71
 
  Root() :
72
 
    free(0),
73
 
    used(0),
74
 
    pre_alloc(0),
75
 
    min_malloc(0),
76
 
    block_size(0),
77
 
    block_num(0),
78
 
    first_block_usage(0),
79
 
    error_handler(0)
80
 
  { }
81
 
 
82
 
  Root(size_t block_size_arg)
83
 
  {
84
 
    free= used= pre_alloc= 0;
85
 
    min_malloc= 32;
86
 
    block_size= block_size_arg - memory::ROOT_MIN_BLOCK_SIZE;
87
 
    block_num= 4;                       /* We shift this with >>2 */
88
 
    first_block_usage= 0;
89
 
    error_handler= 0;
90
 
  }
91
 
 
92
 
  ~Root();
93
 
 
94
 
  /**
95
 
   * blocks with free memory in it 
96
 
   */
97
 
  internal::UsedMemory *free;
98
 
 
99
 
  /**
100
 
   * blocks almost without free memory 
101
 
   */
102
 
  internal::UsedMemory *used;
103
 
 
104
 
  /**
105
 
   * preallocated block 
106
 
   */
107
 
  internal::UsedMemory *pre_alloc;
108
 
 
109
 
  /**
110
 
   * if block have less memory it will be put in 'used' list 
111
 
   */
 
26
#if defined(__cplusplus)
 
27
extern "C" {
 
28
#endif
 
29
 
 
30
#define ALLOC_MAX_BLOCK_TO_DROP                 4096
 
31
#define ALLOC_MAX_BLOCK_USAGE_BEFORE_DROP       10
 
32
 
 
33
typedef struct st_used_mem
 
34
{                                  /* struct for once_alloc (block) */
 
35
  struct st_used_mem *next;        /* Next block in use */
 
36
  unsigned int  left;              /* memory left in block  */
 
37
  unsigned int  size;              /* size of block */
 
38
} USED_MEM;
 
39
 
 
40
 
 
41
typedef struct st_mem_root
 
42
{
 
43
  USED_MEM *free;                  /* blocks with free memory in it */
 
44
  USED_MEM *used;                  /* blocks almost without free memory */
 
45
  USED_MEM *pre_alloc;             /* preallocated block */
 
46
  /* if block have less memory it will be put in 'used' list */
112
47
  size_t min_malloc;
113
 
 
114
 
  size_t block_size;         ///< initial block size
115
 
  unsigned int block_num;    ///< allocated blocks counter 
116
 
 
117
 
  /**
118
 
   * first free block in queue test counter (if it exceed
119
 
   * MAX_BLOCK_USAGE_BEFORE_DROP block will be dropped in 'used' list)
120
 
   */
 
48
  size_t block_size;               /* initial block size */
 
49
  unsigned int block_num;          /* allocated blocks counter */
 
50
  /*
 
51
     first free block in queue test counter (if it exceed
 
52
     MAX_BLOCK_USAGE_BEFORE_DROP block will be dropped in 'used' list)
 
53
  */
121
54
  unsigned int first_block_usage;
122
55
 
123
56
  void (*error_handler)(void);
124
 
  void reset_root_defaults(size_t block_size, size_t prealloc_size);
125
 
  void *alloc_root(size_t Size);
126
 
  void mark_blocks_free();
127
 
  void *memdup_root(const void *str, size_t len);
128
 
  char *strdup_root(const char *str);
129
 
  char *strmake_root(const char *str,size_t len);
130
 
  void init_alloc_root(size_t block_size= ROOT_MIN_BLOCK_SIZE);
131
 
 
132
 
  inline bool alloc_root_inited()
133
 
  {
134
 
    return min_malloc != 0;
135
 
  }
136
 
  void free_root(myf MyFLAGS);
137
 
  void *multi_alloc_root(int unused, ...);
138
 
};
139
 
 
140
 
} /* namespace memory */
141
 
} /* namespace drizzled */
142
 
 
143
 
#endif /* DRIZZLED_MEMORY_ROOT_H */
 
57
} MEM_ROOT;
 
58
 
 
59
void init_alloc_root(MEM_ROOT *mem_root, size_t block_size,
 
60
                     size_t pre_alloc_size);
 
61
void *alloc_root(MEM_ROOT *mem_root, size_t Size);
 
62
void *multi_alloc_root(MEM_ROOT *mem_root, ...);
 
63
void free_root(MEM_ROOT *root, myf MyFLAGS);
 
64
void set_prealloc_root(MEM_ROOT *root, char *ptr);
 
65
void reset_root_defaults(MEM_ROOT *mem_root, size_t block_size,
 
66
                         size_t prealloc_size);
 
67
char *strdup_root(MEM_ROOT *root,const char *str);
 
68
char *strmake_root(MEM_ROOT *root,const char *str,size_t len);
 
69
void *memdup_root(MEM_ROOT *root,const void *str, size_t len);
 
70
 
 
71
#if defined(__cplusplus)
 
72
}
 
73
#endif
 
74
#endif