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 <osl/mutex.hxx>
23 #include <rtl/alloc.h>
24 #include <rtl/ustring.hxx>
25 #include <sal/types.h>
26 #include <typelib/typedescription.hxx>
27 
28 #include <memory>
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 
38 namespace bridges::cpp_uno::shared {
39 
43 public:
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
63 
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 
103  VtableFactory();
104 
105  ~VtableFactory();
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 
118 private:
120  friend class GuardedBlocks;
121 
122  class BaseOffset;
123 
124  VtableFactory(VtableFactory const &) = delete;
125  VtableFactory& operator =(const VtableFactory&) = 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  osl::Mutex m_mutex;
214  Map m_map;
215 
216  rtl_arena_type * m_arena;
217 };
218 
219 }
220 
221 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_Int32 createVtables(GuardedBlocks &blocks, BaseOffset const &baseOffset, typelib_InterfaceTypeDescription *type, sal_Int32 vtableNumber, typelib_InterfaceTypeDescription *mostDerived, bool includePrimary) const
std::unique_ptr< Block[]> blocks
An array of blocks, representing the multiple vtables of a (multiple-inheritance) type...
Hand out vtable structures for interface type descriptions.
static std::size_t getBlockSize(sal_Int32 slotCount)
Calculate the size of a raw vtable block.
#define USE_DOUBLE_MMAP
static Slot * initializeBlock(void *block, sal_Int32 slotCount, sal_Int32 vtableNumber, typelib_InterfaceTypeDescription *type)
Initialize a raw vtable block.
static Slot * mapBlockToVtable(void *block)
Given a pointer to a block, turn it into a vtable pointer.
int fd
File handle for the underlying anonymous file.
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...
The vtable structure corresponding to an interface type.
std::unordered_map< OUString, Vtables > Map
void * start
The start of the raw vtable block.
bool createBlock(Block &block, sal_Int32 slotCount) const
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...
VtableFactory & operator=(const VtableFactory &)=delete
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.
sal_Int32 count
The number of blocks/vtables.
void freeBlock(Block const &block) const
const Vtables & getVtables(typelib_InterfaceTypeDescription *type)
Given an interface type description, return its corresponding vtable structure.