summaryrefslogtreecommitdiff
path: root/ACE/apps/JAWS3/jaws3/Cached_Allocator_T.h
blob: 589d5710e47749fe903c2d1f734cb4ac66ead644 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/* -*- c++ -*- */
// $Id$

#ifndef JAWS_CACHED_ALLOCATOR_T_H
#define JAWS_CACHED_ALLOCATOR_T_H

#include "ace/ACE.h"
#include "ace/Synch.h"
#include "ace/Malloc.h"
#include "ace/Free_List.h"

#define JAWS_DEFAULT_ALLOCATOR_CHUNKS 10
#define JAWS_CACHED_ALLOCATOR(T) \
        JAWS_Cached_Allocator< T, ACE_SYNCH_NULL_MUTEX >

template <class T, class ACE_LOCK>
class JAWS_Cached_Allocator : public ACE_New_Allocator
// = TITLE
//   Create a cached memory pool with <n_chunks> chunks each with
//   sizeof (<TYPE>) size.
//
// = DESCRIPTION
//   This class enables caching of dynamically allocated,
//   fixed-sized classes.
{
public:

  JAWS_Cached_Allocator (size_t n_chunks = JAWS_DEFAULT_ALLOCATOR_CHUNKS);

  ~JAWS_Cached_Allocator (void);

  void* malloc (size_t);
  // get a chunk of memory from free store.

  void free (void *);
  // return a chunk of memory back to free store.

protected:

  char * get_next_pool (char *pool);

  void set_next_pool (char *pool, char *next_pool);

  void extend_pool (void);

private:

  size_t pool_size_;

  char *pool_head_;
  // Head of memory pool.

  char *pool_tail_;
  // Tail of memory pool.

  ACE_Locked_Free_List<ACE_Cached_Mem_Pool_Node<T>, ACE_LOCK> free_list_;
  // Maintain a cached memory free list.

};



template <class T>
class JAWS_TSS_Cached_Allocator : public ACE_New_Allocator
// = TITLE
//   Create a thread specific cached memory pool with <n_chunks>
//   chunks each with sizeof (<TYPE>) size.
//
// = DESCRIPTION
//   This class enables caching of dynamically allocated,
//   fixed-sized classes.
{
public: 

  JAWS_TSS_Cached_Allocator (size_t n_chunks = JAWS_DEFAULT_ALLOCATOR_CHUNKS);

  ~JAWS_TSS_Cached_Allocator (void);

  void * malloc (size_t);
  // get a chunk of memory from free store.

  void free (void *);
  // return a chunk of memory back to free store.

protected:

  JAWS_Cached_Allocator<T, ACE_SYNCH_NULL_MUTEX> * ts_allocator (void);

private:

  size_t n_chunks_;

  ACE_TSS_TYPE (JAWS_CACHED_ALLOCATOR(T)) ts_allocator_;

};


#if defined (ACE_TEMPLATES_REQUIRE_SOURCE)
#include "jaws3/Cached_Allocator_T.cpp"
#endif /* ACE_TEMPLATES_REQUIRE_SOURCE */

#if defined (ACE_TEMPLATES_REQUIRE_PRAGMA)
#pragma implementation ("Cached_Allocator_T.cpp")
#endif /* ACE_TEMPLATES_REQUIRE_PRAGMA */


#endif /* JAWS_CACHED_ALLOCATOR_T_H */