xref: /aoo42x/main/sal/rtl/source/alloc_cache.h (revision 514f4c20)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef INCLUDED_RTL_ALLOC_CACHE_H
25 #define INCLUDED_RTL_ALLOC_CACHE_H
26 
27 #include "sal/types.h"
28 #include "rtl/alloc.h"
29 #include "alloc_impl.h"
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
35 /** rtl_cache_stat_type
36  *  @internal
37  */
38 typedef struct rtl_cache_stat_st rtl_cache_stat_type;
39 struct rtl_cache_stat_st
40 {
41 	sal_uInt64 m_alloc;
42 	sal_uInt64 m_free;
43 
44 	sal_Size   m_mem_total;
45 	sal_Size   m_mem_alloc;
46 };
47 
48 
49 /** rtl_cache_bufctl_type
50  *  @internal
51  */
52 typedef struct rtl_cache_bufctl_st rtl_cache_bufctl_type;
53 struct rtl_cache_bufctl_st
54 {
55 	rtl_cache_bufctl_type * m_next; /* linkage */
56 
57 	sal_uIntPtr             m_addr; /* buffer address  */
58 	sal_uIntPtr             m_slab; /* parent slab address */
59 };
60 
61 
62 /** rtl_cache_slab_type
63  *  @internal
64  */
65 typedef struct rtl_cache_slab_st rtl_cache_slab_type;
66 struct rtl_cache_slab_st
67 {
68 	rtl_cache_slab_type *   m_slab_next; /* slab linkage */
69 	rtl_cache_slab_type *   m_slab_prev; /* slab linkage */
70 
71 	sal_Size                m_ntypes;    /* number of buffers used */
72 	sal_uIntPtr             m_data;      /* buffer start addr */
73 
74 	sal_uIntPtr             m_bp;        /* free buffer linkage 'base pointer'  */
75 	rtl_cache_bufctl_type * m_sp;        /* free buffer linkage 'stack pointer' */
76 };
77 
78 
79 /** rtl_cache_magazine_type
80  *  @internal
81  */
82 #define RTL_CACHE_MAGAZINE_SIZE 61
83 
84 typedef struct rtl_cache_magazine_st rtl_cache_magazine_type;
85 struct rtl_cache_magazine_st
86 {
87 	rtl_cache_magazine_type * m_mag_next; /* depot linkage */
88 
89 	sal_Size                  m_mag_size;
90 	sal_Size                  m_mag_used;
91 
92 	void *                    m_objects[RTL_CACHE_MAGAZINE_SIZE];
93 };
94 
95 
96 /** rtl_cache_depot_type
97  *  @internal
98  */
99 typedef struct rtl_cache_depot_st rtl_cache_depot_type;
100 struct rtl_cache_depot_st
101 {
102 	/* magazine list */
103 	rtl_cache_magazine_type * m_mag_next;  /* linkage */
104 	sal_Size                  m_mag_count; /* count */
105 
106 	/* working set parameters */
107 	sal_Size                  m_curr_min;
108 	sal_Size                  m_prev_min;
109 };
110 
111 
112 /** rtl_cache_type
113  *  @internal
114  */
115 #define RTL_CACHE_HASH_SIZE        8
116 
117 #define RTL_CACHE_FEATURE_HASH        1
118 #define RTL_CACHE_FEATURE_BULKDESTROY 2
119 #define RTL_CACHE_FEATURE_RESCALE     4 /* within hash rescale operation */
120 
121 struct rtl_cache_st
122 {
123 	/* linkage */
124 	rtl_cache_type *          m_cache_next;
125 	rtl_cache_type *          m_cache_prev;
126 
127 	/* properties */
128 	char                      m_name[RTL_CACHE_NAME_LENGTH + 1];
129 	long                      m_features;
130 
131 	sal_Size                  m_type_size;   /* const */
132 	sal_Size                  m_type_align;  /* const */
133 	sal_Size                  m_type_shift;  /* log2(m_type_size); const */
134 
135 	int  (SAL_CALL * m_constructor)(void * obj, void * userarg); /* const */
136 	void (SAL_CALL * m_destructor) (void * obj, void * userarg); /* const */
137 	void (SAL_CALL * m_reclaim)    (void * userarg);             /* const */
138 	void *                    m_userarg;
139 
140 	/* slab layer */
141 	rtl_memory_lock_type      m_slab_lock;
142 	rtl_cache_stat_type       m_slab_stats;
143 
144 	rtl_arena_type *          m_source;     /* slab supplier; const */
145 	sal_Size                  m_slab_size;  /* const */
146 	sal_Size                  m_ntypes;     /* number of buffers per slab; const */
147 	sal_Size                  m_ncolor;     /* next slab color */
148 	sal_Size                  m_ncolor_max; /* max. slab color */
149 
150 	rtl_cache_slab_type       m_free_head;
151 	rtl_cache_slab_type       m_used_head;
152 
153 	rtl_cache_bufctl_type **  m_hash_table;
154 	rtl_cache_bufctl_type *   m_hash_table_0[RTL_CACHE_HASH_SIZE];
155 	sal_Size                  m_hash_size;  /* m_hash_mask + 1   */
156 	sal_Size                  m_hash_shift; /* log2(m_hash_size) */
157 
158 	/* depot layer */
159 	rtl_memory_lock_type      m_depot_lock;
160 
161 	rtl_cache_depot_type      m_depot_empty;
162 	rtl_cache_depot_type      m_depot_full;
163 
164 	rtl_cache_type *          m_magazine_cache; /* magazine supplier; const */
165 
166 	/* cpu layer */
167 	rtl_cache_magazine_type * m_cpu_curr;
168 	rtl_cache_magazine_type * m_cpu_prev;
169 
170 	rtl_cache_stat_type       m_cpu_stats;
171 };
172 
173 
174 #ifdef __cplusplus
175 }
176 #endif
177 
178 #endif /* INCLUDED_RTL_ALLOC_CACHE_H */
179