vbscript: Make vbscode_t own the memory it uses.
[wine] / dlls / vbscript / vbscript_main.c
1 /*
2  * Copyright 2011 Jacek Caban for CodeWeavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #include "initguid.h"
20
21 #include "vbscript.h"
22 #include "objsafe.h"
23 #include "rpcproxy.h"
24 #include "vbscript_classes.h"
25
26 #include "wine/debug.h"
27
28 WINE_DEFAULT_DEBUG_CHANNEL(vbscript);
29
30 DEFINE_GUID(GUID_NULL,0,0,0,0,0,0,0,0,0,0,0);
31
32 static HINSTANCE vbscript_hinstance;
33
34 #define MIN_BLOCK_SIZE  128
35
36 static inline DWORD block_size(DWORD block)
37 {
38     return MIN_BLOCK_SIZE << block;
39 }
40
41 void vbsheap_init(vbsheap_t *heap)
42 {
43     memset(heap, 0, sizeof(*heap));
44     list_init(&heap->custom_blocks);
45 }
46
47 void *vbsheap_alloc(vbsheap_t *heap, size_t size)
48 {
49     struct list *list;
50     void *tmp;
51
52     size = (size+3)&~3;
53
54     if(!heap->block_cnt) {
55         if(!heap->blocks) {
56             heap->blocks = heap_alloc(sizeof(void*));
57             if(!heap->blocks)
58                 return NULL;
59         }
60
61         tmp = heap_alloc(block_size(0));
62         if(!tmp)
63             return NULL;
64
65         heap->blocks[0] = tmp;
66         heap->block_cnt = 1;
67     }
68
69     if(heap->offset + size <= block_size(heap->last_block)) {
70         tmp = ((BYTE*)heap->blocks[heap->last_block])+heap->offset;
71         heap->offset += size;
72         return tmp;
73     }
74
75     if(size <= block_size(heap->last_block+1)) {
76         if(heap->last_block+1 == heap->block_cnt) {
77             tmp = heap_realloc(heap->blocks, (heap->block_cnt+1)*sizeof(void*));
78             if(!tmp)
79                 return NULL;
80
81             heap->blocks = tmp;
82             heap->blocks[heap->block_cnt] = heap_alloc(block_size(heap->block_cnt));
83             if(!heap->blocks[heap->block_cnt])
84                 return NULL;
85
86             heap->block_cnt++;
87         }
88
89         heap->last_block++;
90         heap->offset = size;
91         return heap->blocks[heap->last_block];
92     }
93
94     list = heap_alloc(size + sizeof(struct list));
95     if(!list)
96         return NULL;
97
98     list_add_head(&heap->custom_blocks, list);
99     return list+1;
100 }
101
102 void vbsheap_free(vbsheap_t *heap)
103 {
104     struct list *iter;
105     DWORD i;
106
107     while((iter = list_next(&heap->custom_blocks, &heap->custom_blocks))) {
108         list_remove(iter);
109         heap_free(iter);
110     }
111
112     for(i=0; i < heap->block_cnt; i++)
113         heap_free(heap->blocks[i]);
114     heap_free(heap->blocks);
115 }
116
117 static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFIID riid, void **ppv)
118 {
119     *ppv = NULL;
120
121     if(IsEqualGUID(&IID_IUnknown, riid)) {
122         TRACE("(%p)->(IID_IUnknown %p)\n", iface, ppv);
123         *ppv = iface;
124     }else if(IsEqualGUID(&IID_IClassFactory, riid)) {
125         TRACE("(%p)->(IID_IClassFactory %p)\n", iface, ppv);
126         *ppv = iface;
127     }
128
129     if(*ppv) {
130         IUnknown_AddRef((IUnknown*)*ppv);
131         return S_OK;
132     }
133
134     FIXME("(%p)->(%s %p)\n", iface, debugstr_guid(riid), ppv);
135     return E_NOINTERFACE;
136 }
137
138 static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
139 {
140     TRACE("(%p)\n", iface);
141     return 2;
142 }
143
144 static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
145 {
146     TRACE("(%p)\n", iface);
147     return 1;
148 }
149
150 static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL fLock)
151 {
152     TRACE("(%p)->(%x)\n", iface, fLock);
153     return S_OK;
154 }
155
156 static const IClassFactoryVtbl VBScriptFactoryVtbl = {
157     ClassFactory_QueryInterface,
158     ClassFactory_AddRef,
159     ClassFactory_Release,
160     VBScriptFactory_CreateInstance,
161     ClassFactory_LockServer
162 };
163
164 static IClassFactory VBScriptFactory = { &VBScriptFactoryVtbl };
165
166 /******************************************************************
167  *              DllMain (vbscript.@)
168  */
169 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
170 {
171     TRACE("(%p %d %p)\n", hInstDLL, fdwReason, lpv);
172
173     switch(fdwReason)
174     {
175     case DLL_WINE_PREATTACH:
176         return FALSE;  /* prefer native version */
177     case DLL_PROCESS_ATTACH:
178         DisableThreadLibraryCalls(hInstDLL);
179         vbscript_hinstance = hInstDLL;
180         break;
181     }
182
183     return TRUE;
184 }
185
186 /***********************************************************************
187  *              DllGetClassObject       (vbscript.@)
188  */
189 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
190 {
191     if(IsEqualGUID(&CLSID_VBScript, rclsid)) {
192         TRACE("(CLSID_VBScript %s %p)\n", debugstr_guid(riid), ppv);
193         return IClassFactory_QueryInterface(&VBScriptFactory, riid, ppv);
194     }
195
196     FIXME("%s %s %p\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
197     return CLASS_E_CLASSNOTAVAILABLE;
198 }
199
200 /***********************************************************************
201  *          DllCanUnloadNow (vbscript.@)
202  */
203 HRESULT WINAPI DllCanUnloadNow(void)
204 {
205     return S_FALSE;
206 }
207
208 /***********************************************************************
209  *          DllRegisterServer (vbscript.@)
210  */
211 HRESULT WINAPI DllRegisterServer(void)
212 {
213     TRACE("()\n");
214     return __wine_register_resources(vbscript_hinstance);
215 }
216
217 /***********************************************************************
218  *          DllUnregisterServer (vbscript.@)
219  */
220 HRESULT WINAPI DllUnregisterServer(void)
221 {
222     TRACE("()\n");
223     return __wine_unregister_resources(vbscript_hinstance);
224 }