|
1 | 1 | /* |
2 | | - * Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | 4 | * |
5 | 5 | * This code is free software; you can redistribute it and/or modify it |
@@ -117,16 +117,34 @@ class ResourceArea: public Arena { |
117 | 117 | size_in_bytes(), state._size_in_bytes); |
118 | 118 | set_size_in_bytes(state._size_in_bytes); |
119 | 119 | state._chunk->next_chop(); |
| 120 | + assert(_hwm != state._hwm, "Sanity check: HWM moves when we have later chunks"); |
120 | 121 | } else { |
121 | 122 | assert(size_in_bytes() == state._size_in_bytes, "Sanity check"); |
122 | 123 | } |
123 | | - _chunk = state._chunk; // Roll back to saved chunk. |
124 | | - _hwm = state._hwm; |
125 | | - _max = state._max; |
126 | 124 |
|
127 | | - // Clear out this chunk (to detect allocation bugs) |
128 | | - if (ZapResourceArea) { |
129 | | - memset(state._hwm, badResourceValue, state._max - state._hwm); |
| 125 | + if (_hwm != state._hwm) { |
| 126 | + // HWM moved: resource area was used. Roll back! |
| 127 | + |
| 128 | + char* replaced_hwm = _hwm; |
| 129 | + |
| 130 | + _chunk = state._chunk; |
| 131 | + _hwm = state._hwm; |
| 132 | + _max = state._max; |
| 133 | + |
| 134 | + // Clear out this chunk (to detect allocation bugs). |
| 135 | + // If current chunk contains the replaced HWM, this means we are |
| 136 | + // doing the rollback within the same chunk, and we only need to |
| 137 | + // clear up to replaced HWM. |
| 138 | + if (ZapResourceArea) { |
| 139 | + char* limit = _chunk->contains(replaced_hwm) ? replaced_hwm : _max; |
| 140 | + assert(limit >= _hwm, "Sanity check: non-negative memset size"); |
| 141 | + memset(_hwm, badResourceValue, limit - _hwm); |
| 142 | + } |
| 143 | + } else { |
| 144 | + // No allocations. Nothing to rollback. Check it. |
| 145 | + assert(_chunk == state._chunk, "Sanity check: idempotence"); |
| 146 | + assert(_hwm == state._hwm, "Sanity check: idempotence"); |
| 147 | + assert(_max == state._max, "Sanity check: idempotence"); |
130 | 148 | } |
131 | 149 | } |
132 | 150 | }; |
|
0 commit comments