gboolean sgen_memgov_try_alloc_space (mword size, int space) { if (sgen_memgov_available_free_space () < size) return FALSE; SGEN_ATOMIC_ADD_P (allocated_heap, size); mono_runtime_resource_check_limit (MONO_RESOURCE_GC_HEAP, allocated_heap); return TRUE; }
static CodeChunk* new_codechunk (CodeChunk *last, int dynamic, int size) { int minsize, flags = CODE_FLAG_MMAP; int chunk_size, bsize = 0; int pagesize, valloc_granule; CodeChunk *chunk; void *ptr; #ifdef FORCE_MALLOC flags = CODE_FLAG_MALLOC; #endif pagesize = mono_pagesize (); valloc_granule = mono_valloc_granule (); if (dynamic) { chunk_size = size; flags = CODE_FLAG_MALLOC; } else { minsize = MAX (pagesize * MIN_PAGES, valloc_granule); if (size < minsize) chunk_size = minsize; else { /* Allocate MIN_ALIGN-1 more than we need so we can still */ /* guarantee MIN_ALIGN alignment for individual allocs */ /* from mono_code_manager_reserve_align. */ size += MIN_ALIGN - 1; size &= ~(MIN_ALIGN - 1); chunk_size = size; chunk_size += valloc_granule - 1; chunk_size &= ~ (valloc_granule - 1); } } #ifdef BIND_ROOM if (dynamic) /* Reserve more space since there are no other chunks we might use if this one gets full */ bsize = (chunk_size * 2) / BIND_ROOM; else bsize = chunk_size / BIND_ROOM; if (bsize < MIN_BSIZE) bsize = MIN_BSIZE; bsize += MIN_ALIGN -1; bsize &= ~ (MIN_ALIGN - 1); if (chunk_size - size < bsize) { chunk_size = size + bsize; if (!dynamic) { chunk_size += valloc_granule - 1; chunk_size &= ~ (valloc_granule - 1); } } #endif if (flags == CODE_FLAG_MALLOC) { ptr = dlmemalign (MIN_ALIGN, chunk_size + MIN_ALIGN - 1); if (!ptr) return NULL; } else { /* Try to allocate code chunks next to each other to help the VM */ ptr = NULL; if (last) ptr = codechunk_valloc ((guint8*)last->data + last->size, chunk_size); if (!ptr) ptr = codechunk_valloc (NULL, chunk_size); if (!ptr) return NULL; } if (flags == CODE_FLAG_MALLOC) { #ifdef BIND_ROOM /* Make sure the thunks area is zeroed */ memset (ptr, 0, bsize); #endif } chunk = (CodeChunk *) g_malloc (sizeof (CodeChunk)); if (!chunk) { if (flags == CODE_FLAG_MALLOC) dlfree (ptr); else mono_vfree (ptr, chunk_size, MONO_MEM_ACCOUNT_CODE); return NULL; } chunk->next = NULL; chunk->size = chunk_size; chunk->data = (char *) ptr; chunk->flags = flags; chunk->pos = bsize; chunk->bsize = bsize; if (code_manager_callbacks.chunk_new) code_manager_callbacks.chunk_new ((gpointer)chunk->data, chunk->size); mono_profiler_code_chunk_new((gpointer) chunk->data, chunk->size); code_memory_used += chunk_size; mono_runtime_resource_check_limit (MONO_RESOURCE_JIT_CODE, code_memory_used); /*printf ("code chunk at: %p\n", ptr);*/ return chunk; }