odyssey/third_party/machinarium/sources/msg.c

74 lines
1.3 KiB
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(void)
2017-05-19 11:17:58 +00:00
{
mm_errno_set(0);
mm_msg_t *msg = mm_msgcache_pop(&machinarium.msg_cache);
if (msg == NULL) {
mm_errno_set(ENOMEM);
2017-05-19 11:17:58 +00:00
return NULL;
}
msg->type = 0;
return (machine_msg_t*)msg;
2017-05-19 11:17:58 +00:00
}
MACHINE_API void
machine_msg_free(machine_msg_t *obj)
2017-05-19 11:17:58 +00:00
{
mm_msg_t *msg = mm_cast(mm_msg_t*, obj);
mm_msgcache_push(&machinarium.msg_cache, msg);
2017-05-19 11:17:58 +00:00
}
MACHINE_API void
machine_msg_set_type(machine_msg_t *obj, int type)
2017-05-19 11:17:58 +00:00
{
mm_msg_t *msg = mm_cast(mm_msg_t*, obj);
msg->type = type;
2017-05-19 11:17:58 +00:00
}
MACHINE_API int
machine_msg_get_type(machine_msg_t *obj)
2017-05-19 11:17:58 +00:00
{
mm_msg_t *msg = mm_cast(mm_msg_t*, obj);
2017-05-19 11:17:58 +00:00
return msg->type;
}
MACHINE_API void*
machine_msg_get_data(machine_msg_t *obj)
{
mm_msg_t *msg = mm_cast(mm_msg_t*, obj);
return msg->data.start;
}
MACHINE_API int
machine_msg_get_size(machine_msg_t *obj)
{
mm_msg_t *msg = mm_cast(mm_msg_t*, obj);
return mm_buf_used(&msg->data);
}
MACHINE_API int
machine_msg_write(machine_msg_t *obj, char *buf, int size)
{
mm_msg_t *msg = mm_cast(mm_msg_t*, obj);
int rc;
if (buf == NULL)
rc = mm_buf_ensure(&msg->data, size);
else
rc = mm_buf_add(&msg->data, buf, size);
if (rc == -1) {
mm_errno_set(ENOMEM);
return -1;
}
return 0;
}