Memory pools 3.0

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
mobyfab
Posts: 484
Joined: Sat Nov 19, 2011 6:47 pm
Has thanked: 21 times
Been thanked: 31 times

Memory pools 3.0

Post by mobyfab »

Hi,

I'm trying to get the mem pool working on 3.0, but I must be missing something :)

Here's my code

Code: Select all


typedef struct {
  void* location;
  size_t size;
} samples_message_t __attribute__ ((aligned (sizeof(stkalign_t))));

MEMORYPOOL_DECL(samples_pool, sizeof(samples_message_t), NULL);

void setupIPC(void)
{
  chPoolObjectInit(&samples_pool, sizeof(samples_message_t), NULL);
  chMBObjectInit(&knockMb, buf1, sizeof(buf1)/sizeof(msg_t));
  chMBObjectInit(&sensorsMb, buf2, sizeof(buf2)/sizeof(msg_t));
}

void allocSendSamplesI(mailbox_t* mb, void * buffer, size_t size)
{
  samples_message_t* info = chPoolAllocI(&samples_pool);

  if (info == NULL) return;

  info->location = buffer;
  info->size = size;
  chMBPostI(mb, (msg_t)info);
}

bool recvFreeSamples(mailbox_t* mb, void ** buffer, size_t * size)
{
  msg_t msg;
  samples_message_t data;

  if(chMBFetch(mb, &msg, TIME_IMMEDIATE) != MSG_OK)
    return false;

  if ((samples_message_t*)msg == NULL) return false;

  data = *(samples_message_t*)msg;
  chPoolFree(&samples_pool, (void*)msg);

  if (data.location == NULL) return false;

  *buffer = data.location;
  *size = data.size;

  return true;
}


chPoolAllocI always returns NULL. :(
mobyfab
Posts: 484
Joined: Sat Nov 19, 2011 6:47 pm
Has thanked: 21 times
Been thanked: 31 times

Re: Memory pools 3.0

Post by mobyfab »

figured it myself:

Code: Select all

ipc.h

#define POOL_SIZE 10

extern mailbox_t knockMb;
extern mailbox_t sensorsMb;

typedef struct {
  void* location;
  size_t size;
} samples_message_t;

void setupIPC(void);
void allocSendSamplesI(mailbox_t* mb, void * buffer, size_t size);
bool recvFreeSamples(mailbox_t* mb, void ** buffer, size_t * size);

-------
ipc.c

samples_message_t samples_messages[POOL_SIZE] __attribute__((aligned(sizeof(stkalign_t))));

static MEMORYPOOL_DECL(samples_pool, sizeof(samples_message_t), NULL);

void setupIPC(void)
{
  uint8_t i;
  chMBObjectInit(&knockMb, buf1, sizeof(buf1)/sizeof(msg_t));
  chMBObjectInit(&sensorsMb, buf2, sizeof(buf2)/sizeof(msg_t));

  chPoolObjectInit(&samples_pool, sizeof(samples_message_t), NULL);
  for(i=0; i < POOL_SIZE; i++)
      chPoolFree(&samples_pool, &samples_messages[i]);
}

void allocSendSamplesI(mailbox_t* mb, void * buffer, size_t size)
{
  samples_message_t* info = chPoolAllocI(&samples_pool);

  if (info == NULL) return;

  info->location = buffer;
  info->size = size;
  chMBPostI(mb, (msg_t)info);
}

bool recvFreeSamples(mailbox_t* mb, void ** buffer, size_t * size)
{
  msg_t msg;
  samples_message_t data;

  if(chMBFetch(mb, &msg, TIME_IMMEDIATE) != MSG_OK)
    return false;

  if ((samples_message_t*)msg == NULL) return false;

  data = *(samples_message_t*)msg;
  chPoolFree(&samples_pool, (void*)msg);

  if (data.location == NULL) return false;

  *buffer = data.location;
  *size = data.size;

  return true;
}



In case that helps :)
Post Reply