Browse Source

faux: lib use faux base functions

Serj Kalichev 4 years ago
parent
commit
482ffbc77e
6 changed files with 36 additions and 40 deletions
  1. 8 8
      faux/argv/argv.c
  2. 6 6
      faux/ini/ini.c
  3. 2 3
      faux/ini/pair.c
  4. 5 7
      faux/list/list.c
  5. 3 4
      faux/str/str.c
  6. 12 12
      faux/sysdb/sysdb.c

+ 8 - 8
faux/argv/argv.c

@@ -26,7 +26,7 @@ static void lub_argv_init(lub_argv_t * this, const char *line, size_t offset)
 	if (0 == this->argc)
 		return;
 	/* allocate space to hold the vector */
-	arg = this->argv = malloc(sizeof(lub_arg_t) * this->argc);
+	arg = this->argv = faux_zmalloc(sizeof(lub_arg_t) * this->argc);
 	assert(arg);
 
 	/* then fill out the array with the words */
@@ -52,7 +52,7 @@ lub_argv_t *lub_argv_new(const char *line, size_t offset)
 {
 	lub_argv_t *this;
 
-	this = malloc(sizeof(lub_argv_t));
+	this = faux_zmalloc(sizeof(lub_argv_t));
 	if (this)
 		lub_argv_init(this, line, offset);
 
@@ -80,8 +80,8 @@ static void lub_argv_fini(lub_argv_t * this)
 	unsigned i;
 
 	for (i = 0; i < this->argc; i++)
-		free(this->argv[i].arg);
-	free(this->argv);
+		faux_free(this->argv[i].arg);
+	faux_free(this->argv);
 	this->argv = NULL;
 }
 
@@ -89,7 +89,7 @@ static void lub_argv_fini(lub_argv_t * this)
 void lub_argv_delete(lub_argv_t * this)
 {
 	lub_argv_fini(this);
-	free(this);
+	faux_free(this);
 }
 
 /*--------------------------------------------------------- */
@@ -131,7 +131,7 @@ char **lub_argv__get_argv(const lub_argv_t * this, const char *argv0)
 	if (argv0)
 		a = 1;
 
-	result = malloc(sizeof(char *) * (this->argc + 1 + a));
+	result = faux_zmalloc(sizeof(char *) * (this->argc + 1 + a));
 
 	if (argv0)
 		result[0] = strdup(argv0);
@@ -151,8 +151,8 @@ void lub_argv__free_argv(char **argv)
 		return;
 
 	for (i = 0; argv[i]; i++)
-		free(argv[i]);
-	free(argv);
+		faux_free(argv[i]);
+	faux_free(argv);
 }
 
 /*--------------------------------------------------------- */

+ 6 - 6
faux/ini/ini.c

@@ -9,6 +9,7 @@
 #include <ctype.h>
 
 #include "private.h"
+#include "faux/faux.h"
 #include "faux/str.h"
 #include "faux/ini.h"
 
@@ -17,12 +18,11 @@ faux_ini_t *faux_ini_new(void) {
 
 	faux_ini_t *ini;
 
-	ini = malloc(sizeof(*ini));
+	ini = faux_zmalloc(sizeof(*ini));
 	if (!ini)
 		return NULL;
 
 	// Init
-	memset(ini, 0, sizeof(*ini));
 	ini->list = faux_list_new(faux_pair_compare, faux_pair_free);
 
 	return ini;
@@ -36,11 +36,11 @@ void faux_ini_free(faux_ini_t *ini) {
 		return;
 
 	faux_list_free(ini->list);
-	free(ini);
+	faux_free(ini);
 }
 
 
-static int faux_ini_del(faux_ini_t *ini, faux_ini_node_t node) {
+static int faux_ini_del(faux_ini_t *ini, faux_ini_node_t *node) {
 
 }
 
@@ -194,7 +194,7 @@ int faux_ini_parse_file(faux_ini_t *ini, const char *fn) {
 	if (!f)
 		return -1;
 
-	buf = malloc(size);
+	buf = faux_zmalloc(size);
 	while (fgets(buf + p, size - p, f)) {
 		char *tmp = NULL;
 
@@ -213,7 +213,7 @@ int faux_ini_parse_file(faux_ini_t *ini, const char *fn) {
 
 	ret = 0;
 error:
-	free(buf);
+	faux_free(buf);
 	fclose(f);
 
 	return ret;

+ 2 - 3
faux/ini/pair.c

@@ -23,13 +23,12 @@ faux_pair_t *faux_pair_new(const char *name, const char *value) {
 
 	faux_pair_t *pair = NULL;
 
-	pair = malloc(sizeof(*pair));
+	pair = faux_zmalloc(sizeof(*pair));
 	assert(pair);
 	if (!pair)
 		return NULL;
 
 	// Initialize
-	memset(pair, 0, sizeof(*pair));
 	pair->name = faux_str_dup(name);
 	pair->value = faux_str_dup(value);
 
@@ -46,7 +45,7 @@ void faux_pair_free(void *ptr) {
 		return;
 	faux_str_free(pair->name);
 	faux_str_free(pair->value);
-	free(pair);
+	faux_free(pair);
 }
 
 

+ 5 - 7
faux/list/list.c

@@ -28,7 +28,7 @@ static faux_list_node_t *faux_list_new_node(void *data) {
 
 	faux_list_node_t *node = NULL;
 
-	node = malloc(sizeof(*node));
+	node = faux_zmalloc(sizeof(*node));
 	assert(node);
 	if (!node)
 		return NULL;
@@ -49,9 +49,7 @@ static faux_list_node_t *faux_list_new_node(void *data) {
 static void faux_list_free_node(faux_list_node_t *node) {
 
 	assert(node);
-
-	if (node)
-		free(node);
+	faux_free(node);
 }
 
 
@@ -156,9 +154,9 @@ void *faux_list_each(faux_list_node_t **iter) {
 faux_list_t *faux_list_new(faux_list_compare_fn compareFn,
 	faux_list_free_fn freeFn) {
 
-	faux_list_t *list;
+	faux_list_t *list = NULL;
 
-	list = malloc(sizeof(*list));
+	list = faux_zmalloc(sizeof(*list));
 	assert(list);
 	if (!list)
 		return NULL;
@@ -194,7 +192,7 @@ void faux_list_free(faux_list_t *list) {
 	while ((iter = faux_list_head(list))) {
 		faux_list_del(list, iter);
 	}
-	free(list);
+	faux_free(list);
 }
 
 

+ 3 - 4
faux/str/str.c

@@ -28,8 +28,7 @@
  */
 void faux_str_free(char *str) {
 
-	if (str)
-		free(str);
+	faux_free(str);
 }
 
 
@@ -72,7 +71,7 @@ char *faux_str_dupn(const char *str, size_t n) {
 		return NULL;
 	len = strlen(str);
 	len = (len < n) ? len : n;
-	res = malloc(len + 1);
+	res = faux_zmalloc(len + 1);
 	if (!res)
 		return NULL;
 	strncpy(res, str, len);
@@ -294,7 +293,7 @@ char *lub_string_ndecode(const char *string, unsigned int len)
 	if (!string)
 		return NULL;
 
-	p = res = malloc(len + 1);
+	p = res = faux_zmalloc(len + 1);
 
 	while (*s && (s < (string +len))) {
 		if (!esc) {

+ 12 - 12
faux/sysdb/sysdb.c

@@ -24,7 +24,7 @@
  *
  * @param [in] name User name.
  * @return Pointer to allocated passwd structure.
- * @warning The resulting pointer (return value) must be freed by free().
+ * @warning The resulting pointer (return value) must be freed by faux_free().
  */
 struct passwd *faux_sysdb_getpwnam(const char *name) {
 
@@ -40,7 +40,7 @@ struct passwd *faux_sysdb_getpwnam(const char *name) {
 #else
 	size = DEFAULT_GETPW_R_SIZE_MAX;
 #endif
-	pwbuf = malloc(sizeof(*pwbuf) + size);
+	pwbuf = faux_zmalloc(sizeof(*pwbuf) + size);
 	if (!pwbuf)
 		return NULL;
 	buf = (char *)pwbuf + sizeof(*pwbuf);
@@ -48,7 +48,7 @@ struct passwd *faux_sysdb_getpwnam(const char *name) {
 	res = getpwnam_r(name, pwbuf, buf, size, &pw);
 
 	if (res || !pw) {
-		free(pwbuf);
+		faux_free(pwbuf);
 		if (res != 0)
 			errno = res;
 		else
@@ -65,7 +65,7 @@ struct passwd *faux_sysdb_getpwnam(const char *name) {
  *
  * @param [in] uid UID.
  * @return Pointer to allocated passwd structure.
- * @warning The resulting pointer (return value) must be freed by free().
+ * @warning The resulting pointer (return value) must be freed by faux_free().
  */
 struct passwd *faux_sysdb_getpwuid(uid_t uid) {
 
@@ -81,7 +81,7 @@ struct passwd *faux_sysdb_getpwuid(uid_t uid) {
 #else
 	size = DEFAULT_GETPW_R_SIZE_MAX;
 #endif
-	pwbuf = malloc(sizeof(*pwbuf) + size);
+	pwbuf = faux_zmalloc(sizeof(*pwbuf) + size);
 	if (!pwbuf)
 		return NULL;
 	buf = (char *)pwbuf + sizeof(*pwbuf);
@@ -89,7 +89,7 @@ struct passwd *faux_sysdb_getpwuid(uid_t uid) {
 	res = getpwuid_r(uid, pwbuf, buf, size, &pw);
 
 	if (NULL == pw) {
-		free(pwbuf);
+		faux_free(pwbuf);
 		if (res != 0)
 			errno = res;
 		else
@@ -106,7 +106,7 @@ struct passwd *faux_sysdb_getpwuid(uid_t uid) {
  *
  * @param [in] name Group name.
  * @return Pointer to allocated group structure.
- * @warning The resulting pointer (return value) must be freed by free().
+ * @warning The resulting pointer (return value) must be freed by faux_free().
  */
 struct group *faux_sysdb_getgrnam(const char *name) {
 
@@ -122,7 +122,7 @@ struct group *faux_sysdb_getgrnam(const char *name) {
 #else
 	size = DEFAULT_GETPW_R_SIZE_MAX;
 #endif
-	grbuf = malloc(sizeof(*grbuf) + size);
+	grbuf = faux_zmalloc(sizeof(*grbuf) + size);
 	if (!grbuf)
 		return NULL;
 	buf = (char *)grbuf + sizeof(*grbuf);
@@ -130,7 +130,7 @@ struct group *faux_sysdb_getgrnam(const char *name) {
 	res = getgrnam_r(name, grbuf, buf, size, &gr);
 
 	if (!gr) {
-		free(grbuf);
+		faux_free(grbuf);
 		if (res != 0)
 			errno = res;
 		else
@@ -147,7 +147,7 @@ struct group *faux_sysdb_getgrnam(const char *name) {
  *
  * @param [in] gid GID.
  * @return Pointer to allocated group structure.
- * @warning The resulting pointer (return value) must be freed by free().
+ * @warning The resulting pointer (return value) must be freed by faux_free().
  */
 struct group *faux_sysdb_getgrgid(gid_t gid) {
 
@@ -163,7 +163,7 @@ struct group *faux_sysdb_getgrgid(gid_t gid) {
 #else
 	size = DEFAULT_GETPW_R_SIZE_MAX;
 #endif
-	grbuf = malloc(sizeof(struct group) + size);
+	grbuf = faux_zmalloc(sizeof(struct group) + size);
 	if (!grbuf)
 		return NULL;
 	buf = (char *)grbuf + sizeof(struct group);
@@ -171,7 +171,7 @@ struct group *faux_sysdb_getgrgid(gid_t gid) {
 	res = getgrgid_r(gid, grbuf, buf, size, &gr);
 
 	if (!gr) {
-		free(grbuf);
+		faux_free(grbuf);
 		if (res != 0)
 			errno = res;
 		else