MCPcopy Create free account
hub / github.com/GoogleChrome/samples / __allocStruct

Function __allocStruct

sqlite-wasm-cos/sqlite3-node.mjs:9973–10033  ·  view source on GitHub ↗
(ctor, obj, xm)

Source from the content-addressed store, hash-verified

9971 /** Allocates obj's memory buffer based on the size defined in
9972 ctor.structInfo.sizeof. */
9973 const __allocStruct = function f(ctor, obj, xm){
9974 let opt;
9975 const checkPtr = (ptr)=>{
9976 __isNonNullPtr(ptr) ||
9977 toss("Invalid pointer value",arguments[0],"for",ctor.structName,"constructor.");
9978 };
9979 if( arguments.length>=3 ){
9980 if( xm && ('object'===typeof xm) ){
9981 opt = xm;
9982 xm = opt?.wrap;
9983 }else{
9984 checkPtr(xm);
9985 opt = {wrap: xm};
9986 }
9987 }else{
9988 opt = {}
9989 }
9990
9991 const fill = !xm /* true if we need to zero the memory */;
9992 let nAlloc = 0;
9993 let ownsPointer = false;
9994 if(xm){
9995 /* Externally-allocated memory. */
9996 checkPtr(xm);
9997 ownsPointer = !!opt?.takeOwnership;
9998 }else{
9999 const nX = opt?.extraBytes ?? 0;
10000 if( nX<0 || (nX!==(nX|0)) ){
10001 toss("Invalid extraBytes value:",opt?.extraBytes);
10002 }
10003 nAlloc = ctor.structInfo.sizeof + nX;
10004 xm = alloc(nAlloc)
10005 || toss("Allocation of",ctor.structName,"structure failed.");
10006 ownsPointer = true;
10007 }
10008 try {
10009 if( opt?.debugFlags ){
10010 /* specifically undocumented */
10011 obj.debugFlags(opt.debugFlags);
10012 }
10013 if(ctor./*prototype.???*/debugFlags.__flags.alloc){
10014 log("debug.alloc:",(fill?"":"EXTERNAL"),
10015 ctor.structName,"instance:",
10016 ctor.structInfo.sizeof,"bytes @"+xm);
10017 }
10018 if(fill){
10019 heap().fill(0, Number(xm), Number(xm) + nAlloc);
10020 }
10021 const ii = getInstanceHandle(obj);
10022 ii.p = xm;
10023 ii.ownsPointer = ownsPointer;
10024 ii.xb = nAlloc ? (nAlloc-ctor.structInfo.sizeof) : 0;
10025 ii.zod = !!opt?.zeroOnDispose;
10026 if( opt?.ondispose && opt.ondispose!==xm ){
10027 obj.addOnDispose( opt.ondispose );
10028 }
10029 }catch(e){
10030 __freeStruct(ctor, obj, xm);

Callers 1

StructCtorFunction · 0.70

Calls 5

checkPtrFunction · 0.70
tossFunction · 0.70
logFunction · 0.70
getInstanceHandleFunction · 0.70
__freeStructFunction · 0.70

Tested by

no test coverage detected