2017-04-18 02:40:39 +00:00
|
|
|
/*
|
2021-05-31 18:46:02 +00:00
|
|
|
* Copyright (c) 2021 Calvin Rose
|
2017-07-02 01:51:16 +00:00
|
|
|
*
|
2017-04-18 02:40:39 +00:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to
|
|
|
|
* deal in the Software without restriction, including without limitation the
|
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
2017-07-02 01:51:16 +00:00
|
|
|
*
|
2017-04-18 02:40:39 +00:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
2017-07-02 01:51:16 +00:00
|
|
|
*
|
2017-04-18 02:40:39 +00:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
* IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2019-01-24 05:15:58 +00:00
|
|
|
#ifndef JANET_AMALG
|
2019-12-31 00:06:15 +00:00
|
|
|
#include "features.h"
|
2019-02-19 01:13:35 +00:00
|
|
|
#include <janet.h>
|
2017-12-21 04:03:34 +00:00
|
|
|
#include "gc.h"
|
2021-08-20 01:56:48 +00:00
|
|
|
#include "state.h"
|
|
|
|
#ifdef JANET_EV
|
|
|
|
#ifdef JANET_WINDOWS
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
|
|
|
#endif
|
2019-01-24 05:15:58 +00:00
|
|
|
#endif
|
2017-04-15 20:05:59 +00:00
|
|
|
|
2017-11-01 21:53:43 +00:00
|
|
|
/* Create new userdata */
|
2019-06-20 16:37:57 +00:00
|
|
|
void *janet_abstract_begin(const JanetAbstractType *atype, size_t size) {
|
|
|
|
JanetAbstractHead *header = janet_gcalloc(JANET_MEMORY_NONE,
|
2019-02-22 15:10:41 +00:00
|
|
|
sizeof(JanetAbstractHead) + size);
|
2017-11-01 21:53:43 +00:00
|
|
|
header->size = size;
|
2018-01-04 02:36:10 +00:00
|
|
|
header->type = atype;
|
2019-02-21 16:22:29 +00:00
|
|
|
return (void *) & (header->data);
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
2019-06-20 16:37:57 +00:00
|
|
|
|
|
|
|
void *janet_abstract_end(void *x) {
|
2019-06-30 15:32:52 +00:00
|
|
|
janet_gc_settype((void *)(janet_abstract_head(x)), JANET_MEMORY_ABSTRACT);
|
2019-06-20 16:37:57 +00:00
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *janet_abstract(const JanetAbstractType *atype, size_t size) {
|
|
|
|
return janet_abstract_end(janet_abstract_begin(atype, size));
|
|
|
|
}
|
2021-08-20 01:56:48 +00:00
|
|
|
|
|
|
|
#ifdef JANET_EV
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Threaded abstracts
|
|
|
|
*/
|
|
|
|
|
|
|
|
void *janet_abstract_begin_threaded(const JanetAbstractType *atype, size_t size) {
|
|
|
|
JanetAbstractHead *header = janet_malloc(sizeof(JanetAbstractHead) + size);
|
|
|
|
if (NULL == header) {
|
|
|
|
JANET_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
janet_vm.next_collection += size + sizeof(JanetAbstractHead);
|
|
|
|
header->gc.flags = JANET_MEMORY_THREADED_ABSTRACT;
|
2021-08-20 02:16:20 +00:00
|
|
|
header->gc.next = NULL; /* Clear memory for address sanitizers */
|
2021-08-20 01:56:48 +00:00
|
|
|
header->gc.refcount = 1;
|
|
|
|
header->size = size;
|
|
|
|
header->type = atype;
|
|
|
|
void *abstract = (void *) & (header->data);
|
|
|
|
janet_table_put(&janet_vm.threaded_abstracts, janet_wrap_abstract(abstract), janet_wrap_false());
|
|
|
|
return abstract;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *janet_abstract_end_threaded(void *x) {
|
|
|
|
janet_gc_settype((void *)(janet_abstract_head(x)), JANET_MEMORY_THREADED_ABSTRACT);
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *janet_abstract_threaded(const JanetAbstractType *atype, size_t size) {
|
|
|
|
return janet_abstract_end_threaded(janet_abstract_begin_threaded(atype, size));
|
|
|
|
}
|
|
|
|
|
2021-08-20 22:57:23 +00:00
|
|
|
/* Refcounting primitives and sync primitives */
|
2021-08-20 01:56:48 +00:00
|
|
|
|
|
|
|
#ifdef JANET_WINDOWS
|
|
|
|
|
|
|
|
static int32_t janet_incref(JanetAbstractHead *ab) {
|
|
|
|
return InterlockedIncrement(&ab->gc.refcount);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int32_t janet_decref(JanetAbstractHead *ab) {
|
|
|
|
return InterlockedDecrement(&ab->gc.refcount);
|
|
|
|
}
|
|
|
|
|
2021-08-20 22:57:23 +00:00
|
|
|
void janet_os_mutex_init(JanetOSMutex *mutex) {
|
|
|
|
InitializeCriticalSection(mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void janet_os_mutex_deinit(JanetOSMutex *mutex) {
|
|
|
|
DeleteCriticalSection(mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void janet_os_mutex_lock(JanetOSMutex *mutex) {
|
|
|
|
EnterCriticalSection(mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void janet_os_mutex_unlock(JanetOSMutex *mutex) {
|
|
|
|
LeaveCriticalSection(mutex);
|
|
|
|
}
|
|
|
|
|
2021-08-20 01:56:48 +00:00
|
|
|
#else
|
|
|
|
|
|
|
|
static int32_t janet_incref(JanetAbstractHead *ab) {
|
|
|
|
return __atomic_add_fetch(&ab->gc.refcount, 1, __ATOMIC_RELAXED);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int32_t janet_decref(JanetAbstractHead *ab) {
|
|
|
|
return __atomic_add_fetch(&ab->gc.refcount, -1, __ATOMIC_RELAXED);
|
|
|
|
}
|
|
|
|
|
2021-08-20 22:57:23 +00:00
|
|
|
void janet_os_mutex_init(JanetOSMutex *mutex) {
|
|
|
|
pthread_mutex_init(mutex, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void janet_os_mutex_deinit(JanetOSMutex *mutex) {
|
|
|
|
pthread_mutex_destroy(mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void janet_os_mutex_lock(JanetOSMutex *mutex) {
|
|
|
|
pthread_mutex_lock(mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void janet_os_mutex_unlock(JanetOSMutex *mutex) {
|
|
|
|
pthread_mutex_unlock(mutex);
|
|
|
|
}
|
|
|
|
|
2021-08-20 01:56:48 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
int32_t janet_abstract_incref(void *abst) {
|
|
|
|
return janet_incref(janet_abstract_head(abst));
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t janet_abstract_decref(void *abst) {
|
|
|
|
return janet_decref(janet_abstract_head(abst));
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|