xref: /php-src/Zend/zend_map_ptr.h (revision 2f4973fd)
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 #define ZEND_MAP_PTR_KIND_PTR           0
25 #define ZEND_MAP_PTR_KIND_PTR_OR_OFFSET 1
26 
27 #define ZEND_MAP_PTR_KIND ZEND_MAP_PTR_KIND_PTR_OR_OFFSET
28 
29 #define ZEND_MAP_PTR(ptr) \
30 	ptr ## __ptr
31 #define ZEND_MAP_PTR_DEF(type, name) \
32 	type ZEND_MAP_PTR(name)
33 #define ZEND_MAP_PTR_OFFSET2PTR(offset) \
34 	((void**)((char*)CG(map_ptr_base) + offset))
35 #define ZEND_MAP_PTR_PTR2OFFSET(ptr) \
36 	((void*)(((char*)(ptr)) - ((char*)CG(map_ptr_base))))
37 #define ZEND_MAP_PTR_INIT(ptr, val) do { \
38 		ZEND_MAP_PTR(ptr) = (val); \
39 	} while (0)
40 #define ZEND_MAP_PTR_NEW(ptr) do { \
41 		ZEND_MAP_PTR(ptr) = zend_map_ptr_new(); \
42 	} while (0)
43 
44 #if ZEND_MAP_PTR_KIND == ZEND_MAP_PTR_KIND_PTR_OR_OFFSET
45 # define ZEND_MAP_PTR_NEW_OFFSET() \
46 	((uint32_t)(uintptr_t)zend_map_ptr_new())
47 # define ZEND_MAP_PTR_IS_OFFSET(ptr) \
48 	(((uintptr_t)ZEND_MAP_PTR(ptr)) & 1L)
49 # define ZEND_MAP_PTR_GET(ptr) \
50 	((ZEND_MAP_PTR_IS_OFFSET(ptr) ? \
51 		ZEND_MAP_PTR_GET_IMM(ptr) : \
52 		((void*)(ZEND_MAP_PTR(ptr)))))
53 # define ZEND_MAP_PTR_GET_IMM(ptr) \
54 	(*ZEND_MAP_PTR_OFFSET2PTR((uintptr_t)ZEND_MAP_PTR(ptr)))
55 # define ZEND_MAP_PTR_SET(ptr, val) do { \
56 		if (ZEND_MAP_PTR_IS_OFFSET(ptr)) { \
57 			ZEND_MAP_PTR_SET_IMM(ptr, val); \
58 		} else { \
59 			ZEND_MAP_PTR_INIT(ptr, val); \
60 		} \
61 	} while (0)
62 # define ZEND_MAP_PTR_SET_IMM(ptr, val) do { \
63 		void **__p = ZEND_MAP_PTR_OFFSET2PTR((uintptr_t)ZEND_MAP_PTR(ptr)); \
64 		*__p = (val); \
65 	} while (0)
66 # define ZEND_MAP_PTR_BIASED_BASE(real_base) \
67 	((void*)(((uintptr_t)(real_base)) - 1))
68 #else
69 # error "Unknown ZEND_MAP_PTR_KIND"
70 #endif
71 
72 BEGIN_EXTERN_C()
73 
74 ZEND_API void  zend_map_ptr_reset(void);
75 ZEND_API void *zend_map_ptr_new(void);
76 ZEND_API void  zend_map_ptr_extend(size_t last);
77 ZEND_API void zend_alloc_ce_cache(zend_string *type_name);
78 
79 END_EXTERN_C()
80 
81 #endif /* ZEND_MAP_PTR_H */
82