update for HEAD-2003091401
[reactos.git] / subsys / win32k / eng / handle.c
1 /*
2  *  ReactOS W32 Subsystem
3  *  Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 ReactOS Team
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 /* $Id$
20  * 
21  * COPYRIGHT:         See COPYING in the top level directory
22  * PROJECT:           ReactOS kernel
23  * PURPOSE:           Manage GDI Handles
24  * FILE:              subsys/win32k/eng/handle.c
25  * PROGRAMER:         Jason Filby
26  * REVISION HISTORY:
27  *                 29/8/1999: Created
28  */
29
30 #include <ddk/winddi.h>
31 #include "handle.h"
32
33 #define NDEBUG
34 #include <win32k/debug1.h>
35
36 static int LastHandle = MAX_GDI_HANDLES;
37
38 ULONG FASTCALL CreateGDIHandle(ULONG InternalSize, ULONG UserSize)
39 {
40   ULONG size;
41   PENGOBJ pObj;
42   int i;
43
44   //size = sizeof( ENGOBJ ) + InternalSize + UserSize;
45   size = InternalSize;          //internal size includes header and user portions
46   pObj = EngAllocMem( FL_ZERO_MEMORY, size, 0 );
47
48   if( !pObj )
49         return 0;
50
51   pObj->InternalSize = InternalSize;
52   pObj->UserSize = UserSize;
53
54   for( i = (MAX_GDI_HANDLES - 1 <= LastHandle ? 1 : LastHandle + 1); i != LastHandle;
55        i = (MAX_GDI_HANDLES - 1 <= i ? 1 : i + 1) ){
56         if( GDIHandles[ i ].pEngObj == NULL ){
57                 pObj->hObj = i;
58                 GDIHandles[ i ].pEngObj = pObj;
59                 DPRINT("CreateGDIHandle: obj: %x, handle: %d, usersize: %d\n", pObj, i, UserSize );
60                 LastHandle = i;
61                 return i;
62         }
63   }
64   DPRINT1("CreateGDIHandle: Out of available handles!!!\n");
65   EngFreeMem( pObj );
66   return 0;
67 }
68
69 VOID FASTCALL FreeGDIHandle(ULONG Handle)
70 {
71   if( Handle == 0 || Handle >= MAX_GDI_HANDLES ){
72         DPRINT1("FreeGDIHandle: invalid handle!!!!\n");
73         return;
74   }
75   DPRINT("FreeGDIHandle: handle: %d\n", Handle);
76   EngFreeMem( GDIHandles[Handle].pEngObj );
77   GDIHandles[Handle].pEngObj = NULL;
78 }
79
80 PVOID FASTCALL AccessInternalObject(ULONG Handle)
81 {
82   PENGOBJ pEngObj;
83
84   if( Handle == 0 || Handle >= MAX_GDI_HANDLES ){
85         DPRINT1("AccessInternalObject: invalid handle: %d!!!!\n", Handle);
86         return NULL;
87   }
88
89   pEngObj = GDIHandles[Handle].pEngObj;
90   return (PVOID)pEngObj;
91 }
92
93 PVOID FASTCALL AccessUserObject(ULONG Handle)
94 {
95   PENGOBJ pEngObj;
96
97   if( Handle == 0 || Handle >= MAX_GDI_HANDLES ){
98         DPRINT1("AccessUserObject: invalid handle: %d!!!!\n", Handle);
99         return NULL;
100   }
101
102   pEngObj = GDIHandles[Handle].pEngObj;
103   return (PVOID)( (PCHAR)pEngObj + sizeof( ENGOBJ ) );
104 }
105
106 ULONG FASTCALL AccessHandleFromUserObject(PVOID UserObject)
107 {
108   PENGOBJ pEngObj;
109   ULONG Handle;
110
111   if( !UserObject )
112         return INVALID_HANDLE;
113
114   pEngObj = (PENGOBJ)((PCHAR) UserObject - sizeof( ENGOBJ ));
115   Handle = pEngObj->hObj;
116
117   if( Handle == 0 || Handle >= MAX_GDI_HANDLES ){
118         DPRINT1("AccessHandleFromUserObject: inv handle: %d, obj: %x!!!!\n", Handle, pEngObj);
119         return INVALID_HANDLE;
120   }
121   return Handle;
122 }
123
124 PVOID FASTCALL AccessInternalObjectFromUserObject(PVOID UserObject)
125 {
126
127   return AccessInternalObject( AccessHandleFromUserObject( UserObject ) );
128 }
129
130 VOID FASTCALL InitEngHandleTable( void )
131 {
132         ULONG i;
133         for( i=1; i < MAX_GDI_HANDLES; i++ ){
134                 GDIHandles[ i ].pEngObj = NULL;
135         }
136 }
137 /* EOF */