summaryrefslogtreecommitdiff
path: root/src/lib/eina/eina_safepointer.h
blob: 8120e48bfaca91cc63476fdd43666dad674d8275 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* 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
 */
EAPI 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
 */
EAPI 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