-
Notifications
You must be signed in to change notification settings - Fork 0
/
mem_2.c
59 lines (49 loc) · 1.57 KB
/
mem_2.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
58
59
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include "memlayout.h"
static void *handle;
struct memregion* after;
int array_size;
/**
* Dynamically loaded library. This causes memory layout to be altered at run time
*/
void dynamicLoadingExample();
/**
* In this example, we will consider dynamic load. The memory map before will be
* different from after the dynamic loading
* @author: Danh Nguyen
* @return
*/
int main() {
array_size = 20;
struct memregion* before = (struct memregion*)malloc(array_size * sizeof(struct memregion));
after = (struct memregion*)malloc(array_size * sizeof(struct memregion));
printf("========================\n");
printf("Memory before dynamic load is:\n");
get_mem_layout(before, array_size);
for (unsigned int i = 0; i < array_size; i++) {
print_memregion(before[i]);
}
dynamicLoadingExample();
for (unsigned int i = 0; i < array_size; i++) {
print_memregion(after[i]);
}
memregion_compare(after, before, array_size);
}
/**
* Code snippet in courtesy of Lab3 - Memory Mapped Files, CMPUT 379, University of Alberta,
* Winter 2021.
* @authors: Matt Gallivan and Yan Wang
*/
void dynamicLoadingExample() {
double (*cosine)(double);
handle = dlopen("/usr/lib32/libm.so", RTLD_LAZY);
*(void **)(&cosine) = dlsym(handle, "cos");
printf("\nTesting DL with cosine function...");
printf("Cosine of 0 : %f\n", (*cosine)(0.0));
printf("Test run successfully\n");
printf("Memory after DL\n");
get_mem_layout(after, array_size);
dlclose(handle);
}