From d73fd4e398c06ca3a5cbe1539c61f0a3063a9adf Mon Sep 17 00:00:00 2001 From: Arnaud Blanchard <arnaud.blanchard@ensea.fr> Date: Mon, 24 May 2021 22:59:52 +0200 Subject: [PATCH] Simplification --- include/blc.h | 7 +- include/blc_array_network.h | 2 +- include/blc_channel.h | 14 +-- include/blc_core.h | 10 -- include/blc_image.h | 5 +- include/blc_network_base.h | 3 +- include/blc_program.h | 1 - include/blc_text.h | 4 +- src/channel/blc_channel.cpp | 170 +++++++++++++++--------------- src/channel/channel_file.cpp | 11 +- src/core/blc_array.cpp | 13 ++- src/core/blc_mem.cpp | 2 +- src/core/blc_text.cpp | 60 ++++++++++- src/network/blc_array_network.cpp | 1 - src/process/blc_process.cpp | 3 +- src/program/blc_loop.cpp | 4 + 16 files changed, 177 insertions(+), 133 deletions(-) delete mode 100644 include/blc_core.h diff --git a/include/blc.h b/include/blc.h index 61079b7..f0ccc5f 100644 --- a/include/blc.h +++ b/include/blc.h @@ -1,9 +1,12 @@ #ifndef BLC_H #define BLC_H - +#include "blc_text.h" +#include "blc_tools.h" +#include "blc_mem.h" +#include "blc_array.h" +#include "blc_realtime.h" #include "blc_channel.h" -#include "blc_core.h" #include "blc_image.h" #include "blc_network.h" #include "blc_process.h" diff --git a/include/blc_array_network.h b/include/blc_array_network.h index 9a15d8c..69fffff 100644 --- a/include/blc_array_network.h +++ b/include/blc_array_network.h @@ -8,7 +8,7 @@ #ifndef blc_array_network_h #define blc_array_network_h -#include "blc_core.h" +#include "blc_array.h" typedef struct blc_array_network #ifdef __cplusplus diff --git a/include/blc_channel.h b/include/blc_channel.h index 4faa15e..938c623 100644 --- a/include/blc_channel.h +++ b/include/blc_channel.h @@ -19,12 +19,12 @@ #ifndef BLC_CHANNEL_H #define BLC_CHANNEL_H -#include "blc_core.h" - #include <limits.h> //< NAME_MAX, ... #include <fcntl.h> //< O_... constants O_RDONLY, 0_RDWR, O_WRONLY #include <semaphore.h> +#include "blc_array.h" + #ifdef __cplusplus #include <map> #include <string> @@ -32,6 +32,8 @@ #include <vector> #endif + + /**\mainpage Summary The principle of blc_channel is to share data among different process through shared memory. Shared memory are opened with POSIX shm_open. The file /tmp/blc_channels.txt maintain a list of all the blc_channels. @@ -73,12 +75,9 @@ typedef struct blc_channel //Warning copying channel with data, the new data is not mapped anymore blc_channel(blc_channel const &channel); - blc_channel(blc_channel &&channel); - blc_channel& operator=(blc_channel const &input); - //Unmap the memory and close the file but the other informations are kept. The semaphores are not affected. @TODO See what to do with semaphores ~blc_channel(); @@ -160,7 +159,7 @@ typedef struct blc_channel int id, access_mode; char name[NAME_MAX+1]; char parameter[BLC_PARAMETER_MAX+1]; - int fd ; ///< shared memory file descriptor + int fd ; //< shared memory file descriptor sem_t *sem_new_data, *sem_ack_data; }blc_channel; @@ -231,10 +230,7 @@ typedef struct blc_channel ///Remove the blc_channel **name**. The other processes using it will still work but no new ones can use it. void blc_remove_channel_with_name(char const *name); - - void blc_channel_post_event(); - void blc_channel_check_for_event(void (*callback)(void*user_data), void *user_data); END_EXTERN_C diff --git a/include/blc_core.h b/include/blc_core.h deleted file mode 100644 index 305f4ee..0000000 --- a/include/blc_core.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef BLC_CORE_H -#define BLC_CORE_H - -#include "blc_text.h" -#include "blc_tools.h" -#include "blc_mem.h" -#include "blc_array.h" -#include "blc_realtime.h" - -#endif diff --git a/include/blc_image.h b/include/blc_image.h index eae2ef3..094fe98 100644 --- a/include/blc_image.h +++ b/include/blc_image.h @@ -27,9 +27,9 @@ Specific functions to manipulate blc_array as images: #ifndef BLC_IMAGE_H #define BLC_IMAGE_H -#include "blc_core.h" #include "stdint.h" //uint32_t #include <array> +#include "blc_array.h" /** @defgroup blc_image blc_image functions @@ -48,9 +48,6 @@ struct blc_image:blc_array{ int elements_per_pixel, width, height; }; - - -auto create_blc_image(uint32_t type, uint32_t format, int width, int height); #endif START_EXTERN_C diff --git a/include/blc_network_base.h b/include/blc_network_base.h index 3afea85..97758f6 100644 --- a/include/blc_network_base.h +++ b/include/blc_network_base.h @@ -18,9 +18,8 @@ #include <netinet/in.h> #include <netdb.h> #include <sys/socket.h> -#include "blc_core.h" #include <string> - +#include "blc_mem.h" /*By default it is a client*/ #define BLC_SERVER 1 #define BLC_UDP4 2 diff --git a/include/blc_program.h b/include/blc_program.h index 3ab7eaa..1e6fd87 100644 --- a/include/blc_program.h +++ b/include/blc_program.h @@ -26,7 +26,6 @@ #ifndef BLC_PROGRAM_H #define BLC_PROGRAM_H -#include "blc_core.h" #include "blc_command.h" diff --git a/include/blc_text.h b/include/blc_text.h index 6a7f360..2574273 100644 --- a/include/blc_text.h +++ b/include/blc_text.h @@ -163,7 +163,9 @@ void fscan_tsv_floats(FILE *file, float *values, int values_nb); ///Read tab separated uchars in the files and set values with the result. void fscan_tsv_uchars(FILE *file, uchar *values, int values_nb); -void blc_fprint_char_graph(FILE *file, char *values, int values_nb, char const *title, int width, int height, int max, int min, char const* abscissa_name, char const* ordinate_name); +void blc_fprint_char_graph(FILE *file, char const *values, int values_nb, char const *title, int width, int height, int max, int min, char const* abscissa_name, char const* ordinate_name); +void blc_fprint_uchar_graph(FILE *file, uchar const *values, int values_nb, char const *title, int width, int height, int max, int min, char const* abscissa_name, char const* ordinate_name); + void blc_fprint_float_graph(FILE *file, float *values, int values_nb, char const *title, int width, int height, float max, float min, char const* abscissa_name, char const* ordinate_name); /**Display a surface graph of a 3D array in text mode ( tipically a image). If step0 and lenght0 = 1 it works with a 2D array. diff --git a/src/channel/blc_channel.cpp b/src/channel/blc_channel.cpp index dd57769..9bb8e15 100644 --- a/src/channel/blc_channel.cpp +++ b/src/channel/blc_channel.cpp @@ -36,8 +36,6 @@ #include <fstream> #include <array> -#include "blc_core.h" - #define TMP_BUFFER_SIZE 4096 #define BLC_CHANNELS_LIST_PATH "/tmp/blc_channels.txt" @@ -61,6 +59,7 @@ blc_channel::blc_channel(char const *new_name, int mode, uint32_t type, uint32_t uint32_t new_type_str, new_format_str; blc_channel info; + this->access_mode=mode; id = blc_channel_get_info_with_name(&info, new_name); if (id==-1) { def_array(type, format, lengths); @@ -81,12 +80,12 @@ blc_channel::blc_channel(char const *new_name, int mode, uint32_t type, uint32_t } } /* -blc_channel::blc_channel(char const *new_name, int mode, uint32_t type, uint32_t format, int dims_nb, int length, ...):blc_channel(){ - va_list arguments; - va_start(arguments, length); - vcreate_or_open(new_name, mode, type,format, dims_nb, length, arguments); - va_end(arguments); -} + blc_channel::blc_channel(char const *new_name, int mode, uint32_t type, uint32_t format, int dims_nb, int length, ...):blc_channel(){ + va_list arguments; + va_start(arguments, length); + vcreate_or_open(new_name, mode, type,format, dims_nb, length, arguments); + va_end(arguments); + } */ blc_channel::blc_channel(blc_channel const &channel):blc_array(channel),id(channel.id), fd(channel.fd), sem_new_data(nullptr), sem_ack_data(nullptr) { @@ -101,7 +100,7 @@ blc_channel::blc_channel(blc_channel &&channel):blc_array(channel), id(channel.i blc_channel& blc_channel::operator=(blc_channel const &other){ if (this==&other) return *this; - + blc_array::operator=(other); STRCPY(name, other.name); STRCPY(parameter, other.parameter); @@ -328,11 +327,11 @@ void blc_channel::create(char const *new_name, int access_mode, uint32_t type, u } /* -///Create a blc channel -void blc_channel::vcreate(char const *new_name, int mode, uint32_t type, uint32_t format, int dims_nb, size_t length, va_list arguments){ - vdef_array(type, format, dims_nb, length, arguments); - create(new_name, mode); -} + ///Create a blc channel + void blc_channel::vcreate(char const *new_name, int mode, uint32_t type, uint32_t format, int dims_nb, size_t length, va_list arguments){ + vdef_array(type, format, dims_nb, length, arguments); + create(new_name, mode); + } */ void blc_channel::create(char const *new_name, int mode, uint32_t type, uint32_t format, const char *size_string){ @@ -455,17 +454,17 @@ int blc_channel::create_or_open(char const *new_name, int access_mode, uint32_t ///Create a blc channel /*int blc_channel::create_or_open(char const *new_name, int access_mode, uint32_t type, uint32_t format, vector<size_t> lengths){ - int dims_nb; - blc_dim dims[BLC_ARRAY_DIMS_MAX]; - - size_t size= - dims_nb = lengths.size(); - for (int i; i!=dims_nb; i++) { - dims[i].step=; - dims[i].length=lengths[i]; - } - return create_or_open(new_name, access_mode, type, format, dims_nb, dims); -}*/ + int dims_nb; + blc_dim dims[BLC_ARRAY_DIMS_MAX]; + + size_t size= + dims_nb = lengths.size(); + for (int i; i!=dims_nb; i++) { + dims[i].step=; + dims[i].length=lengths[i]; + } + return create_or_open(new_name, access_mode, type, format, dims_nb, dims); + }*/ int blc_channel::create_or_open(char const *new_name, int mode, uint32_t type, uint32_t format, char const *dims_string){ int created=1; @@ -476,7 +475,6 @@ int blc_channel::create_or_open(char const *new_name, int mode, uint32_t type, u id = blc_channel_get_info_with_name(this, new_name); if (id==-1) create(new_name, mode, type, format, dims_string); else{ - if (this->dims_nb != dims_nb) EXIT_ON_CHANNEL_ERROR(this, "Reopening a blc_channel with different dims_nb '%d'.", dims_nb); if (this->type!=type) EXIT_ON_CHANNEL_ERROR(this, "Reopening blc_channel with another type '%.4s'", UINT32_TO_STRING(new_type_str, type)); if (this->format!=format) EXIT_ON_CHANNEL_ERROR(this, "Reopening blc_channel with another format '%.4s'", UINT32_TO_STRING(new_format_str, format)); @@ -504,13 +502,11 @@ int blc_channel::create_or_open(char const *name, int mode){ } blc_channel::~blc_channel(){ - if (data){ + if (fd != -1){ //Not optimal but works the same with C and C++ SYSTEM_SUCCESS_CHECK(munmap(data, size), 0, "Closing blc_channel '%s'. Size '%lu'", name, size); - data=NULL; - } - - if (fd!=-1){ + data=nullptr; //Avoid ~blc_array to free the memory close(fd); + fd=-1; blc_channel_post_event(); } } @@ -578,26 +574,26 @@ bool blc_channel::operator<(blc_channel &b){ /** This reuse the vecor to fill it with new values. The vector must be and will be sorted with id. This admit that any new channel have an higher id. - -pair<vector<int>, vector<int>> blc_channel::get_update_infos(vector<blc_channel> &channels, string filter){ - FILE *file=fopen(BLC_CHANNELS_LIST_PATH, "r"); - if (file == nullptr) { - channels.clear(); - return; - } - - while(fscanf(file, "%*[ \t\n]s")!=EOF){ - channel_info.fscan_info(file, 1); - - lower_bound(channels.cbegin(), channels.cend(), channel_info) - if (filter.empty() || strcmp(filter.data(), channel_info.name)==0){ - - channels_vector.insert(); - channel_info.dims=nullptr; //Avoid dims to be removed by the destructor or fscan_info - } - } - fclose(file); -}*/ + + pair<vector<int>, vector<int>> blc_channel::get_update_infos(vector<blc_channel> &channels, string filter){ + FILE *file=fopen(BLC_CHANNELS_LIST_PATH, "r"); + if (file == nullptr) { + channels.clear(); + return; + } + + while(fscanf(file, "%*[ \t\n]s")!=EOF){ + channel_info.fscan_info(file, 1); + + lower_bound(channels.cbegin(), channels.cend(), channel_info) + if (filter.empty() || strcmp(filter.data(), channel_info.name)==0){ + + channels_vector.insert(); + channel_info.dims=nullptr; //Avoid dims to be removed by the destructor or fscan_info + } + } + fclose(file); + }*/ vector<blc_channel> blc_channel::get_all_infos(string filter){ blc_channel channel_info; @@ -606,7 +602,7 @@ vector<blc_channel> blc_channel::get_all_infos(string filter){ ifstream file(BLC_CHANNELS_LIST_PATH, ifstream::in); if (file.is_open()) { - // while(sscanf(line.data(), "%*[ \t\n]s")!=EOF){ + // while(sscanf(line.data(), "%*[ \t\n]s")!=EOF){ do{ file.getline(line.data(), line.size()); channel_info.sscan_info(line.data(), 1); @@ -620,38 +616,38 @@ vector<blc_channel> blc_channel::get_all_infos(string filter){ /* -pair<unique_ptr<vector<blc_channel>new_channels>,unique_ptr<vector<blc_channel*> removed_channels>> blc_channel::update_infos(map<int, blc_channel> &channels, string filter){ - blc_channel channel_info; - FILE *file; - unique_ptr<vector<blc_channel>*> new_channels; - unique_ptr<vector<blc_channel>> removed_channels; - - file=fopen(BLC_CHANNELS_LIST_PATH, "r"); - if (file == nullptr) { - removed_channels->insert(removed_channels->begin(), channels); - channels.clear(); - return make_pair(make_unique<vector<blc_channel*>>, removed_channels); - } - - auto it=channels.begin(); - while(fscanf(file, "%*[ \t\n]s")!=EOF){ - channel_info.fscan_info(file, 1); - if (filter.empty() || strcmp(filter.data(), channel_info.name)==0){ - while (it->first < channel_info.id){ - channels.erase(it); - it++; - } - if (it->first != channel_info.id) { - channels.emplace(channel_info.id, channel_info); - channel_info.dims=nullptr; //Avoid dims to be removed by the destructor or fscan_info - } - it++; - } - } - while (it!=channels.end()){ - channels.erase(it); - it++; - } - fclose(file); -} -*/ + pair<unique_ptr<vector<blc_channel>new_channels>,unique_ptr<vector<blc_channel*> removed_channels>> blc_channel::update_infos(map<int, blc_channel> &channels, string filter){ + blc_channel channel_info; + FILE *file; + unique_ptr<vector<blc_channel>*> new_channels; + unique_ptr<vector<blc_channel>> removed_channels; + + file=fopen(BLC_CHANNELS_LIST_PATH, "r"); + if (file == nullptr) { + removed_channels->insert(removed_channels->begin(), channels); + channels.clear(); + return make_pair(make_unique<vector<blc_channel*>>, removed_channels); + } + + auto it=channels.begin(); + while(fscanf(file, "%*[ \t\n]s")!=EOF){ + channel_info.fscan_info(file, 1); + if (filter.empty() || strcmp(filter.data(), channel_info.name)==0){ + while (it->first < channel_info.id){ + channels.erase(it); + it++; + } + if (it->first != channel_info.id) { + channels.emplace(channel_info.id, channel_info); + channel_info.dims=nullptr; //Avoid dims to be removed by the destructor or fscan_info + } + it++; + } + } + while (it!=channels.end()){ + channels.erase(it); + it++; + } + fclose(file); + } + */ diff --git a/src/channel/channel_file.cpp b/src/channel/channel_file.cpp index d4ec22b..8f374ae 100644 --- a/src/channel/channel_file.cpp +++ b/src/channel/channel_file.cpp @@ -32,11 +32,13 @@ #include <sys/stat.h> // mode S_ ... constants #include <sys/time.h> //gettimeofday #include <semaphore.h> -#include "blc_core.h" #include <stack> #include <vector> #include <tuple> +#include "blc_realtime.h" +#include "blc_text.h" + #define TMP_BUFFER_SIZE 4096 #define BLC_CHANNELS_LIST_PATH "/tmp/blc_channels.txt" @@ -48,7 +50,7 @@ static int blc_channel_event_id=0; static void (*blc_channel_event_callback)(void*)=NULL; static void *blc_channel_event_user_data=NULL; -pthread_t thread; +pthread_t pthread; START_EXTERN_C @@ -260,12 +262,11 @@ void blc_channel_check_for_event(void (*callback)(void*), void *user_data) { blc_channel_event_callback=callback; blc_channel_event_user_data=user_data; - BLC_PTHREAD_CHECK(pthread_create(&thread, NULL, blc_channel_thread_manager, NULL), NULL); + BLC_PTHREAD_CHECK(pthread_create(&pthread, NULL, blc_channel_thread_manager, NULL), NULL); } - void blc_channel_destroy(blc_channel *channel){ - channel->~blc_channel(); + delete channel; } void blc_channel_wait_new_data(void *channel_pt){ diff --git a/src/core/blc_array.cpp b/src/core/blc_array.cpp index 892370a..8c64a89 100644 --- a/src/core/blc_array.cpp +++ b/src/core/blc_array.cpp @@ -34,15 +34,13 @@ blc_array::blc_array(uint32_t type, uint32_t format, vector<size_t> const &lengt } void blc_array::def_array(uint32_t type, uint32_t format, vector<size_t> const &lengths){ - assert(lengths.size()>BLC_ARRAY_DIMS_MAX); - total_length=1; - this->dims_nb=lengths.size(); this->type=type; this->format=format; - total_length=size/get_type_size(); + set_dims(type, lengths); } void blc_array::def_array(uint32_t type, uint32_t format, int dims_nb, blc_dim const dims[BLC_ARRAY_DIMS_MAX]){ + assert(dims_nb<=BLC_ARRAY_DIMS_MAX); this->type=type; this->format=format; this->dims_nb=dims_nb; @@ -63,12 +61,13 @@ void blc_array::set_dims(uint32_t type, vector<size_t> const &lengths){ dims_nb=lengths.size(); size=blc_get_type_size(type); size_t length; - for (int i; i!=dims_nb; i++){ + for (int i=0; i!=dims_nb; i++){ length=lengths[i]; dims[i].step=size; - size*=length; dims[i].length=length; + size*=length; } + total_length=size/get_type_size(); } /* void blc_array::init(char const *properties){ @@ -94,7 +93,7 @@ void blc_array::init(uint32_t type, uint32_t format, int dims_nb, int length, .. } */ void blc_array::add_dim(size_t length, size_t step){ - assert(dims_nb<BLC_ARRAY_DIMS_MAX); + assert(dims_nb<=BLC_ARRAY_DIMS_MAX); dims[dims_nb].length=length; dims[dims_nb].step=step; size=step*length; //Check if it always true ?? diff --git a/src/core/blc_mem.cpp b/src/core/blc_mem.cpp index f40d40e..5c4a362 100644 --- a/src/core/blc_mem.cpp +++ b/src/core/blc_mem.cpp @@ -139,7 +139,7 @@ void blc_mem_append(blc_mem *mem, const char *data, size_t size){ mem->append(data, size); } -void blc_mems_fprint_graph_uchars(blc_mem *const*mems, int mems_nb, FILE *file, char const * const *titles, int height, int max, int min, char const* abscissa_name, char const* ordinate_name ){ +void blc_mems_fprint_graph_uchars(blc_mem *const*mems, int mems_nb, FILE *file, char const * const *titles, int height, int max, int min, char const* abscissa_name, char const* ordinate_name ){ unsigned char value; size_t i, total_size = 0; diff --git a/src/core/blc_text.cpp b/src/core/blc_text.cpp index 9e48c53..f6d130d 100644 --- a/src/core/blc_text.cpp +++ b/src/core/blc_text.cpp @@ -299,7 +299,65 @@ static void fprint_ordinate(FILE *file, const char* ordinate_name, int line){ } -void blc_fprint_char_graph(FILE *file, char *values, int values_nb, char const *title, int width, int height, int max, int min, char const* abscissa_name, char const* ordinate_name){ + + + +void blc_fprint_char_graph(FILE *file, char const *values, int values_nb, char const *title, int width, int height, int max, int min, char const* abscissa_name, char const* ordinate_name){ + float width_ratio; + int range, value; + int percent; + int column, line; + float threshold, threshold1; + int i, display_values_nb; + + + //Size for the title + height-=2; + range = max - min; + + //If width == -1 we take the size we need + if (width==-1) width = values_nb * 3+1; + + display_values_nb=(width-1)/3; + if (display_values_nb > values_nb) display_values_nb=values_nb; + width =display_values_nb*3+1; //round + + width_ratio=values_nb/(float)display_values_nb; + + for (i = fprintf(file, "100%%(%.2d) [%s] ", max, title); i < width-1; ++i){ + fputc('-', file); + } + fprintf(file, "\n"); + + FOR(line, height){ + fprint_ordinate(file, ordinate_name, line); + + threshold = range-(line+1) * range / height + min; + threshold1 = range-line * range / height + min; + + //We draw data + FOR(column, display_values_nb){ + value = values[(int) (column * width_ratio)]; + if (value < threshold) fprintf(file, " "); + else if (value <= threshold1) { + percent=99 * (value - min) / range; + if (percent >= 100) percent=99; + fprintf(file, " %.2d", percent); + } + else fprintf(file, " []"); + } + fprintf(file, "\n"); + } + + if (abscissa_name==NULL) abscissa_name=""; + for (i = fprintf(file, " 0%%(%.3d) %s ", min, abscissa_name); i < width-1; ++i) + fputc('-', file); + fputc('>', file); + + fprintf(file, "\n"); +} + +void blc_fprint_uchar_graph(FILE *file, uchar const *values, int values_nb, char const *title, int width, int height, int max, int min, char const* abscissa_name, char const* ordinate_name){ float width_ratio; int range, value; int percent; diff --git a/src/network/blc_array_network.cpp b/src/network/blc_array_network.cpp index 57537bb..b4b2ffe 100644 --- a/src/network/blc_array_network.cpp +++ b/src/network/blc_array_network.cpp @@ -2,7 +2,6 @@ #include <unistd.h> - blc_array_network::~blc_array_network(){ SYSTEM_ERROR_CHECK(close(socket_fd), -1, nullptr); } diff --git a/src/process/blc_process.cpp b/src/process/blc_process.cpp index f857e7d..1013901 100644 --- a/src/process/blc_process.cpp +++ b/src/process/blc_process.cpp @@ -9,12 +9,13 @@ #include "blc_process.h" //#include "blc_program.h" //#include "blc_command.h" -#include "blc_core.h" #include <unistd.h> //read #include <errno.h> //EINTR #include <sys/wait.h> //waitpid #include <inttypes.h> +#include "blc_mem.h" + extern char** environ; //From unistd for env var blc_file *blc_files=NULL; diff --git a/src/program/blc_loop.cpp b/src/program/blc_loop.cpp index d3d8afe..dc6286e 100644 --- a/src/program/blc_loop.cpp +++ b/src/program/blc_loop.cpp @@ -6,6 +6,10 @@ #include <sys/time.h> //gettimeofday #include <pthread.h> //pthread_mutex #include "blc_program.h" +#include <semaphore.h> //sem_t +#include "blc_realtime.h" +#include "blc_text.h" + struct timeval blc_loop_timer; -- GitLab