2017-05-19 11:12:00 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* machinarium.
|
|
|
|
*
|
|
|
|
* cooperative multitasking engine.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <machinarium.h>
|
|
|
|
#include <machinarium_private.h>
|
|
|
|
|
2017-05-24 10:38:00 +00:00
|
|
|
void mm_msgcache_init(mm_msgcache_t *cache)
|
2017-05-19 11:12:00 +00:00
|
|
|
{
|
2017-05-24 10:38:00 +00:00
|
|
|
mm_list_init(&cache->list);
|
|
|
|
cache->count = 0;
|
2018-08-30 14:23:14 +00:00
|
|
|
cache->count_allocated = 0;
|
2018-09-05 13:04:36 +00:00
|
|
|
cache->count_gc = 0;
|
2018-08-30 14:23:14 +00:00
|
|
|
cache->size = 0;
|
2018-09-26 15:18:17 +00:00
|
|
|
cache->gc_watermark = 0;
|
2017-05-19 11:12:00 +00:00
|
|
|
}
|
|
|
|
|
2017-05-24 10:38:00 +00:00
|
|
|
void mm_msgcache_free(mm_msgcache_t *cache)
|
2017-05-19 11:12:00 +00:00
|
|
|
{
|
|
|
|
mm_list_t *i, *n;
|
2017-05-24 10:38:00 +00:00
|
|
|
mm_list_foreach_safe(&cache->list, i, n) {
|
2017-05-19 11:12:00 +00:00
|
|
|
mm_msg_t *msg = mm_container_of(i, mm_msg_t, link);
|
2017-05-23 12:34:35 +00:00
|
|
|
mm_buf_free(&msg->data);
|
2017-05-19 11:12:00 +00:00
|
|
|
free(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-05 13:04:36 +00:00
|
|
|
void mm_msgcache_stat(mm_msgcache_t *cache,
|
|
|
|
uint64_t *count_allocated,
|
|
|
|
uint64_t *count_gc,
|
|
|
|
uint64_t *count,
|
|
|
|
uint64_t *size)
|
2018-08-30 14:23:14 +00:00
|
|
|
{
|
|
|
|
*count_allocated = cache->count_allocated;
|
2018-09-05 13:04:36 +00:00
|
|
|
*count_gc = cache->count_gc;
|
2018-08-30 14:23:14 +00:00
|
|
|
*count = cache->count;
|
|
|
|
*size = cache->size;
|
|
|
|
}
|
|
|
|
|
2017-05-19 11:12:00 +00:00
|
|
|
mm_msg_t*
|
2017-05-24 10:38:00 +00:00
|
|
|
mm_msgcache_pop(mm_msgcache_t *cache)
|
2017-05-19 11:12:00 +00:00
|
|
|
{
|
|
|
|
mm_msg_t *msg = NULL;
|
2017-05-24 10:38:00 +00:00
|
|
|
if (cache->count > 0) {
|
|
|
|
mm_list_t *first = mm_list_pop(&cache->list);
|
|
|
|
cache->count--;
|
2017-05-19 11:12:00 +00:00
|
|
|
msg = mm_container_of(first, mm_msg_t, link);
|
2018-08-30 14:23:14 +00:00
|
|
|
cache->size -= mm_buf_size(&msg->data);
|
2017-05-19 11:12:00 +00:00
|
|
|
goto init;
|
|
|
|
}
|
2018-08-30 14:23:14 +00:00
|
|
|
cache->count_allocated++;
|
2017-05-19 11:12:00 +00:00
|
|
|
|
|
|
|
msg = malloc(sizeof(mm_msg_t));
|
|
|
|
if (msg == NULL)
|
|
|
|
return NULL;
|
2017-05-23 18:47:28 +00:00
|
|
|
mm_buf_init(&msg->data);
|
2017-05-19 11:12:00 +00:00
|
|
|
init:
|
2018-11-20 16:08:27 +00:00
|
|
|
msg->machine_id = mm_self->id;
|
|
|
|
msg->refs = 0;
|
|
|
|
msg->type = 0;
|
2017-05-23 18:47:28 +00:00
|
|
|
mm_buf_reset(&msg->data);
|
2017-05-19 11:12:00 +00:00
|
|
|
mm_list_init(&msg->link);
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
2017-05-24 10:38:00 +00:00
|
|
|
void mm_msgcache_push(mm_msgcache_t *cache, mm_msg_t *msg)
|
2017-05-19 11:12:00 +00:00
|
|
|
{
|
2018-11-20 16:08:27 +00:00
|
|
|
if (msg->machine_id != mm_self->id ||
|
|
|
|
mm_buf_size(&msg->data) > cache->gc_watermark) {
|
2018-09-05 13:04:36 +00:00
|
|
|
cache->count_gc++;
|
|
|
|
mm_buf_free(&msg->data);
|
|
|
|
free(msg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-24 10:38:00 +00:00
|
|
|
mm_list_append(&cache->list, &msg->link);
|
|
|
|
cache->count++;
|
2018-08-30 14:23:14 +00:00
|
|
|
cache->size += mm_buf_size(&msg->data);
|
2017-05-19 11:12:00 +00:00
|
|
|
}
|