From 4d0a9b6aaa872895b8b859711aa690f5af0b240e Mon Sep 17 00:00:00 2001 From: Tommy Olofsson Date: Tue, 22 Mar 2016 15:58:14 +0100 Subject: [PATCH 01/13] Library compiles in tc31 and cygwin. --- lib/c/2014/Makefile | 4 +- lib/c/2014/labcomm2014.c | 117 +++++++++++++----- lib/c/2014/labcomm2014.h | 2 +- lib/c/2014/labcomm2014.vcxproj | 105 ++++++++++++++++ lib/c/2014/labcomm2014_compat_tc31.h | 14 +++ lib/c/2014/labcomm2014_decoder.c | 2 +- lib/c/2014/labcomm2014_default_scheduler.c | 22 ++-- .../2014/labcomm2014_dynamic_buffer_writer.c | 12 +- lib/c/2014/labcomm2014_fd_reader.c | 1 - lib/c/2014/labcomm2014_fd_writer.c | 1 - lib/c/2014/labcomm2014_renaming.c | 2 +- lib/c/2014/labcomm2014_scheduler.c | 48 +++---- lib/c/2014/labcomm2014_time.c | 16 +-- lib/c/2014/labcomm2014_type_signature.c | 11 +- lib/c/os_compat.mk | 2 + 15 files changed, 268 insertions(+), 91 deletions(-) create mode 100644 lib/c/2014/labcomm2014.vcxproj create mode 100644 lib/c/2014/labcomm2014_compat_tc31.h diff --git a/lib/c/2014/Makefile b/lib/c/2014/Makefile index b18a9e4..6c7d812 100644 --- a/lib/c/2014/Makefile +++ b/lib/c/2014/Makefile @@ -102,7 +102,7 @@ endif # compilation rules %.pic.o: %.c - $(CC) -fPIC $(CFLAGS) -c -o $@ $< + $(CC) $(FPIC) $(CFLAGS) -c -o $@ $< %.o: %.c %.h $(CC) $(CFLAGS) -c -o $@ $< @@ -110,7 +110,7 @@ endif # rules invoked by 'test' .PHONY: run-test-% run-test-%: $(TEST_DIR)/gen/% | $(TEST_DIR)/gen - $(VALGRIND) $< + LD_LIBRARY_PATH=.. $(VALGRIND) $< .PRECIOUS: $(TEST_DIR)/gen/% diff --git a/lib/c/2014/labcomm2014.c b/lib/c/2014/labcomm2014.c index f599e78..97b983a 100644 --- a/lib/c/2014/labcomm2014.c +++ b/lib/c/2014/labcomm2014.c @@ -43,26 +43,28 @@ void *labcomm_void_instance = &labcomm_void_instance; -/* Unwrapping reader/writer functions */ -#define UNWRAP_ac(rw, ac, ...) ac -#define UNWRAP(func, ...) \ - while (1) { \ - if (UNWRAP_ac(__VA_ARGS__)->action->func) { \ - return UNWRAP_ac(__VA_ARGS__)->action->func(__VA_ARGS__); } \ - if (UNWRAP_ac(__VA_ARGS__)->next == NULL) { return -ENOSYS; } \ - UNWRAP_ac( __VA_ARGS__) = UNWRAP_ac(__VA_ARGS__)->next; \ - } - int labcomm2014_reader_alloc(struct labcomm2014_reader *r, struct labcomm2014_reader_action_context *action_context) { - UNWRAP(alloc, r, action_context); + while (1) { + if (action_context->action->alloc) + return action_context->action->alloc(r, action_context); + if (action_context->next == NULL) + return -ENOSYS; + action_context = action_context->next; + } } int labcomm2014_reader_free(struct labcomm2014_reader *r, struct labcomm2014_reader_action_context *action_context) { - UNWRAP(free, r, action_context); + while (1) { + if (action_context->action->free) + return action_context->action->free(r, action_context); + if (action_context->next == NULL) + return -ENOSYS; + action_context = action_context->next; + } } int labcomm2014_reader_start(struct labcomm2014_reader *r, @@ -71,19 +73,37 @@ int labcomm2014_reader_start(struct labcomm2014_reader *r, const struct labcomm2014_signature *signature, void *value) { - UNWRAP(start, r, action_context, local_index, remote_index, signature, value); + while (1) { + if (action_context->action->start) + return action_context->action->start(r, action_context, local_index, remote_index, signature, value); + if (action_context->next == NULL) + return -ENOSYS; + action_context = action_context->next; + } } int labcomm2014_reader_end(struct labcomm2014_reader *r, struct labcomm2014_reader_action_context *action_context) { - UNWRAP(end, r, action_context); + while (1) { + if (action_context->action->end) + return action_context->action->end(r, action_context); + if (action_context->next == NULL) + return -ENOSYS; + action_context = action_context->next; + } } int labcomm2014_reader_fill(struct labcomm2014_reader *r, struct labcomm2014_reader_action_context *action_context) { - UNWRAP(fill, r, action_context); + while (1) { + if (action_context->action->fill) + return action_context->action->fill(r, action_context); + if (action_context->next == NULL) + return -ENOSYS; + action_context = action_context->next; + } } int labcomm2014_reader_ioctl(struct labcomm2014_reader *r, @@ -92,20 +112,37 @@ int labcomm2014_reader_ioctl(struct labcomm2014_reader *r, const struct labcomm2014_signature *signature, uint32_t ioctl_action, va_list args) { - UNWRAP(ioctl, r, action_context, - local_index, remote_index, signature, ioctl_action, args); + while (1) { + if (action_context->action->ioctl) + return action_context->action->ioctl(r, action_context, local_index, remote_index, signature, ioctl_action, args); + if (action_context->next == NULL) + return -ENOSYS; + action_context = action_context->next; + } } int labcomm2014_writer_alloc(struct labcomm2014_writer *w, struct labcomm2014_writer_action_context *action_context) { - UNWRAP(alloc, w, action_context); + while (1) { + if (action_context->action->alloc) + return action_context->action->alloc(w, action_context); + if (action_context->next == NULL) + return -ENOSYS; + action_context = action_context->next; + } } int labcomm2014_writer_free(struct labcomm2014_writer *w, struct labcomm2014_writer_action_context *action_context) { - UNWRAP(free, w, action_context); + while (1) { + if (action_context->action->free) + return action_context->action->free(w, action_context); + if (action_context->next == NULL) + return -ENOSYS; + action_context = action_context->next; + } } int labcomm2014_writer_start(struct labcomm2014_writer *w, @@ -113,19 +150,37 @@ int labcomm2014_writer_start(struct labcomm2014_writer *w, int index, const struct labcomm2014_signature *signature, void *value) { - UNWRAP(start, w, action_context, index, signature, value); + while (1) { + if (action_context->action->start) + return action_context->action->start(w, action_context, index, signature, value); + if (action_context->next == NULL) + return -ENOSYS; + action_context = action_context->next; + } } int labcomm2014_writer_end(struct labcomm2014_writer *w, struct labcomm2014_writer_action_context *action_context) { - UNWRAP(end, w, action_context); + while (1) { + if (action_context->action->end) + return action_context->action->end(w, action_context); + if (action_context->next == NULL) + return -ENOSYS; + action_context = action_context->next; + } } int labcomm2014_writer_flush(struct labcomm2014_writer *w, struct labcomm2014_writer_action_context *action_context) { - UNWRAP(flush, w, action_context); + while (1) { + if (action_context->action->flush) + return action_context->action->flush(w, action_context); + if (action_context->next == NULL) + return -ENOSYS; + action_context = action_context->next; + } } int labcomm2014_writer_ioctl(struct labcomm2014_writer *w, @@ -134,15 +189,15 @@ int labcomm2014_writer_ioctl(struct labcomm2014_writer *w, const struct labcomm2014_signature *signature, uint32_t ioctl_action, va_list args) { - UNWRAP(ioctl, w, action_context, index, signature, ioctl_action, args); + while (1) { + if (action_context->action->ioctl) + return action_context->action->ioctl(w, action_context, index, signature, ioctl_action, args); + if (action_context->next == NULL) + return -ENOSYS; + action_context = action_context->next; + } } -#undef UNWRAP -#undef UNWRAP_ac - - - - static const char *labcomm2014_error_string[] = { #define LABCOMM2014_ERROR(name, description) description , #include "labcomm2014_error.h" @@ -227,7 +282,7 @@ void *labcomm2014_signature_array_ref(struct labcomm2014_memory *memory, *data = labcomm2014_memory_alloc(memory, 0, n * size); if (*data) { memset(*data, 0, n * size); - memcpy(*data + (old_first - *first) * size, + memcpy((char *) *data + (old_first - *first) * size, old_data, (old_last - old_first) * size); } @@ -236,7 +291,7 @@ void *labcomm2014_signature_array_ref(struct labcomm2014_memory *memory, } if (*data) { // dump(*data, size, *first, *last); - return *data + (index - *first) * size; + return (char *) *data + (index - *first) * size; } else { return NULL; } diff --git a/lib/c/2014/labcomm2014.h b/lib/c/2014/labcomm2014.h index ca490cc..e0d19f6 100644 --- a/lib/c/2014/labcomm2014.h +++ b/lib/c/2014/labcomm2014.h @@ -30,8 +30,8 @@ #else #include #include - #include #endif +#include #include "labcomm2014_error.h" #include "labcomm2014_scheduler.h" diff --git a/lib/c/2014/labcomm2014.vcxproj b/lib/c/2014/labcomm2014.vcxproj new file mode 100644 index 0000000..6922d42 --- /dev/null +++ b/lib/c/2014/labcomm2014.vcxproj @@ -0,0 +1,105 @@ + + + + + + Debug + TwinCAT RT (x64) + + + Release + TwinCAT RT (x64) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + {1841FE1E-1E6C-449E-882D-7F26F60384E8} + labcomm2014 + Win32Proj + + + + StaticLibrary + + + StaticLibrary + + + + + + + + + + + <_ProjectFileVersion>10.0.21006.1 + AllRules.ruleset + + + AllRules.ruleset + + + + + + + + + + LABCOMM_COMPAT="labcomm2014_compat_tc31.h";LABCOMM_NO_STDIO;USE_CRT_OWN_IMPL;_X64_;_AMD64_;_WIN64;_DEBUG;STD_CALL;CONDITION_HANDLING;WIN32_LEAN_AND_MEAN;RDRDBG;SRVDBG;DBG;_IDWBUILD;IS_R0;WINNT;DEBUG;_WDM_INCLUDED_;_WIN32_WINNT=_WIN32_WINNT_WINXP;NTDDI_VERSION=NTDDI_WINXPSP2;TC_VER=301;%(PreprocessorDefinitions) + NotUsing + + + + + + + + + NotUsing + + + + + + \ No newline at end of file diff --git a/lib/c/2014/labcomm2014_compat_tc31.h b/lib/c/2014/labcomm2014_compat_tc31.h new file mode 100644 index 0000000..7e22acf --- /dev/null +++ b/lib/c/2014/labcomm2014_compat_tc31.h @@ -0,0 +1,14 @@ +#ifndef LABCOMM_COMPAT_TC31 +#define LABCOMM_COMPAT_TC31 + +#include + +#define ECONNRESET 101 +#define ENOTSUP 102 +#define EALREADY 103 + +#define inline __inline + +typedef unsigned __int32 uint32_t; + +#endif diff --git a/lib/c/2014/labcomm2014_decoder.c b/lib/c/2014/labcomm2014_decoder.c index e270afc..860628b 100644 --- a/lib/c/2014/labcomm2014_decoder.c +++ b/lib/c/2014/labcomm2014_decoder.c @@ -26,7 +26,7 @@ #include "labcomm2014_ioctl.h" #include "labcomm2014_dynamic_buffer_writer.h" -#ifdef DEBUG +#if defined(DEBUG) && !defined(LABCOMM_NO_STDIO) #define DEBUG_FPRINTF(str, ...) fprintf(str, ##__VA_ARGS__) #else #define DEBUG_FPRINTF(str, ...) diff --git a/lib/c/2014/labcomm2014_default_scheduler.c b/lib/c/2014/labcomm2014_default_scheduler.c index f1fd3d6..04b9144 100644 --- a/lib/c/2014/labcomm2014_default_scheduler.c +++ b/lib/c/2014/labcomm2014_default_scheduler.c @@ -90,20 +90,20 @@ static int scheduler_enqueue(struct labcomm2014_scheduler *s, } static const struct labcomm2014_scheduler_action scheduler_action = { - .free = scheduler_free, - .writer_lock = scheduler_writer_lock, - .writer_unlock = scheduler_writer_unlock, - .data_lock = scheduler_data_lock, - .data_unlock = scheduler_data_unlock, - .now = scheduler_now, - .sleep = scheduler_sleep, - .wakeup = scheduler_wakeup, - .enqueue = scheduler_enqueue + scheduler_free, + scheduler_writer_lock, + scheduler_writer_unlock, + scheduler_data_lock, + scheduler_data_unlock, + scheduler_now, + scheduler_sleep, + scheduler_wakeup, + scheduler_enqueue }; static struct labcomm2014_scheduler scheduler = { - .action = &scheduler_action, - .context = NULL + &scheduler_action, + NULL }; struct labcomm2014_scheduler *labcomm2014_default_scheduler = &scheduler; diff --git a/lib/c/2014/labcomm2014_dynamic_buffer_writer.c b/lib/c/2014/labcomm2014_dynamic_buffer_writer.c index c209862..8609ce1 100644 --- a/lib/c/2014/labcomm2014_dynamic_buffer_writer.c +++ b/lib/c/2014/labcomm2014_dynamic_buffer_writer.c @@ -123,12 +123,12 @@ static int dyn_ioctl(struct labcomm2014_writer *w, } static const struct labcomm2014_writer_action action = { - .alloc = dyn_alloc, - .free = dyn_free, - .start = dyn_start, - .end = dyn_end, - .flush = dyn_flush, - .ioctl = dyn_ioctl + dyn_alloc, + dyn_free, + dyn_start, + dyn_end, + dyn_flush, + dyn_ioctl }; const struct labcomm2014_writer_action *labcomm2014_dynamic_buffer_writer_action = &action; diff --git a/lib/c/2014/labcomm2014_fd_reader.c b/lib/c/2014/labcomm2014_fd_reader.c index ad2d222..23d5ba5 100644 --- a/lib/c/2014/labcomm2014_fd_reader.c +++ b/lib/c/2014/labcomm2014_fd_reader.c @@ -20,7 +20,6 @@ */ #include -#include #include #include #include "labcomm2014_private.h" diff --git a/lib/c/2014/labcomm2014_fd_writer.c b/lib/c/2014/labcomm2014_fd_writer.c index 42c9dbc..0e4d009 100644 --- a/lib/c/2014/labcomm2014_fd_writer.c +++ b/lib/c/2014/labcomm2014_fd_writer.c @@ -20,7 +20,6 @@ */ #include -#include #include #include #include diff --git a/lib/c/2014/labcomm2014_renaming.c b/lib/c/2014/labcomm2014_renaming.c index 332c638..fbdb851 100644 --- a/lib/c/2014/labcomm2014_renaming.c +++ b/lib/c/2014/labcomm2014_renaming.c @@ -29,7 +29,7 @@ char *labcomm2014_renaming_prefix(struct labcomm2014_memory *m, char *result, *prefix = context; int length; - length = strlen(name) + strlen(prefix) + 1; + length = (int) (strlen(name) + strlen(prefix) + 1); result = labcomm2014_memory_alloc(m, 0, length); if (result != NULL) { strcpy(result, prefix); diff --git a/lib/c/2014/labcomm2014_scheduler.c b/lib/c/2014/labcomm2014_scheduler.c index 4028820..465398b 100644 --- a/lib/c/2014/labcomm2014_scheduler.c +++ b/lib/c/2014/labcomm2014_scheduler.c @@ -22,56 +22,62 @@ #include #include "labcomm2014_scheduler_private.h" -#define SCHEDULER_scheduler(scheduler, ...) scheduler -#define SCHEDULER(func, ...) \ - if (SCHEDULER_scheduler(__VA_ARGS__) && \ - SCHEDULER_scheduler(__VA_ARGS__)->action->func) { \ - return SCHEDULER_scheduler(__VA_ARGS__)->action->func(__VA_ARGS__); \ - } \ - return -ENOSYS; - int labcomm2014_scheduler_free(struct labcomm2014_scheduler *s) { - SCHEDULER(free, s); + if (s && s->action->free) + return s->action->free(s); + return -ENOSYS; } int labcomm2014_scheduler_writer_lock(struct labcomm2014_scheduler *s) { - SCHEDULER(writer_lock, s); + if (s && s->action->writer_lock) + return s->action->writer_lock(s); + return -ENOSYS; } int labcomm2014_scheduler_writer_unlock(struct labcomm2014_scheduler *s) { - SCHEDULER(writer_unlock, s); + if (s && s->action->writer_unlock) + return s->action->writer_unlock(s); + return -ENOSYS; } int labcomm2014_scheduler_data_lock(struct labcomm2014_scheduler *s) { - SCHEDULER(data_lock, s); + if (s && s->action->data_lock) + return s->action->data_lock(s); + return -ENOSYS; } int labcomm2014_scheduler_data_unlock(struct labcomm2014_scheduler *s) { - SCHEDULER(data_unlock, s); + if (s && s->action->data_unlock) + return s->action->data_unlock(s); + return -ENOSYS; } struct labcomm2014_time *labcomm2014_scheduler_now(struct labcomm2014_scheduler *s) { if (s && s->action->now) { - return s->action->now(s); - } - return NULL; + return s->action->now(s); + } + return 0; } int labcomm2014_scheduler_sleep(struct labcomm2014_scheduler *s, struct labcomm2014_time *wakeup) { - SCHEDULER(sleep, s, wakeup); + if (s && s->action->sleep) + return s->action->sleep(s, wakeup); + return -ENOSYS; } int labcomm2014_scheduler_wakeup(struct labcomm2014_scheduler *s) { - SCHEDULER(wakeup, s); + if (s && s->action->wakeup) + return s->action->wakeup(s); + return -ENOSYS; } int labcomm2014_scheduler_enqueue(struct labcomm2014_scheduler *s, @@ -79,7 +85,7 @@ int labcomm2014_scheduler_enqueue(struct labcomm2014_scheduler *s, void (*func)(void *context), void *context) { - SCHEDULER(enqueue, s, delay, func, context); + if (s && s->action->enqueue) + return s->action->enqueue(s, delay, func, context); + return -ENOSYS; } - - diff --git a/lib/c/2014/labcomm2014_time.c b/lib/c/2014/labcomm2014_time.c index eb5e1f5..8805f90 100644 --- a/lib/c/2014/labcomm2014_time.c +++ b/lib/c/2014/labcomm2014_time.c @@ -22,21 +22,17 @@ #include #include "labcomm2014_scheduler_private.h" -#define TIME_time(time, ...) time -#define TIME(func, ...) \ - if (TIME_time(__VA_ARGS__) && \ - TIME_time(__VA_ARGS__)->action->func) { \ - return TIME_time(__VA_ARGS__)->action->func(__VA_ARGS__); \ - } \ - return -ENOSYS; - int labcomm2014_time_free(struct labcomm2014_time *s) { - TIME(free, s); + if (s && s->action->free) + return s->action->free(s); + return -ENOSYS; } int labcomm2014_time_add_usec(struct labcomm2014_time *s, uint32_t usec) { - TIME(add_usec, s, usec); + if (s && s->action->add_usec) + return s->action->add_usec(s, usec); + return -ENOSYS; } diff --git a/lib/c/2014/labcomm2014_type_signature.c b/lib/c/2014/labcomm2014_type_signature.c index dce1836..b01c79c 100644 --- a/lib/c/2014/labcomm2014_type_signature.c +++ b/lib/c/2014/labcomm2014_type_signature.c @@ -2,6 +2,8 @@ #include // for memcmp #include // for debug printf +#define CMP_BUFLEN 512 + /* Dump signature bytes on stdout */ @@ -75,11 +77,10 @@ static labcomm2014_bool sig_dump_checked(struct labcomm2014_signature_data *sign labcomm2014_bool labcomm2014_signature_cmp( struct labcomm2014_signature_data *s1, struct labcomm2014_signature_data *s2) { - int buflen=512; - char buf1[buflen]; - int len1=buflen; - char buf2[buflen]; - int len2=buflen; + char buf1[CMP_BUFLEN]; + int len1=CMP_BUFLEN; + char buf2[CMP_BUFLEN]; + int len2=CMP_BUFLEN; labcomm2014_bool res1 = labcomm2014_signature_dump(s1, buf1, &len1); labcomm2014_bool res2 = labcomm2014_signature_dump(s2, buf2, &len2); if(res1 || res2) { diff --git a/lib/c/os_compat.mk b/lib/c/os_compat.mk index 505c7a2..12986ea 100644 --- a/lib/c/os_compat.mk +++ b/lib/c/os_compat.mk @@ -2,6 +2,7 @@ UNAME_S=$(shell uname -s) VERSION=2014 LIBVERSION=2014 +FPIC=-fPIC ifeq ($(UNAME_S),Linux) CC=$(CROSS_COMPILE)gcc @@ -29,6 +30,7 @@ else ifneq ($(findstring CYGWIN,$(UNAME_S)),) CC=$(CROSS_COMPILE)gcc LD=$(CROSS_COMPILE)ld CFLAGS=-std=c99 -g -Wall -Werror -O3 -I. + FPIC= LDFLAGS=-L.. LDLIBS=-llabcomm$(LIBVERSION) -lrt ALL_DEPS:=$(filter-out %.so.1, $(ALL_DEPS)) # No -fPIC supported in windows? -- GitLab From 4afb966022bd347c861f0fe32cafc498b122bb46 Mon Sep 17 00:00:00 2001 From: Tommy Olofsson Date: Tue, 22 Mar 2016 16:28:52 +0100 Subject: [PATCH 02/13] Fix VS release target. --- lib/c/2014/labcomm2014.vcxproj | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/c/2014/labcomm2014.vcxproj b/lib/c/2014/labcomm2014.vcxproj index 6922d42..4bf8b0c 100644 --- a/lib/c/2014/labcomm2014.vcxproj +++ b/lib/c/2014/labcomm2014.vcxproj @@ -97,6 +97,7 @@ NotUsing + LABCOMM_COMPAT="labcomm2014_compat_tc31.h";LABCOMM_NO_STDIO;USE_CRT_OWN_IMPL;_AMD64_;_X64_;_WIN64;STD_CALL;CONDITION_HANDLING;WIN32_LEAN_AND_MEAN;_IDWBUILD;IS_R0;WINNT;_WDM_INCLUDED_;_WIN32_WINNT=_WIN32_WINNT_WINXP;NTDDI_VERSION=NTDDI_WINXPSP2;TC_VER=301;%(PreprocessorDefinitions) -- GitLab From b8aa1ed16f68483242fbd75784704dd75ee9a3ca Mon Sep 17 00:00:00 2001 From: Tommy Olofsson Date: Wed, 23 Mar 2016 15:52:17 +0100 Subject: [PATCH 03/13] Consider NOSTDIO define and don't die on warning. --- lib/c/2014/labcomm2014_error.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/c/2014/labcomm2014_error.c b/lib/c/2014/labcomm2014_error.c index a002026..48c3e4c 100644 --- a/lib/c/2014/labcomm2014_error.c +++ b/lib/c/2014/labcomm2014_error.c @@ -36,12 +36,16 @@ void labcomm2014_error_fatal_global(enum labcomm2014_error error, { va_list args; +#ifndef LABCOMM_NO_STDIO fprintf(stderr, "Fatal error %d (%s)\n", error, description[error]); va_start(args, format); vfprintf(stderr, format, args); va_end(args); +#endif +#ifdef LABCOMM_EXIT exit(1); +#endif } void labcomm2014_error_warning(struct labcomm2014_error_handler *e, @@ -51,11 +55,15 @@ void labcomm2014_error_warning(struct labcomm2014_error_handler *e, { va_list args; +#ifndef LABCOMM_NO_STDIO fprintf(stderr, "Fatal warning %d (%s)\n", error, description[error]); va_start(args, format); vfprintf(stderr, format, args); va_end(args); +#endif +#ifdef LABCOMM_EXIT exit(1); +#endif } -- GitLab From 4a74d30cb193dcedebaa1abff5e2916a40dc829e Mon Sep 17 00:00:00 2001 From: Tommy Olofsson Date: Sun, 12 Jun 2016 18:05:07 +0200 Subject: [PATCH 04/13] Defs to make generated code work. --- lib/c/2014/labcomm2014_compat_tc31.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/c/2014/labcomm2014_compat_tc31.h b/lib/c/2014/labcomm2014_compat_tc31.h index 7e22acf..e525e87 100644 --- a/lib/c/2014/labcomm2014_compat_tc31.h +++ b/lib/c/2014/labcomm2014_compat_tc31.h @@ -3,12 +3,21 @@ #include +#define LABCOMM_CONSTRUCTOR + #define ECONNRESET 101 #define ENOTSUP 102 #define EALREADY 103 #define inline __inline +typedef __int8 int8_t; +typedef unsigned __int8 uint8_t; +typedef __int16 int16_t; +typedef unsigned __int16 uint16_t; +typedef __int32 int32_t; typedef unsigned __int32 uint32_t; +typedef __int64 int64_t; +typedef unsigned __int64 uint64_t; #endif -- GitLab From 6eede51a3d48752d7bf99f88b9f8f42a3aecf300 Mon Sep 17 00:00:00 2001 From: Tommy Olofsson Date: Mon, 13 Jun 2016 00:48:23 +0200 Subject: [PATCH 05/13] More undef constants. --- lib/c/2014/labcomm2014_compat_tc31.h | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/c/2014/labcomm2014_compat_tc31.h b/lib/c/2014/labcomm2014_compat_tc31.h index e525e87..4f6f975 100644 --- a/lib/c/2014/labcomm2014_compat_tc31.h +++ b/lib/c/2014/labcomm2014_compat_tc31.h @@ -8,6 +8,7 @@ #define ECONNRESET 101 #define ENOTSUP 102 #define EALREADY 103 +#define ENODATA 104 #define inline __inline -- GitLab From c4cf4260e3da538d8eb6f80d01253406f48b36e7 Mon Sep 17 00:00:00 2001 From: Tommy Olofsson Date: Mon, 13 Jun 2016 00:49:26 +0200 Subject: [PATCH 06/13] Dont deref. NULL. --- lib/c/2014/labcomm2014_decoder.c | 3 ++- lib/c/2014/labcomm2014_encoder.c | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/c/2014/labcomm2014_decoder.c b/lib/c/2014/labcomm2014_decoder.c index 8e48c26..fe534f3 100644 --- a/lib/c/2014/labcomm2014_decoder.c +++ b/lib/c/2014/labcomm2014_decoder.c @@ -716,8 +716,9 @@ struct labcomm2014_decoder *labcomm2014_decoder_new( LABCOMM_SIGNATURE_ARRAY_INIT(result->local_ref, const struct labcomm2014_signature*); LABCOMM_SIGNATURE_ARRAY_INIT(result->remote_to_local_ref, int); + return &(result->decoder); } - return &(result->decoder); + return NULL; } diff --git a/lib/c/2014/labcomm2014_encoder.c b/lib/c/2014/labcomm2014_encoder.c index 70a2f9c..745edb5 100644 --- a/lib/c/2014/labcomm2014_encoder.c +++ b/lib/c/2014/labcomm2014_encoder.c @@ -420,8 +420,9 @@ static struct labcomm2014_encoder *internal_encoder_new( labcomm2014_writer_end(result->encoder.writer, result->encoder.writer->action_context); } + return &(result->encoder); } - return &(result->encoder); + return NULL; } struct labcomm2014_encoder *labcomm2014_encoder_new( -- GitLab From 4bb8da747a930b806a06c1857fdc0f3059b44bf8 Mon Sep 17 00:00:00 2001 From: Tommy Olofsson Date: Wed, 15 Jun 2016 10:39:07 +0200 Subject: [PATCH 07/13] Added compiler aspect for copying to preallocated structure(s). --- compiler/2014/C_CodeGen.jrag | 135 +++++++++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) diff --git a/compiler/2014/C_CodeGen.jrag b/compiler/2014/C_CodeGen.jrag index a68fa0e..19dd58a 100644 --- a/compiler/2014/C_CodeGen.jrag +++ b/compiler/2014/C_CodeGen.jrag @@ -221,6 +221,7 @@ aspect C_CodeGen { getDecl(i).C_emitDecoderDeclaration(env); getDecl(i).C_emitEncoderDeclaration(env); getDecl(i).C_emitSizeofDeclaration(env); + getDecl(i).C_emitCopyStaticDeclaration(env); getDecl(i).C_emitCopyDeclaration(env); getDecl(i).C_emitCopyDeallocationDeclaration(env); env.println(""); @@ -239,6 +240,7 @@ aspect C_CodeGen { getDecl(i).C_emitEncoderRegisterHandler(env); getDecl(i).C_emitEncoderIoctl(env); getDecl(i).C_emitSizeof(env); + getDecl(i).C_emitCopyStatic(env); getDecl(i).C_emitCopy(env); getDecl(i).C_emitCopyDeallocation(env); } @@ -957,6 +959,139 @@ aspect C_copy { } } +aspect C_copy_static { + + private void SampleDecl.C_emitCopyStaticFunctionParam(C_env env, String src, + String dst) + { + env.println("void labcomm2014_copy_static_" + + env.prefix + getName() + "("); + env.indent(); + env.println(env.prefix + getName() + " *" + dst + ","); + env.println(env.prefix + getName() + " *" + src); + env.unindent(); + env.print(")"); + } + + public void Decl.C_emitCopyStaticDeclaration(C_env env) { + } + + public void SampleDecl.C_emitCopyStaticDeclaration(C_env env) { + C_emitCopyStaticFunctionParam(env, "src", "dst"); + env.println(";"); + } + + public void Decl.C_emitCopyStatic(C_env env) { + throw new Error(this.getClass().getName() + + ".C_emitCopyStatic(C_env env)" + + " not declared"); + } + + public void TypeDecl.C_emitCopyStatic(C_env env) { + } + + public void SampleDecl.C_emitCopyStatic(C_env env) { + final String dst = "dst"; + final String src = "src"; + C_env env_src = env.nestStruct(src).setPointer(); + C_env env_dst = env.nestStruct(dst).setPointer(); + + C_emitCopyStaticFunctionParam(env_src, src, dst); + env_src.println(""); + env_src.println("{"); + env_src.indent(); + getDataType().C_emitCopyStatic(env_src, env_dst); + env_src.unindent(); + env_src.println("}"); + } + + public void DataType.C_emitCopyStatic(C_env env_src, C_env env_dst) { + throw new Error(this.getClass().getName() + + ".C_emitCopyStatic(C_env env)" + + " not declared"); + } + + public void VoidType.C_emitCopyStatic(C_env env_src, C_env env_dst) { + } + + public void PrimType.C_emitCopyStatic(C_env env_src, C_env env_dst) { + if (C_isDynamic()) { + env_src.println(String.format( + "memcpy(%s%s, %s%s, strlen(%s%s)+1);", + env_dst.accessor(), env_dst.qualid, + env_src.accessor(), env_src.qualid, + env_src.accessor(), env_src.qualid)); + } else { + env_src.println(env_dst.accessor() + env_dst.qualid + " = " + + env_src.accessor() + env_src.qualid + ";"); + } + } + + public void UserType.C_emitCopyStatic(C_env env_src, C_env env_dst) { + lookupType(getName()).getDataType().C_emitCopyStatic(env_src, env_dst); + } + + public void StructType.C_emitCopyStatic(C_env env_src, C_env env_dst) { + for (int i = 0 ; i < getNumField() ; i++) { + getField(i).C_emitCopyStatic(env_src, env_dst); + } + } + + public void ArrayType.C_emitCopyStatic(C_env env_src, C_env env_dst) { + C_emitCopyStaticDecodeLimit(env_src, env_dst); + C_emitCopyStaticArrayAllocate(env_src, env_dst); + env_src.println("{"); + env_src.indent(); + C_emitLoopVariables(env_src); + for (int i = 0 ; i < getNumExp() ; i++) { + String iterator = "i_" + env_src.depth + "_" + i; + env_src.println("for (" + iterator + " = 0" + + " ; " + + iterator + " < " + getExp(i).C_getLimit(env_src, i) + + " ; " + + iterator + "++) {"); + env_src.indent(); + } + C_emitCalcIndex(env_src); + getDataType().C_emitCopyStatic(C_Nest(env_src), C_Nest(env_dst)); + for (int i = getNumExp() - 1 ; i >= 0 ; i--) { + env_src.unindent(); + env_src.println("}"); + } + env_src.unindent(); + env_src.println("}"); + } + + public void Field.C_emitCopyStatic(C_env env_src, C_env env_dst) { + String fnam = env_src.memberAccessor() + getName(); + getDataType().C_emitCopyStatic(env_src.nestStruct(fnam), env_dst.nestStruct(fnam)); + } + + public void Exp.C_emitCopyStaticDecodeLimit(C_env env_src, C_env env_dst, int i) { + // Ordinary array has no length-member. + } + + public void VariableSize.C_emitCopyStaticDecodeLimit(C_env env_src, C_env env_dst, int i) { + String src = env_src.qualid + env_src.memberAccessor() + "n_" + i; + String dst = env_dst.qualid + env_dst.memberAccessor() + "n_" + i; + env_src.println(dst + " = " + src + ";"); + } + + public void ArrayType.C_emitCopyStaticDecodeLimit(C_env env_src, C_env env_dst) { + for (int i = 0 ; i < getNumExp() ; i++) { + getExp(i).C_emitCopyStaticDecodeLimit(env_src, env_dst, i); + } + } + + public void ArrayType.C_emitCopyStaticArrayAllocate(C_env env_src, C_env env_dst) { + } + + public void VariableArrayType.C_emitCopyStaticArrayAllocate(C_env env_src, + C_env env_dst) + { + } +} + aspect C_DecoderIoctl { public void Decl.C_emitDecoderIoctl(C_env env) { -- GitLab From cd1ab8d1a2bb253169731ab0ef960ee9b64a1f4e Mon Sep 17 00:00:00 2001 From: Tommy Olofsson Date: Mon, 20 Jun 2016 13:37:52 +0200 Subject: [PATCH 08/13] Try to catch memory errors. --- lib/c/2014/labcomm2014_decoder.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/c/2014/labcomm2014_decoder.c b/lib/c/2014/labcomm2014_decoder.c index fe534f3..2a43982 100644 --- a/lib/c/2014/labcomm2014_decoder.c +++ b/lib/c/2014/labcomm2014_decoder.c @@ -158,6 +158,10 @@ static int decode_sample_def_or_ref(struct labcomm2014_decoder *d, int kind) goto out; } signature.name = labcomm2014_read_string(d->reader); + if (signature.name == NULL) { + result = -ENOMEM; + goto out; + } if (d->reader->error < 0) { result = d->reader->error; goto free_signature_name; @@ -168,6 +172,10 @@ static int decode_sample_def_or_ref(struct labcomm2014_decoder *d, int kind) goto free_signature_name; } signature.signature = labcomm2014_memory_alloc(d->memory, 1, signature.size); + if (signature.signature == NULL) { + result = -ENOMEM; + goto free_signature_name; + } if (d->reader->error < 0) { result = d->reader->error; goto free_signature_name; @@ -253,6 +261,10 @@ static int decode_pragma(struct labcomm2014_decoder *d, char *pragma_type; int result; pragma_type = labcomm2014_read_string(d->reader); + if (pragma_type == NULL) { + result = -ENOMEM; + goto out; + } if (d->reader->error < 0) { result = d->reader->error; goto out; @@ -346,7 +358,9 @@ static int do_decode_one(struct labcomm2014_decoder *d) } if (remote_index == LABCOMM_VERSION) { char *version = labcomm2014_read_string(d->reader); - if (d->reader->error < 0) { + if (version == NULL) { + result = -ENOMEM; + } else if (d->reader->error < 0) { result = d->reader->error; } else if (strcmp(version, CURRENT_VERSION) == 0) { result = LABCOMM_VERSION; @@ -488,6 +502,7 @@ static void decode_raw_type_def( v.index = labcomm2014_read_packed32(r); if (r->error < 0) { goto out; } v.name = labcomm2014_read_string(r); + if (v.name == NULL) { goto out; } if (r->error < 0) { goto free_name; } v.length = labcomm2014_read_packed32(r); if (r->error < 0) { goto free_name; } -- GitLab From e9009bc292c4e82ac258378a97fc5f233ab7fcec Mon Sep 17 00:00:00 2001 From: Tommy Olofsson Date: Tue, 28 Jun 2016 11:43:26 +0200 Subject: [PATCH 09/13] Some macros had wrong prefix. --- lib/c/2014/labcomm2014_decoder.c | 2 +- lib/c/2014/labcomm2014_error.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/c/2014/labcomm2014_decoder.c b/lib/c/2014/labcomm2014_decoder.c index 529c553..4b078e4 100644 --- a/lib/c/2014/labcomm2014_decoder.c +++ b/lib/c/2014/labcomm2014_decoder.c @@ -26,7 +26,7 @@ #include "labcomm2014_ioctl.h" #include "labcomm2014_dynamic_buffer_writer.h" -#if defined(DEBUG) && !defined(LABCOMM_NO_STDIO) +#if defined(DEBUG) && !defined(LABCOMM2014_NO_STDIO) #define DEBUG_FPRINTF(str, ...) fprintf(str, ##__VA_ARGS__) #else #define DEBUG_FPRINTF(str, ...) diff --git a/lib/c/2014/labcomm2014_error.c b/lib/c/2014/labcomm2014_error.c index 48c3e4c..794096a 100644 --- a/lib/c/2014/labcomm2014_error.c +++ b/lib/c/2014/labcomm2014_error.c @@ -36,7 +36,7 @@ void labcomm2014_error_fatal_global(enum labcomm2014_error error, { va_list args; -#ifndef LABCOMM_NO_STDIO +#ifndef LABCOMM2014_NO_STDIO fprintf(stderr, "Fatal error %d (%s)\n", error, description[error]); va_start(args, format); vfprintf(stderr, format, args); @@ -55,7 +55,7 @@ void labcomm2014_error_warning(struct labcomm2014_error_handler *e, { va_list args; -#ifndef LABCOMM_NO_STDIO +#ifndef LABCOMM2014_NO_STDIO fprintf(stderr, "Fatal warning %d (%s)\n", error, description[error]); va_start(args, format); vfprintf(stderr, format, args); -- GitLab From b26b5fcc6023e8769be90d0d0e50a3c6512f7fa0 Mon Sep 17 00:00:00 2001 From: Tommy Olofsson Date: Thu, 30 Jun 2016 14:18:59 +0200 Subject: [PATCH 10/13] Undo unnecc. changes. --- lib/c/2014/labcomm2014_default_scheduler.c | 22 +++++++++---------- .../2014/labcomm2014_dynamic_buffer_writer.c | 12 +++++----- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/lib/c/2014/labcomm2014_default_scheduler.c b/lib/c/2014/labcomm2014_default_scheduler.c index 04b9144..f1fd3d6 100644 --- a/lib/c/2014/labcomm2014_default_scheduler.c +++ b/lib/c/2014/labcomm2014_default_scheduler.c @@ -90,20 +90,20 @@ static int scheduler_enqueue(struct labcomm2014_scheduler *s, } static const struct labcomm2014_scheduler_action scheduler_action = { - scheduler_free, - scheduler_writer_lock, - scheduler_writer_unlock, - scheduler_data_lock, - scheduler_data_unlock, - scheduler_now, - scheduler_sleep, - scheduler_wakeup, - scheduler_enqueue + .free = scheduler_free, + .writer_lock = scheduler_writer_lock, + .writer_unlock = scheduler_writer_unlock, + .data_lock = scheduler_data_lock, + .data_unlock = scheduler_data_unlock, + .now = scheduler_now, + .sleep = scheduler_sleep, + .wakeup = scheduler_wakeup, + .enqueue = scheduler_enqueue }; static struct labcomm2014_scheduler scheduler = { - &scheduler_action, - NULL + .action = &scheduler_action, + .context = NULL }; struct labcomm2014_scheduler *labcomm2014_default_scheduler = &scheduler; diff --git a/lib/c/2014/labcomm2014_dynamic_buffer_writer.c b/lib/c/2014/labcomm2014_dynamic_buffer_writer.c index 61bdef1..67db27b 100644 --- a/lib/c/2014/labcomm2014_dynamic_buffer_writer.c +++ b/lib/c/2014/labcomm2014_dynamic_buffer_writer.c @@ -123,12 +123,12 @@ static int dyn_ioctl(struct labcomm2014_writer *w, } static const struct labcomm2014_writer_action action = { - dyn_alloc, - dyn_free, - dyn_start, - dyn_end, - dyn_flush, - dyn_ioctl + .alloc = dyn_alloc, + .free = dyn_free, + .start = dyn_start, + .end = dyn_end, + .flush = dyn_flush, + .ioctl = dyn_ioctl }; const struct labcomm2014_writer_action *labcomm2014_dynamic_buffer_writer_action = &action; -- GitLab From 1464b736e81a588e53bd5c6f0612e5db9ba686cb Mon Sep 17 00:00:00 2001 From: Tommy Olofsson Date: Thu, 30 Jun 2016 14:23:38 +0200 Subject: [PATCH 11/13] Decided not to use labcomm as VS subproject. --- lib/c/2014/labcomm2014.vcxproj | 106 --------------------------------- 1 file changed, 106 deletions(-) delete mode 100644 lib/c/2014/labcomm2014.vcxproj diff --git a/lib/c/2014/labcomm2014.vcxproj b/lib/c/2014/labcomm2014.vcxproj deleted file mode 100644 index 4bf8b0c..0000000 --- a/lib/c/2014/labcomm2014.vcxproj +++ /dev/null @@ -1,106 +0,0 @@ - - - - - - Debug - TwinCAT RT (x64) - - - Release - TwinCAT RT (x64) - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - {1841FE1E-1E6C-449E-882D-7F26F60384E8} - labcomm2014 - Win32Proj - - - - StaticLibrary - - - StaticLibrary - - - - - - - - - - - <_ProjectFileVersion>10.0.21006.1 - AllRules.ruleset - - - AllRules.ruleset - - - - - - - - - - LABCOMM_COMPAT="labcomm2014_compat_tc31.h";LABCOMM_NO_STDIO;USE_CRT_OWN_IMPL;_X64_;_AMD64_;_WIN64;_DEBUG;STD_CALL;CONDITION_HANDLING;WIN32_LEAN_AND_MEAN;RDRDBG;SRVDBG;DBG;_IDWBUILD;IS_R0;WINNT;DEBUG;_WDM_INCLUDED_;_WIN32_WINNT=_WIN32_WINNT_WINXP;NTDDI_VERSION=NTDDI_WINXPSP2;TC_VER=301;%(PreprocessorDefinitions) - NotUsing - - - - - - - - - NotUsing - LABCOMM_COMPAT="labcomm2014_compat_tc31.h";LABCOMM_NO_STDIO;USE_CRT_OWN_IMPL;_AMD64_;_X64_;_WIN64;STD_CALL;CONDITION_HANDLING;WIN32_LEAN_AND_MEAN;_IDWBUILD;IS_R0;WINNT;_WDM_INCLUDED_;_WIN32_WINNT=_WIN32_WINNT_WINXP;NTDDI_VERSION=NTDDI_WINXPSP2;TC_VER=301;%(PreprocessorDefinitions) - - - - - - \ No newline at end of file -- GitLab From 0a5a512cc3bf8d41ca92a6f6cad4042032eed878 Mon Sep 17 00:00:00 2001 From: Tommy Olofsson Date: Thu, 30 Jun 2016 14:32:53 +0200 Subject: [PATCH 12/13] Undo more unnecc. changes. --- lib/c/2014/labcomm2014_fd_reader.c | 1 + lib/c/2014/labcomm2014_fd_writer.c | 1 + lib/c/2014/labcomm2014_time.c | 16 ++++++++++------ 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/c/2014/labcomm2014_fd_reader.c b/lib/c/2014/labcomm2014_fd_reader.c index 23d5ba5..ad2d222 100644 --- a/lib/c/2014/labcomm2014_fd_reader.c +++ b/lib/c/2014/labcomm2014_fd_reader.c @@ -20,6 +20,7 @@ */ #include +#include #include #include #include "labcomm2014_private.h" diff --git a/lib/c/2014/labcomm2014_fd_writer.c b/lib/c/2014/labcomm2014_fd_writer.c index 0e4d009..42c9dbc 100644 --- a/lib/c/2014/labcomm2014_fd_writer.c +++ b/lib/c/2014/labcomm2014_fd_writer.c @@ -20,6 +20,7 @@ */ #include +#include #include #include #include diff --git a/lib/c/2014/labcomm2014_time.c b/lib/c/2014/labcomm2014_time.c index 8805f90..eb5e1f5 100644 --- a/lib/c/2014/labcomm2014_time.c +++ b/lib/c/2014/labcomm2014_time.c @@ -22,17 +22,21 @@ #include #include "labcomm2014_scheduler_private.h" +#define TIME_time(time, ...) time +#define TIME(func, ...) \ + if (TIME_time(__VA_ARGS__) && \ + TIME_time(__VA_ARGS__)->action->func) { \ + return TIME_time(__VA_ARGS__)->action->func(__VA_ARGS__); \ + } \ + return -ENOSYS; + int labcomm2014_time_free(struct labcomm2014_time *s) { - if (s && s->action->free) - return s->action->free(s); - return -ENOSYS; + TIME(free, s); } int labcomm2014_time_add_usec(struct labcomm2014_time *s, uint32_t usec) { - if (s && s->action->add_usec) - return s->action->add_usec(s, usec); - return -ENOSYS; + TIME(add_usec, s, usec); } -- GitLab From 0c0368aef44be8eb2ad4e97fcdc2a467f0245bd2 Mon Sep 17 00:00:00 2001 From: Tommy Olofsson Date: Thu, 30 Jun 2016 14:42:23 +0200 Subject: [PATCH 13/13] Undo even more unnecc. changes. --- lib/c/2014/labcomm2014_type_signature.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/c/2014/labcomm2014_type_signature.c b/lib/c/2014/labcomm2014_type_signature.c index 1aedd83..85be200 100644 --- a/lib/c/2014/labcomm2014_type_signature.c +++ b/lib/c/2014/labcomm2014_type_signature.c @@ -2,7 +2,6 @@ #include // for memcmp #include // for debug printf -#define CMP_BUFLEN 512 #ifdef LABCOMM2014_USE_UNSUPPORTED_TYPEDEFS /* Dump signature bytes on stdout @@ -78,10 +77,11 @@ static labcomm2014_bool sig_dump_checked(struct labcomm2014_signature_data *sign labcomm2014_bool labcomm2014_signature_cmp( struct labcomm2014_signature_data *s1, struct labcomm2014_signature_data *s2) { - char buf1[CMP_BUFLEN]; - int len1=CMP_BUFLEN; - char buf2[CMP_BUFLEN]; - int len2=CMP_BUFLEN; + int buflen=512; + char buf1[buflen]; + int len1=buflen; + char buf2[buflen]; + int len2=buflen; labcomm2014_bool res1 = labcomm2014_signature_dump(s1, buf1, &len1); labcomm2014_bool res2 = labcomm2014_signature_dump(s2, buf2, &len2); if(res1 || res2) { -- GitLab