ukrbublik / shm-typed-array

IPC shared memory for NodeJs. Use as Buffer or TypedArray.
MIT License
71 stars 9 forks source link

I'd like to use a fixed key. #1

Closed joonyong-jinong closed 7 years ago

joonyong-jinong commented 7 years ago

Hi.

Thank to develop and open this module.

In my project, I need to use a fixed key not a generated key. So I wanna suggest to change create method like following. I added defkey parameter. When it is not defined, the key would be generated as it was. But if it is given, the key would be set defkey value.

/**
 * Create shared memory segment
 * @param {int} count - number of elements
 * @param {string} typeKey - see keys of BufferType
 * @param {int} defkey - default key
 * @return {mixed} shared memory buffer/array object
 *  Class depends on param typeKey: Buffer or descendant of TypedArray
 *  Return object has property 'key' - integer key of created shared memory segment
 */
function create(count, typeKey, defkey) {
    if (typeKey === undefined)
        typeKey = 'Buffer';
    if (BufferType[typeKey] === undefined)
        throw new Error("Unknown type key " + typeKey);
    var type = BufferType[typeKey];

    if (!(Number.isSafeInteger(count) && count >= lengthMin && count <= lengthMax))
        throw new RangeError('Count should be ' + lengthMin + ' .. ' + lengthMax);
    let key, res;
    do {
        if (defkey === undefined)
            key = _keyGen();
        else
            key = defkey;
        res = shm.get(key, count, shm.IPC_CREAT|shm.IPC_EXCL|perm, 0, type);
    } while(!res);
    res.key = key;
    return res;
}
ukrbublik commented 7 years ago

Hi. Thanks for suggestion. Updated module to v0.0.10