1 /*
2    +----------------------------------------------------------------------+
3    | Yet Another Cache                                                    |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 2013-2013 The PHP Group                                |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 3.01 of the PHP 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.php.net/license/3_01.txt                                  |
11    | If you did not receive a copy of the PHP license and are unable to   |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@php.net so we can mail you a copy immediately.               |
14    +----------------------------------------------------------------------+
15    | Authors: Xinchen Hui <laruence@php.net>                              |
16    +----------------------------------------------------------------------+
17 */
18 
19 #ifndef YAC_ALLOCATOR_H
20 #define YAC_ALLOCATOR_H
21 
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 
26 #define YAC_SMM_ALIGNMENT           8
27 #define YAC_SMM_ALIGNMENT_LOG2      3
28 #define YAC_SMM_ALIGNMENT_MASK      ~(YAC_SMM_ALIGNMENT - 1)
29 #define YAC_SMM_BLOCK_HEADER_SIZE   YAC_SMM_ALIGNED_SIZE(sizeof(yac_shared_block_header))
30 
31 #define YAC_SMM_MAIN_SEG_SIZE       (4*1024*1024)
32 #define YAC_SMM_SEGMENT_MAX_SIZE    (32*1024*1024)
33 #define YAC_SMM_SEGMENT_MIN_SIZE    (4*1024*1024)
34 #define YAC_SMM_MIN_BLOCK_SIZE      128
35 #define YAC_SMM_ALIGNED_SIZE(x)     (((x) + YAC_SMM_ALIGNMENT - 1) & YAC_SMM_ALIGNMENT_MASK)
36 #define YAC_SMM_TRUE_SIZE(x)        ((x < YAC_SMM_MIN_BLOCK_SIZE)? (YAC_SMM_MIN_BLOCK_SIZE) : (YAC_SMM_ALIGNED_SIZE(x)))
37 
38 #ifdef PHP_WIN32
39 #  define USE_FILE_MAPPING	1
40 #  define inline __inline
41 #elif defined(HAVE_SHM_MMAP_ANON)
42 #  define USE_MMAP      1
43 #elif defined(HAVE_SHM_IPC)
44 #  define USE_SHM       1
45 #else
46 #error(no builtin shared memory supported)
47 #endif
48 
49 #define ALLOC_FAILURE           0
50 #define ALLOC_SUCCESS           1
51 #define FAILED_REATTACHED       2
52 #define SUCCESSFULLY_REATTACHED 4
53 #define ALLOC_FAIL_MAPPING      8
54 
55 typedef int (*create_segments_t)(unsigned long k_size, unsigned long v_size, yac_shared_segment **shared_segments, int *shared_segment_count, char **error_in);
56 typedef int (*detach_segment_t)(yac_shared_segment *shared_segment);
57 
58 typedef struct {
59 	create_segments_t create_segments;
60 	detach_segment_t detach_segment;
61 	unsigned long (*segment_type_size)(void);
62 } yac_shared_memory_handlers;
63 
64 typedef struct {
65 	const char *name;
66 	yac_shared_memory_handlers *handler;
67 } yac_shared_memory_handler_entry;
68 
69 int yac_allocator_startup(unsigned long first_seg_size, unsigned long size, char **err);
70 void yac_allocator_shutdown(void);
71 unsigned long yac_allocator_real_size(unsigned long size);
72 void *yac_allocator_raw_alloc(unsigned long real_size, int seg);
73 int  yac_allocator_free(void *p);
74 
75 static inline void * yac_allocator_alloc(unsigned long size, int seg) {
76     unsigned long real_size = yac_allocator_real_size(size);
77     if (!real_size) {
78         return (void *)0;
79     }
80     return yac_allocator_raw_alloc(real_size, seg);
81 }
82 
83 #if defined(USE_MMAP)
84 extern yac_shared_memory_handlers yac_alloc_mmap_handlers;
85 #define yac_shared_memory_handler yac_alloc_mmap_handlers
86 #define YAC_SHARED_MEMORY_HANDLER_NAME "mmap"
87 #elif defined(USE_SHM)
88 extern yac_shared_memory_handlers yac_alloc_shm_handlers;
89 #define yac_shared_memory_handler yac_alloc_shm_handlers
90 #define YAC_SHARED_MEMORY_HANDLER_NAME "shm"
91 #elif defined(USE_FILE_MAPPING)
92 extern yac_shared_memory_handlers yac_alloc_create_file_handlers;
93 #define yac_shared_memory_handler yac_alloc_create_file_handlers
94 #define YAC_SHARED_MEMORY_HANDLER_NAME "file_mapping"
95 #endif
96 
97 #endif /* YAC_ALLOCATOR_H */
98