efl/src/lib/eina/eina_safepointer.h

120 lines
3.4 KiB
C
Raw Normal View History

/* EINA - EFL data type library
* Copyright (C) 2015-2016 Carsten Haitzler, Cedric Bail
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library;
* if not, see <http://www.gnu.org/licenses/>.
*/
#ifndef EINA_SAFEPOINTER_H__
#define EINA_SAFEPOINTER_H__
/**
* @addtogroup Eina_Safepointer_Group Safe Pointer
*
* @brief These functions provide a wrapper that protect access to pointers
*
* Eina_Safepointer is an pointer to index converter that allows an increased
* level of safety by forbidding direct access to the pointer. The protection
* works by using a set of indirection tables that are mmapped and mprotected
* against write access. Thus the pointer they store and that map to a specific
* index is always correct. Also once a pointer is unregistered the index
* won't be served back for 2^8 on 32 bits system and 2^28 on 64 bits system
* for that specific slot. Finally we guarantee that the lower 2 bits of the
* returned index are actually never used and completely ignored by our API.
* So you can safely store whatever information you want in it, we will ignore
* it and treat as if it wasn't there.
*
* @note The use of Eina_Safepointer is thread safe.
*/
/**
* @addtogroup Eina_Data_Types_Group Data Types
*
* @{
*/
/**
* @addtogroup Eina_Containers_Group Containers
*
* @{
*/
/**
* @defgroup Eina_Safepointer_Group Safe Pointer
*
* @{
*/
/**
* @typedef Eina_Safepointer
* Type of the protected index.
*/
typedef struct _Eina_Safepointer Eina_Safepointer;
/**
* @brief Register a pointer and get an Eina_Safepointer that maps to it.
*
* @param[in] target The pointer to register.
* @return A valid pointer that is an index to the mapped pointer.
*
* @note It will return @c NULL on error or if @p target is @c NULL.
*
* @note The lower 2 bits of the returned pointer will always be 0.
*
* @note The returned pointer can be used like a pointer, but cannot
* be touched except with Eina_Safepointer functions.
*
* @since 1.18
*/
eina: Rename EAPI macro to EINA_API in Eina library Patch from a series of patches to rename EAPI symbols to specific library DSOs. EAPI was designed to be able to pass ```__attribute__ ((visibility ("default")))``` for symbols with GCC, which would mean that even if -fvisibility=hidden was used when compiling the library, the needed symbols would get exported. MSVC __almost__ works like GCC (or mingw) in which you can declare everything as export and it will just work (slower, but it will work). But there's a caveat: global variables will not work the same way for MSVC, but works for mingw and GCC. For global variables (as opposed to functions), MSVC requires correct DSO visibility for MSVC: instead of declaring a symbol as export for everything, you need to declare it as import when importing from another DSO and export when defining it locally. With current EAPI definitions, we get the following example working in mingw and MSVC (observe it doesn't define any global variables as exported symbols). Example 1: dll1: ``` EAPI void foo(void); EAPI void bar() { foo(); } ``` dll2: ``` EAPI void foo() { printf ("foo\n"); } ``` This works fine with API defined as __declspec(dllexport) in both cases and for gcc defining as ```__atttribute__((visibility("default")))```. However, the following: Example 2: dll1: ``` EAPI extern int foo; EAPI void foobar(void); EAPI void bar() { foo = 5; foobar(); } ``` dll2: ``` EAPI int foo = 0; EAPI void foobar() { printf ("foo %d\n", foo); } ``` This will work on mingw but will not work for MSVC. And that's why EAPI is the only solution that worked for MSVC. Co-authored-by: João Paulo Taylor Ienczak Zanette <jpaulotiz@gmail.com> Co-authored-by: Ricardo Campos <ricardo.campos@expertise.dev> Co-authored-by: Lucas Cavalcante de Sousa <lucks.sousa@gmail.com>
2020-08-15 11:26:00 -07:00
EINA_API const Eina_Safepointer *eina_safepointer_register(const void *target);
/**
* @brief Unregister an Eina_Safepointer and the pointer that maps to it.
*
* @param[in] safe The index to unregister from the mapping.
*
* @note This function will ignore the lower 2 bits of the given pointer.
*
* @since 1.18
*/
eina: Rename EAPI macro to EINA_API in Eina library Patch from a series of patches to rename EAPI symbols to specific library DSOs. EAPI was designed to be able to pass ```__attribute__ ((visibility ("default")))``` for symbols with GCC, which would mean that even if -fvisibility=hidden was used when compiling the library, the needed symbols would get exported. MSVC __almost__ works like GCC (or mingw) in which you can declare everything as export and it will just work (slower, but it will work). But there's a caveat: global variables will not work the same way for MSVC, but works for mingw and GCC. For global variables (as opposed to functions), MSVC requires correct DSO visibility for MSVC: instead of declaring a symbol as export for everything, you need to declare it as import when importing from another DSO and export when defining it locally. With current EAPI definitions, we get the following example working in mingw and MSVC (observe it doesn't define any global variables as exported symbols). Example 1: dll1: ``` EAPI void foo(void); EAPI void bar() { foo(); } ``` dll2: ``` EAPI void foo() { printf ("foo\n"); } ``` This works fine with API defined as __declspec(dllexport) in both cases and for gcc defining as ```__atttribute__((visibility("default")))```. However, the following: Example 2: dll1: ``` EAPI extern int foo; EAPI void foobar(void); EAPI void bar() { foo = 5; foobar(); } ``` dll2: ``` EAPI int foo = 0; EAPI void foobar() { printf ("foo %d\n", foo); } ``` This will work on mingw but will not work for MSVC. And that's why EAPI is the only solution that worked for MSVC. Co-authored-by: João Paulo Taylor Ienczak Zanette <jpaulotiz@gmail.com> Co-authored-by: Ricardo Campos <ricardo.campos@expertise.dev> Co-authored-by: Lucas Cavalcante de Sousa <lucks.sousa@gmail.com>
2020-08-15 11:26:00 -07:00
EINA_API void eina_safepointer_unregister(const Eina_Safepointer *safe);
/**
* @brief Get the associated pointer from an Eina_Safepointer mapping.
*
* @param[in] safe The Eina_Safepointer index to lookup at.
* @return The pointer registered with that index or @c NULL in any other case.
*
* @note It is always safe to ask for a pointer for any value of the mapping.
* If the pointer is invalid or @c NULL, we will return @c NULL and not crash.
*/
static inline void *eina_safepointer_get(const Eina_Safepointer *safe);
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
# include "eina_inline_safepointer.x"
#endif