2017-05-19 11:17:58 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* machinarium.
|
|
|
|
*
|
|
|
|
* cooperative multitasking engine.
|
2020-04-02 11:00:56 +00:00
|
|
|
*/
|
2017-05-19 11:17:58 +00:00
|
|
|
|
|
|
|
#include <machinarium.h>
|
|
|
|
#include <machinarium_private.h>
|
|
|
|
|
2020-12-28 10:43:31 +00:00
|
|
|
MACHINE_API machine_msg_t *machine_msg_create(int reserve)
|
2017-05-19 11:17:58 +00:00
|
|
|
{
|
2018-11-19 14:52:37 +00:00
|
|
|
mm_msg_t *msg = mm_msgcache_pop(&mm_self->msg_cache);
|
2020-11-23 09:13:28 +00:00
|
|
|
if (msg == NULL) {
|
2017-05-19 11:17:58 +00:00
|
|
|
return NULL;
|
2020-11-23 09:13:28 +00:00
|
|
|
}
|
|
|
|
|
2018-08-22 12:32:57 +00:00
|
|
|
msg->type = 0;
|
2018-08-23 12:12:40 +00:00
|
|
|
if (reserve > 0) {
|
|
|
|
int rc;
|
|
|
|
rc = mm_buf_ensure(&msg->data, reserve);
|
|
|
|
if (rc == -1) {
|
2018-11-19 14:52:37 +00:00
|
|
|
mm_msg_unref(&mm_self->msg_cache, msg);
|
2018-08-23 12:12:40 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
mm_buf_advance(&msg->data, reserve);
|
|
|
|
}
|
2020-04-02 11:00:56 +00:00
|
|
|
return (machine_msg_t *)msg;
|
2017-05-19 11:17:58 +00:00
|
|
|
}
|
|
|
|
|
2020-12-28 10:43:31 +00:00
|
|
|
MACHINE_API machine_msg_t *machine_msg_create_or_advance(machine_msg_t *obj,
|
|
|
|
int size)
|
2019-01-23 15:43:52 +00:00
|
|
|
{
|
2020-11-23 09:13:28 +00:00
|
|
|
if (obj == NULL) {
|
2019-01-23 15:43:52 +00:00
|
|
|
return machine_msg_create(size);
|
2020-11-23 09:13:28 +00:00
|
|
|
}
|
|
|
|
|
2020-04-02 11:00:56 +00:00
|
|
|
mm_msg_t *msg = mm_cast(mm_msg_t *, obj);
|
2019-01-23 15:43:52 +00:00
|
|
|
int rc;
|
|
|
|
rc = mm_buf_ensure(&msg->data, size);
|
2020-11-23 09:13:28 +00:00
|
|
|
if (rc == -1) {
|
2019-01-23 15:43:52 +00:00
|
|
|
return NULL;
|
2020-11-23 09:13:28 +00:00
|
|
|
}
|
|
|
|
|
2019-01-23 15:43:52 +00:00
|
|
|
mm_buf_advance(&msg->data, size);
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2020-12-28 10:43:31 +00:00
|
|
|
MACHINE_API inline void machine_msg_free(machine_msg_t *obj)
|
2017-05-19 11:17:58 +00:00
|
|
|
{
|
2020-04-02 11:00:56 +00:00
|
|
|
mm_msg_t *msg = mm_cast(mm_msg_t *, obj);
|
2018-11-19 14:52:37 +00:00
|
|
|
mm_msgcache_push(&mm_self->msg_cache, msg);
|
2017-05-19 11:17:58 +00:00
|
|
|
}
|
|
|
|
|
2020-12-28 10:43:31 +00:00
|
|
|
MACHINE_API inline void machine_msg_set_type(machine_msg_t *obj, int type)
|
2017-05-19 11:17:58 +00:00
|
|
|
{
|
2020-04-02 11:00:56 +00:00
|
|
|
mm_msg_t *msg = mm_cast(mm_msg_t *, obj);
|
2020-12-28 10:43:31 +00:00
|
|
|
msg->type = type;
|
2017-05-19 11:17:58 +00:00
|
|
|
}
|
|
|
|
|
2020-12-28 10:43:31 +00:00
|
|
|
MACHINE_API inline int machine_msg_type(machine_msg_t *obj)
|
2017-05-19 11:17:58 +00:00
|
|
|
{
|
2020-04-02 11:00:56 +00:00
|
|
|
mm_msg_t *msg = mm_cast(mm_msg_t *, obj);
|
2017-05-19 11:17:58 +00:00
|
|
|
return msg->type;
|
|
|
|
}
|
2018-08-22 12:19:06 +00:00
|
|
|
|
2020-12-28 10:43:31 +00:00
|
|
|
MACHINE_API inline void *machine_msg_data(machine_msg_t *obj)
|
2018-08-22 12:32:57 +00:00
|
|
|
{
|
2020-04-02 11:00:56 +00:00
|
|
|
mm_msg_t *msg = mm_cast(mm_msg_t *, obj);
|
2018-08-22 12:32:57 +00:00
|
|
|
return msg->data.start;
|
|
|
|
}
|
|
|
|
|
2020-12-28 10:43:31 +00:00
|
|
|
MACHINE_API int machine_msg_size(machine_msg_t *obj)
|
2018-08-22 13:36:28 +00:00
|
|
|
{
|
2020-04-02 11:00:56 +00:00
|
|
|
mm_msg_t *msg = mm_cast(mm_msg_t *, obj);
|
2018-08-22 13:36:28 +00:00
|
|
|
return mm_buf_used(&msg->data);
|
|
|
|
}
|
|
|
|
|
2020-12-28 10:43:31 +00:00
|
|
|
MACHINE_API int machine_msg_write(machine_msg_t *obj, void *buf, int size)
|
2018-08-22 12:19:06 +00:00
|
|
|
{
|
2020-04-02 11:00:56 +00:00
|
|
|
mm_msg_t *msg = mm_cast(mm_msg_t *, obj);
|
2018-08-22 14:32:41 +00:00
|
|
|
int rc;
|
2018-08-23 09:27:19 +00:00
|
|
|
if (buf == NULL) {
|
2018-08-22 14:32:41 +00:00
|
|
|
rc = mm_buf_ensure(&msg->data, size);
|
2018-08-23 09:27:19 +00:00
|
|
|
if (rc == -1)
|
|
|
|
return -1;
|
|
|
|
mm_buf_advance(&msg->data, size);
|
|
|
|
return 0;
|
2018-08-22 12:32:57 +00:00
|
|
|
}
|
2018-08-23 09:27:19 +00:00
|
|
|
rc = mm_buf_add(&msg->data, buf, size);
|
|
|
|
return rc;
|
2018-08-22 12:19:06 +00:00
|
|
|
}
|