bool spage_table_put_file (struct file *file, int32_t ofs, uint8_t *upage, uint32_t read_bytes, uint32_t zero_bytes, bool writable, enum spage_types type) { if (type != FILE && type != MMAP) return false; struct spage_table_entry *spte = malloc (sizeof (struct spage_table_entry)); if (!spte) return false; spte->file = file; spte->file_offset = ofs; spte->upage = upage; spte->file_read_bytes = read_bytes; spte->file_zero_bytes = zero_bytes; spte->writable = writable; spte->in_memory = false; spte->inevictable = false; spte->spage_type = type; if (type == MMAP) { if (!mmap_alloc (spte)) { free(spte); return false; } } if (hash_insert (&thread_current()->spage_table, &spte->elem)) { spte->spage_type = ERROR; return false; } return true; }
page( int64_t claim_pos ) { fprintf( stderr, "CLAIM POS %lld\n", claim_pos ); _data = (char*)mmap_alloc( PageSize, (void*)((ItemSize << 32) + claim_pos * PageSize) ); fprintf( stderr, " PAGE DATA: %p\n", _data ); assert( (int64_t(_data) >> 32) == ItemSize ); _next_data = _data; _page_end = _data + PageSize; _alloc_free = nullptr; _gc_free_at_bat = nullptr; _gc_free_on_deck = nullptr; _claim_pos = claim_pos; _alloc = 0; _free = 0; }
int shmem_internal_symmetric_init(size_t requested_length, int use_malloc) { shmem_internal_use_malloc = use_malloc; /* add library overhead such that the max can be shmalloc()'ed */ shmem_internal_heap_length = requested_length + (1024*1024); if (0 == shmem_internal_use_malloc) { shmem_internal_heap_base = shmem_internal_heap_curr = mmap_alloc(shmem_internal_heap_length); } else { shmem_internal_heap_base = shmem_internal_heap_curr = malloc(shmem_internal_heap_length); } return (NULL == shmem_internal_heap_base) ? -1 : 0; }
void * mmap_realloc (void **var, size_t nbytes) { MEMORY_BASIC_INFORMATION memInfo, m2; void *old_ptr; if (*var == NULL) return mmap_alloc (var, nbytes); /* This case happens in init_buffer(). */ if (nbytes == 0) { mmap_free (var); return mmap_alloc (var, nbytes); } memset (&memInfo, 0, sizeof (memInfo)); if (VirtualQuery (*var, &memInfo, sizeof (memInfo)) == 0) DebPrint (("mmap_realloc: VirtualQuery error = %ld\n", GetLastError ())); /* We need to enlarge the block. */ if (memInfo.RegionSize < nbytes) { memset (&m2, 0, sizeof (m2)); if (VirtualQuery ((char *)*var + memInfo.RegionSize, &m2, sizeof(m2)) == 0) DebPrint (("mmap_realloc: VirtualQuery error = %ld\n", GetLastError ())); /* If there is enough room in the current reserved area, then commit more pages as needed. */ if (m2.State == MEM_RESERVE && m2.AllocationBase == memInfo.AllocationBase && nbytes <= memInfo.RegionSize + m2.RegionSize) { void *p; p = VirtualAlloc (*var, nbytes, MEM_COMMIT, PAGE_READWRITE); if (!p /* && GetLastError() != ERROR_NOT_ENOUGH_MEMORY */) { DebPrint (("realloc enlarge: VirtualAlloc (%p + %I64x, %I64x) error %ld\n", *var, (uint64_t)memInfo.RegionSize, (uint64_t)(nbytes - memInfo.RegionSize), GetLastError ())); DebPrint (("next region: %p %p %I64x %x\n", m2.BaseAddress, m2.AllocationBase, (uint64_t)m2.RegionSize, m2.AllocationProtect)); } else return *var; } /* Else we must actually enlarge the block by allocating a new one and copying previous contents from the old to the new one. */ old_ptr = *var; if (mmap_alloc (var, nbytes)) { CopyMemory (*var, old_ptr, memInfo.RegionSize); mmap_free (&old_ptr); return *var; } else { /* We failed to reallocate the buffer. */ *var = old_ptr; return NULL; } } /* If we are shrinking by more than one page... */ if (memInfo.RegionSize > nbytes + getpagesize()) { /* If we are shrinking a lot... */ if ((memInfo.RegionSize / 2) > nbytes) { /* Let's give some memory back to the system and release some pages. */ old_ptr = *var; if (mmap_alloc (var, nbytes)) { CopyMemory (*var, old_ptr, nbytes); mmap_free (&old_ptr); return *var; } else { /* In case we fail to shrink, try to go on with the old block. But that means there is a lot of memory pressure. We could also decommit pages. */ *var = old_ptr; return *var; } } /* We still can decommit pages. */ if (VirtualFree ((char *)*var + nbytes + get_page_size(), memInfo.RegionSize - nbytes - get_page_size(), MEM_DECOMMIT) == 0) DebPrint (("mmap_realloc: VirtualFree error %ld\n", GetLastError ())); return *var; } /* Not enlarging, not shrinking by more than one page. */ return *var; }