@@ -143,88 +143,49 @@ int
143143ngx_http_lua_ffi_configure_shared_dict (ngx_str_t * name , ngx_str_t * size ,
144144 u_char * errstr , size_t * err_len )
145145{
146- ssize_t ssize ;
147- ngx_shm_zone_t * * zp ;
148- ngx_shm_zone_t * zone ;
149- ngx_http_lua_shdict_ctx_t * ctx ;
150- ngx_http_lua_main_conf_t * lmcf ;
151- lua_State * L ;
152- ngx_conf_t * cf = cfp ;
146+ ngx_int_t rc ;
147+ ssize_t ssize ;
148+ lua_State * L ;
149+ ngx_http_lua_main_conf_t * lmcf ;
150+ ngx_conf_t * cf = cfp ;
151+ ngx_shm_zone_t * * zone ;
153152
154153 lmcf = ngx_http_conf_get_module_main_conf (cf , ngx_http_lua_module );
155154
156155 ssize = ngx_parse_size (size );
157156 if (ssize <= NGX_HTTP_LUA_SHDICT_MINSIZE ) {
158157 * err_len = ngx_snprintf (errstr , * err_len ,
159- "invalid lua shared dict size \"%s\"" ,
160- size -> data )
161- - errstr ;
162- return NGX_ERROR ;
163- }
164-
165- ctx = ngx_pcalloc (cf -> cycle -> pool , sizeof (ngx_http_lua_shdict_ctx_t ));
166- if (ctx == NULL ) {
167- * err_len = ngx_snprintf (errstr , * err_len , "no memory" )
158+ "invalid lua shared dict size \"%s\"" ,
159+ size -> data )
168160 - errstr ;
169- return NGX_ERROR ;
170- }
171-
172- ctx -> name = * name ;
173- ctx -> main_conf = lmcf ;
174- ctx -> log = & cf -> cycle -> new_log ;
175-
176- zone = ngx_http_lua_shared_memory_add (cf , name , (size_t ) ssize ,
177- & ngx_http_lua_module );
178- if (zone == NULL ) {
179- * err_len = ngx_snprintf (errstr , * err_len , "no memory" )
180- - errstr ;
181- return NGX_ERROR ;
182- }
183-
184- if (zone -> data ) {
185161 return NGX_DECLINED ;
186162 }
187163
188- zone -> init = ngx_http_lua_shdict_init_zone ;
189- zone -> data = ctx ;
190-
191- if (lmcf -> shdict_zones == NULL ) {
192- lmcf -> shdict_zones = ngx_palloc (cf -> pool , sizeof (ngx_array_t ));
193- if (lmcf -> shdict_zones == NULL ) {
194- * err_len = ngx_snprintf (errstr , * err_len , "no memory" )
195- - errstr ;
196- return NGX_ERROR ;
197- }
198-
199- if (ngx_array_init (lmcf -> shdict_zones , cf -> pool , 2 ,
200- sizeof (ngx_shm_zone_t * ))
201- != NGX_OK )
202- {
203- * err_len = ngx_snprintf (errstr , * err_len , "no memory" )
164+ rc = ngx_http_lua_shared_dict_add (cf , name , ssize );
165+ if (rc != NGX_OK ) {
166+ if (rc == NGX_DECLINED ) {
167+ * err_len = ngx_snprintf (errstr , * err_len ,
168+ "lua_shared_dict \"%V\" is already defined"
169+ " as \"%V\"" , name , name )
204170 - errstr ;
205- return NGX_ERROR ;
206171 }
207- }
208172
209- zp = ngx_array_push (lmcf -> shdict_zones );
210- if (zp == NULL ) {
211- * err_len = ngx_snprintf (errstr , * err_len , "no memory" )
212- - errstr ;
213- return NGX_ERROR ;
173+ return rc ;
214174 }
215175
216- * zp = zone ;
176+ zone = lmcf -> shdict_zones -> elts ;
217177
218178 L = lmcf -> lua ;
219179
220180 lua_getglobal (L , "ngx" );
221181 lua_getfield (L , -1 , "shared" );
222- if (!lua_getmetatable (L , -1 )) {
223- ngx_http_lua_create_shdict_mt (L ); /* ngx.shared shmt */
224- }
182+ ngx_http_lua_create_shdict_mt (L );
183+
184+ /* ngx ngx.shared shmt */
185+
186+ ngx_http_lua_attach_shdict (L , name , zone [lmcf -> shdict_zones -> nelts - 1 ]);
225187
226- ngx_http_lua_attach_shdict (L , name , zone );
227- lua_pop (L , 2 ); /* pop: ngx.shared + shmt */
188+ lua_pop (L , 3 ); /* pop: ngx ngx.shared shmt */
228189
229190 return NGX_OK ;
230191}
0 commit comments