Newk, unpool, repool, kap - kevinlawler/kona GitHub Wiki

Basics of memory management : https://groups.google.com/forum/#!topic/kona-dev/fs5GoSBtF3Y/discussion

Q1. The C memory functions (newK/unpool/repool/kap) are to be called from your C code of functions that you dynamically load into the ./k interpreter. Are those same functions called by the compiled interpreter code when it interprets interactive K scripts.

A1. yes

Q2. How does unpool/repool memory manager decide which lane to use?

A2. First it calls function lsz to map the requested size to lane number.

It finds n such that 2^n is large enough to hold the requested size.

That n is the lane.

For example if you request 24 bytes, lane 5 is needed (2^4 <= 32 <= 2^5) .

A single K scalar e.g. an integer requires 32 bytes, as it holds 3 8-byte integers and one 8-byte variant type.

(The variant type is integer or float or pointer)

Thus when you allocate a vector, there may be unused space in the block of the selected lane, beyond the end of the vector.

This could later be utilized to grow the vector without needing to move it. (See question 3)

Q3. what is kap, kapn ?

A3. It is used to grow a vector of scalar type by adding another vector to it.

- kap (K* a, V v) adds a single elemnent from vector v to vector a

- kapn(K* a, V v, I n) adds n elements from vector v to vector a

It works by computing checking if the amount of space used by the vector y, when added to the space used by vector x, will exceed the slack in the lane when x was originally allocated.

If so, it will reallocate the result vector from a larger lane.

Otherwise, it will just copy the data of vector y into the unused part of the block of the current lane.

Advanced memory management issues discussed in #162 https://github.com/kevinlawler/kona/issues/162