madhead
02/18/2020, 4:07 PMclass Node(prev: Node?, value: Char)
class Stack(tail: Node?)
Redis provides a specific API for memory allocation: void *RedisModule_Alloc(size_t bytes)
. They say it should be preferred over malloc
because “memory allocated with this function is reported in Redis INFO memory, used for keys eviction according to maxmemory settings and in general is taken into account as memory allocated by Redis”.
So, I want to use this API to allocate memory for my nodes. My plan was to reinterpret that pointers to my types later.
The problem is that I cannot find a way to know the size needed by my claasses, like sizeOf<Node>
. How do I know it?Artyom Degtyarev [JB]
02/19/2020, 10:28 AMmadhead
02/19/2020, 5:27 PMStableRef
to pass void *
to the C code here, on lines 113, 117, 155. I guess, Redis just stores it somewhere in the memory for some time. My concern is that the object tree (the linked list) may become obsolete, invalid, and I could get a segfault.
2. I'm using memScoped { "string".cstr.ptr }
every time I need to pass a const char *
to the C code (RedisModule_ReplyWithLongLong
). Is it ok? The generated Kotlin interfaces expects a `CPointer``, not a CValues
napperley
02/19/2020, 7:56 PMArtyom Degtyarev [JB]
02/20/2020, 1:10 PMArtyom Degtyarev [JB]
02/20/2020, 1:54 PMAny StableRef should be manually disposed
. If you do not dispose StableRefs
, it will result in memory leaks, as reference counting would be working incorrectly for them. In fact, that’s what I was talking originally - Kotlin/Native classes are hard to get cut away from its runtime, it was laid in the compiler design.
2. The problem here is that all guarantees of your const char presence under this pointer will be gone as soon as the memScoped
block ends. I’m still unfamiliar with all this Redis stuff, so I cannot say if this is okay here, but presumably, this is an error-prone approach. Maybe it would be better to allocate all these strings in some Arena
class instance, with clearing it by your demand.madhead
02/20/2020, 2:05 PMbracketsFree
in my code. It will be called when Redis is about to dispose the key’s value (when it’s deleted or evicted due to low memory). Am I safe if i call a dispose
here?
2. I’ll try to check if Redis copies that strings, it’s unclear from the docs. I guess that if if does a memcopy here — I am safe, if it’s not — I should probably allocate that string in nativeHeap
Artyom Degtyarev [JB]
02/20/2020, 2:43 PMnativeHeap
, documentation example of it’s use is kinda incorrect, see here(https://kotlinlang.slack.com/archives/C3SGXARS6/p1556275050056600?thread_ts=1556274437.055700&cid=C3SGXARS6).madhead
02/20/2020, 3:08 PM"ss".cstr.place(nativeHeap.allocArray(s.length * 4))
madhead
02/20/2020, 3:08 PM