arena.hpp revision 1472:c18cbe5936b8
1/*
2 * Copyright (c) 1998, 2002, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25// All classes in the virtual machine must be subclassed
26// by one of the following allocation classes:
27//
28//
29// For objects allocated in the C-heap (managed by: free & malloc).
30// - CHeapObj
31//
32//
33// For embedded objects.
34// - ValueObj
35//
36// For classes used as name spaces.
37// - AllStatic
38//
39
40class CHeapObj {
41 public:
42  void* operator new(size_t size);
43  void  operator delete(void* p);
44  void* new_array(size_t size);
45};
46
47
48// Base class for objects used as value objects.
49// Calling new or delete will result in fatal error.
50
51class ValueObj {
52 public:
53  void* operator new(size_t size);
54  void operator delete(void* p);
55};
56
57// Base class for classes that constitute name spaces.
58
59class AllStatic {
60 public:
61  void* operator new(size_t size);
62  void operator delete(void* p);
63};
64
65
66//------------------------------Chunk------------------------------------------
67// Linked list of raw memory chunks
68class Chunk: public CHeapObj {
69 public:
70  void* operator new(size_t size, size_t length);
71  void  operator delete(void* p, size_t length);
72  Chunk(size_t length);
73
74  enum {
75      init_size =  1*1024,      // Size of first chunk
76      size      = 32*1024       // Default size of an Arena chunk (following the first)
77  };
78  Chunk*       _next;           // Next Chunk in list
79  size_t       _len;            // Size of this Chunk
80
81  void chop();                  // Chop this chunk
82  void next_chop();             // Chop next chunk
83
84  // Boundaries of data area (possibly unused)
85  char* bottom() const { return ((char*) this) + sizeof(Chunk);  }
86  char* top()    const { return bottom() + _len; }
87};
88
89
90//------------------------------Arena------------------------------------------
91// Fast allocation of memory
92class Arena: public CHeapObj {
93protected:
94  friend class ResourceMark;
95  friend class HandleMark;
96  friend class NoHandleMark;
97  Chunk *_first;                // First chunk
98  Chunk *_chunk;                // current chunk
99  char *_hwm, *_max;            // High water mark and max in current chunk
100  void* grow(size_t x);         // Get a new Chunk of at least size x
101  size_t _size_in_bytes;          // Size of arena (used for memory usage tracing)
102public:
103  Arena();
104  Arena(size_t init_size);
105  Arena(Arena *old);
106  ~Arena()                      { _first->chop(); }
107  char* hwm() const             { return _hwm; }
108
109  // Fast allocate in the arena.  Common case is: pointer test + increment.
110  void* Amalloc(size_t x) {
111#ifdef _LP64
112    x = (x + (8-1)) & ((unsigned)(-8));
113#else
114    x = (x + (4-1)) & ((unsigned)(-4));
115#endif
116    if (_hwm + x > _max) {
117      return grow(x);
118    } else {
119      char *old = _hwm;
120      _hwm += x;
121      return old;
122    }
123  }
124  // Further assume size is padded out to words
125  // Warning:  in LP64, Amalloc_4 is really Amalloc_8
126  void *Amalloc_4(size_t x) {
127    assert( (x&(sizeof(char*)-1)) == 0, "misaligned size" );
128    if (_hwm + x > _max) {
129      return grow(x);
130    } else {
131      char *old = _hwm;
132      _hwm += x;
133      return old;
134    }
135  }
136
137  // Fast delete in area.  Common case is: NOP (except for storage reclaimed)
138  void Afree(void *ptr, size_t size) {
139    if (((char*)ptr) + size == _hwm) _hwm = (char*)ptr;
140  }
141
142  void *Acalloc( size_t items, size_t x );
143  void *Arealloc( void *old_ptr, size_t old_size, size_t new_size );
144
145  // Reset this Arena to empty, and return this Arenas guts in a new Arena.
146  Arena *reset(void);
147
148  // Determine if pointer belongs to this Arena or not.
149  bool contains( const void *ptr ) const;
150
151  // Total of all chunks in use (not thread-safe)
152  size_t used() const;
153
154  // Total # of bytes used
155  size_t size_in_bytes() const         {  return _size_in_bytes; }
156  void   set_size_in_bytes(size_t size)  { _size_in_bytes = size;   }
157};
158