src/share/vm/memory/heap.cpp
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File 8173151 Sdiff src/share/vm/memory

src/share/vm/memory/heap.cpp

Print this page




 173 }
 174 
 175 void CodeHeap::clear() {
 176   _next_segment = 0;
 177   mark_segmap_as_free(0, _number_of_committed_segments);
 178 }
 179 
 180 
 181 void* CodeHeap::allocate(size_t instance_size) {
 182   size_t number_of_segments = size_to_segments(instance_size + header_size());
 183   assert(segments_to_size(number_of_segments) >= sizeof(FreeBlock), "not enough room for FreeList");
 184 
 185   // First check if we can satisfy request from freelist
 186   NOT_PRODUCT(verify());
 187   HeapBlock* block = search_freelist(number_of_segments);
 188   NOT_PRODUCT(verify());
 189 
 190   if (block != NULL) {
 191     assert(block->length() >= number_of_segments && block->length() < number_of_segments + CodeCacheMinBlockLength, "sanity check");
 192     assert(!block->free(), "must be marked free");



 193     DEBUG_ONLY(memset((void*)block->allocated_space(), badCodeHeapNewVal, instance_size));
 194     _max_allocated_capacity = MAX2(_max_allocated_capacity, allocated_capacity());
 195     _blob_count++;
 196     return block->allocated_space();
 197   }
 198 
 199   // Ensure minimum size for allocation to the heap.
 200   number_of_segments = MAX2((int)CodeCacheMinBlockLength, (int)number_of_segments);
 201 
 202   if (_next_segment + number_of_segments <= _number_of_committed_segments) {
 203     mark_segmap_as_used(_next_segment, _next_segment + number_of_segments);
 204     HeapBlock* b =  block_at(_next_segment);
 205     b->initialize(number_of_segments);
 206     _next_segment += number_of_segments;



 207     DEBUG_ONLY(memset((void *)b->allocated_space(), badCodeHeapNewVal, instance_size));
 208     _max_allocated_capacity = MAX2(_max_allocated_capacity, allocated_capacity());
 209     _blob_count++;
 210     return b->allocated_space();
 211   } else {
 212     return NULL;
 213   }
 214 }
 215 
 216 
 217 void CodeHeap::deallocate(void* p) {
 218   assert(p == find_start(p), "illegal deallocation");
 219   // Find start of HeapBlock
 220   HeapBlock* b = (((HeapBlock *)p) - 1);
 221   assert(b->allocated_space() == p, "sanity check");



 222   DEBUG_ONLY(memset((void *)b->allocated_space(), badCodeHeapFreeVal,
 223              segments_to_size(b->length()) - sizeof(HeapBlock)));
 224   add_to_freelist(b);
 225   NOT_PRODUCT(verify());
 226 }
 227 
 228 /**
 229  * Uses segment map to find the the start (header) of a nmethod. This works as follows:
 230  * The memory of the code cache is divided into 'segments'. The size of a segment is
 231  * determined by -XX:CodeCacheSegmentSize=XX. Allocation in the code cache can only
 232  * happen at segment boundaries. A pointer in the code cache can be mapped to a segment
 233  * by calling segment_for(addr). Each time memory is requested from the code cache,
 234  * the segmap is updated accordingly. See the following example, which illustrates the
 235  * state of code cache and the segment map: (seg -> segment, nm ->nmethod)
 236  *
 237  *          code cache          segmap
 238  *         -----------        ---------
 239  * seg 1   | nm 1    |   ->   | 0     |
 240  * seg 2   | nm 1    |   ->   | 1     |
 241  * ...     | nm 1    |   ->   | ..    |




 173 }
 174 
 175 void CodeHeap::clear() {
 176   _next_segment = 0;
 177   mark_segmap_as_free(0, _number_of_committed_segments);
 178 }
 179 
 180 
 181 void* CodeHeap::allocate(size_t instance_size) {
 182   size_t number_of_segments = size_to_segments(instance_size + header_size());
 183   assert(segments_to_size(number_of_segments) >= sizeof(FreeBlock), "not enough room for FreeList");
 184 
 185   // First check if we can satisfy request from freelist
 186   NOT_PRODUCT(verify());
 187   HeapBlock* block = search_freelist(number_of_segments);
 188   NOT_PRODUCT(verify());
 189 
 190   if (block != NULL) {
 191     assert(block->length() >= number_of_segments && block->length() < number_of_segments + CodeCacheMinBlockLength, "sanity check");
 192     assert(!block->free(), "must be marked free");
 193     guarantee((char*) block >= _memory.low(),
 194               "The newly allocated block " INTPTR_FORMAT " is not within the heap starting with "  INTPTR_FORMAT,
 195               p2i(block), p2i(_memory.low()));
 196     DEBUG_ONLY(memset((void*)block->allocated_space(), badCodeHeapNewVal, instance_size));
 197     _max_allocated_capacity = MAX2(_max_allocated_capacity, allocated_capacity());
 198     _blob_count++;
 199     return block->allocated_space();
 200   }
 201 
 202   // Ensure minimum size for allocation to the heap.
 203   number_of_segments = MAX2((int)CodeCacheMinBlockLength, (int)number_of_segments);
 204 
 205   if (_next_segment + number_of_segments <= _number_of_committed_segments) {
 206     mark_segmap_as_used(_next_segment, _next_segment + number_of_segments);
 207     HeapBlock* b =  block_at(_next_segment);
 208     b->initialize(number_of_segments);
 209     _next_segment += number_of_segments;
 210     guarantee((char*) b >= _memory.low(),
 211               "The newly allocated block " INTPTR_FORMAT " is not within the heap starting with "  INTPTR_FORMAT,
 212               p2i(b), p2i(_memory.low()));
 213     DEBUG_ONLY(memset((void *)b->allocated_space(), badCodeHeapNewVal, instance_size));
 214     _max_allocated_capacity = MAX2(_max_allocated_capacity, allocated_capacity());
 215     _blob_count++;
 216     return b->allocated_space();
 217   } else {
 218     return NULL;
 219   }
 220 }
 221 
 222 
 223 void CodeHeap::deallocate(void* p) {
 224   assert(p == find_start(p), "illegal deallocation");
 225   // Find start of HeapBlock
 226   HeapBlock* b = (((HeapBlock *)p) - 1);
 227   assert(b->allocated_space() == p, "sanity check");
 228   guarantee((char*) b >= _memory.low(),
 229             "The block to be deallocated " INTPTR_FORMAT " is not within the heap starting with "  INTPTR_FORMAT,
 230             p2i(b), p2i(_memory.low()));
 231   DEBUG_ONLY(memset((void *)b->allocated_space(), badCodeHeapFreeVal,
 232              segments_to_size(b->length()) - sizeof(HeapBlock)));
 233   add_to_freelist(b);
 234   NOT_PRODUCT(verify());
 235 }
 236 
 237 /**
 238  * Uses segment map to find the the start (header) of a nmethod. This works as follows:
 239  * The memory of the code cache is divided into 'segments'. The size of a segment is
 240  * determined by -XX:CodeCacheSegmentSize=XX. Allocation in the code cache can only
 241  * happen at segment boundaries. A pointer in the code cache can be mapped to a segment
 242  * by calling segment_for(addr). Each time memory is requested from the code cache,
 243  * the segmap is updated accordingly. See the following example, which illustrates the
 244  * state of code cache and the segment map: (seg -> segment, nm ->nmethod)
 245  *
 246  *          code cache          segmap
 247  *         -----------        ---------
 248  * seg 1   | nm 1    |   ->   | 0     |
 249  * seg 2   | nm 1    |   ->   | 1     |
 250  * ...     | nm 1    |   ->   | ..    |


src/share/vm/memory/heap.cpp
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File