1 /***********************************************************
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.
10 ******************************************************************/
15 #include <X11/Xdefs.h>
16 #include <X11/Xosdefs.h>
17 #include <X11/Xfuncproto.h>
20 /*****************************************************************
22 *****************************************************************/
24 typedef struct _Private PrivateRec, *PrivatePtr;
27 /* XSELinux uses the same private keys for numerous objects */
30 /* Otherwise, you get a private in just the requested structure
32 /* These can have objects created before all of the keys are registered */
38 /* These cannot have any objects before all relevant keys are registered */
48 /* extension privates */
56 /* last private type */
60 typedef struct _DevPrivateKeyRec {
66 struct _DevPrivateKeyRec *next;
67 } DevPrivateKeyRec, *DevPrivateKey;
69 typedef struct _DevPrivateSetRec {
74 } DevPrivateSetRec, *DevPrivateSetPtr;
76 typedef struct _DevScreenPrivateKeyRec {
77 DevPrivateKeyRec screenKey;
78 } DevScreenPrivateKeyRec, *DevScreenPrivateKey;
81 * Let drivers know how to initialize private keys
84 #define HAS_DEVPRIVATEKEYREC 1
85 #define HAS_DIXREGISTERPRIVATEKEY 1
88 * Register a new private index for the private type.
90 * This initializes the specified key and optionally requests pre-allocated
91 * private space for your driver/module. If you request no extra space, you
92 * may set and get a single pointer value using this private key. Otherwise,
93 * you can get the address of the extra space and store whatever data you like
96 * You may call dixRegisterPrivateKey more than once on the same key, but the
97 * size and type must match or the server will abort.
99 * dixRegisterPrivateKey returns FALSE if it fails to allocate memory
100 * during its operation.
102 extern _X_EXPORT Bool
103 dixRegisterPrivateKey(DevPrivateKey key, DevPrivateType type, unsigned size);
106 * Check whether a private key has been registered
109 dixPrivateKeyRegistered(DevPrivateKey key)
111 return key->initialized;
115 * Get the address of the private storage.
117 * For keys with pre-defined storage, this gets the base of that storage
118 * Otherwise, it returns the place where the private pointer is stored.
121 dixGetPrivateAddr(PrivatePtr *privates, const DevPrivateKey key)
123 assert(key->initialized);
124 return (char *) (*privates) + key->offset;
128 * Fetch a private pointer stored in the object
130 * Returns the pointer stored with dixSetPrivate.
131 * This must only be used with keys that have
132 * no pre-defined storage
135 dixGetPrivate(PrivatePtr *privates, const DevPrivateKey key)
137 assert(key->size == 0);
138 return *(void **) dixGetPrivateAddr(privates, key);
142 * Associate 'val' with 'key' in 'privates' so that later calls to
143 * dixLookupPrivate(privates, key) will return 'val'.
146 dixSetPrivate(PrivatePtr *privates, const DevPrivateKey key, pointer val)
148 assert(key->size == 0);
149 *(pointer *) dixGetPrivateAddr(privates, key) = val;
153 #include "resource.h"
156 * Lookup a pointer to the private record.
158 * For privates with defined storage, return the address of the
159 * storage. For privates without defined storage, return the pointer
162 static inline pointer
163 dixLookupPrivate(PrivatePtr *privates, const DevPrivateKey key)
166 return dixGetPrivateAddr(privates, key);
168 return dixGetPrivate(privates, key);
172 * Look up the address of the pointer to the storage
174 * This returns the place where the private pointer is stored,
175 * which is only valid for privates without predefined storage.
177 static inline pointer *
178 dixLookupPrivateAddr(PrivatePtr *privates, const DevPrivateKey key)
180 assert(key->size == 0);
181 return (pointer *) dixGetPrivateAddr(privates, key);
184 extern _X_EXPORT Bool
186 dixRegisterScreenPrivateKey(DevScreenPrivateKey key, ScreenPtr pScreen,
187 DevPrivateType type, unsigned size);
189 extern _X_EXPORT DevPrivateKey
190 _dixGetScreenPrivateKey(const DevScreenPrivateKey key, ScreenPtr pScreen);
193 dixGetScreenPrivateAddr(PrivatePtr *privates, const DevScreenPrivateKey key,
196 return dixGetPrivateAddr(privates, _dixGetScreenPrivateKey(key, pScreen));
200 dixGetScreenPrivate(PrivatePtr *privates, const DevScreenPrivateKey key,
203 return dixGetPrivate(privates, _dixGetScreenPrivateKey(key, pScreen));
207 dixSetScreenPrivate(PrivatePtr *privates, const DevScreenPrivateKey key,
208 ScreenPtr pScreen, pointer val)
210 dixSetPrivate(privates, _dixGetScreenPrivateKey(key, pScreen), val);
213 static inline pointer
214 dixLookupScreenPrivate(PrivatePtr *privates, const DevScreenPrivateKey key,
217 return dixLookupPrivate(privates, _dixGetScreenPrivateKey(key, pScreen));
220 static inline pointer *
221 dixLookupScreenPrivateAddr(PrivatePtr *privates, const DevScreenPrivateKey key,
224 return dixLookupPrivateAddr(privates,
225 _dixGetScreenPrivateKey(key, pScreen));
229 * These functions relate to allocations related to a specific screen;
230 * space will only be available for objects allocated for use on that
231 * screen. As such, only objects which are related directly to a specific
232 * screen are candidates for allocation this way, this includes
233 * windows, pixmaps, gcs, pictures and colormaps. This key is
234 * used just like any other key using dixGetPrivate and friends.
236 * This is distinctly different from the ScreenPrivateKeys above which
237 * allocate space in global objects like cursor bits for a specific
238 * screen, allowing multiple screen-related chunks of storage in a
239 * single global object.
242 #define HAVE_SCREEN_SPECIFIC_PRIVATE_KEYS 1
244 extern _X_EXPORT Bool
245 dixRegisterScreenSpecificPrivateKey(ScreenPtr pScreen, DevPrivateKey key,
246 DevPrivateType type, unsigned size);
248 /* Clean up screen-specific privates before CloseScreen */
250 dixFreeScreenSpecificPrivates(ScreenPtr pScreen);
252 /* Initialize screen-specific privates in AddScreen */
254 dixInitScreenSpecificPrivates(ScreenPtr pScreen);
256 extern _X_EXPORT void *
257 _dixAllocateScreenObjectWithPrivates(ScreenPtr pScreen,
261 DevPrivateType type);
263 #define dixAllocateScreenObjectWithPrivates(s, t, type) _dixAllocateScreenObjectWithPrivates(s, sizeof(t), sizeof(t), offsetof(t, devPrivates), type)
266 dixScreenSpecificPrivatesSize(ScreenPtr pScreen, DevPrivateType type);
268 extern _X_EXPORT void
269 _dixInitScreenPrivates(ScreenPtr pScreen, PrivatePtr *privates, void *addr, DevPrivateType type);
271 #define dixInitScreenPrivates(s, o, v, type) _dixInitScreenPrivates(s, &(o)->devPrivates, (v), type);
274 * Allocates private data separately from main object.
276 * For objects created during server initialization, this allows those
277 * privates to be re-allocated as new private keys are registered.
279 * This includes screens, the serverClient, default colormaps and
280 * extensions entries.
282 extern _X_EXPORT Bool
283 dixAllocatePrivates(PrivatePtr *privates, DevPrivateType type);
286 * Frees separately allocated private data
288 extern _X_EXPORT void
289 dixFreePrivates(PrivatePtr privates, DevPrivateType type);
292 * Initialize privates by zeroing them
294 extern _X_EXPORT void
295 _dixInitPrivates(PrivatePtr *privates, void *addr, DevPrivateType type);
297 #define dixInitPrivates(o, v, type) _dixInitPrivates(&(o)->devPrivates, (v), type);
302 extern _X_EXPORT void
303 _dixFiniPrivates(PrivatePtr privates, DevPrivateType type);
305 #define dixFiniPrivates(o,t) _dixFiniPrivates((o)->devPrivates,t)
308 * Allocates private data at object creation time. Required
309 * for almost all objects, except for the list described
310 * above for dixAllocatePrivates.
312 extern _X_EXPORT void *_dixAllocateObjectWithPrivates(unsigned size,
315 DevPrivateType type);
317 #define dixAllocateObjectWithPrivates(t, type) (t *) _dixAllocateObjectWithPrivates(sizeof(t), sizeof(t), offsetof(t, devPrivates), type)
319 extern _X_EXPORT void
321 _dixFreeObjectWithPrivates(void *object, PrivatePtr privates,
322 DevPrivateType type);
324 #define dixFreeObjectWithPrivates(o,t) _dixFreeObjectWithPrivates(o, (o)->devPrivates, t)
327 * Return size of privates for the specified type
330 dixPrivatesSize(DevPrivateType type);
333 * Dump out private stats to ErrorF
336 dixPrivateUsage(void);
339 * Resets the privates subsystem. dixResetPrivates is called from the main loop
340 * before each server generation. This function must only be called by main().
342 extern _X_EXPORT void
343 dixResetPrivates(void);
346 * Looks up the offset where the devPrivates field is located.
348 * Returns -1 if the specified resource has no dev privates.
349 * The position of the devPrivates field varies by structure
350 * and calling code might only know the resource type, not the
351 * structure definition.
354 dixLookupPrivateOffset(RESTYPE type);
357 * Convenience macro for adding an offset to an object pointer
358 * when making a call to one of the devPrivates functions
360 #define DEVPRIV_AT(ptr, offset) ((PrivatePtr *)((char *)(ptr) + offset))
362 #endif /* PRIVATES_H */