Ticket #15995: MemTest.cpp

File MemTest.cpp, 1.1 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 = 4096*32
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 (;;) {
50 it = NewItem(allocSize);
51 if (it == NULL) break;
52 totalSize += it->size;
53 it->next = list; list = it;
54 }
55 printf("totalSize: %ld\n", totalSize); getc(stdin);
56 while (list != NULL) {
57 it = list; list = list->next;
58 totalSize -= it->size;
59 DeleteItem(it);
60 }
61 printf("totalSize: %ld\n", totalSize); getc(stdin);
62 return 0;
63}