Author Topic: new/malloc in SlickC?  (Read 8030 times)

rowbearto

  • Senior Community Member
  • Posts: 2335
  • Hero Points: 132
new/malloc in SlickC?
« on: September 30, 2018, 05:47:32 PM »
Is there a heap in SlickC programming language? I don't see any calls to new() or malloc() in SlickC macros.

I'm trying to create a class instance on the heap within a function, then pass a pointer to this instance to _set_timer() and use it until the timer handler decides the instance is not needed anymore and can be deleted. So I can't use the stack for this, and if I use a global variable then I can have only 1 instance instead of many.

How to handle something like this in SlickC?

Graeme

  • Senior Community Member
  • Posts: 2796
  • Hero Points: 347
Re: new/malloc in SlickC?
« Reply #1 on: October 01, 2018, 11:26:52 AM »
You could maybe use my linked list module dlinklist.e.  It's loosely modelled on C++ list container.  Because slick c has dynamic typing you can push all different type of objects onto the list.  Below the object is created first on the stack (or what appears to be a stack), then the entire object gets copied by value into the list (or maybe it's reference counted?) and the stack object dies.  When the item is removed from the list the contained object lives on - I should probably set it to null so it gets garbage collected  -  hmm.  Wow, look how much I don't know about slick C.  You could also use an array - it has a dynamic length like c++ vector.

Code: [Select]
// ...
   xretrace_item item;
   item.buf_name = bufname;
   item.line_marker_id = marker_id;
   item.marker_id_valid = true;
   item.mid_line = mid_line;
   item.last_line = last_line;
   item.col = col;
   item.flags = flags;
   item.window_id = window_id;

   if (!dlist_push_front(alist, item))
   { ... }
// ...

boolean dlist_push_front(dlist & dl, typeless & val)
{
   int nnode = dlist_get_new_node(dl, true);
   if (nnode < 0) {
      return false;
   }
   insert_at_front(dl, nnode);
   dl.nodes[nnode].s_data = val;
   return true;
}




rowbearto

  • Senior Community Member
  • Posts: 2335
  • Hero Points: 132
Re: new/malloc in SlickC?
« Reply #2 on: October 01, 2018, 01:27:05 PM »
Thanks Graeme!

I ended up using an array. Example code below:

Code: [Select]
class ObjectToAllocateType {
    public bool m_inUse = false;
    public int m_timerHandle = 0;
    (other useful member data)
}
static ObjectToAllocateType objectArray[];
...
void some_function_that_starts_timer()
{
    ...
    // Allocate an instance of ObjectToAllocateType and pass to _set_timer()
    int arrayIdxToUse = -1;
    int arrayIdx;
    for (arrayIdx = 0; arrayIdx < objectArray._length(); arrayIdx++) {
        if ( !(objectArray[arrayIdx].m_inUse) ) {
            arrayIdxToUse = arrayIdx;
            break;
        }
    }
    if ( arrayIdxToUse == -1 ) {
        // SE User guide states: "Space for array elements is allocated when you index into the array"
        arrayIdxToUse = objectArray._length();
    }
    // SE User guide states: "Space for array elements is allocated when you index into the array"
    ObjectToAllocateType* pObject = &(objectArray[arrayIdxToUse]);
    pObject->m_inUse = true;
    // Set other members
    pObject->m_member1 = ...;
    ...
    // Start timer callback and pass newly allocated object as callback argument
    pObject->m_timerHandle = _set_timer(interval, timer_callback, pObject);
}

void timer_callback(ObjectToAllocateType* pObject)
{
...
  if (ready to deallocate) {
    _kill_timer(pObject->m_timerHandle);
    pObject->m_inUse = false;
    bool performEmptyFlag = true;
    int objectArrayIdx = 0;
    for (objectArrayIdx = 0; objectArrayIdx < objectArray._length(); objectArrayIdx++) {
        if ( objectArray[objectArrayIdx].m_inUse ) {
            performEmptyFlag = false;
            break;
        }
    }
    if ( performEmptyFlag ) {
        objectArray._makeempty();
    }
  }
}