Browse Source

faux: base functions

Serj Kalichev 4 years ago
parent
commit
ee7197d4d2
12 changed files with 106 additions and 11 deletions
  1. 3 1
      faux/Makefile.am
  2. 1 1
      faux/argv.h
  3. 3 0
      faux/base/Makefile.am
  4. 81 0
      faux/base/base.c
  5. 1 1
      faux/conv.h
  6. 1 1
      faux/ctype.h
  7. 11 2
      faux/faux.h
  8. 1 1
      faux/ini.h
  9. 1 1
      faux/list.h
  10. 1 1
      faux/log.h
  11. 1 1
      faux/str.h
  12. 1 1
      faux/sysdb.h

+ 3 - 1
faux/Makefile.am

@@ -3,7 +3,7 @@ libfaux_la_SOURCES =
 libfaux_la_LIBADD =
 
 nobase_include_HEADERS += \
-	faux/types.h \
+	faux/faux.h \
 	faux/ctype.h \
 	faux/str.h \
 	faux/sysdb.h \
@@ -17,6 +17,7 @@ nobase_include_HEADERS += \
 #	faux/system.h
 
 EXTRA_DIST += \
+	faux/base/Makefile.am \
 	faux/ctype/Makefile.am \
 	faux/str/Makefile.am \
 	faux/sysdb/Makefile.am \
@@ -30,6 +31,7 @@ EXTRA_DIST += \
 #	faux/system/Makefile.am \
 #	faux/README
 
+include $(top_srcdir)/faux/base/Makefile.am
 include $(top_srcdir)/faux/ctype/Makefile.am
 include $(top_srcdir)/faux/str/Makefile.am
 include $(top_srcdir)/faux/sysdb/Makefile.am

+ 1 - 1
faux/argv.h

@@ -25,7 +25,7 @@ contains four "words" the third of which is a string.
 #include <stddef.h>
 
 #include "c_decl.h"
-#include "types.h"
+#include "faux.h"
 
 _BEGIN_C_DECL
 /**

+ 3 - 0
faux/base/Makefile.am

@@ -0,0 +1,3 @@
+libfaux_la_SOURCES += \
+	faux/base/base.c
+

+ 81 - 0
faux/base/base.c

@@ -0,0 +1,81 @@
+/** @file base.c
+ * @brief Base faux functions.
+ */
+
+#include <stdlib.h>
+#include <string.h>
+
+#include "faux/faux.h"
+
+
+/** Portable implementation of free() function.
+ *
+ * The POSIX standard says the free() must check pointer for NULL value
+ * and must not try to free NULL pointer. I.e. it do nothing in a case of NULL.
+ * The Linux glibc do so. But some non-fully POSIX compatible systems don't.
+ * For these systems our implementation of free() must check for NULL itself.
+ *
+ * For now function simply call free() but it can be changed while working on
+ * portability.
+ *
+ * @param [in] ptr Memory pointer to free.
+ * @sa free()
+ */
+void faux_free(void *ptr) {
+
+#if 0
+	if (ptr)
+#endif
+	free(ptr);
+}
+
+
+/** Portable implementation of malloc() function.
+ *
+ * The faux library implements its own free() function (called faux_free()) so
+ * it's suitable to implement their own complementary malloc() function. May be
+ * somedays it will be more portable than standard malloc().
+ *
+ * @param [in] size Memory size to allocate.
+ * @return Allocated memory or NULL on error.
+ * @sa malloc()
+ */
+void *faux_malloc(size_t size) {
+
+	return malloc(size);
+}
+
+/** Portable implementation of bzero().
+ *
+ * The POSIX standard says the bzero() is legacy now. It recommends to use
+ * memset() instead it. But bzero() is easier to use. So bzero() can be
+ * implemented using memset().
+ *
+ * @param [in] ptr Pointer
+ * @param [in] size Size of memory (in bytes) to zero it.
+ * @sa bzero()
+ */
+void faux_bzero(void *ptr, size_t size) {
+
+	memset(ptr, '\0', size);
+}
+
+/** The malloc() implementation with writing zeroes to allocated buffer.
+ *
+ * The POSIX defines calloc() function to allocate memory and write zero bytes
+ * to the whole buffer. But calloc() has strange set of arguments. This function
+ * is simply combination of malloc() and bzero().
+ *
+ * @param [in] size Memory size to allocate.
+ * @return Allocated zeroed memory or NULL on error.
+ */
+void *faux_zmalloc(size_t size) {
+
+	void *ptr = NULL;
+
+	ptr = faux_malloc(size);
+	if (ptr)
+		faux_bzero(ptr, size);
+
+	return ptr;
+}

+ 1 - 1
faux/conv.h

@@ -5,7 +5,7 @@
 #ifndef _faux_conv_h
 #define _faux_conv_h
 
-#include "faux/types.h"
+#include "faux/faux.h"
 
 C_DECL_BEGIN
 

+ 1 - 1
faux/ctype.h

@@ -7,7 +7,7 @@
 
 #include <ctype.h>
 
-#include "faux/types.h"
+#include "faux/faux.h"
 
 C_DECL_BEGIN
 

+ 11 - 2
faux/types.h → faux/faux.h

@@ -1,10 +1,11 @@
-/** @file types.h
- * @brief Additional usefull data types
+/** @file faux.h
+ * @brief Additional usefull data types and base functions.
  */
 
 #ifndef _faux_types_h
 #define _faux_types_h
 
+#include <stdlib.h>
 
 /**
  * A standard boolean type. The possible values are
@@ -55,5 +56,13 @@ typedef enum {
 #define C_DECL_END
 #endif
 
+C_DECL_BEGIN
+
+void faux_free(void *ptr);
+void *faux_malloc(size_t size);
+void faux_bzero(void *ptr, size_t size);
+void *faux_zmalloc(size_t size);
+
+C_DECL_END
 
 #endif /* _faux_types_h */

+ 1 - 1
faux/ini.h

@@ -5,7 +5,7 @@
 #ifndef _faux_ini_h
 #define _faux_ini_h
 
-#include "faux/types.h"
+#include "faux/faux.h"
 #include "faux/list.h"
 
 typedef struct faux_pair_s faux_pair_t;

+ 1 - 1
faux/list.h

@@ -7,7 +7,7 @@
 
 #include <stddef.h>
 
-#include "types.h"
+#include "faux.h"
 
 typedef struct faux_list_node_s faux_list_node_t;
 typedef struct faux_list_s faux_list_t;

+ 1 - 1
faux/log.h

@@ -5,7 +5,7 @@
 #ifndef _faux_log_h
 #define _faux_log_h
 
-#include "faux/types.h"
+#include "faux/faux.h"
 
 C_DECL_BEGIN
 

+ 1 - 1
faux/str.h

@@ -7,7 +7,7 @@
 
 #include <stddef.h>
 
-#include "faux/types.h"
+#include "faux/faux.h"
 
 #define UTF8_MASK 0xC0
 #define UTF8_7BIT_MASK 0x80 /* One byte or multibyte */

+ 1 - 1
faux/sysdb.h

@@ -18,7 +18,7 @@
 #include <grp.h>
 #endif
 
-#include "faux/types.h"
+#include "faux/faux.h"
 
 C_DECL_BEGIN