]> git.proxmox.com Git - ceph.git/blame - ceph/src/arrow/cpp/src/plasma/malloc.cc
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / cpp / src / plasma / malloc.cc
CommitLineData
1d09f67e
TL
1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements. See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership. The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License. You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied. See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18#include "plasma/malloc.h"
19
20#include <assert.h>
21#include <stddef.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <sys/mman.h>
26#include <unistd.h>
27
28#include <cerrno>
29#include <string>
30#include <vector>
31
32#include "plasma/common.h"
33#include "plasma/plasma.h"
34
35namespace plasma {
36
37std::unordered_map<void*, MmapRecord> mmap_records;
38
39static void* pointer_advance(void* p, ptrdiff_t n) { return (unsigned char*)p + n; }
40
41static ptrdiff_t pointer_distance(void const* pfrom, void const* pto) {
42 return (unsigned char const*)pto - (unsigned char const*)pfrom;
43}
44
45void GetMallocMapinfo(void* addr, int* fd, int64_t* map_size, ptrdiff_t* offset) {
46 // TODO(rshin): Implement a more efficient search through mmap_records.
47 for (const auto& entry : mmap_records) {
48 if (addr >= entry.first && addr < pointer_advance(entry.first, entry.second.size)) {
49 *fd = entry.second.fd;
50 *map_size = entry.second.size;
51 *offset = pointer_distance(entry.first, addr);
52 return;
53 }
54 }
55 *fd = -1;
56 *map_size = 0;
57 *offset = 0;
58}
59
60int64_t GetMmapSize(int fd) {
61 for (const auto& entry : mmap_records) {
62 if (entry.second.fd == fd) {
63 return entry.second.size;
64 }
65 }
66 ARROW_LOG(FATAL) << "failed to find entry in mmap_records for fd " << fd;
67 return -1; // This code is never reached.
68}
69
70} // namespace plasma