1516
1517 char* os::reserve_memory(size_t bytes, char* addr, size_t alignment_hint,
1518 MEMFLAGS flags) {
1519 char* result = pd_reserve_memory(bytes, addr, alignment_hint);
1520 if (result != NULL) {
1521 MemTracker::record_virtual_memory_reserve((address)result, bytes, CALLER_PC);
1522 MemTracker::record_virtual_memory_type((address)result, flags);
1523 }
1524
1525 return result;
1526 }
1527
1528 char* os::attempt_reserve_memory_at(size_t bytes, char* addr) {
1529 char* result = pd_attempt_reserve_memory_at(bytes, addr);
1530 if (result != NULL) {
1531 MemTracker::record_virtual_memory_reserve((address)result, bytes, CALLER_PC);
1532 }
1533 return result;
1534 }
1535
1536 void os::split_reserved_memory(char *base, size_t size,
1537 size_t split, bool realloc) {
1538 pd_split_reserved_memory(base, size, split, realloc);
1539 }
1540
1541 bool os::commit_memory(char* addr, size_t bytes, bool executable) {
1542 bool res = pd_commit_memory(addr, bytes, executable);
1543 if (res) {
1544 MemTracker::record_virtual_memory_commit((address)addr, bytes, CALLER_PC);
1545 }
1546 return res;
1547 }
1548
1549 bool os::commit_memory(char* addr, size_t size, size_t alignment_hint,
1550 bool executable) {
1551 bool res = os::pd_commit_memory(addr, size, alignment_hint, executable);
1552 if (res) {
1553 MemTracker::record_virtual_memory_commit((address)addr, size, CALLER_PC);
1554 }
1555 return res;
|
1516
1517 char* os::reserve_memory(size_t bytes, char* addr, size_t alignment_hint,
1518 MEMFLAGS flags) {
1519 char* result = pd_reserve_memory(bytes, addr, alignment_hint);
1520 if (result != NULL) {
1521 MemTracker::record_virtual_memory_reserve((address)result, bytes, CALLER_PC);
1522 MemTracker::record_virtual_memory_type((address)result, flags);
1523 }
1524
1525 return result;
1526 }
1527
1528 char* os::attempt_reserve_memory_at(size_t bytes, char* addr) {
1529 char* result = pd_attempt_reserve_memory_at(bytes, addr);
1530 if (result != NULL) {
1531 MemTracker::record_virtual_memory_reserve((address)result, bytes, CALLER_PC);
1532 }
1533 return result;
1534 }
1535
1536 // A convenience function which attempts to reserve memory at a number
1537 // of given addresses. An address array is given and first the first,
1538 // then the second is tried and so on.
1539 // Function returns NULL if memory could not be obtained at any of the
1540 // given addresses.
1541 //
1542 // parameters
1543 // bytes - size of block
1544 // addr - array of addresses, NULL terminated
1545 //
1546 char* os::attempt_reserve_memory_at_multiple(size_t bytes, char* addr[]) {
1547 for (int i = 0; addr[i]; i ++) {
1548 char* const attach_point = addr[i];
1549 assert(attach_point >= (char *)HeapBaseMinAddress, "Flag support broken");
1550 char* const addr = os::attempt_reserve_memory_at(bytes, attach_point);
1551 if (addr) {
1552 // Note: depending on platform and OS, it is not guaranteed that
1553 // os::attempt_reserve_memory_at only returns requested addr or nothing :/
1554 if (addr != attach_point) {
1555 os::release_memory(addr, bytes);
1556 } else {
1557 return addr;
1558 }
1559 }
1560 }
1561
1562 return NULL;
1563 }
1564
1565 // A convenience function which attempts to reserve memory
1566 // in a given memory range.
1567 char* os::attempt_reserve_memory_in_range(size_t size, size_t alignment,
1568 char* range_from, char* range_to, int num_attempts) {
1569 // sanity checks
1570 if (range_from == 0) {
1571 range_from = (char*) 1;
1572 }
1573
1574 if (size == 0) {
1575 return NULL;
1576 }
1577
1578 if (range_to <= range_from) {
1579 return NULL;
1580 } else {
1581 size_t d = range_to - range_from;
1582 if (d < size) {
1583 return NULL;
1584 }
1585 }
1586
1587 // The necessary attach point alignment (for mmap or shmat to have a
1588 // chance of attaching).
1589 const size_t os_attach_point_alignment =
1590 AIX_ONLY(SIZE_256M) // Known shm boundary alignment.
1591 NOT_AIX(os::vm_allocation_granularity());
1592
1593 const size_t attach_point_alignment =
1594 alignment > 0 ?
1595 lcm(alignment, os_attach_point_alignment) : os_attach_point_alignment;
1596
1597 // Calc address range within we try to attach (range of possible start addresses).
1598 char* const highest_start = (char *)align_ptr_down(range_to - size, attach_point_alignment);
1599 char* const lowest_start = (char *)align_ptr_up(range_from, attach_point_alignment);
1600 const size_t attach_range = highest_start - lowest_start;
1601
1602 // Default is 20 ...
1603 if (num_attempts <= 0) {
1604 num_attempts = 20;
1605 }
1606
1607 // ... but cap at possible
1608 const uint64_t num_attempts_possible =
1609 (attach_range / attach_point_alignment) + 1; // at least one is possible even for 0 sized attach range
1610
1611 if (num_attempts_possible < (uint64_t)num_attempts) {
1612 num_attempts = num_attempts_possible;
1613 }
1614
1615 const size_t stepsize =
1616 align_size_up(attach_range / num_attempts, attach_point_alignment);
1617
1618 // Build up attach point array.
1619 char** attach_points = (char**) os::malloc((num_attempts + 1) * sizeof(char*), mtInternal);
1620 assert(attach_points, "OOM");
1621
1622 // Fill attach points array.
1623 char* attach_point = highest_start;
1624 int i = 0;
1625 while (i < num_attempts && attach_point >= lowest_start) {
1626 attach_points[i] = attach_point;
1627 i++;
1628 // Handle overflow correctly!
1629 if ((uintptr_t)attach_point > stepsize) {
1630 attach_point -= stepsize;
1631 } else {
1632 attach_point = 0;
1633 }
1634 }
1635
1636 attach_points[i] = NULL;
1637
1638 char* addr = os::attempt_reserve_memory_at_multiple(size, attach_points);
1639
1640 os::free(attach_points);
1641
1642 return addr;
1643 }
1644
1645 void os::split_reserved_memory(char *base, size_t size,
1646 size_t split, bool realloc) {
1647 pd_split_reserved_memory(base, size, split, realloc);
1648 }
1649
1650 bool os::commit_memory(char* addr, size_t bytes, bool executable) {
1651 bool res = pd_commit_memory(addr, bytes, executable);
1652 if (res) {
1653 MemTracker::record_virtual_memory_commit((address)addr, bytes, CALLER_PC);
1654 }
1655 return res;
1656 }
1657
1658 bool os::commit_memory(char* addr, size_t size, size_t alignment_hint,
1659 bool executable) {
1660 bool res = os::pd_commit_memory(addr, size, alignment_hint, executable);
1661 if (res) {
1662 MemTracker::record_virtual_memory_commit((address)addr, size, CALLER_PC);
1663 }
1664 return res;
|