alloc.c 12.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Unit test suite for memory allocation functions.
 *
 * Copyright 2002 Geoffrey Hausheer
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 20
 */

21 22
#include <stdarg.h>

23
#include "wine/test.h"
24
#include "windef.h"
25 26 27 28
#include "winbase.h"
#include "winerror.h"


29
 /* The following functions don't have tests, because either I don't know how
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
   to test them, or they are WinNT only, or require multiple threads.
   Since the last two issues shouldn't really stop the tests from being
   written, assume for now that it is all due to the first case
       HeapCompact
       HeapLock
       HeapQueryInformation
       HeapSetInformation
       HeapUnlock
       HeapValidate
       HeapWalk
*/
/* In addition, these features aren't being tested
       HEAP_NO_SERIALIZE
       HEAP_GENERATE_EXCEPTIONS
       STATUS_ACCESS_VIOLATION (error code from HeapAlloc)
*/

static void test_Heap(void)
{
    SYSTEM_INFO sysInfo;
    ULONG memchunk;
    HANDLE heap;
    LPVOID mem1,mem1a,mem3;
    UCHAR *mem2,*mem2a;
    UINT error,i;
55
    DWORD dwSize;
56

Andreas Mohr's avatar
Andreas Mohr committed
57
/* Retrieve the page size for this system */
58 59
    sysInfo.dwPageSize=0;
    GetSystemInfo(&sysInfo);
60
    ok(sysInfo.dwPageSize>0,"GetSystemInfo should return a valid page size\n");
61 62 63 64 65 66 67 68 69

/* Create a Heap with a minimum and maximum size */
/* Note that Windows and Wine seem to behave a bit differently with respect
   to memory allocation.  In Windows, you can't access all the memory
   specified in the heap (due to overhead), so choosing a reasonable maximum
   size for the heap was done mostly by trial-and-error on Win2k.  It may need
   more tweaking for otherWindows variants.
*/
    memchunk=10*sysInfo.dwPageSize;
70
    heap=HeapCreate(0,2*memchunk,5*memchunk);
71 72

/* Check that HeapCreate allocated the right amount of ram */
73 74 75
    mem1=HeapAlloc(heap,0,5*memchunk+1);
    ok(mem1==NULL,"HeapCreate allocated more Ram than it should have\n");
    HeapFree(heap,0,mem1);
76 77

/* Check that a normal alloc works */
78
    mem1=HeapAlloc(heap,0,memchunk);
79
    ok(mem1!=NULL,"HeapAlloc failed\n");
80
    if(mem1) {
81
      ok(HeapSize(heap,0,mem1)>=memchunk, "HeapAlloc should return a big enough memory block\n");
82 83 84
    }

/* Check that a 'zeroing' alloc works */
85
    mem2=HeapAlloc(heap,HEAP_ZERO_MEMORY,memchunk);
86
    ok(mem2!=NULL,"HeapAlloc failed\n");
87
    if(mem2) {
88
      ok(HeapSize(heap,0,mem2)>=memchunk,"HeapAlloc should return a big enough memory block\n");
89 90 91 92 93 94
      error=0;
      for(i=0;i<memchunk;i++) {
        if(mem2[i]!=0) {
          error=1;
        }
      }
95
      ok(!error,"HeapAlloc should have zeroed out it's allocated memory\n");
96 97 98
    }

/* Check that HeapAlloc returns NULL when requested way too much memory */
99
    mem3=HeapAlloc(heap,0,5*memchunk);
100
    ok(mem3==NULL,"HeapAlloc should return NULL\n");
101
    if(mem3) {
102
      ok(HeapFree(heap,0,mem3),"HeapFree didn't pass successfully\n");
103 104 105
    }

/* Check that HeapRealloc works */
106
    mem2a=HeapReAlloc(heap,HEAP_ZERO_MEMORY,mem2,memchunk+5*sysInfo.dwPageSize);
107
    ok(mem2a!=NULL,"HeapReAlloc failed\n");
108
    if(mem2a) {
109
      ok(HeapSize(heap,0,mem2a)>=memchunk+5*sysInfo.dwPageSize,"HeapReAlloc failed\n");
110 111 112 113 114 115
      error=0;
      for(i=0;i<5*sysInfo.dwPageSize;i++) {
        if(mem2a[memchunk+i]!=0) {
          error=1;
        }
      }
116
      ok(!error,"HeapReAlloc should have zeroed out it's allocated memory\n");
117 118 119 120 121 122 123 124 125 126
    }

/* Check that HeapRealloc honours HEAP_REALLOC_IN_PLACE_ONLY */
    error=0;
    mem1a=HeapReAlloc(heap,HEAP_REALLOC_IN_PLACE_ONLY,mem1,memchunk+sysInfo.dwPageSize);
    if(mem1a!=NULL) {
      if(mem1a!=mem1) {
        error=1;
      }
    }
127
    ok(mem1a==NULL || error==0,"HeapReAlloc didn't honour HEAP_REALLOC_IN_PLACE_ONLY\n");
128 129 130

/* Check that HeapFree works correctly */
   if(mem1a) {
131
     ok(HeapFree(heap,0,mem1a),"HeapFree failed\n");
132
   } else {
133
     ok(HeapFree(heap,0,mem1),"HeapFree failed\n");
134 135
   }
   if(mem2a) {
136
     ok(HeapFree(heap,0,mem2a),"HeapFree failed\n");
137
   } else {
138
     ok(HeapFree(heap,0,mem2),"HeapFree failed\n");
139 140
   }

141 142
   /* 0-length buffer */
   mem1 = HeapAlloc(heap, 0, 0);
143
   ok(mem1 != NULL, "Reserved memory\n");
144 145 146

   dwSize = HeapSize(heap, 0, mem1);
   /* should work with 0-length buffer */
147
   ok(dwSize < 0xFFFFFFFF, "The size of the 0-length buffer\n");
148
   ok(HeapFree(heap, 0, mem1), "Freed the 0-length buffer\n");
149

150
/* Check that HeapDestry works */
151
   ok(HeapDestroy(heap),"HeapDestroy failed\n");
152
}
153 154 155 156 157 158 159 160 161 162

/* The following functions don't have tests, because either I don't know how
   to test them, or they are WinNT only, or require multiple threads.
   Since the last two issues shouldn't really stop the tests from being
   written, assume for now that it is all due to the first case
       GlobalFlags
       GlobalMemoryStatus
       GlobalMemoryStatusEx
*/
/* In addition, these features aren't being tested
163
       GMEM_DISCARDABLE
164 165 166 167 168 169 170 171 172 173 174 175
       GMEM_NOCOMPACT
*/
static void test_Global(void)
{
    ULONG memchunk;
    HGLOBAL mem1,mem2,mem2a,mem2b;
    UCHAR *mem2ptr;
    UINT error,i;
    memchunk=100000;

    SetLastError(NO_ERROR);
/* Check that a normal alloc works */
176
    mem1=GlobalAlloc(0,memchunk);
177
    ok(mem1!=NULL,"GlobalAlloc failed\n");
178
    if(mem1) {
179
      ok(GlobalSize(mem1)>=memchunk, "GlobalAlloc should return a big enough memory block\n");
180 181 182 183
    }

/* Check that a 'zeroing' alloc works */
    mem2=GlobalAlloc(GMEM_ZEROINIT,memchunk);
184
    ok(mem2!=NULL,"GlobalAlloc failed: error=%d\n",GetLastError());
185
    if(mem2) {
186
      ok(GlobalSize(mem2)>=memchunk,"GlobalAlloc should return a big enough memory block\n");
187
      mem2ptr=GlobalLock(mem2);
188
      ok(mem2ptr==mem2,"GlobalLock should have returned the same memory as was allocated\n");
189 190 191 192 193 194 195
      if(mem2ptr) {
        error=0;
        for(i=0;i<memchunk;i++) {
          if(mem2ptr[i]!=0) {
            error=1;
          }
        }
196
        ok(!error,"GlobalAlloc should have zeroed out it's allocated memory\n");
197 198 199 200 201
      }
   }
/* Check that GlobalReAlloc works */
/* Check that we can change GMEM_FIXED to GMEM_MOVEABLE */
    mem2a=GlobalReAlloc(mem2,0,GMEM_MODIFY | GMEM_MOVEABLE);
202
    if(mem2a!=NULL) {
203
      mem2=mem2a;
204 205 206
      mem2ptr=GlobalLock(mem2a);
      ok(mem2ptr!=NULL && !GlobalUnlock(mem2a)&&GetLastError()==NO_ERROR,
         "Converting from FIXED to MOVEABLE didn't REALLY work\n");
207 208 209 210
    }

/* Check that ReAllocing memory works as expected */
    mem2a=GlobalReAlloc(mem2,2*memchunk,GMEM_MOVEABLE | GMEM_ZEROINIT);
211
    ok(mem2a!=NULL,"GlobalReAlloc failed\n");
212
    if(mem2a) {
213
      ok(GlobalSize(mem2a)>=2*memchunk,"GlobalReAlloc failed\n");
214
      mem2ptr=GlobalLock(mem2a);
215
      ok(mem2ptr!=NULL,"GlobalLock Failed\n");
216 217 218 219 220 221 222
      if(mem2ptr) {
        error=0;
        for(i=0;i<memchunk;i++) {
          if(mem2ptr[memchunk+i]!=0) {
            error=1;
          }
        }
223
        ok(!error,"GlobalReAlloc should have zeroed out it's allocated memory\n");
224 225 226

/* Check that GlobalHandle works */
        mem2b=GlobalHandle(mem2ptr);
227
        ok(mem2b==mem2a,"GlobalHandle didn't return the correct memory handle\n");
228 229 230

/* Check that we can't discard locked memory */
        mem2b=GlobalDiscard(mem2a);
231 232 233
        if(mem2b==NULL) {
          ok(!GlobalUnlock(mem2a) && GetLastError()==NO_ERROR,"GlobalUnlock Failed\n");
        }
234 235 236
      }
    }
    if(mem1) {
237
      ok(GlobalFree(mem1)==NULL,"GlobalFree failed\n");
238 239
    }
    if(mem2a) {
240
      ok(GlobalFree(mem2a)==NULL,"GlobalFree failed\n");
241
    } else {
242
      ok(GlobalFree(mem2)==NULL,"GlobalFree failed\n");
243 244 245 246 247 248 249 250 251 252 253 254
    }
}


/* The following functions don't have tests, because either I don't know how
   to test them, or they are WinNT only, or require multiple threads.
   Since the last two issues shouldn't really stop the tests from being
   written, assume for now that it is all due to the first case
       LocalDiscard
       LocalFlags
*/
/* In addition, these features aren't being tested
255
       LMEM_DISCARDABLE
256 257 258 259 260 261 262 263 264 265 266
       LMEM_NOCOMPACT
*/
static void test_Local(void)
{
    ULONG memchunk;
    HLOCAL mem1,mem2,mem2a,mem2b;
    UCHAR *mem2ptr;
    UINT error,i;
    memchunk=100000;

/* Check that a normal alloc works */
267
    mem1=LocalAlloc(0,memchunk);
268
    ok(mem1!=NULL,"LocalAlloc failed: error=%d\n",GetLastError());
269
    if(mem1) {
270
      ok(LocalSize(mem1)>=memchunk, "LocalAlloc should return a big enough memory block\n");
271 272
    }

273 274
/* Check that a 'zeroing' and lock alloc works */
    mem2=LocalAlloc(LMEM_ZEROINIT|LMEM_MOVEABLE,memchunk);
275
    ok(mem2!=NULL,"LocalAlloc failed: error=%d\n",GetLastError());
276
    if(mem2) {
277
      ok(LocalSize(mem2)>=memchunk,"LocalAlloc should return a big enough memory block\n");
278
      mem2ptr=LocalLock(mem2);
279
      ok(mem2ptr!=NULL,"LocalLock: error=%d\n",GetLastError());
280 281 282 283 284 285 286
      if(mem2ptr) {
        error=0;
        for(i=0;i<memchunk;i++) {
          if(mem2ptr[i]!=0) {
            error=1;
          }
        }
287
        ok(!error,"LocalAlloc should have zeroed out it's allocated memory\n");
288 289 290
        SetLastError(0);
        error=LocalUnlock(mem2);
        ok(error==0 && GetLastError()==NO_ERROR,
291
           "LocalUnlock Failed: rc=%d err=%d\n",error,GetLastError());
292 293
      }
    }
294
   mem2a=LocalFree(mem2);
295
   ok(mem2a==NULL, "LocalFree failed: %p\n",mem2a);
296 297 298

/* Reallocate mem2 as moveable memory */
   mem2=LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT,memchunk);
299
   ok(mem2!=NULL, "LocalAlloc failed to create moveable memory, error=%d\n",GetLastError());
300 301 302

/* Check that ReAllocing memory works as expected */
    mem2a=LocalReAlloc(mem2,2*memchunk,LMEM_MOVEABLE | LMEM_ZEROINIT);
303
    ok(mem2a!=NULL,"LocalReAlloc failed, error=%d\n",GetLastError());
304
    if(mem2a) {
305
      ok(LocalSize(mem2a)>=2*memchunk,"LocalReAlloc failed\n");
306
      mem2ptr=LocalLock(mem2a);
307
      ok(mem2ptr!=NULL,"LocalLock Failed\n");
308 309 310 311 312 313 314
      if(mem2ptr) {
        error=0;
        for(i=0;i<memchunk;i++) {
          if(mem2ptr[memchunk+i]!=0) {
            error=1;
          }
        }
315
        ok(!error,"LocalReAlloc should have zeroed out it's allocated memory\n");
316 317
/* Check that LocalHandle works */
        mem2b=LocalHandle(mem2ptr);
318
        ok(mem2b==mem2a,"LocalHandle didn't return the correct memory handle\n");
319 320
/* Check that we can't discard locked memory */
        mem2b=LocalDiscard(mem2a);
321
        ok(mem2b==NULL,"Discarded memory we shouldn't have\n");
322
        SetLastError(NO_ERROR);
323
        ok(!LocalUnlock(mem2a) && GetLastError()==NO_ERROR, "LocalUnlock Failed\n");
324 325 326
      }
    }
    if(mem1) {
327
      ok(LocalFree(mem1)==NULL,"LocalFree failed\n");
328 329
    }
    if(mem2a) {
330
      ok(LocalFree(mem2a)==NULL,"LocalFree failed\n");
331
    } else {
332
      ok(LocalFree(mem2)==NULL,"LocalFree failed\n");
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
    }
}

/* The Virtual* routines are not tested as thoroughly,
   since I don't really understand how to use them correctly :)
   The following routines are not tested at all
      VirtualAllocEx
      VirtualFreeEx
      VirtualLock
      VirtualProtect
      VirtualProtectEx
      VirtualQuery
      VirtualQueryEx
      VirtualUnlock
    And the only features (flags) being tested are
      MEM_COMMIT
      MEM_RELEASE
      PAGE_READWRITE
    Testing the rest requires using exceptions, which I really don't
    understand well
*/
static void test_Virtual(void)
{
    SYSTEM_INFO sysInfo;
    ULONG memchunk;
    UCHAR *mem1;
    UINT error,i;

Andreas Mohr's avatar
Andreas Mohr committed
361
/* Retrieve the page size for this system */
362 363
    sysInfo.dwPageSize=0;
    GetSystemInfo(&sysInfo);
364
    ok(sysInfo.dwPageSize>0,"GetSystemInfo should return a valid page size\n");
365

Andreas Mohr's avatar
Andreas Mohr committed
366
/* Choose a reasonable allocation size */
367 368 369 370
    memchunk=10*sysInfo.dwPageSize;

/* Check that a normal alloc works */
    mem1=VirtualAlloc(NULL,memchunk,MEM_COMMIT,PAGE_READWRITE);
371
    ok(mem1!=NULL,"VirtualAlloc failed\n");
372 373 374 375 376 377 378 379
    if(mem1) {
/* check that memory is initialized to 0 */
      error=0;
      for(i=0;i<memchunk;i++) {
        if(mem1[i]!=0) {
          error=1;
        }
      }
380
      ok(!error,"VirtualAlloc did not initialize memory to '0's\n");
381 382 383 384 385 386 387 388
/* Check that we can read/write to memory */
      error=0;
      for(i=0;i<memchunk;i+=100) {
        mem1[i]='a';
        if(mem1[i]!='a') {
          error=1;
        }
      }
389
      ok(!error,"Virtual memory was not writable\n");
390
    }
391
    ok(VirtualFree(mem1,0,MEM_RELEASE),"VirtualFree failed\n");
392 393 394 395 396 397 398 399
}
START_TEST(alloc)
{
    test_Heap();
    test_Global();
    test_Local();
    test_Virtual();
}