devPrivates rework: register an offset for every resource type, use

signed values so -1 actually works correctly, and provide a macro for
adding an offset to a pointer.
This commit is contained in:
Eamon Walsh 2007-09-06 16:55:51 -04:00 committed by Eamon Walsh
parent 0003ccfcdf
commit 57907e0943
3 changed files with 33 additions and 20 deletions

View File

@ -35,6 +35,7 @@ from The Open Group.
#include "resource.h"
#include "privates.h"
#include "gcstruct.h"
#include "cursorstr.h"
#include "colormapst.h"
#include "inputstr.h"
@ -174,21 +175,34 @@ dixRegisterPrivateDeleteFunc(const DevPrivateKey key,
}
/* Table of devPrivates offsets */
static unsigned *offsets = NULL;
static unsigned offsetsSize = 0;
static const int offsetDefaults[] = {
-1, /* RT_NONE */
offsetof(WindowRec, devPrivates), /* RT_WINDOW */
offsetof(PixmapRec, devPrivates), /* RT_PIXMAP */
offsetof(GC, devPrivates), /* RT_GC */
-1, /* RT_FONT */
offsetof(CursorRec, devPrivates), /* RT_CURSOR */
offsetof(ColormapRec, devPrivates), /* RT_COLORMAP */
-1, /* RT_CMAPENTRY */
-1, /* RT_OTHERCLIENT */
-1 /* RT_PASSIVEGRAB */
};
static int *offsets = NULL;
static int offsetsSize = 0;
/*
* Specify where the devPrivates field is located in a structure type
*/
_X_EXPORT int
dixRegisterPrivateOffset(RESTYPE type, unsigned offset)
dixRegisterPrivateOffset(RESTYPE type, int offset)
{
type = type & TypeMask;
/* resize offsets table if necessary */
while (type >= offsetsSize) {
unsigned i = offsetsSize * 2 * sizeof(int);
offsets = (unsigned *)xrealloc(offsets, i);
offsets = (int *)xrealloc(offsets, i);
if (!offsets) {
offsetsSize = 0;
return FALSE;
@ -214,7 +228,6 @@ int
dixResetPrivates(void)
{
PrivateDescRec *next;
unsigned i;
/* reset internal structures */
while (items) {
@ -224,20 +237,11 @@ dixResetPrivates(void)
}
if (offsets)
xfree(offsets);
offsetsSize = 16;
offsets = (unsigned *)xalloc(offsetsSize * sizeof(unsigned));
offsetsSize = sizeof(offsetDefaults);
offsets = (int *)xalloc(offsetsSize);
offsetsSize /= sizeof(int);
if (!offsets)
return FALSE;
for (i=0; i < offsetsSize; i++)
offsets[i] = -1;
/* register basic resource offsets */
return dixRegisterPrivateOffset(RT_WINDOW,
offsetof(WindowRec, devPrivates)) &&
dixRegisterPrivateOffset(RT_PIXMAP,
offsetof(PixmapRec, devPrivates)) &&
dixRegisterPrivateOffset(RT_GC,
offsetof(GC, devPrivates)) &&
dixRegisterPrivateOffset(RT_COLORMAP,
offsetof(ColormapRec, devPrivates));
memcpy(offsets, offsetDefaults, sizeof(offsetDefaults));
return TRUE;
}

View File

@ -225,6 +225,8 @@ CreateNewResourceType(DeleteType deleteFunc)
(next + 1) * sizeof(DeleteType));
if (!funcs)
return 0;
if (!dixRegisterPrivateOffset(next, -1))
return 0;
#ifdef XResExtension
{

View File

@ -143,8 +143,15 @@ dixLookupPrivateOffset(RESTYPE type);
/*
* Specifies the offset where the devPrivates field is located.
* A negative value indicates no devPrivates field is available.
*/
extern int
dixRegisterPrivateOffset(RESTYPE type, unsigned offset);
dixRegisterPrivateOffset(RESTYPE type, int offset);
/*
* Convenience macro for adding an offset to an object pointer
* when making a call to one of the devPrivates functions
*/
#define DEVPRIV_AT(ptr, offset) ((PrivateRec **)((char *)ptr + offset))
#endif /* PRIVATES_H */