ddraw: Move the wined3d_texture_update_desc() call into ddraw_surface_create_wined3d_...
[wine.git] / dlls / vbscript / vbscript_main.c
blobfe63c7660a72797215a8adee5b3f13b0f7e9eace
1 /*
2 * Copyright 2011 Jacek Caban for CodeWeavers
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.
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.
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
19 #include "initguid.h"
21 #include "vbscript.h"
22 #include "objsafe.h"
23 #include "mshtmhst.h"
24 #include "rpcproxy.h"
25 #include "vbscript_classes.h"
26 #include "vbsglobal.h"
27 #include "vbsregexp55.h"
29 #include "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(vbscript);
32 WINE_DECLARE_DEBUG_CHANNEL(heap);
34 DEFINE_GUID(GUID_NULL,0,0,0,0,0,0,0,0,0,0,0);
36 static HINSTANCE vbscript_hinstance;
37 static ITypeInfo *dispatch_typeinfo;
39 BSTR get_vbscript_string(int id)
41 WCHAR buf[512];
42 if(!LoadStringW(vbscript_hinstance, id, buf, ARRAY_SIZE(buf))) return NULL;
43 return SysAllocString(buf);
46 #define MIN_BLOCK_SIZE 128
47 #define ARENA_FREE_FILLER 0xaa
49 static inline DWORD block_size(DWORD block)
51 return MIN_BLOCK_SIZE << block;
54 void heap_pool_init(heap_pool_t *heap)
56 memset(heap, 0, sizeof(*heap));
57 list_init(&heap->custom_blocks);
60 void *heap_pool_alloc(heap_pool_t *heap, size_t size)
62 struct list *list;
63 void *tmp;
65 size = (size+3)&~3;
67 if(!heap->block_cnt) {
68 if(!heap->blocks) {
69 heap->blocks = malloc(sizeof(void*));
70 if(!heap->blocks)
71 return NULL;
74 tmp = malloc(block_size(0));
75 if(!tmp)
76 return NULL;
78 heap->blocks[0] = tmp;
79 heap->block_cnt = 1;
82 if(heap->offset + size <= block_size(heap->last_block)) {
83 tmp = ((BYTE*)heap->blocks[heap->last_block])+heap->offset;
84 heap->offset += size;
85 return tmp;
88 if(size <= block_size(heap->last_block+1)) {
89 if(heap->last_block+1 == heap->block_cnt) {
90 tmp = realloc(heap->blocks, (heap->block_cnt+1)*sizeof(void*));
91 if(!tmp)
92 return NULL;
94 heap->blocks = tmp;
95 heap->blocks[heap->block_cnt] = malloc(block_size(heap->block_cnt));
96 if(!heap->blocks[heap->block_cnt])
97 return NULL;
99 heap->block_cnt++;
102 heap->last_block++;
103 heap->offset = size;
104 return heap->blocks[heap->last_block];
107 list = malloc(size + sizeof(struct list));
108 if(!list)
109 return NULL;
111 list_add_head(&heap->custom_blocks, list);
112 return list+1;
115 void *heap_pool_grow(heap_pool_t *heap, void *mem, DWORD size, DWORD inc)
117 void *ret;
119 if(mem == (BYTE*)heap->blocks[heap->last_block] + heap->offset-size
120 && heap->offset+inc < block_size(heap->last_block)) {
121 heap->offset += inc;
122 return mem;
125 ret = heap_pool_alloc(heap, size+inc);
126 if(ret) /* FIXME: avoid copying for custom blocks */
127 memcpy(ret, mem, size);
128 return ret;
131 void heap_pool_clear(heap_pool_t *heap)
133 struct list *tmp;
135 if(!heap)
136 return;
138 while((tmp = list_next(&heap->custom_blocks, &heap->custom_blocks))) {
139 list_remove(tmp);
140 free(tmp);
143 if(WARN_ON(heap)) {
144 DWORD i;
146 for(i=0; i < heap->block_cnt; i++)
147 memset(heap->blocks[i], ARENA_FREE_FILLER, block_size(i));
150 heap->last_block = heap->offset = 0;
151 heap->mark = FALSE;
154 void heap_pool_free(heap_pool_t *heap)
156 DWORD i;
158 heap_pool_clear(heap);
160 for(i=0; i < heap->block_cnt; i++)
161 free(heap->blocks[i]);
162 free(heap->blocks);
164 heap_pool_init(heap);
167 heap_pool_t *heap_pool_mark(heap_pool_t *heap)
169 if(heap->mark)
170 return NULL;
172 heap->mark = TRUE;
173 return heap;
176 HRESULT get_dispatch_typeinfo(ITypeInfo **out)
178 ITypeInfo *typeinfo;
179 ITypeLib *typelib;
180 HRESULT hr;
182 if (!dispatch_typeinfo)
184 hr = LoadRegTypeLib(&IID_StdOle, STDOLE_MAJORVERNUM, STDOLE_MINORVERNUM, STDOLE_LCID, &typelib);
185 if (FAILED(hr)) return hr;
187 hr = ITypeLib_GetTypeInfoOfGuid(typelib, &IID_IDispatch, &typeinfo);
188 ITypeLib_Release(typelib);
189 if (FAILED(hr)) return hr;
191 if (InterlockedCompareExchangePointer((void**)&dispatch_typeinfo, typeinfo, NULL))
192 ITypeInfo_Release(typeinfo);
195 *out = dispatch_typeinfo;
196 return S_OK;
199 static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFIID riid, void **ppv)
201 *ppv = NULL;
203 if(IsEqualGUID(&IID_IUnknown, riid)) {
204 TRACE("(%p)->(IID_IUnknown %p)\n", iface, ppv);
205 *ppv = iface;
206 }else if(IsEqualGUID(&IID_IClassFactory, riid)) {
207 TRACE("(%p)->(IID_IClassFactory %p)\n", iface, ppv);
208 *ppv = iface;
211 if(*ppv) {
212 IUnknown_AddRef((IUnknown*)*ppv);
213 return S_OK;
216 FIXME("(%p)->(%s %p)\n", iface, debugstr_guid(riid), ppv);
217 return E_NOINTERFACE;
220 static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
222 TRACE("(%p)\n", iface);
223 return 2;
226 static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
228 TRACE("(%p)\n", iface);
229 return 1;
232 static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL fLock)
234 TRACE("(%p)->(%x)\n", iface, fLock);
235 return S_OK;
238 static const IClassFactoryVtbl VBScriptFactoryVtbl = {
239 ClassFactory_QueryInterface,
240 ClassFactory_AddRef,
241 ClassFactory_Release,
242 VBScriptFactory_CreateInstance,
243 ClassFactory_LockServer
246 static IClassFactory VBScriptFactory = { &VBScriptFactoryVtbl };
248 static const IClassFactoryVtbl VBScriptRegExpFactoryVtbl = {
249 ClassFactory_QueryInterface,
250 ClassFactory_AddRef,
251 ClassFactory_Release,
252 VBScriptRegExpFactory_CreateInstance,
253 ClassFactory_LockServer
256 static IClassFactory VBScriptRegExpFactory = { &VBScriptRegExpFactoryVtbl };
258 /******************************************************************
259 * DllMain (vbscript.@)
261 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
263 TRACE("(%p %ld %p)\n", hInstDLL, fdwReason, lpv);
265 switch(fdwReason)
267 case DLL_PROCESS_ATTACH:
268 DisableThreadLibraryCalls(hInstDLL);
269 vbscript_hinstance = hInstDLL;
270 break;
271 case DLL_PROCESS_DETACH:
272 if (lpv) break;
273 if (dispatch_typeinfo) ITypeInfo_Release(dispatch_typeinfo);
274 release_regexp_typelib();
277 return TRUE;
280 /***********************************************************************
281 * DllGetClassObject (vbscript.@)
283 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
285 if(IsEqualGUID(&CLSID_VBScript, rclsid)) {
286 TRACE("(CLSID_VBScript %s %p)\n", debugstr_guid(riid), ppv);
287 return IClassFactory_QueryInterface(&VBScriptFactory, riid, ppv);
288 }else if(IsEqualGUID(&CLSID_VBScriptRegExp, rclsid)) {
289 TRACE("(CLSID_VBScriptRegExp %s %p)\n", debugstr_guid(riid), ppv);
290 return IClassFactory_QueryInterface(&VBScriptRegExpFactory, riid, ppv);
293 FIXME("%s %s %p\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
294 return CLASS_E_CLASSNOTAVAILABLE;