~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/my_alloc.h

  • Committer: Brian Aker
  • Date: 2009-08-15 00:59:30 UTC
  • mfrom: (1115.1.7 merge)
  • Revision ID: brian@gaz-20090815005930-q47yenjrq1esiwsz
Merge of Trond + Brian

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
   along with this program; if not, write to the Free Software
14
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
15
 
16
 
 
17
 
#ifndef DRIZZLED_MEMORY_ROOT_H
18
 
#define DRIZZLED_MEMORY_ROOT_H
19
 
 
20
 
#include <cstddef>
21
 
 
 
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>
22
24
#include <drizzled/definitions.h>
23
25
 
24
 
namespace drizzled
25
 
{
26
 
namespace memory
27
 
{
28
 
 
29
 
static const int KEEP_PREALLOC= 1;
30
 
/* move used to free list and reuse them */
31
 
static const int MARK_BLOCKS_FREE= 2;
32
 
 
33
 
namespace internal
34
 
{
35
 
 
36
 
class UsedMemory
37
 
{                          /* struct for once_alloc (block) */
38
 
public:
39
 
  UsedMemory *next;        /* Next block in use */
40
 
  size_t left;             /* memory left in block  */            
41
 
  size_t size;             /* size of block */
42
 
};
43
 
 
44
 
}
45
 
 
46
 
static const size_t ROOT_MIN_BLOCK_SIZE= (MALLOC_OVERHEAD + sizeof(internal::UsedMemory) + 8);
47
 
 
48
 
 
49
 
 
50
 
class Root
51
 
{
52
 
public:
53
 
  /* blocks with free memory in it */
54
 
  internal::UsedMemory *free;
55
 
 
56
 
  /* blocks almost without free memory */
57
 
  internal::UsedMemory *used;
58
 
 
59
 
  /* preallocated block */
60
 
  internal::UsedMemory *pre_alloc;
61
 
 
 
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 */
62
46
  /* if block have less memory it will be put in 'used' list */
63
47
  size_t min_malloc;
64
48
  size_t block_size;               /* initial block size */
70
54
  unsigned int first_block_usage;
71
55
 
72
56
  void (*error_handler)(void);
73
 
};
74
 
 
75
 
inline static bool alloc_root_inited(Root *root)
76
 
{
77
 
  return root->min_malloc != 0;
78
 
}
79
 
 
80
 
void init_alloc_root(Root *mem_root,
81
 
                     size_t block_size= ROOT_MIN_BLOCK_SIZE);
82
 
void *alloc_root(Root *mem_root, size_t Size);
83
 
void *multi_alloc_root(Root *mem_root, ...);
84
 
void free_root(Root *root, myf MyFLAGS);
85
 
void set_prealloc_root(Root *root, char *ptr);
86
 
void reset_root_defaults(Root *mem_root, size_t block_size,
 
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,
87
66
                         size_t prealloc_size);
88
 
char *strdup_root(Root *root,const char *str);
89
 
char *strmake_root(Root *root,const char *str,size_t len);
90
 
void *memdup_root(Root *root,const void *str, size_t len);
91
 
 
92
 
} /* namespace memory */
93
 
} /* namespace drizzled */
94
 
 
95
 
#endif /* DRIZZLED_MEMORY_ROOT_H */
 
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