summaryrefslogtreecommitdiff
path: root/ace/Obstack.h
blob: 1500e1e98637d960d30b1e77a37a255166cdaa85 (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* -*- C++ -*- */

//=============================================================================
/**
 *  @file    Obstack.h
 *
 *  $Id$
 *
 *  @author Doug Schmidt
 */
//=============================================================================


#ifndef ACE_OBSTACK_H
#define ACE_OBSTACK_H
#include "ace/pre.h"

#include "ace/Malloc.h"

#if !defined (ACE_LACKS_PRAGMA_ONCE)
# pragma once
#endif /* ACE_LACKS_PRAGMA_ONCE */

/**
 * @class ACE_Obchunk
 *
 * @brief Defines the state that represents a "chunk" of memory.
 */
class ACE_Export ACE_Obchunk
{
public:
  friend class ACE_Obstack;

  /// Constructor.
  ACE_Obchunk (size_t size);

  /// dtor.
  ~ACE_Obchunk (void);

  /// Dump the state of an object.
  void dump (void) const;

  /// Declare the dynamic allocation hooks.
  ACE_ALLOC_HOOK_DECLARE;

private:
  /// Pointer to the end of the chunk.
  ACE_TCHAR *end_;

  /// Pointer to the current location in the chunk.
  ACE_TCHAR *cur_;

  /// Next chunk in the chain.
  ACE_Obchunk *next_;

  /**
   * Pointer to the beginning contents of this chunk.  This field is
   * actually overlayed by the memory allocated by
   * <ACE_Obstack::new_chunk>.  Therefore, it *must* come last.
   */
  ACE_TCHAR contents_[4];
};

/**
 * @class ACE_Obstack
 *
 * @brief Define a simple "mark and release" memory allocation utility.
 *
 * The implementation is similar to the GNU obstack utility,
 * which is used extensively in the GCC compiler.
 */
class ACE_Export ACE_Obstack
{
public:
  // = Initialization and termination methods.
  ACE_Obstack (size_t size = 4096 - sizeof (ACE_Obchunk),
               ACE_Allocator *allocator_strategy = 0);
  ~ACE_Obstack (void);

  /// Copy the data into the current Obchunk.
  ACE_TCHAR *copy (const ACE_TCHAR *data,
                   size_t len);

  /// "Release" the entire stack of Obchunks, putting it back on the
  /// free list.
  void release (void);

  /// Dump the state of an object.
  void dump (void) const;

  /// Declare the dynamic allocation hooks.
  ACE_ALLOC_HOOK_DECLARE;

protected:
  class ACE_Obchunk *new_chunk (void);

  /// Pointer to the allocator used by this Obstack.
  ACE_Allocator *allocator_strategy_;

  /// Current size of the Obstack;
  size_t size_;

  // = Don't change the order of the following two fields.
/**
 * @class ACE_Obchunk
 Head of the Obchunk chain.
 */
  class ACE_Obchunk *head_;

/**
 * @class ACE_Obchunk
 Pointer to the current Obchunk.
 */
  class ACE_Obchunk *curr_;
};

#if defined (__ACE_INLINE__)
#include "ace/Obstack.i"
#endif /* __ACE_INLINE__ */

#include "ace/post.h"
#endif /* ACE_OBSTACK_H */