1 | /***********************************************************
|
---|
2 |
|
---|
3 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
---|
4 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
---|
5 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
---|
6 | AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
---|
7 | AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
---|
8 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
---|
9 |
|
---|
10 | ******************************************************************/
|
---|
11 |
|
---|
12 | #ifndef PRIVATES_H
|
---|
13 | #define PRIVATES_H 1
|
---|
14 |
|
---|
15 | #include "dix.h"
|
---|
16 | #include "resource.h"
|
---|
17 |
|
---|
18 | /*****************************************************************
|
---|
19 | * STUFF FOR PRIVATES
|
---|
20 | *****************************************************************/
|
---|
21 |
|
---|
22 | typedef void *DevPrivateKey;
|
---|
23 |
|
---|
24 | typedef struct _Private {
|
---|
25 | DevPrivateKey key;
|
---|
26 | pointer value;
|
---|
27 | struct _Private *next;
|
---|
28 | } PrivateRec;
|
---|
29 |
|
---|
30 | /*
|
---|
31 | * Request pre-allocated private space for your driver/module.
|
---|
32 | * Calling this is not necessary if only a pointer by itself is needed.
|
---|
33 | */
|
---|
34 | extern int
|
---|
35 | dixRequestPrivate(const DevPrivateKey key, unsigned size);
|
---|
36 |
|
---|
37 | /*
|
---|
38 | * Allocates a new private and attaches it to an existing object.
|
---|
39 | */
|
---|
40 | extern pointer *
|
---|
41 | dixAllocatePrivate(PrivateRec **privates, const DevPrivateKey key);
|
---|
42 |
|
---|
43 | /*
|
---|
44 | * Look up a private pointer.
|
---|
45 | */
|
---|
46 | static _X_INLINE pointer
|
---|
47 | dixLookupPrivate(PrivateRec **privates, const DevPrivateKey key)
|
---|
48 | {
|
---|
49 | PrivateRec *rec = *privates;
|
---|
50 | pointer *ptr;
|
---|
51 |
|
---|
52 | while (rec) {
|
---|
53 | if (rec->key == key)
|
---|
54 | return rec->value;
|
---|
55 | rec = rec->next;
|
---|
56 | }
|
---|
57 |
|
---|
58 | ptr = dixAllocatePrivate(privates, key);
|
---|
59 | return ptr ? *ptr : NULL;
|
---|
60 | }
|
---|
61 |
|
---|
62 | /*
|
---|
63 | * Look up the address of a private pointer.
|
---|
64 | */
|
---|
65 | static _X_INLINE pointer *
|
---|
66 | dixLookupPrivateAddr(PrivateRec **privates, const DevPrivateKey key)
|
---|
67 | {
|
---|
68 | PrivateRec *rec = *privates;
|
---|
69 |
|
---|
70 | while (rec) {
|
---|
71 | if (rec->key == key)
|
---|
72 | return &rec->value;
|
---|
73 | rec = rec->next;
|
---|
74 | }
|
---|
75 |
|
---|
76 | return dixAllocatePrivate(privates, key);
|
---|
77 | }
|
---|
78 |
|
---|
79 | /*
|
---|
80 | * Set a private pointer.
|
---|
81 | */
|
---|
82 | static _X_INLINE int
|
---|
83 | dixSetPrivate(PrivateRec **privates, const DevPrivateKey key, pointer val)
|
---|
84 | {
|
---|
85 | PrivateRec *rec;
|
---|
86 |
|
---|
87 | top:
|
---|
88 | rec = *privates;
|
---|
89 | while (rec) {
|
---|
90 | if (rec->key == key) {
|
---|
91 | rec->value = val;
|
---|
92 | return TRUE;
|
---|
93 | }
|
---|
94 | rec = rec->next;
|
---|
95 | }
|
---|
96 |
|
---|
97 | if (!dixAllocatePrivate(privates, key))
|
---|
98 | return FALSE;
|
---|
99 | goto top;
|
---|
100 | }
|
---|
101 |
|
---|
102 | /*
|
---|
103 | * Register callbacks to be called on private allocation/freeing.
|
---|
104 | * The calldata argument to the callbacks is a PrivateCallbackPtr.
|
---|
105 | */
|
---|
106 | typedef struct _PrivateCallback {
|
---|
107 | DevPrivateKey key; /* private registration key */
|
---|
108 | pointer *value; /* address of private pointer */
|
---|
109 | } PrivateCallbackRec;
|
---|
110 |
|
---|
111 | extern int
|
---|
112 | dixRegisterPrivateInitFunc(const DevPrivateKey key,
|
---|
113 | CallbackProcPtr callback, pointer userdata);
|
---|
114 |
|
---|
115 | extern int
|
---|
116 | dixRegisterPrivateDeleteFunc(const DevPrivateKey key,
|
---|
117 | CallbackProcPtr callback, pointer userdata);
|
---|
118 |
|
---|
119 | /*
|
---|
120 | * Frees private data.
|
---|
121 | */
|
---|
122 | extern void
|
---|
123 | dixFreePrivates(PrivateRec *privates);
|
---|
124 |
|
---|
125 | /*
|
---|
126 | * Resets the subsystem, called from the main loop.
|
---|
127 | */
|
---|
128 | extern int
|
---|
129 | dixResetPrivates(void);
|
---|
130 |
|
---|
131 | /*
|
---|
132 | * These next two functions are necessary because the position of
|
---|
133 | * the devPrivates field varies by structure and calling code might
|
---|
134 | * only know the resource type, not the structure definition.
|
---|
135 | */
|
---|
136 |
|
---|
137 | /*
|
---|
138 | * Looks up the offset where the devPrivates field is located.
|
---|
139 | * Returns -1 if no offset has been registered for the resource type.
|
---|
140 | */
|
---|
141 | extern int
|
---|
142 | dixLookupPrivateOffset(RESTYPE type);
|
---|
143 |
|
---|
144 | /*
|
---|
145 | * Specifies the offset where the devPrivates field is located.
|
---|
146 | * A negative value indicates no devPrivates field is available.
|
---|
147 | */
|
---|
148 | extern int
|
---|
149 | dixRegisterPrivateOffset(RESTYPE type, int offset);
|
---|
150 |
|
---|
151 | /*
|
---|
152 | * Convenience macro for adding an offset to an object pointer
|
---|
153 | * when making a call to one of the devPrivates functions
|
---|
154 | */
|
---|
155 | #define DEVPRIV_AT(ptr, offset) ((PrivateRec **)((char *)ptr + offset))
|
---|
156 |
|
---|
157 | #endif /* PRIVATES_H */
|
---|