mirror of
https://github.com/janet-lang/janet
synced 2024-12-01 12:29:54 +00:00
fix C format
This commit is contained in:
parent
1d50fd9485
commit
6054858359
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2019 Calvin Rose
|
* Copyright (c) 2019 Calvin Rose & contributors
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to
|
* of this software and associated documentation files (the "Software"), to
|
||||||
@ -55,7 +55,7 @@ typedef enum TA_Type {
|
|||||||
|
|
||||||
#define TA_COUNT_TYPES (TA_TYPE_float64 + 1)
|
#define TA_COUNT_TYPES (TA_TYPE_float64 + 1)
|
||||||
|
|
||||||
static char * ta_type_names[]= {
|
static char *ta_type_names[] = {
|
||||||
"uint8",
|
"uint8",
|
||||||
"int8",
|
"int8",
|
||||||
"uint16",
|
"uint16",
|
||||||
@ -68,7 +68,7 @@ static char * ta_type_names[]= {
|
|||||||
"float64",
|
"float64",
|
||||||
};
|
};
|
||||||
|
|
||||||
static size_t ta_type_sizes[]= {
|
static size_t ta_type_sizes[] = {
|
||||||
sizeof(ta_uint8_t),
|
sizeof(ta_uint8_t),
|
||||||
sizeof(ta_int8_t),
|
sizeof(ta_int8_t),
|
||||||
sizeof(ta_uint16_t),
|
sizeof(ta_uint16_t),
|
||||||
@ -82,9 +82,9 @@ static size_t ta_type_sizes[]= {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
static TA_Type get_ta_type_by_name(const uint8_t * name) {
|
static TA_Type get_ta_type_by_name(const uint8_t *name) {
|
||||||
size_t nt=sizeof(ta_type_names)/sizeof(char *);
|
size_t nt = sizeof(ta_type_names) / sizeof(char *);
|
||||||
for (size_t i=0; i<nt; i++) {
|
for (size_t i = 0; i < nt; i++) {
|
||||||
if (!janet_cstrcmp(name, ta_type_names[i]))
|
if (!janet_cstrcmp(name, ta_type_names[i]))
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
@ -94,28 +94,28 @@ static TA_Type get_ta_type_by_name(const uint8_t * name) {
|
|||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
size_t size;
|
size_t size;
|
||||||
uint8_t * data;
|
uint8_t *data;
|
||||||
} TA_Buffer;
|
} TA_Buffer;
|
||||||
|
|
||||||
static TA_Buffer * ta_buffer_init(TA_Buffer * buf,size_t size) {
|
static TA_Buffer *ta_buffer_init(TA_Buffer *buf, size_t size) {
|
||||||
buf->data=(uint8_t *)calloc(size,sizeof(uint8_t));
|
buf->data = (uint8_t *)calloc(size, sizeof(uint8_t));
|
||||||
buf->size=size;
|
buf->size = size;
|
||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int ta_buffer_gc(void *p, size_t s) {
|
static int ta_buffer_gc(void *p, size_t s) {
|
||||||
(void) s;
|
(void) s;
|
||||||
TA_Buffer * buf=(TA_Buffer *)p;
|
TA_Buffer *buf = (TA_Buffer *)p;
|
||||||
free(buf->data);
|
free(buf->data);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static const JanetAbstractType ta_buffer_type= {"ta/buffer",ta_buffer_gc,NULL,NULL,NULL};
|
static const JanetAbstractType ta_buffer_type = {"ta/buffer", ta_buffer_gc, NULL, NULL, NULL};
|
||||||
|
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
TA_Buffer *buffer;
|
TA_Buffer *buffer;
|
||||||
void * data; /* pointer inside buffer->data */
|
void *data; /* pointer inside buffer->data */
|
||||||
size_t size;
|
size_t size;
|
||||||
size_t stride;
|
size_t stride;
|
||||||
TA_Type type;
|
TA_Type type;
|
||||||
@ -124,26 +124,26 @@ typedef struct {
|
|||||||
|
|
||||||
static int ta_mark(void *p, size_t s) {
|
static int ta_mark(void *p, size_t s) {
|
||||||
(void) s;
|
(void) s;
|
||||||
TA_View * view=(TA_View *)p;
|
TA_View *view = (TA_View *)p;
|
||||||
janet_mark(janet_wrap_abstract(view->buffer));
|
janet_mark(janet_wrap_abstract(view->buffer));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#define DEFINE_VIEW_TYPE(type) \
|
#define DEFINE_VIEW_TYPE(type) \
|
||||||
typedef struct { \
|
typedef struct { \
|
||||||
TA_Buffer * buffer; \
|
TA_Buffer * buffer; \
|
||||||
ta_##type##_t * data; \
|
ta_##type##_t * data; \
|
||||||
size_t size; \
|
size_t size; \
|
||||||
size_t stride; \
|
size_t stride; \
|
||||||
TA_Type type; \
|
TA_Type type; \
|
||||||
} TA_View_##type ;
|
} TA_View_##type ;
|
||||||
|
|
||||||
|
|
||||||
#define DEFINE_VIEW_GETTER(type) \
|
#define DEFINE_VIEW_GETTER(type) \
|
||||||
static Janet ta_get_##type(void *p, Janet key) { \
|
static Janet ta_get_##type(void *p, Janet key) { \
|
||||||
Janet value; \
|
Janet value; \
|
||||||
size_t index; \
|
size_t index; \
|
||||||
if (!janet_checkint(key)) \
|
if (!janet_checkint(key)) \
|
||||||
janet_panic("expected integer key"); \
|
janet_panic("expected integer key"); \
|
||||||
index = (size_t)janet_unwrap_integer(key);\
|
index = (size_t)janet_unwrap_integer(key);\
|
||||||
TA_View_##type * array=(TA_View_##type *)p; \
|
TA_View_##type * array=(TA_View_##type *)p; \
|
||||||
@ -173,7 +173,7 @@ void ta_put_##type(void *p, Janet key,Janet value) { \
|
|||||||
#define DEFINE_VIEW_INITIALIZER(type) \
|
#define DEFINE_VIEW_INITIALIZER(type) \
|
||||||
static TA_View * ta_init_##type(TA_View * view,TA_Buffer * buf,size_t size,size_t offset,size_t stride) { \
|
static TA_View * ta_init_##type(TA_View * view,TA_Buffer * buf,size_t size,size_t offset,size_t stride) { \
|
||||||
TA_View_##type * tview=(TA_View_##type *) view; \
|
TA_View_##type * tview=(TA_View_##type *) view; \
|
||||||
size_t buf_size=offset+(size-1)*(sizeof(ta_##type##_t))*stride+1; \
|
size_t buf_size=offset+(size-1)*(sizeof(ta_##type##_t))*stride+1; \
|
||||||
if (buf==NULL) { \
|
if (buf==NULL) { \
|
||||||
buf=(TA_Buffer *)janet_abstract(&ta_buffer_type,sizeof(TA_Buffer)); \
|
buf=(TA_Buffer *)janet_abstract(&ta_buffer_type,sizeof(TA_Buffer)); \
|
||||||
ta_buffer_init(buf,buf_size); \
|
ta_buffer_init(buf,buf_size); \
|
||||||
@ -184,7 +184,7 @@ void ta_put_##type(void *p, Janet key,Janet value) { \
|
|||||||
tview->buffer=buf; \
|
tview->buffer=buf; \
|
||||||
tview->stride=stride; \
|
tview->stride=stride; \
|
||||||
tview->size=size; \
|
tview->size=size; \
|
||||||
tview->data=(ta_##type##_t *)(buf->data+offset); \
|
tview->data=(ta_##type##_t *)(buf->data+offset); \
|
||||||
tview->type=TA_TYPE_##type; \
|
tview->type=TA_TYPE_##type; \
|
||||||
return view; \
|
return view; \
|
||||||
}
|
}
|
||||||
@ -214,7 +214,7 @@ BUILD_TYPE(float64)
|
|||||||
|
|
||||||
#define VIEW_ABSTRACT_DEFINE(type) {"ta/"#type,NULL,ta_mark,ta_get_##type,ta_put_##type}
|
#define VIEW_ABSTRACT_DEFINE(type) {"ta/"#type,NULL,ta_mark,ta_get_##type,ta_put_##type}
|
||||||
|
|
||||||
static const JanetAbstractType ta_array_types[]= {
|
static const JanetAbstractType ta_array_types[] = {
|
||||||
VIEW_ABSTRACT_DEFINE(uint8),
|
VIEW_ABSTRACT_DEFINE(uint8),
|
||||||
VIEW_ABSTRACT_DEFINE(int8),
|
VIEW_ABSTRACT_DEFINE(int8),
|
||||||
VIEW_ABSTRACT_DEFINE(uint16),
|
VIEW_ABSTRACT_DEFINE(uint16),
|
||||||
@ -230,8 +230,8 @@ static const JanetAbstractType ta_array_types[]= {
|
|||||||
static int is_ta_type(Janet x) {
|
static int is_ta_type(Janet x) {
|
||||||
if (janet_checktype(x, JANET_ABSTRACT)) {
|
if (janet_checktype(x, JANET_ABSTRACT)) {
|
||||||
const JanetAbstractType *at = janet_abstract_type(janet_unwrap_abstract(x));
|
const JanetAbstractType *at = janet_abstract_type(janet_unwrap_abstract(x));
|
||||||
for (size_t i=0; i<TA_COUNT_TYPES; i++) {
|
for (size_t i = 0; i < TA_COUNT_TYPES; i++) {
|
||||||
if (at==ta_array_types+i) return 1;
|
if (at == ta_array_types + i) return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
@ -243,39 +243,39 @@ static int is_ta_type(Janet x) {
|
|||||||
#define CASE_TYPE_INITIALIZE(type) case TA_TYPE_##type : ta_init_##type(view,buffer,size,offset,stride); break
|
#define CASE_TYPE_INITIALIZE(type) case TA_TYPE_##type : ta_init_##type(view,buffer,size,offset,stride); break
|
||||||
|
|
||||||
static Janet cfun_typed_array_new(int32_t argc, Janet *argv) {
|
static Janet cfun_typed_array_new(int32_t argc, Janet *argv) {
|
||||||
janet_arity(argc, 2,5 );
|
janet_arity(argc, 2, 5);
|
||||||
size_t offset=0;
|
size_t offset = 0;
|
||||||
size_t stride=1;
|
size_t stride = 1;
|
||||||
TA_Buffer * buffer=NULL;
|
TA_Buffer *buffer = NULL;
|
||||||
const uint8_t *keyw = janet_getkeyword(argv,0);
|
const uint8_t *keyw = janet_getkeyword(argv, 0);
|
||||||
TA_Type type=get_ta_type_by_name(keyw);
|
TA_Type type = get_ta_type_by_name(keyw);
|
||||||
size_t size=(size_t)janet_getinteger(argv,1);
|
size_t size = (size_t)janet_getinteger(argv, 1);
|
||||||
if (argc>2)
|
if (argc > 2)
|
||||||
stride=(size_t)janet_getinteger(argv,2);
|
stride = (size_t)janet_getinteger(argv, 2);
|
||||||
if (argc>3)
|
if (argc > 3)
|
||||||
offset=(size_t)janet_getinteger(argv,3);
|
offset = (size_t)janet_getinteger(argv, 3);
|
||||||
if (argc>4) {
|
if (argc > 4) {
|
||||||
if (is_ta_type(argv[4])) {
|
if (is_ta_type(argv[4])) {
|
||||||
TA_View *view = (TA_View *)janet_unwrap_abstract(argv[4]);
|
TA_View *view = (TA_View *)janet_unwrap_abstract(argv[4]);
|
||||||
offset=(view->buffer->data - (uint8_t *)(view->data))+offset*ta_type_sizes[view->type];
|
offset = (view->buffer->data - (uint8_t *)(view->data)) + offset * ta_type_sizes[view->type];
|
||||||
stride*=view->stride;
|
stride *= view->stride;
|
||||||
buffer=view->buffer;
|
buffer = view->buffer;
|
||||||
} else {
|
} else {
|
||||||
buffer=(TA_Buffer *)janet_getabstract(argv,4,&ta_buffer_type);
|
buffer = (TA_Buffer *)janet_getabstract(argv, 4, &ta_buffer_type);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
TA_View * view=janet_abstract(&ta_array_types[type],sizeof(TA_View));
|
TA_View *view = janet_abstract(&ta_array_types[type], sizeof(TA_View));
|
||||||
switch (type) {
|
switch (type) {
|
||||||
CASE_TYPE_INITIALIZE(uint8);
|
CASE_TYPE_INITIALIZE(uint8);
|
||||||
CASE_TYPE_INITIALIZE(int8);
|
CASE_TYPE_INITIALIZE(int8);
|
||||||
CASE_TYPE_INITIALIZE(uint16);
|
CASE_TYPE_INITIALIZE(uint16);
|
||||||
CASE_TYPE_INITIALIZE(int16);
|
CASE_TYPE_INITIALIZE(int16);
|
||||||
CASE_TYPE_INITIALIZE(uint32);
|
CASE_TYPE_INITIALIZE(uint32);
|
||||||
CASE_TYPE_INITIALIZE(int32);
|
CASE_TYPE_INITIALIZE(int32);
|
||||||
CASE_TYPE_INITIALIZE(uint64);
|
CASE_TYPE_INITIALIZE(uint64);
|
||||||
CASE_TYPE_INITIALIZE(int64);
|
CASE_TYPE_INITIALIZE(int64);
|
||||||
CASE_TYPE_INITIALIZE(float32);
|
CASE_TYPE_INITIALIZE(float32);
|
||||||
CASE_TYPE_INITIALIZE(float64);
|
CASE_TYPE_INITIALIZE(float64);
|
||||||
}
|
}
|
||||||
return janet_wrap_abstract(view);
|
return janet_wrap_abstract(view);
|
||||||
}
|
}
|
||||||
@ -289,9 +289,9 @@ static Janet cfun_typed_array_buffer(int32_t argc, Janet *argv) {
|
|||||||
TA_View *view = (TA_View *)janet_unwrap_abstract(argv[0]);
|
TA_View *view = (TA_View *)janet_unwrap_abstract(argv[0]);
|
||||||
return janet_wrap_abstract(view->buffer);
|
return janet_wrap_abstract(view->buffer);
|
||||||
}
|
}
|
||||||
size_t size=(size_t)janet_getinteger(argv,0);
|
size_t size = (size_t)janet_getinteger(argv, 0);
|
||||||
TA_Buffer * buf=(TA_Buffer *)janet_abstract(&ta_buffer_type,sizeof(TA_Buffer));
|
TA_Buffer *buf = (TA_Buffer *)janet_abstract(&ta_buffer_type, sizeof(TA_Buffer));
|
||||||
ta_buffer_init(buf,size);
|
ta_buffer_init(buf, size);
|
||||||
return janet_wrap_abstract(buf);
|
return janet_wrap_abstract(buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -301,46 +301,50 @@ static Janet cfun_typed_array_size(int32_t argc, Janet *argv) {
|
|||||||
TA_View *view = (TA_View *)janet_unwrap_abstract(argv[0]);
|
TA_View *view = (TA_View *)janet_unwrap_abstract(argv[0]);
|
||||||
return janet_wrap_number(view->size);
|
return janet_wrap_number(view->size);
|
||||||
}
|
}
|
||||||
TA_Buffer * buf=(TA_Buffer *)janet_getabstract(argv,0,&ta_buffer_type);
|
TA_Buffer *buf = (TA_Buffer *)janet_getabstract(argv, 0, &ta_buffer_type);
|
||||||
return janet_wrap_number(buf->size);
|
return janet_wrap_number(buf->size);
|
||||||
}
|
}
|
||||||
|
|
||||||
static Janet cfun_typed_array_properties(int32_t argc, Janet *argv) {
|
static Janet cfun_typed_array_properties(int32_t argc, Janet *argv) {
|
||||||
janet_fixarity(argc, 1);
|
janet_fixarity(argc, 1);
|
||||||
if (!is_ta_type(argv[0]))
|
if (!is_ta_type(argv[0]))
|
||||||
janet_panic("expected typed array");
|
janet_panic("expected typed array");
|
||||||
TA_View *view = (TA_View *)janet_unwrap_abstract(argv[0]);
|
TA_View *view = (TA_View *)janet_unwrap_abstract(argv[0]);
|
||||||
JanetKV * props=janet_struct_begin(6);
|
JanetKV *props = janet_struct_begin(6);
|
||||||
janet_struct_put(props,janet_ckeywordv("size"),janet_wrap_number(view->size));
|
janet_struct_put(props, janet_ckeywordv("size"), janet_wrap_number(view->size));
|
||||||
janet_struct_put(props,janet_ckeywordv("byte-offset"),janet_wrap_number((uint8_t *)(view->data)-view->buffer->data));
|
janet_struct_put(props, janet_ckeywordv("byte-offset"), janet_wrap_number((uint8_t *)(view->data) - view->buffer->data));
|
||||||
janet_struct_put(props,janet_ckeywordv("stride"),janet_wrap_number(view->stride));
|
janet_struct_put(props, janet_ckeywordv("stride"), janet_wrap_number(view->stride));
|
||||||
janet_struct_put(props,janet_ckeywordv("type"),janet_ckeywordv(ta_type_names[view->type]));
|
janet_struct_put(props, janet_ckeywordv("type"), janet_ckeywordv(ta_type_names[view->type]));
|
||||||
janet_struct_put(props,janet_ckeywordv("type-size"),janet_wrap_number(ta_type_sizes[view->type]));
|
janet_struct_put(props, janet_ckeywordv("type-size"), janet_wrap_number(ta_type_sizes[view->type]));
|
||||||
janet_struct_put(props,janet_ckeywordv("buffer"),janet_wrap_abstract(view->buffer));
|
janet_struct_put(props, janet_ckeywordv("buffer"), janet_wrap_abstract(view->buffer));
|
||||||
return janet_wrap_struct(janet_struct_end(props));
|
return janet_wrap_struct(janet_struct_end(props));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static const JanetReg ta_cfuns[] = {
|
static const JanetReg ta_cfuns[] = {
|
||||||
{ "tarray/new", cfun_typed_array_new,
|
{
|
||||||
|
"tarray/new", cfun_typed_array_new,
|
||||||
JDOC("(tarray/new type size [stride = 1 [offset = 0 [tarray | buffer]]] )\n\n"
|
JDOC("(tarray/new type size [stride = 1 [offset = 0 [tarray | buffer]]] )\n\n"
|
||||||
"Create new typed array")
|
"Create new typed array")
|
||||||
},
|
},
|
||||||
{ "tarray/buffer", cfun_typed_array_buffer,
|
{
|
||||||
|
"tarray/buffer", cfun_typed_array_buffer,
|
||||||
JDOC("(tarray/buffer (array | size) )\n\n"
|
JDOC("(tarray/buffer (array | size) )\n\n"
|
||||||
"return typed array buffer or create a new buffer ")
|
"return typed array buffer or create a new buffer ")
|
||||||
},
|
},
|
||||||
{ "tarray/length", cfun_typed_array_size,
|
{
|
||||||
|
"tarray/length", cfun_typed_array_size,
|
||||||
JDOC("(tarray/length (array | buffer) )\n\n"
|
JDOC("(tarray/length (array | buffer) )\n\n"
|
||||||
"return typed array or buffer size ")
|
"return typed array or buffer size ")
|
||||||
},
|
},
|
||||||
{ "tarray/properties", cfun_typed_array_properties,
|
{
|
||||||
|
"tarray/properties", cfun_typed_array_properties,
|
||||||
JDOC("(tarray/properties array )\n\n"
|
JDOC("(tarray/properties array )\n\n"
|
||||||
"return typed array properties as a struct")
|
"return typed array properties as a struct")
|
||||||
},
|
},
|
||||||
|
|
||||||
{NULL,NULL,NULL}
|
{NULL, NULL, NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user