Эх сурвалжийг харах

[vptr/void_pointer] Adding vptr and void_pointer implementaion

Vinicius Teshima 2 жил өмнө
parent
commit
0c899059d2

+ 3 - 4
include/toolbox/void_pointer.h

@@ -1,12 +1,11 @@
 #ifndef TOOLBOX_VOID_POINTER_H
 #define TOOLBOX_VOID_POINTER_H
 
-#include <stdbool.h>
 #include <stddef.h>
 
 __attribute__((__pure__))
-bool
-void_pointer_equal(const void *restrict r_p_vptr1, const size_t p_vptr1_size
-				   , const void *restrict r_p_vptr2, const size_t p_vptr2_size);
+_Bool
+void_pointer_equal(const void *restrict vptr1, const size_t vptr1_size
+		   , const void *restrict vptr2, const size_t vptr2_size);
 
 #endif

+ 3 - 4
include/toolbox/vptr.h

@@ -1,7 +1,6 @@
 #ifndef TOOLBOX_VPTR_H
 #define TOOLBOX_VPTR_H
 
-#include <stdbool.h>
 #include <stddef.h>
 
 #ifdef TOOLBOX_TYPEDEF
@@ -14,8 +13,8 @@ struct vptr {
 };
 
 __attribute__((__pure__))
-bool
-vptr_equal(const struct vptr *restrict r_p_vptr1
-		   , const struct vptr *restrict r_p_vptr2);
+_Bool
+vptr_equal(const struct vptr *restrict vptr1
+	   , const struct vptr *restrict vptr2);
 
 #endif

+ 43 - 0
src/toolbox/void_pointer.c

@@ -0,0 +1,43 @@
+#include "toolbox/void_pointer.h"
+
+#include <stddef.h>
+
+#include "toolbox/errno.h"
+#include "toolbox/log.h"
+
+#define true ((unsigned char) 1L)
+#define false ((unsigned char) 0L)
+
+#define DEREF(PTR, POS) (*(((unsigned char *) (PTR)) + POS))
+
+#define TEST_FOR_NULL(POINTER, RET)					\
+	if ( POINTER == NULL ) {					\
+		LOG_ERROR("Null Pointer Not Expected");		\
+		toolbox_errno = RET_EVN;				\
+		return RET;						\
+	}
+
+_Bool
+void_pointer_equal(const void *restrict p_vptr1, const size_t p_vptr1_size
+		   , const void *restrict p_vptr2, const size_t p_vptr2_size) {
+	toolbox_errno = RET_OK;
+
+	TEST_FOR_NULL(p_vptr1, false);
+	TEST_FOR_NULL(p_vptr2, false);
+
+	if ( p_vptr1_size != p_vptr2_size )
+		return false;
+
+	for ( size_t z = 0; z < p_vptr1_size; ++z ) {
+		if ( DEREF(p_vptr1, z) != DEREF(p_vptr2, z) )
+			return false;
+	}
+
+	return true;
+}
+
+#undef TEST_FOR_NULL
+#undef DEREF
+
+#undef false
+#undef true

+ 47 - 0
src/toolbox/vptr.c

@@ -0,0 +1,47 @@
+#include "toolbox/vptr.h"
+
+#include "toolbox/errno.h"
+#include "toolbox/log.h"
+
+
+
+#define true ((unsigned char) 1L)
+#define false ((unsigned char) 0L)
+
+#define DEREF(PTR, POS) (*(((unsigned char *) (PTR)) + POS))
+
+#define TEST_FOR_NULL(POINTER, RET)					\
+	if ( POINTER == NULL ) {					\
+		LOG_ERROR("Null Pointer Not Expected");		\
+		toolbox_errno = RET_EVN;				\
+		return RET;						\
+	}
+
+_Bool
+vptr_equal(const struct vptr *restrict p_vptr1
+	   , const struct vptr *restrict p_vptr2) {
+	toolbox_errno = RET_OK;
+
+	TEST_FOR_NULL(p_vptr1, false);
+	TEST_FOR_NULL(p_vptr2, false);
+
+	TEST_FOR_NULL(p_vptr1->data, false);
+	TEST_FOR_NULL(p_vptr2->data, false);
+
+	if ( p_vptr1->size != p_vptr2->size )
+		return false;
+
+	for ( size_t z = 0; z < p_vptr1->size; ++z ) {
+		if ( DEREF(p_vptr1->data, z) != DEREF(p_vptr2->data, z) )
+			return false;
+	}
+
+	return true;
+
+}
+
+#undef TEST_FOR_NULL
+#undef DEREF
+
+#undef false
+#undef true