1/*
2 * Copyright (c) 2009 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29#include <vm/vm_map.h>
30
31bool
32first_free_is_valid_ll(vm_map_t map)
33{
34 vm_map_offset_t map_page_mask = VM_MAP_PAGE_MASK(map);
35 vm_map_entry_t entry, next;
36
37 entry = vm_map_to_entry(map);
38 next = entry->vme_next;
39 while (vm_map_trunc_page(next->vme_start, map_page_mask) ==
40 vm_map_trunc_page(entry->vme_end, map_page_mask) ||
41 (vm_map_trunc_page(next->vme_start, map_page_mask) ==
42 vm_map_trunc_page(entry->vme_start, map_page_mask) &&
43 next != vm_map_to_entry(map))) {
44 entry = next;
45 next = entry->vme_next;
46 if (entry == vm_map_to_entry(map)) {
47 break;
48 }
49 }
50 if (map->first_free != entry) {
51 printf(format: "Bad first_free for map %p: %p should be %p\n",
52 map, map->first_free, entry);
53 return FALSE;
54 }
55 return TRUE;
56}
57
58void
59vm_map_store_init_ll(struct vm_map_header *hdr)
60{
61 hdr->links.next = hdr->links.prev = CAST_TO_VM_MAP_ENTRY(hdr);
62}
63
64void
65vm_map_store_entry_link_ll(
66 struct vm_map_header *hdr,
67 vm_map_entry_t after_where,
68 vm_map_entry_t entry)
69{
70 if (entry->map_aligned) {
71 assert(VM_MAP_PAGE_ALIGNED(entry->vme_start,
72 VM_MAP_HDR_PAGE_MASK(hdr)));
73 assert(VM_MAP_PAGE_ALIGNED(entry->vme_end,
74 VM_MAP_HDR_PAGE_MASK(hdr)));
75 }
76 hdr->nentries++;
77 entry->vme_prev = after_where;
78 entry->vme_next = after_where->vme_next;
79 entry->vme_prev->vme_next = entry->vme_next->vme_prev = entry;
80}
81
82void
83vm_map_store_entry_unlink_ll(struct vm_map_header *hdr, vm_map_entry_t entry)
84{
85 hdr->nentries--;
86 entry->vme_next->vme_prev = entry->vme_prev;
87 entry->vme_prev->vme_next = entry->vme_next;
88}
89
90void
91vm_map_store_copy_reset_ll(
92 vm_map_copy_t copy,
93 __unused vm_map_entry_t entry,
94 __unused int nentries)
95{
96 copy->cpy_hdr.nentries = 0;
97 vm_map_copy_first_entry(copy) =
98 vm_map_copy_last_entry(copy) =
99 vm_map_copy_to_entry(copy);
100}
101
102/*
103 * UPDATE_FIRST_FREE:
104 *
105 * Updates the map->first_free pointer to the
106 * entry immediately before the first hole in the map.
107 * The map should be locked.
108 */
109void
110update_first_free_ll(vm_map_t map, vm_map_entry_t new_first_free)
111{
112 vm_map_offset_t map_page_mask = VM_MAP_PAGE_MASK(map);
113 vm_map_entry_t next;
114
115 if (map->holelistenabled || map->disable_vmentry_reuse) {
116 return;
117 }
118
119 next = new_first_free->vme_next;
120 while (vm_map_trunc_page(next->vme_start, map_page_mask) ==
121 vm_map_trunc_page(new_first_free->vme_end, map_page_mask) ||
122 (vm_map_trunc_page(next->vme_start, map_page_mask) ==
123 vm_map_trunc_page(new_first_free->vme_start, map_page_mask) &&
124 next != vm_map_to_entry(map))) {
125 new_first_free = next;
126 next = new_first_free->vme_next;
127 if (new_first_free == vm_map_to_entry(map)) {
128 break;
129 }
130 }
131
132 map->first_free = new_first_free;
133 assert(first_free_is_valid(map));
134}
135