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_boundary() && (char*) block < _memory.high(), 194 "The newly allocated block " INTPTR_FORMAT " is not within the heap " 195 "starting with " INTPTR_FORMAT " and ending with " INTPTR_FORMAT, 196 p2i(block), p2i(_memory.low_boundary()), p2i(_memory.high())); 197 DEBUG_ONLY(memset((void*)block->allocated_space(), badCodeHeapNewVal, instance_size)); 198 _max_allocated_capacity = MAX2(_max_allocated_capacity, allocated_capacity()); 199 _blob_count++; 200 return block->allocated_space(); 201 } 202 203 // Ensure minimum size for allocation to the heap. 204 number_of_segments = MAX2((int)CodeCacheMinBlockLength, (int)number_of_segments); 205 206 if (_next_segment + number_of_segments <= _number_of_committed_segments) { 207 mark_segmap_as_used(_next_segment, _next_segment + number_of_segments); 208 HeapBlock* b = block_at(_next_segment); 209 b->initialize(number_of_segments); 210 _next_segment += number_of_segments; 211 guarantee((char*) b >= _memory.low_boundary() && (char*) block < _memory.high(), 212 "The newly allocated block " INTPTR_FORMAT " is not within the heap " 213 "starting with " INTPTR_FORMAT " and ending with " INTPTR_FORMAT, 214 p2i(b), p2i(_memory.low_boundary()), p2i(_memory.high())); 215 DEBUG_ONLY(memset((void *)b->allocated_space(), badCodeHeapNewVal, instance_size)); 216 _max_allocated_capacity = MAX2(_max_allocated_capacity, allocated_capacity()); 217 _blob_count++; 218 return b->allocated_space(); 219 } else { 220 return NULL; 221 } 222 } 223 224 225 void CodeHeap::deallocate(void* p) { 226 assert(p == find_start(p), "illegal deallocation"); 227 // Find start of HeapBlock 228 HeapBlock* b = (((HeapBlock *)p) - 1); 229 assert(b->allocated_space() == p, "sanity check"); 230 guarantee((char*) b >= _memory.low_boundary() && (char*) b < _memory.high(), 231 "The block to be deallocated " INTPTR_FORMAT " is not within the heap " 232 "starting with " INTPTR_FORMAT " and ending with " INTPTR_FORMAT, 233 p2i(b), p2i(_memory.low_boundary()), p2i(_memory.high())); 234 DEBUG_ONLY(memset((void *)b->allocated_space(), badCodeHeapFreeVal, 235 segments_to_size(b->length()) - sizeof(HeapBlock))); 236 add_to_freelist(b); 237 NOT_PRODUCT(verify()); 238 } 239 240 /** 241 * Uses segment map to find the the start (header) of a nmethod. This works as follows: 242 * The memory of the code cache is divided into 'segments'. The size of a segment is 243 * determined by -XX:CodeCacheSegmentSize=XX. Allocation in the code cache can only 244 * happen at segment boundaries. A pointer in the code cache can be mapped to a segment 245 * by calling segment_for(addr). Each time memory is requested from the code cache, 246 * the segmap is updated accordingly. See the following example, which illustrates the 247 * state of code cache and the segment map: (seg -> segment, nm ->nmethod) 248 * 249 * code cache segmap 250 * ----------- --------- 251 * seg 1 | nm 1 | -> | 0 | 252 * seg 2 | nm 1 | -> | 1 | 253 * ... | nm 1 | -> | .. | |