odyssey/src/mm_msg.c

50 lines
864 B
C
Raw Normal View History

2017-05-19 11:17:58 +00:00
/*
* machinarium.
*
* cooperative multitasking engine.
*/
#include <machinarium.h>
#include <machinarium_private.h>
MACHINE_API machine_msg_t
machine_msg_create(int type, int data_size)
2017-05-19 11:17:58 +00:00
{
mm_msg_t *msg = mm_msgcache_pop(&machinarium.msg_cache);
2017-05-19 11:17:58 +00:00
if (msg == NULL)
return NULL;
msg->type = type;
if (data_size > 0) {
int rc;
rc = mm_buf_ensure(&msg->data, data_size);
if (rc == -1) {
mm_msg_unref(&machinarium.msg_cache, msg);
return NULL;
}
mm_buf_advance(&msg->data, data_size);
}
2017-05-19 11:17:58 +00:00
return msg;
}
MACHINE_API void
machine_msg_free(machine_msg_t obj)
{
mm_msg_t *msg = obj;
mm_msgcache_push(&machinarium.msg_cache, msg);
2017-05-19 11:17:58 +00:00
}
MACHINE_API void*
machine_msg_get_data(machine_msg_t obj)
{
mm_msg_t *msg = obj;
return msg->data.start;
2017-05-19 11:17:58 +00:00
}
MACHINE_API int
machine_msg_get_type(machine_msg_t obj)
{
mm_msg_t *msg = obj;
return msg->type;
}