Skip to content

Commit 62a557f

Browse files
LeonidVasTotktonada
authored andcommitted
ffi: use a new C library namespace when the dynamic library load
If symbols from the dynamic library are loaded into the global namespace (ffi.C), by invoke ffi.load() with global is true and without save the return value, it may be garbage collected. For correct work the return value must be saved. Example - tarantool/tarantool#4999 So, use a new C library namespace instead the global one. This will help to keep the global namespace clean and avoid the problem with garbage collection.
1 parent 8401d47 commit 62a557f

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

memcached/init.lua

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ local fmt = string.format
1010

1111
local internal_so_path = package.search('memcached.internal')
1212
assert(internal_so_path, "Failed to find memcached/internal.so library")
13-
ffi.load(internal_so_path, true)
13+
local memcached_internal = ffi.load(internal_so_path)
1414

1515
ffi.cdef[[
1616
struct memcached_stat {
@@ -267,7 +267,8 @@ local memcached_methods = {
267267
end
268268
for k, v in pairs(opts) do
269269
if conf_table[k] ~= nil then
270-
C.memcached_set_opt(self.service, conf_table[k], v)
270+
memcached_internal.memcached_set_opt(self.service,
271+
conf_table[k], v)
271272
self.opts[k] = v
272273
end
273274
end
@@ -276,14 +277,14 @@ local memcached_methods = {
276277
start = function (self)
277278
local function memcached_handler(socket, addr)
278279
log.debug('client %s:%s connected', addr.host, addr.port)
279-
C.memcached_handler(self.service, socket:fd())
280+
memcached_internal.memcached_handler(self.service, socket:fd())
280281
end
281282
jit.off(memcached_handler)
282283

283284
if self.status == RUNNING then
284285
error(fmt(err_is_started, self.name))
285286
end
286-
C.memcached_start(self.service)
287+
memcached_internal.memcached_start(self.service)
287288
local parsed = uri.parse(self.uri)
288289
self.listener = socket.tcp_server(parsed.host, parsed.service, {
289290
handler = memcached_handler
@@ -293,7 +294,7 @@ local memcached_methods = {
293294
self.status = ERRORED
294295
error(fmt('can\'t bind (%d) %s', errno(), errno.strerror()))
295296
end
296-
if (C.memcached_setsockopt(self.listener:fd(),
297+
if (memcached_internal.memcached_setsockopt(self.listener:fd(),
297298
lname.family,
298299
lname.type) == -1) then
299300
self.status = ERRORED
@@ -309,12 +310,12 @@ local memcached_methods = {
309310
if (self.listener ~= nil) then
310311
self.listener:close()
311312
end
312-
local rc = C.memcached_stop(self.service)
313+
local rc = memcached_internal.memcached_stop(self.service)
313314
self.status = STOPPED
314315
return self
315316
end,
316317
info = function (self)
317-
local stats = C.memcached_get_stat(self.service)
318+
local stats = memcached_internal.memcached_get_stat(self.service)
318319
local retval = {}
319320
for k, v in pairs(stat_table) do
320321
retval[v] = stats[0][v]
@@ -362,11 +363,12 @@ local function memcached_init(name, uri, opts)
362363
else
363364
instance.space = box.space[instance.space_name]
364365
end
365-
local service = C.memcached_create(instance.name, instance.space.id)
366+
local service = memcached_internal.memcached_create(instance.name,
367+
instance.space.id)
366368
if service == nil then
367369
error(fmt(err_enomem, "memcached service"))
368370
end
369-
instance.service = ffi.gc(service, C.memcached_free)
371+
instance.service = ffi.gc(service, memcached_internal.memcached_free)
370372
memcached_services[instance.name] = setmetatable(instance, {
371373
__index = memcached_methods
372374
})

0 commit comments

Comments
 (0)