ソースを参照

Use proper defines for malloc, free, calloc, realloc

Stefan Profanter 6 年 前
コミット
369103e793

+ 2 - 2
examples/discovery/client_find_servers.c

@@ -128,14 +128,14 @@ int main(void) {
 
         UA_Client *client = UA_Client_new(UA_ClientConfig_standard);
 
-        char *discoveryUrl = (char *) malloc(sizeof(char) * description->discoveryUrls[0].length + 1);
+        char *discoveryUrl = (char *) UA_malloc(sizeof(char) * description->discoveryUrls[0].length + 1);
         memcpy(discoveryUrl, description->discoveryUrls[0].data, description->discoveryUrls[0].length);
         discoveryUrl[description->discoveryUrls[0].length] = '\0';
 
         UA_EndpointDescription *endpointArray = NULL;
         size_t endpointArraySize = 0;
         retval = UA_Client_getEndpoints(client, discoveryUrl, &endpointArraySize, &endpointArray);
-        free(discoveryUrl);
+        UA_free(discoveryUrl);
         if (retval != UA_STATUSCODE_GOOD) {
             UA_Client_disconnect(client);
             UA_Client_delete(client);

+ 7 - 7
examples/discovery/server_multicast.c

@@ -76,8 +76,8 @@ serverOnNetworkCallback(const UA_ServerOnNetwork *serverOnNetwork, UA_Boolean is
                 serverOnNetwork->discoveryUrl.length, serverOnNetwork->discoveryUrl.data);
 
     if (discovery_url != NULL)
-        free(discovery_url);
-    discovery_url = (char*)malloc(serverOnNetwork->discoveryUrl.length + 1);
+        UA_free(discovery_url);
+    discovery_url = (char*)UA_malloc(serverOnNetwork->discoveryUrl.length + 1);
     memcpy(discovery_url, serverOnNetwork->discoveryUrl.data, serverOnNetwork->discoveryUrl.length);
     discovery_url[serverOnNetwork->discoveryUrl.length] = 0;
 }
@@ -132,7 +132,7 @@ int main(int argc, char **argv) {
         UA_String_deleteMembers(&config.applicationDescription.applicationUri);
         UA_Server_delete(server);
         nl.deleteMembers(&nl);
-        free(discovery_url);
+        UA_free(discovery_url);
         return 1;
     }
     UA_LOG_INFO(logger, UA_LOGCATEGORY_SERVER,
@@ -143,7 +143,7 @@ int main(int argc, char **argv) {
         UA_String_deleteMembers(&config.applicationDescription.applicationUri);
         UA_Server_delete(server);
         nl.deleteMembers(&nl);
-        free(discovery_url);
+        UA_free(discovery_url);
         return 1;
     }
     UA_LOG_INFO(logger, UA_LOGCATEGORY_SERVER, "LDS-ME server found on %s", discovery_url);
@@ -158,7 +158,7 @@ int main(int argc, char **argv) {
         UA_String_deleteMembers(&config.applicationDescription.applicationUri);
         UA_Server_delete(server);
         nl.deleteMembers(&nl);
-        free(discovery_url);
+        UA_free(discovery_url);
         return 1;
     }
 
@@ -177,7 +177,7 @@ int main(int argc, char **argv) {
         UA_String_deleteMembers(&config.applicationDescription.applicationUri);
         UA_Server_delete(server);
         nl.deleteMembers(&nl);
-        free(discovery_url);
+        UA_free(discovery_url);
         return (int) retval;
     }
 
@@ -186,7 +186,7 @@ int main(int argc, char **argv) {
     //UA_Array_delete(config.serverCapabilities, config.serverCapabilitiesSize, &UA_TYPES[UA_TYPES_STRING]);
     UA_Server_delete(server);
     nl.deleteMembers(&nl);
-    free(discovery_url);
+    UA_free(discovery_url);
 
     return (int) retval;
 }

+ 2 - 2
examples/server.c

@@ -29,7 +29,7 @@ static UA_ByteString loadCertificate(void) {
 
     fseek(fp, 0, SEEK_END);
     certificate.length = (size_t)ftell(fp);
-    certificate.data = (UA_Byte*)malloc(certificate.length*sizeof(UA_Byte));
+    certificate.data = (UA_Byte*)UA_malloc(certificate.length*sizeof(UA_Byte));
     if(!certificate.data)
         return certificate;
 
@@ -77,7 +77,7 @@ helloWorld(void *methodHandle, const UA_NodeId *objectId,
     UA_String hello = UA_STRING("Hello ");
     UA_String greet;
     greet.length = hello.length + name->length;
-    greet.data = (UA_Byte*)malloc(greet.length);
+    greet.data = (UA_Byte*)UA_malloc(greet.length);
     memcpy(greet.data, hello.data, hello.length);
     memcpy(greet.data + hello.length, name->data, name->length);
     UA_Variant_setScalarCopy(output, &greet, &UA_TYPES[UA_TYPES_STRING]);

+ 1 - 1
examples/server_certificate.c

@@ -26,7 +26,7 @@ static UA_ByteString loadCertificate(void) {
 
     fseek(fp, 0, SEEK_END);
     certificate.length = (size_t)ftell(fp);
-    certificate.data = malloc(certificate.length*sizeof(UA_Byte));
+    certificate.data = UA_malloc(certificate.length*sizeof(UA_Byte));
     if(!certificate.data)
         return certificate;
 

+ 1 - 1
examples/tutorial_server_method.c

@@ -41,7 +41,7 @@ helloWorldMethodCallback(void *handle, const UA_NodeId *objectId,
     UA_String *inputStr = (UA_String*)input->data;
     UA_String tmp = UA_STRING_ALLOC("Hello ");
     if(inputStr->length > 0) {
-        tmp.data = (UA_Byte *)realloc(tmp.data, tmp.length + inputStr->length);
+        tmp.data = (UA_Byte *)UA_realloc(tmp.data, tmp.length + inputStr->length);
         memcpy(&tmp.data[tmp.length], inputStr->data, inputStr->length);
         tmp.length += inputStr->length;
     }

+ 6 - 7
plugins/ua_network_tcp.c

@@ -15,7 +15,6 @@
 #include "ua_log_stdout.h"
 #include "queue.h"
 
-#include <stdlib.h> // malloc, free
 #include <stdio.h> // snprintf
 #include <string.h> // memset
 #include <errno.h>
@@ -147,7 +146,7 @@ static UA_StatusCode
 connection_recv(UA_Connection *connection, UA_ByteString *response,
                 UA_UInt32 timeout) {
     response->data =
-        (UA_Byte*)malloc(connection->localConf.recvBufferSize);
+        (UA_Byte*)UA_malloc(connection->localConf.recvBufferSize);
     if(!response->data) {
         response->length = 0;
         return UA_STATUSCODE_BADOUTOFMEMORY; /* not enough memory retry */
@@ -229,7 +228,7 @@ typedef struct {
 static void
 ServerNetworkLayerTCP_freeConnection(UA_Connection *connection) {
     UA_Connection_deleteMembers(connection);
-    free(connection);
+    UA_free(connection);
 }
 
 static UA_StatusCode
@@ -249,7 +248,7 @@ ServerNetworkLayerTCP_add(ServerNetworkLayerTCP *layer, UA_Int32 newsockfd,
     }
 
     /* Allocate and initialize the connection */
-    ConnectionEntry *e = (ConnectionEntry*)malloc(sizeof(ConnectionEntry));
+    ConnectionEntry *e = (ConnectionEntry*)UA_malloc(sizeof(ConnectionEntry));
     if(!e)
         return UA_STATUSCODE_BADOUTOFMEMORY;
     UA_Connection *c = &e->connection;
@@ -513,11 +512,11 @@ ServerNetworkLayerTCP_deleteMembers(UA_ServerNetworkLayer *nl) {
         LIST_REMOVE(e, pointers);
         connection_close(&e->connection);
         CLOSESOCKET(e->connection.sockfd);
-        free(e);
+        UA_free(e);
     }
 
     /* Free the layer */
-    free(layer);
+    UA_free(layer);
 }
 
 UA_ServerNetworkLayer
@@ -532,7 +531,7 @@ UA_ServerNetworkLayerTCP(UA_ConnectionConfig conf, UA_UInt16 port) {
     UA_ServerNetworkLayer nl;
     memset(&nl, 0, sizeof(UA_ServerNetworkLayer));
     ServerNetworkLayerTCP *layer =
-        (ServerNetworkLayerTCP *)calloc(1,sizeof(ServerNetworkLayerTCP));
+        (ServerNetworkLayerTCP *)UA_calloc(1,sizeof(ServerNetworkLayerTCP));
     if(!layer)
         return nl;
 

+ 9 - 10
plugins/ua_network_udp.c

@@ -2,7 +2,6 @@
  * See http://creativecommons.org/publicdomain/zero/1.0/ for more information. */
 
 #include "ua_network_udp.h"
-#include <stdlib.h> // malloc, free
 #include <stdio.h>
 #include <string.h> // memset
 
@@ -121,7 +120,7 @@ static void setFDSet(ServerNetworkLayerUDP *layer) {
 }
 
 static void closeConnectionUDP(UA_Connection *handle) {
-    free(handle);
+    UA_free(handle);
 }
 
 static UA_StatusCode ServerNetworkLayerUDP_start(UA_ServerNetworkLayer *nl, UA_Logger logger) {
@@ -166,12 +165,12 @@ static size_t ServerNetworkLayerUDP_getJobs(UA_ServerNetworkLayer *nl, UA_Job **
         *jobs = items;
         return 0;
     }
-    items = malloc(sizeof(UA_Job)*(unsigned long)resultsize);
+    items = UA_malloc(sizeof(UA_Job)*(unsigned long)resultsize);
     // read from established sockets
     size_t j = 0;
     UA_ByteString buf = {0, NULL};
     if(!buf.data) {
-        buf.data = malloc(sizeof(UA_Byte) * layer->conf.recvBufferSize);
+        buf.data = UA_malloc(sizeof(UA_Byte) * layer->conf.recvBufferSize);
         if(!buf.data)
             UA_LOG_WARNING(layer->logger, UA_LOGCATEGORY_NETWORK, "malloc failed");
     }
@@ -181,9 +180,9 @@ static size_t ServerNetworkLayerUDP_getJobs(UA_ServerNetworkLayer *nl, UA_Job **
     ssize_t rec_result = recvfrom(layer->serversockfd, buf.data, layer->conf.recvBufferSize, 0, &sender, &sendsize);
     if (rec_result > 0) {
         buf.length = (size_t)rec_result;
-        UDPConnection *c = malloc(sizeof(UDPConnection));
+        UDPConnection *c = UA_malloc(sizeof(UDPConnection));
         if(!c){
-                free(items);
+                UA_free(items);
             return UA_STATUSCODE_BADINTERNALERROR;
         }
         UA_Connection_init(&c->connection);
@@ -206,11 +205,11 @@ static size_t ServerNetworkLayerUDP_getJobs(UA_ServerNetworkLayer *nl, UA_Job **
         j++;
         *jobs = items;
     } else {
-        free(items);
+        UA_free(items);
         *jobs = NULL;
     }
     if(buf.data)
-        free(buf.data);
+        UA_free(buf.data);
     return j;
 }
 
@@ -224,7 +223,7 @@ static size_t ServerNetworkLayerUDP_stop(UA_ServerNetworkLayer *nl, UA_Job **job
 
 static void ServerNetworkLayerUDP_deleteMembers(UA_ServerNetworkLayer *nl) {
     ServerNetworkLayerUDP *layer = nl->handle;
-    free(layer);
+    UA_free(layer);
     UA_String_deleteMembers(&nl->discoveryUrl);
 }
 
@@ -233,7 +232,7 @@ UA_ServerNetworkLayerUDP(UA_ConnectionConfig conf, UA_UInt16 port) {
     UA_ServerNetworkLayer nl;
     memset(&nl, 0, sizeof(UA_ServerNetworkLayer));
 
-    ServerNetworkLayerUDP *layer = malloc(sizeof(ServerNetworkLayerUDP));
+    ServerNetworkLayerUDP *layer = UA_malloc(sizeof(ServerNetworkLayerUDP));
     if(!layer)
         return nl;
     memset(layer, 0, sizeof(ServerNetworkLayerUDP));

+ 2 - 2
tests/check_chunking.c

@@ -78,7 +78,7 @@ START_TEST(encodeStringIntoFiveChunksShallWork) {
     counter = 0;
     dataCount = 0;
     UA_String_init(&string);
-    string.data = (UA_Byte*)malloc(stringLength);
+    string.data = (UA_Byte*)UA_malloc(stringLength);
     string.length = stringLength;
     char tmpString[9] = {'o','p','e','n','6','2','5','4','1'};
     //char tmpString[9] = {'1','4','5','2','6','n','e','p','o'};
@@ -125,7 +125,7 @@ START_TEST(encodeTwoStringsIntoTenChunksShallWork) {
     counter = 0;
     dataCount = 0;
     UA_String_init(&string);
-    string.data = (UA_Byte*)malloc(stringLength);
+    string.data = (UA_Byte*)UA_malloc(stringLength);
     string.length = stringLength;
     char tmpString[9] = {'o','p','e','n','6','2','5','4','1'};
     buffers = (UA_ByteString*)UA_Array_new(chunkCount, &UA_TYPES[UA_TYPES_BYTESTRING]);

+ 2 - 2
tests/check_client.c

@@ -23,7 +23,7 @@ addVariable(size_t size) {
     /* Define the attribute of the myInteger variable node */
     UA_VariableAttributes attr;
     UA_VariableAttributes_init(&attr);
-    UA_Int32* array = (UA_Int32*)malloc(size * sizeof(UA_Int32));
+    UA_Int32* array = (UA_Int32*)UA_malloc(size * sizeof(UA_Int32));
     memset(array, 0, size * sizeof(UA_Int32));
     UA_Variant_setArray(&attr.value, array, size, &UA_TYPES[UA_TYPES_INT32]);
 
@@ -41,7 +41,7 @@ addVariable(size_t size) {
                               parentReferenceNodeId, myIntegerName,
                               UA_NODEID_NULL, attr, NULL, NULL);
 
-    free(array);
+    UA_free(array);
 }
 
 static void * serverloop(void *_) {

+ 1 - 1
tests/check_server_binary_messages.c

@@ -24,7 +24,7 @@ static UA_ByteString readFile(char *filename) {
         fseek(f, 0, SEEK_END);
         length = ftell(f);
         rewind(f);
-        buf.data = (UA_Byte*)malloc(length);
+        buf.data = (UA_Byte*)UA_malloc(length);
         fread(buf.data, sizeof(char), length, f);
         buf.length = length;
         fclose(f);

+ 1 - 1
tests/check_server_userspace.c

@@ -44,7 +44,7 @@ START_TEST(Server_addNamespace_writeService)
     ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
     ck_assert_ptr_eq(namespaces.type, &UA_TYPES[UA_TYPES_STRING]);
 
-    namespaces.data = realloc(namespaces.data, (namespaces.arrayLength + 1) * sizeof(UA_String));
+    namespaces.data = UA_realloc(namespaces.data, (namespaces.arrayLength + 1) * sizeof(UA_String));
     ++namespaces.arrayLength;
     UA_String *ns = (UA_String*)namespaces.data;
     ns[namespaces.arrayLength-1] = UA_STRING_ALLOC("test");

+ 2 - 2
tests/testing_networklayers.c

@@ -8,14 +8,14 @@
 
 static UA_StatusCode
 dummyGetSendBuffer(UA_Connection *connection, size_t length, UA_ByteString *buf) {
-    buf->data = (UA_Byte*)malloc(length);
+    buf->data = (UA_Byte*)UA_malloc(length);
     buf->length = length;
     return UA_STATUSCODE_GOOD;
 }
 
 static void
 dummyReleaseSendBuffer(UA_Connection *connection, UA_ByteString *buf) {
-    free(buf->data);
+    UA_free(buf->data);
 }
 
 static UA_StatusCode

+ 2 - 2
tools/pyUANamespace/open62541_MacroHelper.py

@@ -163,7 +163,7 @@ class open62541_MacroHelper():
                         outArgVal = r.target().value().value
         if len(inArgVal)>0:
             code.append("")
-            code.append("inputArguments = (UA_Argument *) malloc(sizeof(UA_Argument) * " + str(len(inArgVal)) + ");")
+            code.append("inputArguments = (UA_Argument *) UA_malloc(sizeof(UA_Argument) * " + str(len(inArgVal)) + ");")
             code.append("int inputArgumentCnt;")
             code.append("for (inputArgumentCnt=0; inputArgumentCnt<" + str(len(inArgVal)) + "; ++inputArgumentCnt) UA_Argument_init(&inputArguments[inputArgumentCnt]); ")
             argumentCnt = 0
@@ -181,7 +181,7 @@ class open62541_MacroHelper():
                 argumentCnt += 1
         if len(outArgVal)>0:
             code.append("")
-            code.append("outputArguments = (UA_Argument *) malloc(sizeof(UA_Argument) * " + str(len(outArgVal)) + ");")
+            code.append("outputArguments = (UA_Argument *) UA_malloc(sizeof(UA_Argument) * " + str(len(outArgVal)) + ");")
             code.append("int outputArgumentCnt;")
             code.append("for (outputArgumentCnt=0; outputArgumentCnt<" + str(len(outArgVal)) + "; ++outputArgumentCnt) UA_Argument_init(&outputArguments[outputArgumentCnt]); ")
             argumentCnt = 0