1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-29 11:29:54 +00:00

fix C format

This commit is contained in:
J.-F. Cap 2019-02-24 22:36:35 +01:00
parent c81bf42f6b
commit 632b920e97
3 changed files with 52 additions and 51 deletions

View File

@ -926,7 +926,7 @@ void janet_unmarshal_byte(JanetMarshalContext *ctx, uint8_t *b) {
void janet_unmarshal_bytes(JanetMarshalContext *ctx, uint8_t *dest, int32_t len) { void janet_unmarshal_bytes(JanetMarshalContext *ctx, uint8_t *dest, int32_t len) {
UnmarshalState *st = (UnmarshalState *)(ctx->u_state); UnmarshalState *st = (UnmarshalState *)(ctx->u_state);
MARSH_EOS(st, ctx->data+len-1); MARSH_EOS(st, ctx->data + len - 1);
memcpy(dest, ctx->data, len); memcpy(dest, ctx->data, len);
ctx->data += len; ctx->data += len;
} }

View File

@ -292,20 +292,20 @@ static int is_ta_anytype(Janet x) {
} }
static int is_ta_type(Janet x, JanetTArrayType type) { static int is_ta_type(Janet x, JanetTArrayType type) {
return (janet_checktype(x, JANET_ABSTRACT) && (type < TA_COUNT_TYPES) && return (janet_checktype(x, JANET_ABSTRACT) && (type < TA_COUNT_TYPES) &&
(janet_abstract_type(janet_unwrap_abstract(x))== &ta_array_types[type])) ? 1 : 0; (janet_abstract_type(janet_unwrap_abstract(x)) == &ta_array_types[type])) ? 1 : 0;
} }
#define CASE_TYPE_INITIALIZE(type) case JANET_TARRAY_TYPE_##type : ta_init_##type(view,buffer,size,offset,stride); break #define CASE_TYPE_INITIALIZE(type) case JANET_TARRAY_TYPE_##type : ta_init_##type(view,buffer,size,offset,stride); break
JanetTArrayBuffer * janet_tarray_buffer(size_t size) { JanetTArrayBuffer *janet_tarray_buffer(size_t size) {
JanetTArrayBuffer *buf = (JanetTArrayBuffer *)janet_abstract(&ta_buffer_type, sizeof(JanetTArrayBuffer)); JanetTArrayBuffer *buf = (JanetTArrayBuffer *)janet_abstract(&ta_buffer_type, sizeof(JanetTArrayBuffer));
ta_buffer_init(buf, size); ta_buffer_init(buf, size);
return buf; return buf;
} }
JanetTArrayView * janet_tarray_view(JanetTArrayType type,size_t size,size_t stride,size_t offset,JanetTArrayBuffer *buffer) { JanetTArrayView *janet_tarray_view(JanetTArrayType type, size_t size, size_t stride, size_t offset, JanetTArrayBuffer *buffer) {
JanetTArrayView *view = janet_abstract(&ta_array_types[type], sizeof(JanetTArrayView)); JanetTArrayView *view = janet_abstract(&ta_array_types[type], sizeof(JanetTArrayView));
switch (type) { switch (type) {
CASE_TYPE_INITIALIZE(uint8); CASE_TYPE_INITIALIZE(uint8);
@ -318,7 +318,8 @@ JanetTArrayView * janet_tarray_view(JanetTArrayType type,size_t size,size_t stri
CASE_TYPE_INITIALIZE(int64); CASE_TYPE_INITIALIZE(int64);
CASE_TYPE_INITIALIZE(float32); CASE_TYPE_INITIALIZE(float32);
CASE_TYPE_INITIALIZE(float64); CASE_TYPE_INITIALIZE(float64);
default : janet_panic("bad typed array type"); default :
janet_panic("bad typed array type");
} }
return view; return view;
@ -327,25 +328,25 @@ JanetTArrayView * janet_tarray_view(JanetTArrayType type,size_t size,size_t stri
#undef CASE_TYPE_INITIALIZE #undef CASE_TYPE_INITIALIZE
JanetTArrayBuffer *janet_gettarray_buffer(const Janet *argv, int32_t n) { JanetTArrayBuffer *janet_gettarray_buffer(const Janet *argv, int32_t n) {
return (JanetTArrayBuffer *)janet_getabstract(argv, n, &ta_buffer_type); return (JanetTArrayBuffer *)janet_getabstract(argv, n, &ta_buffer_type);
} }
int janet_is_tarray_view(Janet x,JanetTArrayType type) { int janet_is_tarray_view(Janet x, JanetTArrayType type) {
return (type==JANET_TARRAY_TYPE_any) ? is_ta_anytype(x) : is_ta_type(x,type); return (type == JANET_TARRAY_TYPE_any) ? is_ta_anytype(x) : is_ta_type(x, type);
} }
int janet_tarray_type_size(JanetTArrayType type) { int janet_tarray_type_size(JanetTArrayType type) {
return (type<TA_COUNT_TYPES)? ta_type_sizes[type] : 0 ; return (type < TA_COUNT_TYPES) ? ta_type_sizes[type] : 0 ;
} }
JanetTArrayView * janet_gettarray_view(const Janet *argv, int32_t n,JanetTArrayType type) { JanetTArrayView *janet_gettarray_view(const Janet *argv, int32_t n, JanetTArrayType type) {
if (janet_is_tarray_view(argv[n],type)) { if (janet_is_tarray_view(argv[n], type)) {
return (JanetTArrayView *)janet_unwrap_abstract(argv[n]); return (JanetTArrayView *)janet_unwrap_abstract(argv[n]);
} else { } else {
janet_panicf("bad slot #%d, expected typed array of type %s, got %v", janet_panicf("bad slot #%d, expected typed array of type %s, got %v",
n, (type <= JANET_TARRAY_TYPE_any) ? ta_type_names[type] : "?" , argv[n]); n, (type <= JANET_TARRAY_TYPE_any) ? ta_type_names[type] : "?", argv[n]);
return NULL; return NULL;
} }
} }
@ -374,7 +375,7 @@ static Janet cfun_typed_array_new(int32_t argc, Janet *argv) {
buffer = (JanetTArrayBuffer *)janet_getabstract(argv, 4, &ta_buffer_type); buffer = (JanetTArrayBuffer *)janet_getabstract(argv, 4, &ta_buffer_type);
} }
} }
JanetTArrayView * view =janet_tarray_view(type,size,stride,offset,buffer); JanetTArrayView *view = janet_tarray_view(type, size, stride, offset, buffer);
return janet_wrap_abstract(view); return janet_wrap_abstract(view);
} }
@ -432,9 +433,9 @@ static Janet cfun_abstract_properties(int32_t argc, Janet *argv) {
static Janet cfun_typed_array_copy_bytes(int32_t argc, Janet *argv) { static Janet cfun_typed_array_copy_bytes(int32_t argc, Janet *argv) {
janet_arity(argc, 4, 5); janet_arity(argc, 4, 5);
JanetTArrayView *src = janet_gettarray_view(argv,0,JANET_TARRAY_TYPE_any); JanetTArrayView *src = janet_gettarray_view(argv, 0, JANET_TARRAY_TYPE_any);
size_t index_src = (size_t)janet_getinteger(argv, 1); size_t index_src = (size_t)janet_getinteger(argv, 1);
JanetTArrayView *dst = janet_gettarray_view(argv,2,JANET_TARRAY_TYPE_any); JanetTArrayView *dst = janet_gettarray_view(argv, 2, JANET_TARRAY_TYPE_any);
size_t index_dst = (size_t)janet_getinteger(argv, 3); size_t index_dst = (size_t)janet_getinteger(argv, 3);
size_t count = (argc == 5) ? (size_t)janet_getinteger(argv, 4) : 1; size_t count = (argc == 5) ? (size_t)janet_getinteger(argv, 4) : 1;
size_t src_atom_size = ta_type_sizes[src->type]; size_t src_atom_size = ta_type_sizes[src->type];
@ -445,23 +446,23 @@ static Janet cfun_typed_array_copy_bytes(int32_t argc, Janet *argv) {
size_t pos_dst = ((uint8_t *)(dst->data) - dst->buffer->data) + (index_dst * step_dst); size_t pos_dst = ((uint8_t *)(dst->data) - dst->buffer->data) + (index_dst * step_dst);
uint8_t *ps = src->buffer->data + pos_src, * pd = dst->buffer->data + pos_dst; uint8_t *ps = src->buffer->data + pos_src, * pd = dst->buffer->data + pos_dst;
if ((pos_dst + (count - 1)*step_dst + src_atom_size <= dst->buffer->size) && if ((pos_dst + (count - 1)*step_dst + src_atom_size <= dst->buffer->size) &&
(pos_src + (count - 1)*step_src + src_atom_size <= src->buffer->size)) { (pos_src + (count - 1)*step_src + src_atom_size <= src->buffer->size)) {
for (size_t i = 0; i < count; i++) { for (size_t i = 0; i < count; i++) {
memmove(pd, ps, src_atom_size); memmove(pd, ps, src_atom_size);
pd += step_dst; pd += step_dst;
ps += step_src; ps += step_src;
} }
} else { } else {
janet_panic("typed array copy out of bound"); janet_panic("typed array copy out of bound");
} }
return janet_wrap_nil(); return janet_wrap_nil();
} }
static Janet cfun_typed_array_swap_bytes(int32_t argc, Janet *argv) { static Janet cfun_typed_array_swap_bytes(int32_t argc, Janet *argv) {
janet_arity(argc, 4, 5); janet_arity(argc, 4, 5);
JanetTArrayView *src = janet_gettarray_view(argv,0,JANET_TARRAY_TYPE_any); JanetTArrayView *src = janet_gettarray_view(argv, 0, JANET_TARRAY_TYPE_any);
size_t index_src = (size_t)janet_getinteger(argv, 1); size_t index_src = (size_t)janet_getinteger(argv, 1);
JanetTArrayView *dst = janet_gettarray_view(argv,2,JANET_TARRAY_TYPE_any); JanetTArrayView *dst = janet_gettarray_view(argv, 2, JANET_TARRAY_TYPE_any);
size_t index_dst = (size_t)janet_getinteger(argv, 3); size_t index_dst = (size_t)janet_getinteger(argv, 3);
size_t count = (argc == 5) ? (size_t)janet_getinteger(argv, 4) : 1; size_t count = (argc == 5) ? (size_t)janet_getinteger(argv, 4) : 1;
size_t src_atom_size = ta_type_sizes[src->type]; size_t src_atom_size = ta_type_sizes[src->type];
@ -473,16 +474,16 @@ static Janet cfun_typed_array_swap_bytes(int32_t argc, Janet *argv) {
uint8_t *ps = src->buffer->data + pos_src, * pd = dst->buffer->data + pos_dst; uint8_t *ps = src->buffer->data + pos_src, * pd = dst->buffer->data + pos_dst;
uint8_t temp[TA_ATOM_MAXSIZE]; uint8_t temp[TA_ATOM_MAXSIZE];
if ((pos_dst + (count - 1)*step_dst + src_atom_size <= dst->buffer->size) && if ((pos_dst + (count - 1)*step_dst + src_atom_size <= dst->buffer->size) &&
(pos_src + (count - 1)*step_src + src_atom_size <= src->buffer->size)) { (pos_src + (count - 1)*step_src + src_atom_size <= src->buffer->size)) {
for (size_t i = 0; i < count; i++) { for (size_t i = 0; i < count; i++) {
memcpy(temp, ps, src_atom_size); memcpy(temp, ps, src_atom_size);
memcpy(ps, pd, src_atom_size); memcpy(ps, pd, src_atom_size);
memcpy(pd, temp, src_atom_size); memcpy(pd, temp, src_atom_size);
pd += step_dst; pd += step_dst;
ps += step_src; ps += step_src;
} }
} else { } else {
janet_panic("typed array swap out of bound"); janet_panic("typed array swap out of bound");
} }
return janet_wrap_nil(); return janet_wrap_nil();
} }

View File

@ -1297,12 +1297,12 @@ typedef struct {
JanetTArrayType type; JanetTArrayType type;
} JanetTArrayView; } JanetTArrayView;
JANET_API JanetTArrayBuffer * janet_tarray_buffer(size_t size); JANET_API JanetTArrayBuffer *janet_tarray_buffer(size_t size);
JANET_API JanetTArrayView * janet_tarray_view(JanetTArrayType type,size_t size,size_t stride,size_t offset,JanetTArrayBuffer *buffer); JANET_API JanetTArrayView *janet_tarray_view(JanetTArrayType type, size_t size, size_t stride, size_t offset, JanetTArrayBuffer *buffer);
JANET_API int janet_is_tarray_view(Janet x,JanetTArrayType type); JANET_API int janet_is_tarray_view(Janet x, JanetTArrayType type);
JANET_API int janet_tarray_type_size(JanetTArrayType type); JANET_API int janet_tarray_type_size(JanetTArrayType type);
JANET_API JanetTArrayBuffer * janet_gettarray_buffer(const Janet *argv, int32_t n); JANET_API JanetTArrayBuffer *janet_gettarray_buffer(const Janet *argv, int32_t n);
JANET_API JanetTArrayView * janet_gettarray_view(const Janet *argv, int32_t n,JanetTArrayType type); JANET_API JanetTArrayView *janet_gettarray_view(const Janet *argv, int32_t n, JanetTArrayType type);
#endif #endif