-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.c
57 lines (48 loc) · 2.23 KB
/
example.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
55
56
57
// include other header files as needed
#include"mems.h"
int main(int argc, char const *argv[])
{
// initialise the MeMS system
mems_init();
int* ptr[10];
/*
This allocates 10 arrays of 250 integers each
*/
printf("\n------- Allocated virtual addresses [mems_malloc] -------\n");
for(int i=0;i<10;i++){
ptr[i] = (int*)mems_malloc(sizeof(int)*250);
printf("Virtual address: %lu\n", (unsigned long)ptr[i]);
}
mems_print_stats();
/*
In this section we are tring to write value to 1st index of array[0] (here it is 0 based indexing).
We get get value of both the 0th index and 1st index of array[0] by using function mems_get.
Then we write value to 1st index using 1st index pointer and try to access it via 0th index pointer.
This section is show that even if we have allocated an array using mems_malloc but we can
retrive MeMS physical address of any of the element from that array using mems_get.
*/
printf("\n------ Assigning value to Virtual address [mems_get] -----\n");
// how to write to the virtual address of the MeMS (this is given to show that the system works on arrays as well)
int* phy_ptr= (int*) mems_get(&ptr[0][1]); // get the address of index 1
phy_ptr[0]=200; // put value at index 1
int* phy_ptr2= (int*) mems_get(&ptr[0][0]); // get the address of index 0
printf("Virtual address: %lu\tPhysical Address: %lu\n",(unsigned long)ptr[0],(unsigned long)phy_ptr2);
printf("Value written: %d\n", phy_ptr2); // print the address of index 1
/*
This shows the stats of the MeMS system.
*/
printf("\n--------- Printing Stats [mems_print_stats] --------\n");
mems_print_stats();
/*
This section shows the effect of freeing up space on free list and also the effect of
reallocating the space that will be fullfilled by the free list.
*/
printf("\n--------- Freeing up the memory [mems_free] --------\n");
mems_free(ptr[3]);
mems_print_stats();
ptr[3] = (int*)mems_malloc(sizeof(int)*250);
mems_print_stats();
printf("\n--------- Unmapping all memory [mems_finish] --------\n\n");
mems_finish();
return 0;
}