Commit 945aac1a authored by NzSN's avatar NzSN

Add CpySchedule.

parent ee63ae87
......@@ -22,19 +22,32 @@ function makeMessage(msgType, datas) {
return {type:msgType, data: datas};
}
class CpySchedule {
first = { pos: 0, size: 0 }
second = { pos: 0, size: 0 }
}
/* An abstraction that help to
* easily to transfer datas to target worker
* in real time manner */
export class Channel {
/* | ReadPointer (4 bytes) | WritePointer (4 Bytes) | Data Area (1-n bytes) */
/* */
/* Size of memory area that help to
* maintain Channel. Types of meta info
* are shown below:
*
* 1.Write Position
* 2.Read Position */
* 2.Read Position
*
* Figure:
* ------------------------------------------------------------------------
* | ReadPointer (4 bytes) | WritePointer (4 Bytes) | Data Area (N bytes) |
* -------------------------------------------------------------------------
* where N >= 2
*
* */
#numOfMetaField = 2
#fieldSize = 0;
......@@ -54,6 +67,9 @@ export class Channel {
// size's unit is byte
// bufferType parameter is mainly for testability.
constructor(WW, size, bufferType = SharedArrayBuffer) {
assert(size >= 2, `Channel require its data area has at least 2 Bytes.`)
this.#WW = WW;
this.#size = size;
......@@ -70,6 +86,8 @@ export class Channel {
this.#view.setUint32(0, this.#metaSize);
this.#view.setUint32(4, this.#metaSize);
// FIXME: Need to transfer channel itself to worker.
}
#readPos() {
......@@ -104,20 +122,21 @@ export class Channel {
* make sure there has enough spaces.*/
#cpySchedule(size) {
let firstCpySize = 0, secondCpySize = 0, spaceToTail = 0;
let schedule;
let schedule = new CpySchedule();
if (this.#writePos >= this.#readPos()) {
spaceToTail = this.#endPos - this.#writePos;
firstCpySize = Math.min(size, spaceToTail);
secondCpySize = firstCpySize < size ? size - firstCpySize : 0;
schedule = {
first : { pos: this.#writePos, size: firstCpySize },
second: { pos: secondCpySize > 0 ? this.#metaSize : 0, size: secondCpySize }
};
schedule.first.pos = this.#writePos;
schedule.first.size = firstCpySize;
schedule.second.pos = secondCpySize > 0 ? this.#metaSize : 0;
schedule.second.size = secondCpySize;
} else {
schedule = { first : { pos: this.#writePos, size: firstCpySize },
second: { pos: 0, size: 0 } };
schedule.first.pos = this.#writePos;
schedule.first.size = firstCpySize;
}
return schedule;
......@@ -182,15 +201,17 @@ export class Channel {
let schedule = this.#cpySchedule(data.byteLength);
// Perfrom write schedule
let srcPos = 0;
let srcPos = 0, plan;
for (let key in schedule) {
let plan = schedule[key];
plan = schedule[key];
if (plan.size == 0)
continue;
let d_ = data.slice(srcPos, srcPos+plan.size);
this.#buffer.set(d_, plan.pos)
srcPos = srcPos + plan.size;
this.#buffer.set(
data.slice(srcPos, srcPos+plan.size), plan.pos)
srcPos += plan.size;
writePos = plan.pos+plan.size;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment