~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Wine Cross Reference
wine/dlls/kernel32/tests/heap.c

Version: ~ [ wine-1.1.25 ] ~ [ wine-1.1.24 ] ~ [ wine-1.1.23 ] ~ [ wine-1.1.22 ] ~ [ wine-1.1.21 ] ~ [ wine-1.1.20 ] ~ [ wine-1.1.19 ] ~ [ wine-1.1.18 ] ~ [ wine-1.1.17 ] ~ [ wine-1.1.16 ] ~ [ wine-1.1.15 ] ~ [ wine-1.1.14 ] ~ [ wine-1.1.13 ] ~ [ wine-1.1.12 ] ~ [ wine-1.1.11 ] ~ [ wine-1.1.10 ] ~ [ wine-1.1.9 ] ~ [ wine-1.1.8 ] ~ [ wine-1.1.7 ] ~ [ wine-1.0.1 ] ~ [ wine-1.1.6 ] ~ [ wine-1.1.5 ] ~ [ wine-1.1.4 ] ~ [ wine-1.1.3 ] ~ [ wine-1.1.2 ] ~ [ wine-1.1.1 ] ~ [ wine-1.1.0 ] ~ [ wine-1.0 ] ~

  1 /*
  2  * Unit test suite for heap functions
  3  *
  4  * Copyright 2003 Dimitrie O. Paun
  5  * Copyright 2006 Detlef Riekenberg
  6  *
  7  * This library is free software; you can redistribute it and/or
  8  * modify it under the terms of the GNU Lesser General Public
  9  * License as published by the Free Software Foundation; either
 10  * version 2.1 of the License, or (at your option) any later version.
 11  *
 12  * This library is distributed in the hope that it will be useful,
 13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 15  * Lesser General Public License for more details.
 16  *
 17  * You should have received a copy of the GNU Lesser General Public
 18  * License along with this library; if not, write to the Free Software
 19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 20  */
 21 
 22 #include <stdarg.h>
 23 #include <stdlib.h>
 24 
 25 #include "windef.h"
 26 #include "winbase.h"
 27 #include "wine/test.h"
 28 
 29 #define MAGIC_DEAD 0xdeadbeef
 30 
 31 static SIZE_T resize_9x(SIZE_T size)
 32 {
 33     DWORD dwSizeAligned = (size + 3) & ~3;
 34     return max(dwSizeAligned, 12); /* at least 12 bytes */
 35 }
 36 
 37 static void test_sized_HeapAlloc(int nbytes)
 38 {
 39     int success;
 40     char *buf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, nbytes);
 41     ok(buf != NULL, "allocate failed\n");
 42     ok(buf[0] == 0, "buffer not zeroed\n");
 43     success = HeapFree(GetProcessHeap(), 0, buf);
 44     ok(success, "free failed\n");
 45 }
 46 
 47 static void test_sized_HeapReAlloc(int nbytes1, int nbytes2)
 48 {
 49     int success;
 50     char *buf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, nbytes1);
 51     ok(buf != NULL, "allocate failed\n");
 52     ok(buf[0] == 0, "buffer not zeroed\n");
 53     buf = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buf, nbytes2);
 54     ok(buf != NULL, "reallocate failed\n");
 55     ok(buf[nbytes2-1] == 0, "buffer not zeroed\n");
 56     success = HeapFree(GetProcessHeap(), 0, buf);
 57     ok(success, "free failed\n");
 58 }
 59 
 60 static void test_heap(void)
 61 {
 62     LPVOID  mem;
 63     LPVOID  msecond;
 64     DWORD   res;
 65     UINT    flags;
 66     HGLOBAL gbl;
 67     HGLOBAL hsecond;
 68     SIZE_T  size;
 69 
 70     /* Heap*() functions */
 71     mem = HeapAlloc(GetProcessHeap(), 0, 0);
 72     ok(mem != NULL, "memory not allocated for size 0\n");
 73 
 74     mem = HeapReAlloc(GetProcessHeap(), 0, NULL, 10);
 75     ok(mem == NULL, "memory allocated by HeapReAlloc\n");
 76 
 77     for (size = 0; size <= 256; size++)
 78     {
 79         SIZE_T heap_size;
 80         mem = HeapAlloc(GetProcessHeap(), 0, size);
 81         heap_size = HeapSize(GetProcessHeap(), 0, mem);
 82         ok(heap_size == size || heap_size == resize_9x(size), 
 83             "HeapSize returned %lu instead of %lu or %lu\n", heap_size, size, resize_9x(size));
 84         HeapFree(GetProcessHeap(), 0, mem);
 85     }
 86 
 87     /* test some border cases of HeapAlloc and HeapReAlloc */
 88     mem = HeapAlloc(GetProcessHeap(), 0, 0);
 89     ok(mem != NULL, "memory not allocated for size 0\n");
 90     msecond = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, mem, ~(SIZE_T)0 - 7);
 91     ok(msecond == NULL, "HeapReAlloc(~0 - 7) should have failed\n");
 92     msecond = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, mem, ~(SIZE_T)0);
 93     ok(msecond == NULL, "HeapReAlloc(~0) should have failed\n");
 94     HeapFree(GetProcessHeap(), 0, mem);
 95     mem = HeapAlloc(GetProcessHeap(), 0, ~(SIZE_T)0);
 96     ok(mem == NULL, "memory allocated for size ~0\n");
 97 
 98     /* large blocks must be 16-byte aligned */
 99     mem = HeapAlloc(GetProcessHeap(), 0, 512 * 1024);
100     ok( mem != NULL, "failed for size 512K\n" );
101     ok( (ULONG_PTR)mem % 16 == 0 || broken((ULONG_PTR)mem % 16) /* win9x */,
102         "512K block not 16-byte aligned\n" );
103     HeapFree(GetProcessHeap(), 0, mem);
104 
105     /* Global*() functions */
106     gbl = GlobalAlloc(GMEM_MOVEABLE, 0);
107     ok(gbl != NULL, "global memory not allocated for size 0\n");
108 
109     gbl = GlobalReAlloc(gbl, 10, GMEM_MOVEABLE);
110     ok(gbl != NULL, "Can't realloc global memory\n");
111     size = GlobalSize(gbl);
112     ok(size >= 10 && size <= 16, "Memory not resized to size 10, instead size=%ld\n", size);
113 
114     gbl = GlobalReAlloc(gbl, 0, GMEM_MOVEABLE);
115     ok(gbl != NULL, "GlobalReAlloc should not fail on size 0\n");
116 
117     size = GlobalSize(gbl);
118     ok(size == 0, "Memory not resized to size 0, instead size=%ld\n", size);
119     ok(GlobalFree(gbl) == NULL, "Memory not freed\n");
120     size = GlobalSize(gbl);
121     ok(size == 0, "Memory should have been freed, size=%ld\n", size);
122 
123     gbl = GlobalReAlloc(0, 10, GMEM_MOVEABLE);
124     ok(gbl == NULL, "global realloc allocated memory\n");
125 
126     /* GlobalLock / GlobalUnlock with a valid handle */
127     gbl = GlobalAlloc(GMEM_MOVEABLE, 256);
128 
129     SetLastError(MAGIC_DEAD);
130     mem = GlobalLock(gbl);      /* #1 */
131     ok(mem != NULL, "returned %p with %d (expected '!= NULL')\n", mem, GetLastError());
132     SetLastError(MAGIC_DEAD);
133     flags = GlobalFlags(gbl);
134     ok( flags == 1, "returned 0x%04x with %d (expected '0x0001')\n",
135         flags, GetLastError());
136 
137     SetLastError(MAGIC_DEAD);
138     msecond = GlobalLock(gbl);   /* #2 */
139     ok( msecond == mem, "returned %p with %d (expected '%p')\n",
140         msecond, GetLastError(), mem);
141     SetLastError(MAGIC_DEAD);
142     flags = GlobalFlags(gbl);
143     ok( flags == 2, "returned 0x%04x with %d (expected '0x0002')\n",
144         flags, GetLastError());
145     SetLastError(MAGIC_DEAD);
146 
147     SetLastError(MAGIC_DEAD);
148     res = GlobalUnlock(gbl);    /* #1 */
149     ok(res, "returned %d with %d (expected '!= 0')\n", res, GetLastError());
150     SetLastError(MAGIC_DEAD);
151     flags = GlobalFlags(gbl);
152     ok( flags , "returned 0x%04x with %d (expected '!= 0')\n",
153         flags, GetLastError());
154 
155     SetLastError(MAGIC_DEAD);
156     res = GlobalUnlock(gbl);    /* #0 */
157     /* NT: ERROR_SUCCESS (documented on MSDN), 9x: untouched */
158     ok(!res && ((GetLastError() == ERROR_SUCCESS) || (GetLastError() == MAGIC_DEAD)),
159         "returned %d with %d (expected '' with: ERROR_SUCCESS or "
160         "MAGIC_DEAD)\n", res, GetLastError());
161     SetLastError(MAGIC_DEAD);
162     flags = GlobalFlags(gbl);
163     ok( !flags , "returned 0x%04x with %d (expected '')\n",
164         flags, GetLastError());
165 
166     /* Unlock an already unlocked Handle */
167     SetLastError(MAGIC_DEAD);
168     res = GlobalUnlock(gbl);
169     /* NT: ERROR_NOT_LOCKED,  9x: untouched */
170     ok( !res &&
171         ((GetLastError() == ERROR_NOT_LOCKED) || (GetLastError() == MAGIC_DEAD)),
172         "returned %d with %d (expected '' with: ERROR_NOT_LOCKED or "
173         "MAGIC_DEAD)\n", res, GetLastError());
174  
175     GlobalFree(gbl);
176     /* invalid handles are caught in windows: */
177     SetLastError(MAGIC_DEAD);
178     hsecond = GlobalFree(gbl);      /* invalid handle: free memory twice */
179     ok( (hsecond == gbl) && (GetLastError() == ERROR_INVALID_HANDLE),
180         "returned %p with 0x%08x (expected %p with ERROR_INVALID_HANDLE)\n",
181         hsecond, GetLastError(), gbl);
182     SetLastError(MAGIC_DEAD);
183     flags = GlobalFlags(gbl);
184     ok( (flags == GMEM_INVALID_HANDLE) && (GetLastError() == ERROR_INVALID_HANDLE),
185         "returned 0x%04x with 0x%08x (expected GMEM_INVALID_HANDLE with "
186         "ERROR_INVALID_HANDLE)\n", flags, GetLastError());
187     SetLastError(MAGIC_DEAD);
188     size = GlobalSize(gbl);
189     ok( (size == 0) && (GetLastError() == ERROR_INVALID_HANDLE),
190         "returned %ld with 0x%08x (expected '' with ERROR_INVALID_HANDLE)\n",
191         size, GetLastError());
192 
193     SetLastError(MAGIC_DEAD);
194     mem = GlobalLock(gbl);
195     ok( (mem == NULL) && (GetLastError() == ERROR_INVALID_HANDLE),
196         "returned %p with 0x%08x (expected NULL with ERROR_INVALID_HANDLE)\n",
197         mem, GetLastError());
198 
199     /* documented on MSDN: GlobalUnlock() return FALSE on failure.
200        Win9x and wine return FALSE with ERROR_INVALID_HANDLE, but on 
201        NT 3.51 and XPsp2, TRUE with ERROR_INVALID_HANDLE is returned.
202        The similar Test for LocalUnlock() works on all Systems */
203     SetLastError(MAGIC_DEAD);
204     res = GlobalUnlock(gbl);
205     ok(GetLastError() == ERROR_INVALID_HANDLE,
206         "returned %d with %d (expected ERROR_INVALID_HANDLE)\n",
207         res, GetLastError());
208 
209     gbl = GlobalAlloc(GMEM_DDESHARE, 100);
210 
211     /* first free */
212     mem = GlobalFree(gbl);
213     ok(mem == NULL, "Expected NULL, got %p\n", mem);
214 
215     /* invalid free */
216     if (sizeof(void *) != 8)  /* crashes on 64-bit Vista */
217     {
218         SetLastError(MAGIC_DEAD);
219         mem = GlobalFree(gbl);
220         ok(mem == gbl || broken(mem == NULL) /* nt4 */, "Expected gbl, got %p\n", mem);
221         if (mem == gbl)
222             ok(GetLastError() == ERROR_INVALID_HANDLE ||
223                GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
224                "Expected ERROR_INVALID_HANDLE or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
225     }
226 
227     gbl = GlobalAlloc(GMEM_DDESHARE, 100);
228 
229     res = GlobalUnlock(gbl);
230     ok(res == 1 ||
231        res == 0, /* win9x */
232        "Expected 1 or 0, got %d\n", res);
233 
234     res = GlobalUnlock(gbl);
235     ok(res == 1 ||
236        res == 0, /* win9x */
237        "Expected 1 or 0, got %d\n", res);
238 
239     /* GlobalSize on an invalid handle */
240     if (sizeof(void *) != 8)  /* crashes on 64-bit Vista */
241     {
242         SetLastError(MAGIC_DEAD);
243         size = GlobalSize((HGLOBAL)0xc042);
244         ok(size == 0, "Expected 0, got %ld\n", size);
245         ok(GetLastError() == ERROR_INVALID_HANDLE ||
246            GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
247            "Expected ERROR_INVALID_HANDLE or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
248     }
249 
250     /* ####################################### */
251     /* Local*() functions */
252     gbl = LocalAlloc(LMEM_MOVEABLE, 0);
253     ok(gbl != NULL, "local memory not allocated for size 0\n");
254 
255     gbl = LocalReAlloc(gbl, 10, LMEM_MOVEABLE);
256     ok(gbl != NULL, "Can't realloc local memory\n");
257     size = LocalSize(gbl);
258     ok(size >= 10 && size <= 16, "Memory not resized to size 10, instead size=%ld\n", size);
259 
260     gbl = LocalReAlloc(gbl, 0, LMEM_MOVEABLE);
261     ok(gbl != NULL, "LocalReAlloc should not fail on size 0\n");
262 
263     size = LocalSize(gbl);
264     ok(size == 0, "Memory not resized to size 0, instead size=%ld\n", size);
265     ok(LocalFree(gbl) == NULL, "Memory not freed\n");
266     size = LocalSize(gbl);
267     ok(size == 0, "Memory should have been freed, size=%ld\n", size);
268 
269     gbl = LocalReAlloc(0, 10, LMEM_MOVEABLE);
270     ok(gbl == NULL, "local realloc allocated memory\n");
271 
272     /* LocalLock / LocalUnlock with a valid handle */
273     gbl = LocalAlloc(LMEM_MOVEABLE, 256);
274     SetLastError(MAGIC_DEAD);
275     mem = LocalLock(gbl);      /* #1 */
276     ok(mem != NULL, "returned %p with %d (expected '!= NULL')\n", mem, GetLastError());
277     SetLastError(MAGIC_DEAD);
278     flags = LocalFlags(gbl);
279     ok( flags == 1, "returned 0x%04x with %d (expected '0x0001')\n",
280         flags, GetLastError());
281 
282     SetLastError(MAGIC_DEAD);
283     msecond = LocalLock(gbl);   /* #2 */
284     ok( msecond == mem, "returned %p with %d (expected '%p')\n",
285         msecond, GetLastError(), mem);
286     SetLastError(MAGIC_DEAD);
287     flags = LocalFlags(gbl);
288     ok( flags == 2, "returned 0x%04x with %d (expected '0x0002')\n",
289         flags, GetLastError());
290     SetLastError(MAGIC_DEAD);
291 
292     SetLastError(MAGIC_DEAD);
293     res = LocalUnlock(gbl);    /* #1 */
294     ok(res, "returned %d with %d (expected '!= 0')\n", res, GetLastError());
295     SetLastError(MAGIC_DEAD);
296     flags = LocalFlags(gbl);
297     ok( flags , "returned 0x%04x with %d (expected '!= 0')\n",
298         flags, GetLastError());
299 
300     SetLastError(MAGIC_DEAD);
301     res = LocalUnlock(gbl);    /* #0 */
302     /* NT: ERROR_SUCCESS (documented on MSDN), 9x: untouched */
303     ok(!res && ((GetLastError() == ERROR_SUCCESS) || (GetLastError() == MAGIC_DEAD)),
304         "returned %d with %d (expected '' with: ERROR_SUCCESS or "
305         "MAGIC_DEAD)\n", res, GetLastError());
306     SetLastError(MAGIC_DEAD);
307     flags = LocalFlags(gbl);
308     ok( !flags , "returned 0x%04x with %d (expected '')\n",
309         flags, GetLastError());
310 
311     /* Unlock an already unlocked Handle */
312     SetLastError(MAGIC_DEAD);
313     res = LocalUnlock(gbl);
314     /* NT: ERROR_NOT_LOCKED,  9x: untouched */
315     ok( !res &&
316         ((GetLastError() == ERROR_NOT_LOCKED) || (GetLastError() == MAGIC_DEAD)),
317         "returned %d with %d (expected '' with: ERROR_NOT_LOCKED or "
318         "MAGIC_DEAD)\n", res, GetLastError());
319 
320     LocalFree(gbl);
321     /* invalid handles are caught in windows: */
322     SetLastError(MAGIC_DEAD);
323     hsecond = LocalFree(gbl);       /* invalid handle: free memory twice */
324     ok( (hsecond == gbl) && (GetLastError() == ERROR_INVALID_HANDLE),
325         "returned %p with 0x%08x (expected %p with ERROR_INVALID_HANDLE)\n",
326         hsecond, GetLastError(), gbl);
327     SetLastError(MAGIC_DEAD);
328     flags = LocalFlags(gbl);
329     ok( (flags == LMEM_INVALID_HANDLE) && (GetLastError() == ERROR_INVALID_HANDLE),
330         "returned 0x%04x with 0x%08x (expected LMEM_INVALID_HANDLE with "
331         "ERROR_INVALID_HANDLE)\n", flags, GetLastError());
332     SetLastError(MAGIC_DEAD);
333     size = LocalSize(gbl);
334     ok( (size == 0) && (GetLastError() == ERROR_INVALID_HANDLE),
335         "returned %ld with 0x%08x (expected '' with ERROR_INVALID_HANDLE)\n",
336         size, GetLastError());
337 
338     SetLastError(MAGIC_DEAD);
339     mem = LocalLock(gbl);
340     ok( (mem == NULL) && (GetLastError() == ERROR_INVALID_HANDLE),
341         "returned %p with 0x%08x (expected NULL with ERROR_INVALID_HANDLE)\n",
342         mem, GetLastError());
343 
344     /* This Test works the same on all Systems (GlobalUnlock() is different) */
345     SetLastError(MAGIC_DEAD);
346     res = LocalUnlock(gbl);
347     ok(!res && (GetLastError() == ERROR_INVALID_HANDLE),
348         "returned %d with %d (expected '' with ERROR_INVALID_HANDLE)\n",
349         res, GetLastError());
350 
351     /* trying to lock empty memory should give an error */
352     gbl = GlobalAlloc(GMEM_MOVEABLE|GMEM_ZEROINIT,0);
353     ok(gbl != NULL, "returned NULL\n");
354     SetLastError(MAGIC_DEAD);
355     mem = GlobalLock(gbl);
356     /* NT: ERROR_DISCARDED,  9x: untouched */
357     ok( (mem == NULL) &&
358         ((GetLastError() == ERROR_DISCARDED) || (GetLastError() == MAGIC_DEAD)),
359         "returned %p with 0x%x/%d (expected 'NULL' with: ERROR_DISCARDED or "
360         "MAGIC_DEAD)\n", mem, GetLastError(), GetLastError());
361 
362     GlobalFree(gbl);
363 }
364 
365 static void test_obsolete_flags(void)
366 {
367     static struct {
368         UINT flags;
369         UINT globalflags;
370     } test_global_flags[] = {
371         {GMEM_FIXED | GMEM_NOTIFY, 0},
372         {GMEM_FIXED | GMEM_DISCARDABLE, 0},
373         {GMEM_MOVEABLE | GMEM_NOTIFY, 0},
374         {GMEM_MOVEABLE | GMEM_DDESHARE, GMEM_DDESHARE},
375         {GMEM_MOVEABLE | GMEM_NOT_BANKED, 0},
376         {GMEM_MOVEABLE | GMEM_NODISCARD, 0},
377         {GMEM_MOVEABLE | GMEM_DISCARDABLE, GMEM_DISCARDABLE},
378         {GMEM_MOVEABLE | GMEM_DDESHARE | GMEM_DISCARDABLE | GMEM_LOWER | GMEM_NOCOMPACT | GMEM_NODISCARD |
379          GMEM_NOT_BANKED | GMEM_NOTIFY, GMEM_DDESHARE | GMEM_DISCARDABLE},
380     };
381 
382     unsigned int i;
383     HGLOBAL gbl;
384     UINT resultflags;
385 
386     UINT (WINAPI *pGlobalFlags)(HGLOBAL);
387 
388     pGlobalFlags = (void *) GetProcAddress(GetModuleHandleA("kernel32"), "GlobalFlags");
389 
390     if (!pGlobalFlags)
391     {
392         win_skip("GlobalFlags is not available\n");
393         return;
394     }
395 
396     for (i = 0; i < sizeof(test_global_flags)/sizeof(test_global_flags[0]); i++)
397     {
398         gbl = GlobalAlloc(test_global_flags[i].flags, 4);
399         ok(gbl != NULL, "GlobalAlloc failed\n");
400 
401         SetLastError(MAGIC_DEAD);
402         resultflags = pGlobalFlags(gbl);
403 
404         ok( resultflags == test_global_flags[i].globalflags ||
405             broken(resultflags == (test_global_flags[i].globalflags & ~GMEM_DDESHARE)), /* win9x */
406             "%u: expected 0x%08x, but returned 0x%08x with %d\n",
407             i, test_global_flags[i].globalflags, resultflags, GetLastError() );
408 
409         GlobalFree(gbl);
410     }
411 }
412 
413 START_TEST(heap)
414 {
415     test_heap();
416     test_obsolete_flags();
417 
418     /* Test both short and very long blocks */
419     test_sized_HeapAlloc(1);
420     test_sized_HeapAlloc(1 << 20);
421     test_sized_HeapReAlloc(1, 100);
422     test_sized_HeapReAlloc(1, (1 << 20));
423     test_sized_HeapReAlloc((1 << 20), (2 << 20));
424     test_sized_HeapReAlloc((1 << 20), 1);
425 }
426 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.