소스 검색

Fix build on windows.
Use alloca instead of C99 variable length arrays.

Julius Pfrommer 10 년 전
부모
커밋
7aa7b32f89
8개의 변경된 파일44개의 추가작업 그리고 12개의 파일을 삭제
  1. 3 0
      CMakeLists.txt
  2. 2 2
      examples/networklayer_tcp.c
  3. 1 1
      examples/opcuaServer.c
  4. 4 4
      src/server/ua_services_attribute.c
  5. 2 2
      src/server/ua_services_nodemanagement.c
  6. 3 0
      src/ua_util.c
  7. 25 1
      src/ua_util.h
  8. 4 2
      tools/type_lists.py

+ 3 - 0
CMakeLists.txt

@@ -140,18 +140,21 @@ file(MAKE_DIRECTORY "${PROJECT_BINARY_DIR}/src_generated")
 include_directories("${PROJECT_BINARY_DIR}/src_generated") 
 add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/src_generated/ua_types_generated.c
                           ${PROJECT_BINARY_DIR}/src_generated/ua_types_generated.h
+                   PRE_BUILD
                    COMMAND python ${PROJECT_SOURCE_DIR}/tools/generate_builtin.py --export-prototypes ${generate_src_options} ${PROJECT_SOURCE_DIR}/tools/schema/Opc.Ua.Types.bsd ${PROJECT_BINARY_DIR}/src_generated/ua_types_generated
                    DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/generate_builtin.py
                            ${CMAKE_CURRENT_SOURCE_DIR}/tools/schema/Opc.Ua.Types.bsd)
 
 add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/src_generated/ua_namespace_0.c
                           ${PROJECT_BINARY_DIR}/src_generated/ua_namespace_0.h
+                   PRE_BUILD
                    COMMAND python ${PROJECT_SOURCE_DIR}/tools/generate_namespace.py ${generate_src_options} ${PROJECT_SOURCE_DIR}/tools/schema/NodeIds.csv ${PROJECT_BINARY_DIR}/src_generated/ua_namespace_0
                    DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/generate_namespace.py
                            ${CMAKE_CURRENT_SOURCE_DIR}/tools/schema/NodeIds.csv)
 
 add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/src_generated/ua_transport_generated.c
                           ${PROJECT_BINARY_DIR}/src_generated/ua_transport_generated.h
+                   PRE_BUILD
                    COMMAND python ${PROJECT_SOURCE_DIR}/tools/generate_builtin.py --additional-includes=ua_transport.h ${PROJECT_SOURCE_DIR}/tools/schema/Custom.Opc.Ua.Transport.bsd ${PROJECT_BINARY_DIR}/src_generated/ua_transport_generated
                    DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/generate_namespace.py
                            ${CMAKE_CURRENT_SOURCE_DIR}/tools/schema/Custom.Opc.Ua.Transport.bsd)

+ 2 - 2
examples/networklayer_tcp.c

@@ -4,6 +4,7 @@
  */
 
 #ifdef WIN32
+#include <malloc.h>
 #include <winsock2.h>
 #include <sys/types.h>
 #include <Windows.h>
@@ -133,7 +134,7 @@ void writeCallback(TCPConnectionHandle *handle, UA_ByteStringArray gather_buf) {
 	UA_UInt32 total_len = 0;
 	UA_UInt32 nWritten = 0;
 #ifdef WIN32
-	LPWSABUF buf = malloc(gather_buf.stringsSize * sizeof(WSABUF));
+	LPWSABUF buf = _alloca(gather_buf.stringsSize * sizeof(WSABUF));
 	int result = 0;
 	for(UA_UInt32 i = 0; i<gather_buf.stringsSize; i++) {
 		buf[i].buf = gather_buf.strings[i].data;
@@ -149,7 +150,6 @@ void writeCallback(TCPConnectionHandle *handle, UA_ByteStringArray gather_buf) {
 		} while (errno == EINTR);
 		nWritten += n;
 	}
-	free(buf);
 #else
 	struct iovec iov[gather_buf.stringsSize];
 	for(UA_UInt32 i=0;i<gather_buf.stringsSize;i++) {

+ 1 - 1
examples/opcuaServer.c

@@ -52,7 +52,7 @@ int main(int argc, char** argv) {
 	signal(SIGINT, stopHandler); /* catches ctrl-c */
 
 	UA_String endpointUrl;
-    UA_String_copycstring("opc.tcp://192.168.56.101:16664",&endpointUrl);
+    UA_String_copycstring("opc.tcp://localhost:16664",&endpointUrl);
 	UA_ByteString certificate = loadCertificate();
 	UA_Server *server = UA_Server_new(&endpointUrl, &certificate);
 

+ 4 - 4
src/server/ua_services_attribute.c

@@ -195,9 +195,9 @@ void Service_Read(UA_Server *server, UA_Session *session, const UA_ReadRequest *
     }
 
     /* ### Begin External Namespaces */
-    UA_Boolean isExternal[request->nodesToReadSize];
+    UA_Boolean *isExternal = UA_alloca(sizeof(UA_Boolean) * request->nodesToReadSize);
     memset(isExternal, UA_FALSE, sizeof(UA_Boolean)*request->nodesToReadSize);
-    UA_UInt32 indices[request->nodesToReadSize];
+    UA_UInt32 *indices = UA_alloca(sizeof(UA_UInt32) * request->nodesToReadSize);
     for(UA_Int32 j = 0;j<server->externalNamespacesSize;j++) {
         UA_UInt32 indexSize = 0;
         for(UA_Int32 i = 0;i < request->nodesToReadSize;i++) {
@@ -361,9 +361,9 @@ void Service_Write(UA_Server *server, UA_Session *session,
     }
 
     /* ### Begin External Namespaces */
-    UA_Boolean isExternal[request->nodesToWriteSize];
+    UA_Boolean *isExternal = UA_alloca(sizeof(UA_Boolean) * request->nodesToWriteSize);
     memset(isExternal, UA_FALSE, sizeof(UA_Boolean)*request->nodesToWriteSize);
-    UA_UInt32 indices[request->nodesToWriteSize];
+    UA_UInt32 *indices = UA_alloca(sizeof(UA_UInt32) * request->nodesToWriteSize);
     for(UA_Int32 j = 0;j<server->externalNamespacesSize;j++) {
         UA_UInt32 indexSize = 0;
         for(UA_Int32 i = 0;i < request->nodesToWriteSize;i++) {

+ 2 - 2
src/server/ua_services_nodemanagement.c

@@ -124,9 +124,9 @@ void Service_AddNodes(UA_Server *server, UA_Session *session, const UA_AddNodesR
     }
 
     /* ### Begin External Namespaces */
-    UA_Boolean isExternal[request->nodesToAddSize];
+    UA_Boolean *isExternal = UA_alloca(sizeof(UA_Boolean) * request->nodesToAddSize);
     memset(isExternal, UA_FALSE, sizeof(UA_Boolean)*request->nodesToAddSize);
-    UA_UInt32 indices[request->nodesToAddSize];
+    UA_UInt32 *indices = UA_alloca(sizeof(UA_UInt32) * request->nodesToAddSize);
     for(UA_Int32 j = 0;j<server->externalNamespacesSize;j++) {
         UA_UInt32 indexSize = 0;
         for(UA_Int32 i = 0;i < request->nodesToAddSize;i++) {

+ 3 - 0
src/ua_util.c

@@ -7,7 +7,10 @@ extern INLINE void UA_memcpy(void *dst, void const *src, UA_Int32 size);
 #ifdef DEBUG
 extern INLINE void _UA_free(void *ptr, char *pname, char *f, UA_Int32 l);
 extern INLINE void * _UA_alloc(UA_Int32 size, char *file, UA_Int32 line);
+extern INLINE void * _UA_alloca(UA_Int32 size, char *file, UA_Int32 line);
+
 #else
 extern INLINE void _UA_free(void *ptr);
 extern INLINE void * _UA_alloc(UA_Int32 size);
+extern INLINE void * _UA_alloca(UA_Int32 size);
 #endif

+ 25 - 1
src/ua_util.h

@@ -9,10 +9,13 @@
 #include "ua_config.h"
 
 #include <stddef.h> /* Needed for sys/queue.h */
-#if !defined(MSVC) && !defined(__MINGW32__)
+
+#ifndef WIN32
 #include <sys/queue.h>
+#include <alloca.h>
 #else
 #include "queue.h"
+#include <malloc.h>
 #endif
 
 #include "ua_types.h"
@@ -78,4 +81,25 @@ INLINE void UA_memcpy(void *dst, void const *src, UA_Int32 size) {
     memcpy(dst, src, size);
 }
 
+#ifdef DEBUG
+#define UA_alloca(size) _UA_alloca(size, __FILE__, __LINE__) 
+INLINE void * _UA_alloca(UA_Int32 size, char *file, UA_Int32 line) {
+	DBG_VERBOSE(printf("UA_alloc - %d;%s;%d\n", size, file, line); fflush(stdout));
+#ifdef WIN32
+	return _alloca(size);
+#else
+	return alloca(size);
+#endif
+}
+#else
+#define UA_alloca(size) _UA_alloca(size) 
+INLINE void * _UA_alloca(UA_Int32 size) {
+#ifdef WIN32
+	return _alloca(size);
+#else
+	return alloca(size);
+#endif
+}
+#endif
+
 #endif /* UA_UTIL_H_ */

+ 4 - 2
tools/type_lists.py

@@ -19,6 +19,8 @@ only_needed_types = set([	"InvalidType", "Node", "NodeClass", "ReferenceNode", "
 							"RelativePath", "BrowsePathTarget", "RelativePathElement", "CreateSubscriptionRequest", "CreateSubscriptionResponse",
 							"BrowseResponse", "BrowseResult", "ReferenceDescription", "BrowseRequest", "ViewDescription", "BrowseDescription",
 							"BrowseDirection", "CloseSessionRequest", "AddNodesRequest", "AddNodesResponse", "AddNodesItem", "AddNodesResult",
-							"DeleteNodesItem","AddReferencesRequest", "AddReferencesResponse", "AddReferencesItem","DeleteReferencesItem", "VariableNode", "MethodNode", 								"VariableTypeNode","ViewNode", "ReferenceTypeNode", "BrowseResultMask", "ServerState", "ServerStatusDataType", "BuildInfo", "ObjectNode",
-							"DataTypeNode", "ObjectTypeNode", "IdType", "VariableAttributes","ObjectAttributes","NodeAttributes","ReferenceTypeAttributes","ViewAttributes","ObjectTypeAttributes","NodeAttributesMask" ])
+							"DeleteNodesItem","AddReferencesRequest", "AddReferencesResponse", "AddReferencesItem","DeleteReferencesItem", "VariableNode",
+							"MethodNode", "VariableTypeNode", "ViewNode", "ReferenceTypeNode", "BrowseResultMask", "ServerState", "ServerStatusDataType",
+							"BuildInfo", "ObjectNode", "DataTypeNode", "ObjectTypeNode", "IdType", "VariableAttributes","ObjectAttributes","NodeAttributes","ReferenceTypeAttributes", "ViewAttributes", "ObjectTypeAttributes", "NodeAttributesMask","DeleteNodesItem",
+							"DeleteNodesRequest", "DeleteNodesResponse", "DeleteReferencesItem", "DeleteReferencesRequest", "DeleteReferencesResponse"])
 only_needed_types = only_needed_types.union(existing_types)