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#ifndef _VM_VM_MAP_STORE_H
30#define _VM_VM_MAP_STORE_H
31
32/*
33#ifndef VM_MAP_STORE_USE_LL
34#define VM_MAP_STORE_USE_LL
35#endif
36*/
37#ifndef VM_MAP_STORE_USE_RB
38#define VM_MAP_STORE_USE_RB
39#endif
40
41#include <libkern/tree.h>
42
43struct _vm_map;
44struct vm_map_entry;
45struct vm_map_copy;
46struct vm_map_header;
47
48struct vm_map_store {
49#ifdef VM_MAP_STORE_USE_RB
50	RB_ENTRY(vm_map_store) entry;
51#endif
52};
53
54#ifdef VM_MAP_STORE_USE_RB
55	RB_HEAD( rb_head, vm_map_store );
56#endif
57
58#include <vm/vm_map.h>
59#include <vm/vm_map_store_ll.h>
60#include <vm/vm_map_store_rb.h>
61
62#define UPDATE_HIGHEST_ENTRY_END(map, highest_entry)	 			\
63	MACRO_BEGIN								\
64	struct _vm_map*	UHEE_map; 						\
65	struct vm_map_entry*	UHEE_entry; 						\
66	UHEE_map = (map); 							\
67	UHEE_entry = (highest_entry);	 					\
68	if( UHEE_map->highest_entry_end < UHEE_entry->vme_end) { 		\
69		UHEE_map->highest_entry_end = UHEE_entry->vme_end;		\
70	}								\
71	MACRO_END
72
73#define	VM_MAP_HIGHEST_ENTRY(map, entry, start)					\
74	MACRO_BEGIN								\
75	struct _vm_map* VMHE_map;							\
76	struct vm_map_entry*	tmp_entry;						\
77	vm_map_offset_t VMHE_start;						\
78	VMHE_map = (map);							\
79	VMHE_start= VMHE_map->highest_entry_end + PAGE_SIZE_64;			\
80	while(vm_map_lookup_entry(VMHE_map, VMHE_start, &tmp_entry)){		\
81		VMHE_map->highest_entry_end = tmp_entry->vme_end;		\
82		VMHE_start = VMHE_map->highest_entry_end + PAGE_SIZE_64;	\
83	}									\
84	entry = tmp_entry;							\
85	start = VMHE_start;							\
86	MACRO_END
87
88/*
89 *	SAVE_HINT_MAP_READ:
90 *
91 *	Saves the specified entry as the hint for
92 *	future lookups.  only a read lock is held on map,
93 * 	so make sure the store is atomic... OSCompareAndSwap
94 *	guarantees this... also, we don't care if we collide
95 *	and someone else wins and stores their 'hint'
96 */
97#define	SAVE_HINT_MAP_READ(map,value) \
98	MACRO_BEGIN							\
99	OSCompareAndSwapPtr((map)->hint, value, &(map)->hint); \
100	MACRO_END
101
102
103/*
104 *	SAVE_HINT_MAP_WRITE:
105 *
106 *	Saves the specified entry as the hint for
107 *	future lookups.  write lock held on map,
108 * 	so no one else can be writing or looking
109 * 	until the lock is dropped, so it's safe
110 * 	to just do an assignment
111 */
112#define	SAVE_HINT_MAP_WRITE(map,value) \
113	MACRO_BEGIN		       \
114	(map)->hint = (value);	       \
115	MACRO_END
116
117#define VM_MAP_ENTRY_CREATE	1
118#define VM_MAP_ENTRY_DELETE	2
119
120void vm_map_store_init( struct vm_map_header*  );
121boolean_t vm_map_store_lookup_entry( struct _vm_map*, vm_map_offset_t, struct vm_map_entry**);
122void	vm_map_store_update( struct _vm_map*, struct vm_map_entry*, int);
123void 	_vm_map_store_entry_link( struct vm_map_header *, struct vm_map_entry*, struct vm_map_entry*);
124void 	vm_map_store_entry_link( struct _vm_map*, struct vm_map_entry*, struct vm_map_entry*);
125void	_vm_map_store_entry_unlink( struct vm_map_header *, struct vm_map_entry*);
126void	vm_map_store_entry_unlink( struct _vm_map*, struct vm_map_entry*);
127void	vm_map_store_update_first_free( struct _vm_map*, struct vm_map_entry*);
128void	vm_map_store_copy_insert( struct _vm_map*, struct vm_map_entry*, struct vm_map_copy*);
129void	vm_map_store_copy_reset( struct vm_map_copy*, struct vm_map_entry*);
130#if MACH_ASSERT
131boolean_t first_free_is_valid_store( struct _vm_map*);
132#endif
133
134#endif /* _VM_VM_MAP_STORE_H */
135
136