Ticket #16199: MemTest.cpp

File MemTest.cpp, 1.3 KB (added by X512, 4 years ago)

Test program.

Line 
1#include <stdio.h>
2#include <stdlib.h>
3#include <malloc.h>
4#include <string.h>
5#include <OS.h>
6
7//#define ALLOC_MALLOC 1
8#define ALLOC_AREA 1
9
10enum {
11 allocSize = 128*1024
12};
13
14struct List {
15 List *next;
16 size_t size;
17};
18
19List *NewItem(size_t size)
20{
21 List *it;
22#if ALLOC_MALLOC
23 it = (List*)malloc(size);
24#elif ALLOC_AREA
25 area_id area = create_area("item area", (void**)&it, B_ANY_ADDRESS, size, B_NO_LOCK, B_READ_AREA | B_WRITE_AREA);
26 if (area < B_OK) it = NULL;
27#endif
28 if (it == NULL) return NULL;
29 memset(it, 0xcc, size); // ensure that physical memory is allocated
30 it->next = NULL;
31 it->size = size;
32 return it;
33}
34
35void DeleteItem(List *it)
36{
37#if ALLOC_MALLOC
38 free(it);
39#elif ALLOC_AREA
40 area_id area = area_for(it);
41 delete_area(area);
42#endif
43}
44
45int main()
46{
47 List *list = NULL, *it = NULL;
48 size_t totalSize = 0;
49 for (int i = 0; i < 48; i++) {
50 for (;totalSize < 2*1024*1024*1024LL;) {
51 it = NewItem(allocSize);
52 if (it == NULL) break;
53 totalSize += it->size;
54 it->next = list; list = it;
55 }
56 printf("alloc totalSize: %ld", totalSize); /*getc(stdin);*/ printf("\n");
57 while (list != NULL) {
58 it = list; list = list->next;
59 totalSize -= it->size;
60 DeleteItem(it);
61 }
62 printf("free totalSize: %ld", totalSize); /*getc(stdin);*/ printf("\n");
63 }
64 printf("done"); getc(stdin);
65 return 0;
66}