1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-25 17:57:17 +00:00

Fix memory issue in allocating decode buffer.

Since the decode table is currently a single table
per thread, we just make it a thread local to avoid
issues.
This commit is contained in:
Calvin Rose 2020-01-15 19:58:14 -06:00
parent b567ece401
commit bc2bac8cd3
2 changed files with 15 additions and 7 deletions

View File

@ -51,9 +51,6 @@ struct JanetMailbox {
pthread_cond_t cond; pthread_cond_t cond;
#endif #endif
/* Receiving messages - (only by owner thread) */
JanetTable *decode;
/* Setup procedure - requires a parent mailbox /* Setup procedure - requires a parent mailbox
* to receive thunk from */ * to receive thunk from */
JanetMailbox *parent; JanetMailbox *parent;
@ -75,6 +72,15 @@ struct JanetMailbox {
static JANET_THREAD_LOCAL JanetMailbox *janet_vm_mailbox = NULL; static JANET_THREAD_LOCAL JanetMailbox *janet_vm_mailbox = NULL;
static JANET_THREAD_LOCAL JanetThread *janet_vm_thread_current = NULL; static JANET_THREAD_LOCAL JanetThread *janet_vm_thread_current = NULL;
static JANET_THREAD_LOCAL JanetTable *janet_vm_thread_decode = NULL;
static JanetTable *janet_thread_get_decode(void) {
if (janet_vm_thread_decode == NULL) {
janet_vm_thread_decode = janet_get_core_table("load-image-dict");
janet_gcroot(janet_wrap_table(janet_vm_thread_decode));
}
return janet_vm_thread_decode;
}
static JanetMailbox *janet_mailbox_create(JanetMailbox *parent, int refCount, uint16_t capacity) { static JanetMailbox *janet_mailbox_create(JanetMailbox *parent, int refCount, uint16_t capacity) {
JanetMailbox *mailbox = malloc(sizeof(JanetMailbox) + sizeof(JanetBuffer) * (size_t) capacity); JanetMailbox *mailbox = malloc(sizeof(JanetMailbox) + sizeof(JanetBuffer) * (size_t) capacity);
@ -356,7 +362,7 @@ int janet_thread_receive(Janet *msg_out, double timeout) {
const uint8_t *nextItem = NULL; const uint8_t *nextItem = NULL;
Janet item = janet_unmarshal( Janet item = janet_unmarshal(
msgbuf->data, msgbuf->count, msgbuf->data, msgbuf->count,
0, mailbox->decode, &nextItem); 0, janet_thread_get_decode(), &nextItem);
*msg_out = item; *msg_out = item;
/* Cleanup */ /* Cleanup */
@ -423,7 +429,6 @@ static int thread_worker(JanetMailbox *mailbox) {
/* Get dictionaries for default encode/decode */ /* Get dictionaries for default encode/decode */
JanetTable *encode = janet_get_core_table("make-image-dict"); JanetTable *encode = janet_get_core_table("make-image-dict");
mailbox->decode = janet_get_core_table("load-image-dict");
/* Create parent thread */ /* Create parent thread */
JanetThread *parent = janet_make_thread(mailbox->parent, encode); JanetThread *parent = janet_make_thread(mailbox->parent, encode);
@ -507,6 +512,8 @@ void janet_threads_init(void) {
if (NULL == janet_vm_mailbox) { if (NULL == janet_vm_mailbox) {
janet_vm_mailbox = janet_mailbox_create(NULL, 1, 10); janet_vm_mailbox = janet_mailbox_create(NULL, 1, 10);
} }
janet_vm_thread_decode = NULL;
janet_vm_thread_current = NULL;
} }
void janet_threads_deinit(void) { void janet_threads_deinit(void) {
@ -515,6 +522,7 @@ void janet_threads_deinit(void) {
janet_mailbox_ref_with_lock(janet_vm_mailbox, -1); janet_mailbox_ref_with_lock(janet_vm_mailbox, -1);
janet_vm_mailbox = NULL; janet_vm_mailbox = NULL;
janet_vm_thread_current = NULL; janet_vm_thread_current = NULL;
janet_vm_thread_decode = NULL;
} }
/* /*

View File

@ -284,7 +284,7 @@ void janet_cfuns(JanetTable *env, const char *regprefix, const JanetReg *cfuns)
while (cfuns->name[nmlen]) nmlen++; while (cfuns->name[nmlen]) nmlen++;
int32_t totallen = (int32_t) prefixlen + nmlen; int32_t totallen = (int32_t) prefixlen + nmlen;
if ((size_t) totallen > bufsize) { if ((size_t) totallen > bufsize) {
bufsize = (size_t) (totallen) + 128; bufsize = (size_t)(totallen) + 128;
longname_buffer = realloc(longname_buffer, bufsize); longname_buffer = realloc(longname_buffer, bufsize);
if (NULL == longname_buffer) { if (NULL == longname_buffer) {
JANET_OUT_OF_MEMORY; JANET_OUT_OF_MEMORY;
@ -324,7 +324,7 @@ typedef struct {
void janet_register_abstract_type(const JanetAbstractType *at) { void janet_register_abstract_type(const JanetAbstractType *at) {
JanetAbstractTypeWrap *abstract = (JanetAbstractTypeWrap *) JanetAbstractTypeWrap *abstract = (JanetAbstractTypeWrap *)
janet_abstract(&type_wrap, sizeof(JanetAbstractTypeWrap)); janet_abstract(&type_wrap, sizeof(JanetAbstractTypeWrap));
abstract->at = at; abstract->at = at;
Janet sym = janet_csymbolv(at->name); Janet sym = janet_csymbolv(at->name);
if (!(janet_checktype(janet_table_get(janet_vm_registry, sym), JANET_NIL))) { if (!(janet_checktype(janet_table_get(janet_vm_registry, sym), JANET_NIL))) {