Copyright © https://mongoose-os.com

Mongoose OS Forum

frame
ATTENTION! This forum has moved to:

https://community.mongoose-os.com

Do not post any new messages.

mJS Sys functions and memory management/ bug in Sys.calloc

Please help with more information or direction to more information on this and related topics. It seems that this part of mJS and Mongoose OS are not well used, but are potentially very useful. There is at least one problem/inconsistency/bug in Sys.calloc(x,y)

api_sys.js has several memory management FFI calls:
Sys._sbuf is a "helper" function. What does it do, and how to use it?
Sys.calloc(x,y) is described, but does not work (because there is no FFI call to calloc anywhere in api_sys.js).
There IS a malloc FFI call. What does it do, and how to use it?

Built in mJS API
let s = mkstr(ptrVar, length);
Create a string backed by a C memory chunk. A string s starts at memory location ptrVar, and is length bytes long.
What does this do? What is the usage? What does the C string look like?

Comments

  • jstevewoodjstevewood California

    This functionality is critical to serious use of mJS. Somebody has to be interested in this. Anybody? Please help!

    Thanks, JSW

  • nliviunliviu Romania

    Sys.calloc(x,y) description is confusing because the actual function is Sys.malloc(size).

    A block of memry allocated with malloc must be freed with free. The corresponding mJS function is Sys.free(ptr)

    mkstr is used to create a mJS string from a C string. Example

    Example using Sys.malloc, Sys.free and ffi

    load('api_sys.js');
    load('api_dataview.js');
    load('api_timer.js');
    
    function makeUnixtime(year, month, day, hour, minutes, seconds)
    {
      /*
       int    tm_sec   seconds [0,61]
       int    tm_min   minutes [0,59]
       int    tm_hour  hour [0,23]
       int    tm_mday  day of month [1,31]
       int    tm_mon   month of year [0,11]
       int    tm_year  years since 1900
       int    tm_wday  day of week [0,6] (Sunday = 0)
       int    tm_yday  day of year [0,365]
       int    tm_isdst daylight savings flag
       */
      let structSize = 9 * 4;
      let structTm = Sys.malloc(structSize); // alloc a struct tm
      // set all members to zero
      ffi('void* memset(void*, int, int)')(structTm, 0, structSize);
      let dw = DataView.create(structTm, 0, structSize);
      let offset = 0;
      dw.setInt32(offset, seconds, true);// set tm_sec
      offset += 4;
      dw.setInt32(offset, minutes, true); // set tm_min
      offset += 4;
      dw.setInt32(offset, hour, true);// set tm_hour
      offset += 4;
      dw.setInt32(offset, day, true);// set tm_mday
      offset += 4;
      dw.setInt32(offset, month - 1, true);  // set tm_mon
      offset += 4;
      year = (year < 2000) ? (year + 2000) : year;
      dw.setInt32(offset, year - 1900, true);  // set tm_year
      offset += 4; // ignore tm_wday
      offset += 4; // ignore tm_yday
      offset += 4;
      dw.setInt32(offset, -1, true);// set tm_isdst
      // create unix time using mktime
      let unixtime = ffi('int mktime(void*)')(structTm);
      Sys.free(structTm);
      return unixtime;
    }
    
    let unixtime = makeUnixtime(2018, 06, 15, 11, 07, 0);
    print(Timer.fmt("%FT%T%Z", unixtime));
    
    Thanked by 1jstevewood
  • jstevewoodjstevewood California

    Hi nliviu, Thanks for the info. I will study this fully.

    I have been using a construction like this to pass a Javascript string to a C function:

    let cprocess=ffi('int cstringfunc(char *, int)');
    let filevar=File.read ('bigfile'); //bigfile is 2560 bytes long
    let flen=filevar.length;
    let cresult=cprocess(filevar,flen); //here i pass the name of the string to a cfunction and it modifies the filevar string; Javascript can then work with the modified string

    This method seems to work perfectly, but is it correct and memory efficient?

    Thanks,

    JSW

Sign In or Register to comment.