Semi-static values as global data and static datatypes

In a ThreadX app for the Cypress device I am using, some data is malloc’ed and never freed, or only freed in rare error conditions. Putting aside whether this is a good practice… and mimicking this in ATS… with malloc and no GC…

I think a datatype as a var is boxed and on the heap. So given the following in file scope:

datatype foo =
| d1 as (int, double)
| d2 as int

var v: foo

in a dats file,

I assume the dynload will create the var on the heap, and will never be freed without other code that manually frees it, and is available via its name plus staload.

Is this correct?

Is there a way make such a data structure static and global? And an array of these as static and global? Without C is what I’m looking for.

Related to C, if I did this in C, say an array of structs, is there a way to directly map this to an array of ATS datatypes? This would imply a datatype having the same layout in memory. The struct could be packed or not. Packed data structures get used when mapping records/structs to blocks of binary data.

Perhaps what I really want is a record type. That would be struct like.

So I assume I can make a array of records, and the array creation would malloc to do it. And I could update pieces of the data like

a[n].d1

Or I could create the array in C and use array ref as I have done before. But, I assume there is a way to make a global static array without C? How?

Then the question is mapping records to packed or unpacked structs. Basically, can a record type be setup to match the struct layout? Can this be done without record full of pointers that have be manually set. Like casting a struct ptr to record ptr.

An application example:

If some C code running outside my processor makes a block of packed structs, where each struct has a size and struct type at known offsets, and the block is passed to me via communication, now I want to map some records over it so I have an array of record pointers where they point into the block of data. The same memory layout question applies. How to make a record layout match a packed or unpacked struct. So perhaps the data comes to ATS as uint8[], and it needs to be processed into a list of records with ptr manipulation, but has to map to the original C structs. And it can be done in statics if it is possible to choose a max len or on heap if that is not possible.

When you write

var v: foo

It is just like writing the following code in C:

void *v;

There is no memory allocation involved.

If you do

val () = v := d1(1, 2.0)

then the value d1(1, 2.0) is created on heap and a pointer to the value
is stored in v.

For embedded programming, it may not be a good idea to use datatypes.
I am not yet clear about your situation. It looks like you may be in need
of union
types.On Thursday, December 31, 2015 at 11:48:52 PM UTC-5, Mike Jones wrote:

In a ThreadX app for the Cypress device I am using, some data is malloc’ed
and never freed, or only freed in rare error conditions. Putting aside
whether this is a good practice… and mimicking this in ATS… with malloc
and no GC…

I think a datatype as a var is boxed and on the heap. So given the
following in file scope:

datatype foo =
| d1 as (int, double)
| d2 as int

var v: foo

in a dats file,

I assume the dynload will create the var on the heap, and will never be
freed without other code that manually frees it, and is available via its
name plus staload.

Is this correct?

Is there a way make such a data structure static and global? And an array
of these as static and global? Without C is what I’m looking for.

Related to C, if I did this in C, say an array of structs, is there a way
to directly map this to an array of ATS datatypes? This would imply a
datatype having the same layout in memory. The struct could be packed or
not. Packed data structures get used when mapping records/structs to blocks
of binary data.

I think you can declare a static array in ATS like this:

var A: @[int][100] // A is an uninitialized integer array of size 100
var A = @[int]100 // A is an integer array of 100 0’s/

I myself almost always do data allocation and initialization in C
and then use the data in ATS. So I just do:

fun{} A_get_at (natLt(100)): int
fun{} A_set_at (natLt(100), int): void

Right now, this style is a bit verbose. The hope is to introduce some
meta-programming support to cut down the verbosity.On Friday, January 1, 2016 at 1:11:03 AM UTC-5, Mike Jones wrote:

Perhaps what I really want is a record type. That would be struct like.

So I assume I can make a array of records, and the array creation would
malloc to do it. And I could update pieces of the data like

a[n].d1

Or I could create the array in C and use array ref as I have done before.
But, I assume there is a way to make a global static array without C? How?

Then the question is mapping records to packed or unpacked structs.
Basically, can a record type be setup to match the struct layout? Can this
be done without record full of pointers that have be manually set. Like
casting a struct ptr to record ptr.

An application example:

If some C code running outside my processor makes a block of packed
structs, where each struct has a size and struct type at known offsets, and
the block is passed to me via communication, now I want to map some records
over it so I have an array of record pointers where they point into the
block of data. The same memory layout question applies. How to make a
record layout match a packed or unpacked struct. So perhaps the data comes
to ATS as uint8, and it needs to be processed into a list of records with
ptr manipulation, but has to map to the original C structs. And it can be
done in statics if it is possible to choose a max len or on heap if that is
not possible.

I’m just exploring mechanisms, not making design decisions. But in my case, where Cypress uses ThreadX and malloc, and memory is relatively large, and the processor fast, it is an option. Hence why my question asked about heap vs non-heap arrays of structured data.

It is possible in my app to read arbitrary blocks of data from the outside world that are lists of packed structures. And I have to choose between heap, static data, and if static, is it defined in C, mapped to data structures, processed into them, etc. I have a lot of trade offs. So I’m trying to understand how ATS works wrt memory so I can make trade offs. A USB device is embedded, but because it does data processing, it is not like a small embedded app with limits. It more like a 500MHz dual core A7 processing data of arbitrary size. And I might even choose GC at some point if heap fragmentation is an issue. Or code might move to Linux with even more memory, etc. This is not for an Arduino.