libperf: Adopt perf_mmap__mmap() function from tools/perf
[linux-2.6-block.git] / tools / perf / lib / mmap.c
CommitLineData
353120b4 1// SPDX-License-Identifier: GPL-2.0
32c261c0 2#include <sys/mman.h>
353120b4 3#include <internal/mmap.h>
bf59b305 4#include <internal/lib.h>
353120b4
JO
5
6void perf_mmap__init(struct perf_mmap *map, bool overwrite)
7{
8 map->fd = -1;
9 map->overwrite = overwrite;
10 refcount_set(&map->refcnt, 0);
11}
bf59b305
JO
12
13size_t perf_mmap__mmap_len(struct perf_mmap *map)
14{
15 return map->mask + 1 + page_size;
16}
32c261c0
JO
17
18int perf_mmap__mmap(struct perf_mmap *map, struct perf_mmap_param *mp,
19 int fd, int cpu)
20{
21 map->prev = 0;
22 map->mask = mp->mask;
23 map->base = mmap(NULL, perf_mmap__mmap_len(map), mp->prot,
24 MAP_SHARED, fd, 0);
25 if (map->base == MAP_FAILED) {
26 map->base = NULL;
27 return -1;
28 }
29
30 map->fd = fd;
31 map->cpu = cpu;
32 return 0;
33}