From c8ad91bb79e5511690471899b322fc25e4dc8870 Mon Sep 17 00:00:00 2001 From: Daniel Maximilian Swoboda Date: Mon, 18 Jan 2016 16:08:01 +0100 Subject: [PATCH] reduced memory consumption --- RSAL/LINKV1.c | 43 ++++++++++++++++++++++++++++++++++++------- RSAL/RSAL.c | 18 +++++++++--------- 2 files changed, 45 insertions(+), 16 deletions(-) diff --git a/RSAL/LINKV1.c b/RSAL/LINKV1.c index 0613f81..43eb630 100644 --- a/RSAL/LINKV1.c +++ b/RSAL/LINKV1.c @@ -36,7 +36,6 @@ int motorStat4 = 0; void init(){ enable_servos(); - } char* getDigitalWrapped(int port){ @@ -143,7 +142,15 @@ char* generateConnLAO(){ json_object_dotset_string(root_object_lao, "ConnLAO.Controller.Motor 3.Motor 4 Button.ControlType", "Button"); json_object_dotset_string(root_object_lao, "ConnLAO.Controller.Motor 3.Motor 4 Button.Descriptor", "Switch"); - return json_serialize_to_string_pretty(root_value_lao); + char* result = json_serialize_to_string_pretty(root_value_lao); + + //Free up variables + free(root_value_lao); + free(root_object_lao); + root_value_lao = 0; + root_object_lao = 0; + + return result; } char* generateDataMsg(){ @@ -167,14 +174,22 @@ char* generateDataMsg(){ json_object_dotset_string(root_object, "Data.Digital 14", getDigitalWrapped(14)); json_object_dotset_string(root_object, "Data.Digital 15", getDigitalWrapped(15)); - return json_serialize_to_string_pretty(root_value); + + char* result = json_serialize_to_string_pretty(root_value); + + //Free up variables + free(root_value); + free(root_object); + root_value = 0; + root_object = 0; + + return result; } void control(char* msg){ ctrl_root_value = json_parse_string(msg); - - input = json_value_get_object(ctrl_root_value); + if(json_object_dotget_string(input, "Control.Motor 1 Button") != NULL){ if(motorStat1 == 0){ motorStat1 = 1; @@ -262,15 +277,29 @@ void control(char* msg){ if((number = (int)json_object_dotget_number(input, "Control.Slider S4")) != 0){ servoPos4 = number; } + + //Free up variables + free(ctrl_root_value); + free(input); + input = 0; + ctrl_root_value = 0; } int is_get(char * msg){ + int returnStatus = 0; ctrl_root_value = json_parse_string(msg); input = json_value_get_object(ctrl_root_value); if(json_object_dotget_string(input, "GET") != NULL){ - return 1; + returnStatus = 1; } - return 0; + + //Free up variables + free(ctrl_root_value); + free(input); + input = 0; + ctrl_root_value = 0; + + return returnStatus; } diff --git a/RSAL/RSAL.c b/RSAL/RSAL.c index cb21427..6eb3673 100644 --- a/RSAL/RSAL.c +++ b/RSAL/RSAL.c @@ -1,6 +1,6 @@ /* -MIDaC KIPR Link ROSL (Robotic System Abstraction Layer) -Version 0.1 +MIDaC KIPR Link RSAL (Robotic System Abstraction Layer) +Version 0.9 */ #include #include @@ -18,15 +18,16 @@ struct sockaddr_un remote; char str[2048]; void sendMsg(char* msg){ - if(strlen(msg) > 2047){ - int ward = (strlen(msg)/2048) + 1; + int lenghtOfMsg = strlen(msg); + if(lenghtOfMsg > 2047){ + int ward = (lenghtOfMsg/2048) + 1; char* message = malloc(2048*ward); strcpy(message, msg); - for(i=strlen(msg); i<2048*ward; i++){ + for(i=lenghtOfMsg; i<2048*ward; i++){ message[i] = ' '; } message[2048*ward] = '\0'; - if (send(sock, message, strlen(message), 0) == -1) { + if (send(sock, message, lenghtOfMsg, 0) == -1) { perror("send"); exit(1); } @@ -34,11 +35,11 @@ void sendMsg(char* msg){ }else{ char* message = malloc(2048); strcpy(message, msg); - for(i=strlen(msg); i<2048; i++){ + for(i=lenghtOfMsg; i<2048; i++){ message[i] = ' '; } message[2048] = '\0'; - if (send(sock, message, strlen(message), 0) == -1) { + if (send(sock, message, lenghtOfMsg, 0) == -1) { perror("send"); exit(1); } @@ -73,7 +74,6 @@ int main(int argc, char *argv[]) exit(1); } - remote.sun_family = AF_UNIX; strcpy(remote.sun_path, SOCK_PATH); len = strlen(remote.sun_path) + sizeof(remote.sun_family) + 1;