src/hotspot/share/gc/z/zMemory.cpp
author eosterlund
Tue, 12 Nov 2019 20:01:23 +0000
changeset 59038 b9a42ca342db
parent 58812 4adca7312d8f
permissions -rw-r--r--
8233061: ZGC: Enforce memory ordering in segmented bit maps Reviewed-by: pliden, stefank
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
     1
/*
54834
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 50525
diff changeset
     2
 * Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
     4
 *
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
     7
 * published by the Free Software Foundation.
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
     8
 *
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    13
 * accompanied this code).
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    14
 *
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    18
 *
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    21
 * questions.
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    22
 */
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    23
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    24
#include "precompiled.hpp"
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    25
#include "gc/z/zList.inline.hpp"
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    26
#include "gc/z/zMemory.inline.hpp"
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    27
#include "memory/allocation.inline.hpp"
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    28
58812
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
    29
ZMemory* ZMemoryManager::create(uintptr_t start, size_t size) {
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
    30
  ZMemory* const area = new ZMemory(start, size);
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
    31
  if (_callbacks._create != NULL) {
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
    32
    _callbacks._create(area);
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
    33
  }
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
    34
  return area;
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
    35
}
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
    36
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
    37
void ZMemoryManager::destroy(ZMemory* area) {
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
    38
  if (_callbacks._destroy != NULL) {
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
    39
    _callbacks._destroy(area);
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
    40
  }
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
    41
  delete area;
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
    42
}
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
    43
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
    44
void ZMemoryManager::shrink_from_front(ZMemory* area, size_t size) {
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
    45
  if (_callbacks._shrink_from_front != NULL) {
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
    46
    _callbacks._shrink_from_front(area, size);
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
    47
  }
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
    48
  area->shrink_from_front(size);
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
    49
}
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
    50
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
    51
void ZMemoryManager::shrink_from_back(ZMemory* area, size_t size) {
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
    52
  if (_callbacks._shrink_from_back != NULL) {
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
    53
    _callbacks._shrink_from_back(area, size);
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
    54
  }
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
    55
  area->shrink_from_back(size);
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
    56
}
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
    57
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
    58
void ZMemoryManager::grow_from_front(ZMemory* area, size_t size) {
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
    59
  if (_callbacks._grow_from_front != NULL) {
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
    60
    _callbacks._grow_from_front(area, size);
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
    61
  }
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
    62
  area->grow_from_front(size);
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
    63
}
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
    64
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
    65
void ZMemoryManager::grow_from_back(ZMemory* area, size_t size) {
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
    66
  if (_callbacks._grow_from_back != NULL) {
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
    67
    _callbacks._grow_from_back(area, size);
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
    68
  }
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
    69
  area->grow_from_back(size);
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
    70
}
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
    71
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
    72
ZMemoryManager::Callbacks::Callbacks() :
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
    73
    _create(NULL),
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
    74
    _destroy(NULL),
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
    75
    _shrink_from_front(NULL),
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
    76
    _shrink_from_back(NULL),
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
    77
    _grow_from_front(NULL),
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
    78
    _grow_from_back(NULL) {}
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
    79
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
    80
ZMemoryManager::ZMemoryManager() :
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
    81
    _freelist(),
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
    82
    _callbacks() {}
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
    83
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
    84
void ZMemoryManager::register_callbacks(const Callbacks& callbacks) {
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
    85
  _callbacks = callbacks;
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
    86
}
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
    87
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    88
uintptr_t ZMemoryManager::alloc_from_front(size_t size) {
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    89
  ZListIterator<ZMemory> iter(&_freelist);
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    90
  for (ZMemory* area; iter.next(&area);) {
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    91
    if (area->size() >= size) {
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    92
      if (area->size() == size) {
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    93
        // Exact match, remove area
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    94
        const uintptr_t start = area->start();
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    95
        _freelist.remove(area);
58812
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
    96
        destroy(area);
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    97
        return start;
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    98
      } else {
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    99
        // Larger than requested, shrink area
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   100
        const uintptr_t start = area->start();
58812
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
   101
        shrink_from_front(area, size);
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   102
        return start;
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   103
      }
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   104
    }
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   105
  }
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   106
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   107
  // Out of memory
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   108
  return UINTPTR_MAX;
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   109
}
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   110
54834
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 50525
diff changeset
   111
uintptr_t ZMemoryManager::alloc_from_front_at_most(size_t size, size_t* allocated) {
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 50525
diff changeset
   112
  ZMemory* area = _freelist.first();
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 50525
diff changeset
   113
  if (area != NULL) {
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 50525
diff changeset
   114
    if (area->size() <= size) {
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 50525
diff changeset
   115
      // Smaller than or equal to requested, remove area
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 50525
diff changeset
   116
      const uintptr_t start = area->start();
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 50525
diff changeset
   117
      *allocated = area->size();
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 50525
diff changeset
   118
      _freelist.remove(area);
58812
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
   119
      destroy(area);
54834
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 50525
diff changeset
   120
      return start;
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 50525
diff changeset
   121
    } else {
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 50525
diff changeset
   122
      // Larger than requested, shrink area
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 50525
diff changeset
   123
      const uintptr_t start = area->start();
58812
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
   124
      shrink_from_front(area, size);
54834
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 50525
diff changeset
   125
      *allocated = size;
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 50525
diff changeset
   126
      return start;
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 50525
diff changeset
   127
    }
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 50525
diff changeset
   128
  }
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 50525
diff changeset
   129
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 50525
diff changeset
   130
  // Out of memory
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 50525
diff changeset
   131
  *allocated = 0;
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 50525
diff changeset
   132
  return UINTPTR_MAX;
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 50525
diff changeset
   133
}
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 50525
diff changeset
   134
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   135
uintptr_t ZMemoryManager::alloc_from_back(size_t size) {
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   136
  ZListReverseIterator<ZMemory> iter(&_freelist);
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   137
  for (ZMemory* area; iter.next(&area);) {
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   138
    if (area->size() >= size) {
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   139
      if (area->size() == size) {
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   140
        // Exact match, remove area
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   141
        const uintptr_t start = area->start();
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   142
        _freelist.remove(area);
58812
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
   143
        destroy(area);
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   144
        return start;
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   145
      } else {
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   146
        // Larger than requested, shrink area
58812
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
   147
        shrink_from_back(area, size);
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   148
        return area->end();
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   149
      }
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   150
    }
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   151
  }
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   152
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   153
  // Out of memory
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   154
  return UINTPTR_MAX;
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   155
}
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   156
54834
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 50525
diff changeset
   157
uintptr_t ZMemoryManager::alloc_from_back_at_most(size_t size, size_t* allocated) {
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 50525
diff changeset
   158
  ZMemory* area = _freelist.last();
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 50525
diff changeset
   159
  if (area != NULL) {
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 50525
diff changeset
   160
    if (area->size() <= size) {
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 50525
diff changeset
   161
      // Smaller than or equal to requested, remove area
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 50525
diff changeset
   162
      const uintptr_t start = area->start();
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 50525
diff changeset
   163
      *allocated = area->size();
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 50525
diff changeset
   164
      _freelist.remove(area);
58812
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
   165
      destroy(area);
54834
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 50525
diff changeset
   166
      return start;
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 50525
diff changeset
   167
    } else {
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 50525
diff changeset
   168
      // Larger than requested, shrink area
58812
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
   169
      shrink_from_back(area, size);
54834
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 50525
diff changeset
   170
      *allocated = size;
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 50525
diff changeset
   171
      return area->end();
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 50525
diff changeset
   172
    }
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 50525
diff changeset
   173
  }
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 50525
diff changeset
   174
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 50525
diff changeset
   175
  // Out of memory
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 50525
diff changeset
   176
  *allocated = 0;
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 50525
diff changeset
   177
  return UINTPTR_MAX;
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 50525
diff changeset
   178
}
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 50525
diff changeset
   179
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   180
void ZMemoryManager::free(uintptr_t start, size_t size) {
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   181
  assert(start != UINTPTR_MAX, "Invalid address");
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   182
  const uintptr_t end = start + size;
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   183
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   184
  ZListIterator<ZMemory> iter(&_freelist);
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   185
  for (ZMemory* area; iter.next(&area);) {
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   186
    if (start < area->start()) {
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   187
      ZMemory* const prev = _freelist.prev(area);
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   188
      if (prev != NULL && start == prev->end()) {
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   189
        if (end == area->start()) {
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   190
          // Merge with prev and current area
58812
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
   191
          grow_from_back(prev, size + area->size());
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   192
          _freelist.remove(area);
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   193
          delete area;
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   194
        } else {
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   195
          // Merge with prev area
58812
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
   196
          grow_from_back(prev, size);
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   197
        }
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   198
      } else if (end == area->start()) {
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   199
        // Merge with current area
58812
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
   200
        grow_from_front(area, size);
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   201
      } else {
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   202
        // Insert new area before current area
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   203
        assert(end < area->start(), "Areas must not overlap");
58812
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
   204
        ZMemory* const new_area = create(start, size);
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   205
        _freelist.insert_before(area, new_area);
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   206
      }
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   207
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   208
      // Done
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   209
      return;
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   210
    }
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   211
  }
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   212
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   213
  // Insert last
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   214
  ZMemory* const last = _freelist.last();
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   215
  if (last != NULL && start == last->end()) {
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   216
    // Merge with last area
58812
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
   217
    grow_from_back(last, size);
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   218
  } else {
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   219
    // Insert new area last
58812
4adca7312d8f 8232649: ZGC: Add callbacks to ZMemoryManager
stefank
parents: 54834
diff changeset
   220
    ZMemory* const new_area = create(start, size);
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   221
    _freelist.insert_last(new_area);
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   222
  }
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   223
}