xref: /php-src/Zend/zend_map_ptr.h (revision 6335264c)
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 
46 #if ZEND_MAP_PTR_KIND == ZEND_MAP_PTR_KIND_PTR_OR_OFFSET
47 # define ZEND_MAP_PTR_NEW_OFFSET() \
48 	((uint32_t)(uintptr_t)zend_map_ptr_new())
49 # define ZEND_MAP_PTR_IS_OFFSET(ptr) \
50 	(((uintptr_t)ZEND_MAP_PTR(ptr)) & 1L)
51 # define ZEND_MAP_PTR_GET(ptr) \
52 	((ZEND_MAP_PTR_IS_OFFSET(ptr) ? \
53 		ZEND_MAP_PTR_GET_IMM(ptr) : \
54 		((void*)(ZEND_MAP_PTR(ptr)))))
55 # define ZEND_MAP_PTR_GET_IMM(ptr) \
56 	(*ZEND_MAP_PTR_OFFSET2PTR((uintptr_t)ZEND_MAP_PTR(ptr)))
57 # define ZEND_MAP_PTR_SET(ptr, val) do { \
58 		if (ZEND_MAP_PTR_IS_OFFSET(ptr)) { \
59 			ZEND_MAP_PTR_SET_IMM(ptr, val); \
60 		} else { \
61 			ZEND_MAP_PTR_INIT(ptr, val); \
62 		} \
63 	} while (0)
64 # define ZEND_MAP_PTR_SET_IMM(ptr, val) do { \
65 		void **__p = ZEND_MAP_PTR_OFFSET2PTR((uintptr_t)ZEND_MAP_PTR(ptr)); \
66 		*__p = (val); \
67 	} while (0)
68 # define ZEND_MAP_PTR_BIASED_BASE(real_base) \
69 	((void*)(((uintptr_t)(real_base)) - 1))
70 #else
71 # error "Unknown ZEND_MAP_PTR_KIND"
72 #endif
73 
74 BEGIN_EXTERN_C()
75 
76 ZEND_API void  zend_map_ptr_reset(void);
77 ZEND_API void *zend_map_ptr_new(void);
78 ZEND_API void  zend_map_ptr_extend(size_t last);
79 ZEND_API void zend_alloc_ce_cache(zend_string *type_name);
80 
81 END_EXTERN_C()
82 
83 #endif /* ZEND_MAP_PTR_H */
84