221
222 static int util_posix_fallocate(int fd, off_t offset, off_t len) {
223 #ifdef __APPLE__
224 fstore_t store = { F_ALLOCATECONTIG, F_PEOFPOSMODE, 0, len };
225 // First we try to get a continuous chunk of disk space
226 int ret = fcntl(fd, F_PREALLOCATE, &store);
227 if (ret == -1) {
228 // Maybe we are too fragmented, try to allocate non-continuous range
229 store.fst_flags = F_ALLOCATEALL;
230 ret = fcntl(fd, F_PREALLOCATE, &store);
231 }
232 if(ret != -1) {
233 return ftruncate(fd, len);
234 }
235 return -1;
236 #else
237 return posix_fallocate(fd, offset, len);
238 #endif
239 }
240
241 // Map the given address range to the provided file descriptor.
242 char* os::map_memory_to_file(char* base, size_t size, int fd) {
243 assert(fd != -1, "File descriptor is not valid");
244
245 // allocate space for the file
246 int ret = util_posix_fallocate(fd, 0, (off_t)size);
247 if (ret != 0) {
248 vm_exit_during_initialization(err_msg("Error in mapping Java heap at the given filesystem directory. error(%d)", ret));
249 return NULL;
250 }
251
252 int prot = PROT_READ | PROT_WRITE;
253 int flags = MAP_SHARED;
254 if (base != NULL) {
255 flags |= MAP_FIXED;
256 }
257 char* addr = (char*)mmap(base, size, prot, flags, fd, 0);
258
259 if (addr == MAP_FAILED) {
260 warning("Failed mmap to file. (%s)", os::strerror(errno));
261 return NULL;
262 }
263 if (base != NULL && addr != base) {
264 if (!os::release_memory(addr, size)) {
265 warning("Could not release memory on unsuccessful file mapping");
266 }
267 return NULL;
268 }
269 return addr;
270 }
271
272 char* os::replace_existing_mapping_with_file_mapping(char* base, size_t size, int fd) {
273 assert(fd != -1, "File descriptor is not valid");
274 assert(base != NULL, "Base cannot be NULL");
275
276 return map_memory_to_file(base, size, fd);
277 }
278
|
221
222 static int util_posix_fallocate(int fd, off_t offset, off_t len) {
223 #ifdef __APPLE__
224 fstore_t store = { F_ALLOCATECONTIG, F_PEOFPOSMODE, 0, len };
225 // First we try to get a continuous chunk of disk space
226 int ret = fcntl(fd, F_PREALLOCATE, &store);
227 if (ret == -1) {
228 // Maybe we are too fragmented, try to allocate non-continuous range
229 store.fst_flags = F_ALLOCATEALL;
230 ret = fcntl(fd, F_PREALLOCATE, &store);
231 }
232 if(ret != -1) {
233 return ftruncate(fd, len);
234 }
235 return -1;
236 #else
237 return posix_fallocate(fd, offset, len);
238 #endif
239 }
240
241 // Allocates space for the file on diven device
242 int os::allocate_file(int file_desc, size_t size) {
243 // allocate space for the file
244 if (file_desc == -1) {
245 vm_exit_during_initialization(err_msg("Invalid file descriptor passed (%d)", file_desc));
246 }
247 int ret = util_posix_fallocate(file_desc, 0, (off_t)size);
248 if (ret != 0) {
249 vm_exit_during_initialization(err_msg("Could not allocate file to map Java heap on given filesystem... error(%d)", ret));
250 }
251 return ret;
252 }
253
254 // Map the given address range to the provided file descriptor.
255 char* os::map_memory_to_file(char* base, size_t size, int fd, int offset, bool exec, bool allocate) {
256 assert(fd != -1, "File descriptor is not valid");
257
258 // allocate space for the file
259 if (allocate == true) {
260 if (os::allocate_file(fd, size) != 0) {
261 return NULL;
262 }
263 }
264
265 int prot = exec ? PROT_READ|PROT_WRITE|PROT_EXEC : PROT_READ|PROT_WRITE;
266 int flags = MAP_SHARED;
267 if (base != NULL) {
268 flags |= MAP_FIXED;
269 }
270
271 char* addr = (char*)mmap(base, size, prot, flags, fd, offset);
272 if (addr == MAP_FAILED) {
273 warning("Failed mmap to file. (%s)", os::strerror(errno));
274 return NULL;
275 }
276 if (base != NULL && addr != base) {
277 if (!os::release_memory(addr, size)) {
278 warning("Could not release memory on unsuccessful file mapping");
279 }
280 return NULL;
281 }
282 return addr;
283 }
284
285 char* os::replace_existing_mapping_with_file_mapping(char* base, size_t size, int fd) {
286 assert(fd != -1, "File descriptor is not valid");
287 assert(base != NULL, "Base cannot be NULL");
288
289 return map_memory_to_file(base, size, fd);
290 }
291
|