update
Signed-off-by: Wenchao Hao <haowenchao@xiaomi.com>
This commit is contained in:
parent
7caf6bea2a
commit
6a5f0990b0
44
demo.c
44
demo.c
|
|
@ -1,25 +1,51 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <sys/mman.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
|
||||||
#define MB_BYTES (1024*1024)
|
#define MB_BYTES (1024*1024)
|
||||||
|
|
||||||
int main(void)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
unsigned long *p;
|
unsigned long *p;
|
||||||
|
unsigned long *m;
|
||||||
int i = 0;
|
int i = 0;
|
||||||
int nr_mb;
|
|
||||||
int bytes;
|
|
||||||
int max;
|
int max;
|
||||||
|
|
||||||
printf("please specify how many memory to alloc\n");
|
const char *file_path = argv[1];
|
||||||
scanf("%d", &nr_mb);
|
int fd = open(file_path, O_RDONLY);
|
||||||
|
if (fd == -1) {
|
||||||
|
perror("open");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
bytes = nr_mb * MB_BYTES;
|
struct stat sb;
|
||||||
max = bytes / 8;
|
if (fstat(fd, &sb) == -1) {
|
||||||
p = malloc(bytes);
|
perror("fstat");
|
||||||
|
close(fd);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t file_size = sb.st_size;
|
||||||
|
|
||||||
|
printf("%ld, %d Kb\n", file_size, file_size/1024);
|
||||||
|
void *mapped = mmap(NULL, file_size, PROT_READ, MAP_PRIVATE, fd, 0);
|
||||||
|
if (mapped == MAP_FAILED) {
|
||||||
|
perror("mmap");
|
||||||
|
close(fd);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
m = mapped;
|
||||||
|
|
||||||
|
max = file_size / 8;
|
||||||
|
p = malloc(file_size);
|
||||||
|
|
||||||
for (i = 0; i < max; i++)
|
for (i = 0; i < max; i++)
|
||||||
*(p + i) = i;
|
*(p + i) = *(m + i);
|
||||||
|
|
||||||
while(1);
|
while(1);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue