LibreOffice Module bridges (master) 1
vtablefactory.hxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20#pragma once
21
22#include <rtl/alloc.h>
23#include <rtl/ustring.hxx>
24#include <sal/types.h>
25#include <typelib/typedescription.hxx>
26
27#include <memory>
28#include <mutex>
29#include <unordered_map>
30
31/*See: http://people.redhat.com/drepper/selinux-mem.html*/
32#if defined(LINUX) || defined(OPENBSD) || defined(FREEBSD) \
33 || defined(NETBSD) || defined(DRAGONFLY) || defined (ANDROID) \
34 || defined(HAIKU)
35#define USE_DOUBLE_MMAP
36#endif
37
39
43public:
44 // This structure is not defined in the generic part, but instead has to be
45 // defined individually for each CPP--UNO bridge:
48 struct Slot;
49
52 struct Block {
60 void * start;
61
62#ifdef USE_DOUBLE_MMAP
67 void * exec;
68
71 int fd;
72#endif
73
76 sal_Size size;
77 };
78
81 struct Vtables {
84 sal_Int32 count;
85
95 std::unique_ptr<Block[]> blocks;
96
98 : count(0)
99 {
100 }
101 };
102
104
106
110 const Vtables& getVtables(typelib_InterfaceTypeDescription * type);
111
112 // This function is not defined in the generic part, but instead has to be
113 // defined individually for each CPP--UNO bridge:
116 static Slot * mapBlockToVtable(void * block);
117
118private:
119 class GuardedBlocks;
120 friend class GuardedBlocks;
121
122 class BaseOffset;
123
124 VtableFactory(VtableFactory const &) = delete;
126
127 bool createBlock(Block &block, sal_Int32 slotCount) const;
128
129 void freeBlock(Block const & block) const;
130
131 sal_Int32 createVtables(
132 GuardedBlocks & blocks, BaseOffset const & baseOffset,
133 typelib_InterfaceTypeDescription * type, sal_Int32 vtableNumber,
134 typelib_InterfaceTypeDescription * mostDerived, bool includePrimary)
135 const;
136
137 // This function is not defined in the generic part, but instead has to be
138 // defined individually for each CPP--UNO bridge:
147 static std::size_t getBlockSize(sal_Int32 slotCount);
148
149 // This function is not defined in the generic part, but instead has to be
150 // defined individually for each CPP--UNO bridge:
161 static Slot * initializeBlock(
162 void * block, sal_Int32 slotCount, sal_Int32 vtableNumber,
163 typelib_InterfaceTypeDescription * type);
164
165 // This function is not defined in the generic part, but instead has to be
166 // defined individually for each CPP--UNO bridge:
192 static unsigned char * addLocalFunctions(
193 Slot ** slots, unsigned char * code,
194#ifdef USE_DOUBLE_MMAP
195 sal_PtrDiff writetoexecdiff,
196#endif
197 typelib_InterfaceTypeDescription const * type, sal_Int32 functionOffset,
198 sal_Int32 functionCount, sal_Int32 vtableOffset);
199
200 // This function is not defined in the generic part, but instead has to be
201 // defined individually for each CPP--UNO bridge:
208 static void flushCode(
209 unsigned char const * begin, unsigned char const * end);
210
211 typedef std::unordered_map< OUString, Vtables > Map;
212
213 std::mutex m_mutex;
215
216 rtl_arena_type * m_arena;
217};
218
219}
220
221/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Hand out vtable structures for interface type descriptions.
std::unordered_map< OUString, Vtables > Map
bool createBlock(Block &block, sal_Int32 slotCount) const
const Vtables & getVtables(typelib_InterfaceTypeDescription *type)
Given an interface type description, return its corresponding vtable structure.
static Slot * mapBlockToVtable(void *block)
Given a pointer to a block, turn it into a vtable pointer.
VtableFactory & operator=(const VtableFactory &)=delete
sal_Int32 createVtables(GuardedBlocks &blocks, BaseOffset const &baseOffset, typelib_InterfaceTypeDescription *type, sal_Int32 vtableNumber, typelib_InterfaceTypeDescription *mostDerived, bool includePrimary) const
static void flushCode(unsigned char const *begin, unsigned char const *end)
Flush all the generated code snippets of a vtable, on platforms that require it.
static unsigned char * addLocalFunctions(Slot **slots, unsigned char *code, sal_PtrDiff writetoexecdiff, typelib_InterfaceTypeDescription const *type, sal_Int32 functionOffset, sal_Int32 functionCount, sal_Int32 vtableOffset)
Fill the vtable slots corresponding to all local (i.e., not inherited) functions of a given interface...
VtableFactory(VtableFactory const &)=delete
void freeBlock(Block const &block) const
static Slot * initializeBlock(void *block, sal_Int32 slotCount, sal_Int32 vtableNumber, typelib_InterfaceTypeDescription *type)
Initialize a raw vtable block.
static std::size_t getBlockSize(sal_Int32 slotCount)
Calculate the size of a raw vtable block.
sal_Size size
The size of the raw vtable block, in bytes.
void * exec
When separately mmapping the block for writing and executing exec points to the same memory as start,...
int fd
File handle for the underlying anonymous file.
void * start
The start of the raw vtable block.
The vtable structure corresponding to an interface type.
std::unique_ptr< Block[]> blocks
An array of blocks, representing the multiple vtables of a (multiple-inheritance) type.
sal_Int32 count
The number of blocks/vtables.
#define USE_DOUBLE_MMAP