xref: /php-src/Zend/zend_map_ptr.h (revision 25d76162)
1 /*
2    +----------------------------------------------------------------------+
3    | Zend Engine                                                          |
4    +----------------------------------------------------------------------+
5    | Copyright (c) Zend Technologies Ltd. (http://www.zend.com)           |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 2.00 of the Zend license,     |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.zend.com/license/2_00.txt.                                |
11    | If you did not receive a copy of the Zend license and are unable to  |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@zend.com so we can mail you a copy immediately.              |
14    +----------------------------------------------------------------------+
15    | Authors: Dmitry Stogov <dmitry@php.net>                              |
16    +----------------------------------------------------------------------+
17 */
18 
19 #ifndef ZEND_MAP_PTR_H
20 #define ZEND_MAP_PTR_H
21 
22 #include "zend_portability.h"
23 
24 typedef struct _zend_string zend_string;
25 
26 #define ZEND_MAP_PTR_KIND_PTR           0
27 #define ZEND_MAP_PTR_KIND_PTR_OR_OFFSET 1
28 
29 #define ZEND_MAP_PTR_KIND ZEND_MAP_PTR_KIND_PTR_OR_OFFSET
30 
31 #define ZEND_MAP_PTR(ptr) \
32 	ptr ## __ptr
33 #define ZEND_MAP_PTR_DEF(type, name) \
34 	type ZEND_MAP_PTR(name)
35 #define ZEND_MAP_PTR_OFFSET2PTR(offset) \
36 	((void**)((char*)CG(map_ptr_base) + offset))
37 #define ZEND_MAP_PTR_PTR2OFFSET(ptr) \
38 	((void*)(((char*)(ptr)) - ((char*)CG(map_ptr_base))))
39 #define ZEND_MAP_PTR_INIT(ptr, val) do { \
40 		ZEND_MAP_PTR(ptr) = (val); \
41 	} while (0)
42 #define ZEND_MAP_PTR_NEW(ptr) do { \
43 		ZEND_MAP_PTR(ptr) = zend_map_ptr_new(); \
44 	} while (0)
45 #define ZEND_MAP_PTR_NEW_STATIC(ptr) do { \
46 		ZEND_MAP_PTR(ptr) = zend_map_ptr_new_static(); \
47 	} while (0)
48 
49 #if ZEND_MAP_PTR_KIND == ZEND_MAP_PTR_KIND_PTR_OR_OFFSET
50 # define ZEND_MAP_PTR_NEW_OFFSET() \
51 	((uint32_t)(uintptr_t)zend_map_ptr_new())
52 # define ZEND_MAP_PTR_IS_OFFSET(ptr) \
53 	(((uintptr_t)ZEND_MAP_PTR(ptr)) & 1L)
54 # define ZEND_MAP_PTR_GET(ptr) \
55 	((ZEND_MAP_PTR_IS_OFFSET(ptr) ? \
56 		ZEND_MAP_PTR_GET_IMM(ptr) : \
57 		((void*)(ZEND_MAP_PTR(ptr)))))
58 # define ZEND_MAP_PTR_GET_IMM(ptr) \
59 	(*ZEND_MAP_PTR_OFFSET2PTR((intptr_t)ZEND_MAP_PTR(ptr)))
60 # define ZEND_MAP_PTR_SET(ptr, val) do { \
61 		if (ZEND_MAP_PTR_IS_OFFSET(ptr)) { \
62 			ZEND_MAP_PTR_SET_IMM(ptr, val); \
63 		} else { \
64 			ZEND_MAP_PTR_INIT(ptr, val); \
65 		} \
66 	} while (0)
67 # define ZEND_MAP_PTR_SET_IMM(ptr, val) do { \
68 		void **__p = ZEND_MAP_PTR_OFFSET2PTR((intptr_t)ZEND_MAP_PTR(ptr)); \
69 		*__p = (val); \
70 	} while (0)
71 # define ZEND_MAP_PTR_BIASED_BASE(real_base) \
72 	((void*)(((uintptr_t)(real_base)) + zend_map_ptr_static_size * sizeof(void *) - 1))
73 #else
74 # error "Unknown ZEND_MAP_PTR_KIND"
75 #endif
76 
77 BEGIN_EXTERN_C()
78 
79 ZEND_API void  zend_map_ptr_reset(void);
80 ZEND_API void *zend_map_ptr_new(void);
81 ZEND_API void *zend_map_ptr_new_static(void);
82 ZEND_API void  zend_map_ptr_extend(size_t last);
83 ZEND_API void zend_alloc_ce_cache(zend_string *type_name);
84 
85 ZEND_API extern size_t zend_map_ptr_static_last;
86 ZEND_API extern size_t zend_map_ptr_static_size;
87 
88 END_EXTERN_C()
89 
90 #endif /* ZEND_MAP_PTR_H */
91