RexxActivationStack.hpp
Go to the documentation of this file.
1 /*----------------------------------------------------------------------------*/
2 /* */
3 /* Copyright (c) 1995, 2004 IBM Corporation. All rights reserved. */
4 /* Copyright (c) 2005-2009 Rexx Language Association. All rights reserved. */
5 /* */
6 /* This program and the accompanying materials are made available under */
7 /* the terms of the Common Public License v1.0 which accompanies this */
8 /* distribution. A copy is also available at the following address: */
9 /* http://www.oorexx.org/license.html */
10 /* */
11 /* Redistribution and use in source and binary forms, with or */
12 /* without modification, are permitted provided that the following */
13 /* conditions are met: */
14 /* */
15 /* Redistributions of source code must retain the above copyright */
16 /* notice, this list of conditions and the following disclaimer. */
17 /* Redistributions in binary form must reproduce the above copyright */
18 /* notice, this list of conditions and the following disclaimer in */
19 /* the documentation and/or other materials provided with the distribution. */
20 /* */
21 /* Neither the name of Rexx Language Association nor the names */
22 /* of its contributors may be used to endorse or promote products */
23 /* derived from this software without specific prior written permission. */
24 /* */
25 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS */
26 /* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT */
27 /* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS */
28 /* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT */
29 /* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, */
30 /* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED */
31 /* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, */
32 /* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY */
33 /* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING */
34 /* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
35 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
36 /* */
37 /*----------------------------------------------------------------------------*/
38 /******************************************************************************/
39 /* REXX Kernel RexxActivationStack.hpp */
40 /* */
41 /* Primitive Activation Stack Frame Definitions */
42 /* */
43 /******************************************************************************/
44 
45 #ifndef Included_RexxActivationStack
46 #define Included_RexxActivationStack
47 
48 
50  public:
51  inline void *operator new(size_t size, void *ptr) { return ptr;};
52  inline void operator delete(void *) { ; }
53  inline void operator delete(void *, void *) { ; }
54 
56  inline RexxActivationFrameBuffer(RESTORETYPE restoreType) { ; }
57  void live(size_t);
58  void liveGeneral(int reason);
59  void flatten(RexxEnvelope *);
60 
61  inline bool hasCapacity(size_t entries) { return size - next >= entries; }
62  inline RexxObject **allocateFrame(size_t entries)
63  {
64  RexxObject **frame = &buffer[next];
65  next += entries;
66  return frame;
67  }
68 
69  inline bool contains(RexxObject **frame)
70  {
71  return frame >= &buffer[0] && frame <= &buffer[size];
72  }
73 
74  inline void releaseFrame(RexxObject **frame)
75  {
76  next = frame - &buffer[0];
77  }
78 
80  {
81  previous = p; // chain this up
82  }
83 
84 
85  inline void reset() { next = 0; } // reset a cached frame buffer
86 
88 
89  static RexxActivationFrameBuffer *newInstance(size_t);
90 
91 protected:
92 
93 
94  size_t size; /* size of the buffer (in slots) */
95  size_t next; /* location of next allocation */
96  RexxActivationFrameBuffer *previous;/* previous entry in the stack */
97  RexxObject *buffer[1]; /* start of the buffer location */
98 };
99 
100 
102  public:
103 
104  enum { DefaultFrameBufferSize = 2048 };
105 
106  inline void *operator new(size_t size, void *ptr) { return ptr;};
108  void live(size_t);
109  void liveGeneral(int reason);
110 
111  void init();
112  void expandCapacity(size_t entries);
113 
114  inline void ensureCapacity(size_t entries) { if (!current->hasCapacity(entries)) { expandCapacity(entries); } }
115  inline RexxObject **allocateFrame(size_t entries)
116  {
117  /* make sure we have space first */
118  ensureCapacity(entries);
119  /* now allocate from the current stack buffer */
120  return current->allocateFrame(entries);
121  }
122  void releaseFrame(RexxObject **frame)
123  {
124  /* we may be popping back one or more buffers. We deactivate */
125  /* the newer ones */
126  while (!current->contains(frame)) {
127  /* we need to pop at least one buffer off of the stack */
129  current = released->getPrevious();
130  /* we'll keep at least one buffer around for reuse. If */
131  /* we've already got one in the cache, just let this one */
132  /* get GCed. */
133  if (unused == OREF_NULL) {
134  unused = released;
135  unused->reset(); // reset to clean state
136  }
137  }
138 
139  /* now back this up to the release point */
140  current->releaseFrame(frame);
141  }
142 
143 protected:
144 
147 };
148 
149 
151 
152 #endif
RESTORETYPE
Definition: ObjectClass.hpp:82
RexxActivationFrameBuffer * new_activationFrameBuffer(size_t s)
#define OREF_NULL
Definition: RexxCore.h:61
RexxActivationFrameBuffer * previous
void push(RexxActivationFrameBuffer *p)
static RexxActivationFrameBuffer * newInstance(size_t)
RexxObject ** allocateFrame(size_t entries)
void releaseFrame(RexxObject **frame)
void flatten(RexxEnvelope *)
bool contains(RexxObject **frame)
RexxActivationFrameBuffer * getPrevious()
bool hasCapacity(size_t entries)
RexxActivationFrameBuffer(RESTORETYPE restoreType)
void expandCapacity(size_t entries)
void liveGeneral(int reason)
RexxObject ** allocateFrame(size_t entries)
RexxActivationFrameBuffer * current
void ensureCapacity(size_t entries)
RexxActivationFrameBuffer * unused
void releaseFrame(RexxObject **frame)