* Copyright 2025, Haiku, Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*/
var fs = require('fs');
var file = fs.readFileSync(process.argv[2], {encoding: "utf-8"});
const lines = file.split("\r\n");
file = '';
console.log("static const AllocationOperation kOperations[] = {");
var live_allocations = {}, allocations_count = 0;
for (var i in lines) {
const line = lines[i].split(/[ \(\),]/);
if (line[0] == "malloc") {
const size = line[1], address = line[4];
live_allocations[address] = allocations_count++;
console.log("\t{ OP_MALLOC, %s },", size);
} else if (line[0] == "free") {
const address = line[1];
if (parseInt(address) === 0)
continue;
let index = live_allocations[address];
if (isNaN(parseInt(index)))
continue;
delete live_allocations[address];
console.log("\t{ OP_FREE, %d },", index);
} else if (line[0] == "realloc") {
const address = line[1], size = line[3], newAddress = line[6];
if (parseInt(address) === 0) {
live_allocations[newAddress] = allocations_count++;
console.log("\t{ OP_MALLOC, %d },", size);
continue;
}
let index = live_allocations[address];
delete live_allocations[address];
live_allocations[newAddress] = index;
console.log("\t{ OP_REALLOC, %d },", index);
console.log("\t{ 0, %d },", size);
}
}
console.log("};");
console.log("static const uint32 kAllocationsCount = %d;\n", allocations_count);