This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Memory Pool (malloc/free) Free Space?

Quite simply, how can I tell how much free memory I have in a memory pool?

I've created a memory pool, initialized it with init_mempool and use malloc & free finctions for memory management.

Is there a simple way to find out how much memory is still available to be allocated?

I have a fairly dynamic application and I would like to insert some degugging triggers if memory gets low.

I thought of simply using malloc to request a block of say 200 bytes and then free it again. Could do this once a second and generate a low memory error if it fails. Seems a bit clunky, is there an easier way that I have missed??

Thanks,
Bryan Green.

Parents
  • Bryan,

    I haven't tested this, but the following function should do what you want.

    long alloc_ttl, alloc_max;
    
    void available_alloc (void)
    {
    struct __mp__ MTYP *q;
    
    alloc_ttl = 0;
    alloc_max = 0;
    
    q = (struct __mp__ MTYP *) &__mp__;
    q = q->next;
    
    while (q)
      {
      alloc_ttl += q->len;
      if (alloc_max < q->len) allox_max = q->len;
      q = q->next;
      }
    }
    

    If you look at the source for malloc, you'll see how it works.

    Jon

Reply
  • Bryan,

    I haven't tested this, but the following function should do what you want.

    long alloc_ttl, alloc_max;
    
    void available_alloc (void)
    {
    struct __mp__ MTYP *q;
    
    alloc_ttl = 0;
    alloc_max = 0;
    
    q = (struct __mp__ MTYP *) &__mp__;
    q = q->next;
    
    while (q)
      {
      alloc_ttl += q->len;
      if (alloc_max < q->len) allox_max = q->len;
      q = q->next;
      }
    }
    

    If you look at the source for malloc, you'll see how it works.

    Jon

Children