Quellcode durchsuchen

inline util functions, add UA_alloca

Julius Pfrommer vor 10 Jahren
Ursprung
Commit
f9b8ab8520
2 geänderte Dateien mit 44 neuen und 16 gelöschten Zeilen
  1. 5 14
      src/util/ua_util.c
  2. 39 2
      src/util/ua_util.h

+ 5 - 14
src/util/ua_util.c

@@ -1,19 +1,10 @@
 #include "ua_util.h"
 
-
-UA_Int32 _UA_free(void *ptr, char *pname, char *f, UA_Int32 l) {
-	DBG_VERBOSE(printf("UA_free;%p;;%s;;%s;%d\n", ptr, pname, f, l); fflush(stdout));
-	free(ptr); // checks if ptr != NULL in the background
-	return UA_SUCCESS;
-}
-
-UA_Int32 _UA_alloc(void **ptr, UA_Int32 size, char *pname, char *sname, char *f, UA_Int32 l) {
-	if(ptr == UA_NULL) return UA_ERR_INVALID_VALUE;
-	UA_alloc_lastptr = *ptr = malloc(size);
-	DBG_VERBOSE(printf("UA_alloc - %p;%d;%s;%s;%s;%d\n", *ptr, size, pname, sname, f, l); fflush(stdout));
-	if(*ptr == UA_NULL) return UA_ERR_NO_MEMORY;
-	return UA_SUCCESS;
-}
+/* the extern inline in a *.c-file is required for other compilation units to
+   see the inline function. */
+extern inline UA_Int32 _UA_free(void *ptr, char *pname, char *f, UA_Int32 l);
+extern inline UA_Int32 _UA_alloc(void **ptr, UA_Int32 size, char *pname, char *sname, char *f, UA_Int32 l);
+extern inline UA_Int32 _UA_alloca(void **ptr, UA_Int32 size, char *pname, char *sname, char *f, UA_Int32 l);
 
 UA_Int32 UA_memcpy(void *dst, void const *src, UA_Int32 size) {
 	if(dst == UA_NULL) return UA_ERR_INVALID_VALUE;

+ 39 - 2
src/util/ua_util.h

@@ -5,6 +5,9 @@
 #include <stdlib.h> // malloc, free
 #include <string.h> // memcpy
 #include <assert.h> // assert
+#ifndef WIN32
+#include <alloca.h> // alloca
+#endif
 #include "ua_types.h"
 
 /* Debug macros */
@@ -28,14 +31,48 @@ void const *UA_alloc_lastptr;
 
 /* Heap memory functions */
 #define UA_NULL ((void *)0)
-UA_Int32 _UA_free(void *ptr, char *pname, char *f, UA_Int32 l);
-UA_Int32 _UA_alloc(void **ptr, UA_Int32 size, char *pname, char *sname, char *f, UA_Int32 l);
+
+inline UA_Int32 _UA_free(void *ptr, char *pname, char *f, UA_Int32 l) {
+	DBG_VERBOSE(printf("UA_free;%p;;%s;;%s;%d\n", ptr, pname, f, l); fflush(stdout));
+	free(ptr); // checks if ptr != NULL in the background
+	return UA_SUCCESS;
+}
+
+inline UA_Int32 _UA_alloc(void **ptr, UA_Int32 size, char *pname, char *sname, char *f, UA_Int32 l) {
+	if(ptr == UA_NULL)
+		return UA_ERR_INVALID_VALUE;
+	UA_alloc_lastptr = *ptr = malloc(size);
+	DBG_VERBOSE(printf("UA_alloc - %p;%d;%s;%s;%s;%d\n", *ptr, size, pname, sname, f, l); fflush(stdout));
+	if(*ptr == UA_NULL)
+		return UA_ERR_NO_MEMORY;
+	return UA_SUCCESS;
+}
+
+inline UA_Int32 _UA_alloca(void **ptr, UA_Int32 size, char *pname, char *sname, char *f, UA_Int32 l) {
+	if(ptr == UA_NULL)
+		return UA_ERR_INVALID_VALUE;
+#ifdef WIN32
+	*ptr = _alloca(size);
+#else
+	*ptr = alloca(size);
+#endif
+	DBG_VERBOSE(printf("UA_alloca - %p;%d;%s;%s;%s;%d\n", *ptr, size, pname, sname, f, l); fflush(stdout));
+	return UA_SUCCESS;
+}
+
 UA_Int32 UA_memcpy(void *dst, void const *src, UA_Int32 size);
 UA_Int32 UA_VTable_isValidType(UA_Int32 type);
 
 #define UA_free(ptr) _UA_free(ptr, # ptr, __FILE__, __LINE__)
 #define UA_alloc(ptr, size) _UA_alloc(ptr, size, # ptr, # size, __FILE__, __LINE__)
 
+/** @brief UA_alloca assigns memory on the stack instead of the heap. It is a
+	very fast alternative to standard malloc. The memory is automatically
+	returned ('freed') when the current function returns. Use this only for
+	small temporary values. For more than 100Byte, it is more reasonable to use
+	proper UA_alloc. */
+#define UA_alloca(ptr, size) _UA_alloca(ptr, size, # ptr, # size, __FILE__, __LINE__)
+
 #define UA_assert(ignore) assert(ignore)
 
 #endif /* UA_UTILITY_H_ */