Commit 5d5cd6af authored by Lucas Russo's avatar Lucas Russo

src/libs/libhutils/*: add new library for utilities

parent dd2619cb
# Set your cross compile prefix with CROSS_COMPILE variable
CROSS_COMPILE ?=
CMDSEP = ;
CC = $(CROSS_COMPILE)gcc
AR = $(CROSS_COMPILE)ar
LD = $(CROSS_COMPILE)ld
OBJDUMP = $(CROSS_COMPILE)objdump
OBJCOPY = $(CROSS_COMPILE)objcopy
SIZE = $(CROSS_COMPILE)size
MAKE = make
PWD = $(shell pwd)
LIBHUTILS = libhutils
# General C flags
CFLAGS = -std=gnu99 -O2
# To enable this option, use: make ERRHAND_DBG=y
ifneq ($(ERRHAND_DBG),)
CFLAGS_DEBUG += -DERRHAND_DBG=$(ERRHAND_DBG)
endif
# To enable this option use: make ERRHAND_MIN_LEVEL=DBG_MIN_TRACE
ifneq ($(ERRHAND_MIN_LEVEL),)
CFLAGS_DEBUG += -DERRHAND_MIN_LEVEL=$(ERRHAND_MIN_LEVEL)
endif
# To enable this option use: make ERRHAND_SUBSYS_ON='"(DBG_DEV_MNGR | \
# DBG_DEV_IO | DBG_SM_IO | DBG_LIB_CLIENT | DBG_SM_PR | DBG_SM_CH | DBG_LL_IO | DBG_HAL_UTILS)"'
#
# You can also OR the available subsytems to enable debug messages in just the
# those subsytems. See file errhand_opts.h for more information
ifneq ($(ERRHAND_SUBSYS_ON),)
CFLAGS_DEBUG += -DERRHAND_SUBSYS_ON=$(ERRHAND_SUBSYS_ON)
endif
# Debug flags -D<flasg_name>=<value>
CFLAGS_DEBUG += -g
# Specific platform Flags
CFLAGS_PLATFORM = -Wall -Wextra -Werror
LDFLAGS_PLATFORM =
# Libraries
LIBS =
# General library flags -L<libdir>
LFLAGS =
# Include directories
INCLUDE_DIRS = -I. -I../liberrhand
# Merge all flags. Optimize for size (-Os)
CFLAGS += $(CFLAGS_PLATFORM) $(CFLAGS_DEBUG)
LDFLAGS = $(LDFLAGS_PLATFORM)
# Output library names
OUT = $(LIBHUTILS)
.SECONDEXPANSION:
# Library objects
$(LIBHUTILS)_OBJS_LIB = hutils_utils.o hutils_math.o hutils_err.o
# Objects common for this library
common_OBJS =
# Objects for each version of library
$(LIBHUTILS)_OBJS = $(common_OBJS) $($(LIBHUTILS)_OBJS_LIB)
$(LIBHUTILS)_CODE_HEADERS = hutils.h
$(LIBHUTILS)_HEADERS = $($(LIBHUTILS)_OBJS_LIB:.o=.h) $($(LIBHUTILS)_CODE_HEADERS)
OBJS_all = $(common_OBJS) $($(LIBHUTILS)_OBJS)
# Libraries suffixes
LIB_STATIC_SUFFIX = .a
# Generate suitable names for static libraries
TARGET_STATIC = $(addsuffix $(LIB_STATIC_SUFFIX), $(OUT))
.PHONY: all clean mrproper install uninstall
# Avoid deletion of intermediate files, such as objects
.SECONDARY: $(OBJS_all)
# Makefile rules
all: $(TARGET_STATIC)
# Compile static library
%.a: $$($$*_OBJS)
$(AR) rcs $@ $^
# Pull in dependency info for *existing* .o files and don't complain if the
# corresponding .d file is not found
-include $(OBJS_all:.o=.d)
# Compile with position-independent objects.
# Autodependencies generatation by Scott McPeak, November 2001,
# from article "Autodependencies with GNU make"
%.o: %.c
$(CC) $(CFLAGS) $(INCLUDE_DIRS) -c $*.c -o $@
# create the dependency files "target: pre-requisites"
${CC} -MM $(CFLAGS) $(INCLUDE_DIRS) $*.c > $*.d
# Workaround to make objects in different folders have
# the correct target path. e.g., "dir/bar.o: dir/bar.c dir/foo.h"
# instead of "bar.o: dir/bar.c dir/foo.h"
@mv -f $*.d $*.d.tmp
@sed -e 's|.*:|$*.o:|' < $*.d.tmp > $*.d
# All prereqs listed will also become command-less,
# prereq-less targets. In this way, the prereq file will be
# treated as changed and the target will be rebuilt
# sed: strip the target (everything before colon)
# sed: remove any continuation backslashes
# fmt -1: list words one per line
# sed: strip leading spaces
# sed: add trailing colons
@sed -e 's/.*://' -e 's/\\$$//' < $*.d.tmp | fmt -1 | \
sed -e 's/^ *//' -e 's/$$/:/' >> $*.d
@rm -f $*.d.tmp
install:
uninstall:
clean:
rm -f $(OBJS_all) $(OBJS_all:.o=.d)
mrproper: clean
rm -f *.a
/*
* Copyright (C) 2015 LNLS (www.lnls.br)
* Author: Lucas Russo <lucas.russo@lnls.br>
* Parts taken from lwIP debug system
*
* Released according to the GNU LGPL, version 3 or any later version.
*/
#ifndef _HUTILS_
#define _HUTILS_
#include "hutils_err.h"
#include "hutils_math.h"
#include "hutils_utils.h"
#endif
/*
* Copyright (C) 2014 LNLS (www.lnls.br)
* Author: Lucas Russo <lucas.russo@lnls.br>
*
* Released according to the GNU LGPL, version 3 or any later version.
*/
/* Error definitions and output stringification based on the work available
* at the libsllp project repository: https://github.com/brunoseivam/libsllp */
#include "hutils_err.h"
static const char *hutils_err [HUTILS_ERR_END] =
{
[HUTILS_SUCCESS] = "Success",
[HUTILS_ERR_ALLOC] = "Could not allocate memory",
[HUTILS_ERR_NULL_POINTER] = "Null pointer received",
[HUTILS_ERR_NO_FUNC_REG] = "No function registered",
[HUTILS_ERR_INV_LESS_ARGS] = "Less arguments than specified passed",
[HUTILS_ERR_INV_MORE_ARGS] = "More arguments than specified passed",
[HUTILS_ERR_INV_SIZE_ARG] = "Invalid size of argument size"
};
/* Convert enumeration type to string */
const char * hutils_err_str (hutils_err_e err)
{
return hutils_err [err];
}
/*
* Copyright (C) 2014 LNLS (www.lnls.br)
* Author: Lucas Russo <lucas.russo@lnls.br>
*
* Released according to the GNU LGPL, version 3 or any later version.
*/
/* Error definitions and output stringification based on the work available
* at the libsllp project repository: https://github.com/brunoseivam/libsllp */
#ifndef _HUTILS_ERR_H_
#define _HUTILS_ERR_H_
enum _hutils_err_e
{
HUTILS_SUCCESS = 0, /* No error */
HUTILS_ERR_ALLOC, /* Could not allocate memory */
HUTILS_ERR_NULL_POINTER, /* Null pointer received */
HUTILS_ERR_NO_FUNC_REG, /* No function registered */
HUTILS_ERR_INV_LESS_ARGS, /* Less arguments than specified passed */
HUTILS_ERR_INV_MORE_ARGS, /* More arguments than specified passed */
HUTILS_ERR_INV_SIZE_ARG, /* Invalid size of argument size */
HUTILS_ERR_END
};
typedef enum _hutils_err_e hutils_err_e;
/* Convert enumeration type to string */
const char * hutils_err_str (hutils_err_e err);
#endif
......@@ -5,7 +5,7 @@
* Released according to the GNU LGPL, version 3 or any later version.
*/
#include "hal_math.h"
#include "hutils_math.h"
#include <inttypes.h>
/* Undef ASSERT_ALLOC to avoid conflicting with other ASSERT_ALLOC */
......@@ -13,54 +13,54 @@
#undef ASSERT_TEST
#endif
#define ASSERT_TEST(test_boolean, err_str, err_goto_label, /* err_core */ ...) \
ASSERT_HAL_TEST(test_boolean, HAL_UTILS, "[halultis:math]", \
ASSERT_HAL_TEST(test_boolean, HAL_UTILS, "[hutils:math]", \
err_str, err_goto_label, /* err_core */ __VA_ARGS__)
#ifdef ASSERT_ALLOC
#undef ASSERT_ALLOC
#endif
#define ASSERT_ALLOC(ptr, err_goto_label, /* err_core */ ...) \
ASSERT_HAL_ALLOC(ptr, HAL_UTILS, "[halutils:math]", \
halutils_err_str(HALUTILS_ERR_ALLOC), \
ASSERT_HAL_ALLOC(ptr, HAL_UTILS, "[hutils:math]", \
hutils_err_str(HUTILS_ERR_ALLOC), \
err_goto_label, /* err_core */ __VA_ARGS__)
#ifdef CHECK_ERR
#undef CHECK_ERR
#endif
#define CHECK_ERR(err, err_type) \
CHECK_HAL_ERR(err, HAL_UTILS, "[halutils:math]", \
halutils_err_str (err_type))
CHECK_HAL_ERR(err, HAL_UTILS, "[hutils:math]", \
hutils_err_str (err_type))
static uint64_t _div_u64_rem (uint64_t dividend, uint32_t divisor,
static uint64_t _hutils_div_u64_rem (uint64_t dividend, uint32_t divisor,
uint32_t *remainder);
/* Cuts from Linux kernel */
uint64_t div_u64_rem (uint64_t dividend, uint32_t divisor,
uint64_t hutils_div_u64_rem (uint64_t dividend, uint32_t divisor,
uint32_t *remainder)
{
return _div_u64_rem (dividend, divisor, remainder);
return _hutils_div_u64_rem (dividend, divisor, remainder);
}
uint64_t div_u64 (uint64_t dividend, uint32_t divisor)
uint64_t hutils_div_u64 (uint64_t dividend, uint32_t divisor)
{
uint32_t remainder;
return div_u64_rem (dividend, divisor, &remainder);
return hutils_div_u64_rem (dividend, divisor, &remainder);
}
uint64_t div64_u64 (uint64_t dividend, uint64_t divisor)
uint64_t hutils_div64_u64 (uint64_t dividend, uint64_t divisor)
{
return dividend / divisor;
}
int64_t div64_s64 (int64_t dividend, int64_t divisor)
int64_t hutils_div64_s64 (int64_t dividend, int64_t divisor)
{
return dividend / divisor;
}
/***************************** Static Functions ******************************/
static uint64_t _div_u64_rem (uint64_t dividend, uint32_t divisor,
static uint64_t _hutils_div_u64_rem (uint64_t dividend, uint32_t divisor,
uint32_t *remainder)
{
*remainder = dividend % divisor;
......
......@@ -5,8 +5,8 @@
* Released according to the GNU LGPL, version 3 or any later version.
*/
#ifndef _HAL_MATH_H_
#define _HAL_MATH_H_
#ifndef _HUTILS_MATH_H_
#define _HUTILS_MATH_H_
#include <math.h>
#include <inttypes.h>
......@@ -19,10 +19,10 @@
#define FLOOR(value) floor (value)
#define CEIL(value) ceil (value)
uint64_t div_u64_rem (uint64_t dividend, uint32_t divisor,
uint32_t *remainder);
uint64_t div_u64 (uint64_t dividend, uint32_t divisor);
uint64_t div64_u64 (uint64_t dividend, uint64_t divisor);
int64_t div64_s64 (int64_t dividend, int64_t divisor);
uint64_t hutils_div_u64_rem (uint64_t dividend, uint32_t divisor,
uint32_t *remainder);
uint64_t hutils_div_u64 (uint64_t dividend, uint32_t divisor);
uint64_t hutils_div64_u64 (uint64_t dividend, uint64_t divisor);
int64_t hutils_div64_s64 (int64_t dividend, int64_t divisor);
#endif
......@@ -5,93 +5,73 @@
* Released according to the GNU LGPL, version 3 or any later version.
*/
#ifndef _HAL_UTILS_H_
#define _HAL_UTILS_H_
#ifndef _HUTILS_H_
#define _HUTILS_H_
#include <inttypes.h>
#include <stddef.h>
/* Returns the necessary string length including the termianting null character
* for a number in a arbitrary base */
uint32_t num_to_str_len (uint32_t key, uint32_t base);
uint32_t hutils_num_to_str_len (uint32_t key, uint32_t base);
/* Returns the necessary string length including the termianting null character
* for a hexadecimal number */
uint32_t hex_to_str_len (uint32_t key);
uint32_t hutils_hex_to_str_len (uint32_t key);
/* Returns the necessary string length including the termianting null character
* for a decimal number */
uint32_t dec_to_str_len (uint32_t key);
uint32_t hutils_dec_to_str_len (uint32_t key);
/* Allocates a string with the necessary size to fit a number in an arbitrary base */
char *halutils_stringify_key (uint32_t key, uint32_t base);
char *hutils_stringify_key (uint32_t key, uint32_t base);
/* Allocates a string with the necessary size to fit a decimal key */
char *halutils_stringify_dec_key (uint32_t key);
char *hutils_stringify_dec_key (uint32_t key);
/* Allocates a string with the necessary size to fit an hexadecimal key */
char *halutils_stringify_hex_key (uint32_t key);
char *hutils_stringify_hex_key (uint32_t key);
/* Converts a key string into the specified numeric base. Must fit into
* a uint32_t */
uint32_t halutils_numerify_key (const char *key, uint32_t base);
uint32_t hutils_numerify_key (const char *key, uint32_t base);
/* Converts a key string into the decimal base. Result must fit into
* a uint32_t */
uint32_t halutils_numerify_dec_key (const char *key);
uint32_t hutils_numerify_dec_key (const char *key);
/* Converts a key string into the hexadecimal base. Result must fit into
* a uint32_t */
uint32_t halutils_numerify_hex_key (const char *key);
uint32_t hutils_numerify_hex_key (const char *key);
/* Concatenates 2 strings together with a separator. returns the string if
* OK, NULL in case of error */
char *halutils_concat_strings (const char *str1, const char* str2, char sep);
char *hutils_concat_strings (const char *str1, const char* str2, char sep);
/* Concatenates 2 strings together without a separator. returns the string if
* OK, NULL in case of error */
char *halutils_concat_strings_no_sep (const char *str1, const char* str2);
char *hutils_concat_strings_no_sep (const char *str1, const char* str2);
/* Concatenates 3 strings together with a separator between the first and second
* strings. returns the string if OK, NULL in case of error */
char *halutils_concat_strings3 (const char *str1, const char* str2,
char *hutils_concat_strings3 (const char *str1, const char* str2,
const char* str3, char sep);
/* Spawns (fork and exec) a new process. Returns, for the parent process, -1
* in case of error and child's PID (> 0) if success. For the child process,
* returns -1 in case of error and 0 in case of success */
int halutils_spawn_chld (const char *program, char *const argv[]);
int hutils_spawn_chld (const char *program, char *const argv[]);
/* Wait for a child process, printing the exit status and possible errors
* in the global LOG. Returns 0 in case of success and -1 in case of error */
int halutils_wait_chld (void);
/* Clone the build revision. Returns the cloned string is successfully or NULL
* on error */
char *halutils_clone_build_revision (void);
/* Copies the build revision into a pre-allocated buffer. Returns a negative number
* on error and the number of copied bytes otherwise */
int halutils_copy_build_revision (char *dest, size_t size);
/* Clone the build date. Returns the cloned string is successfully or NULL
* on error */
char *halutils_clone_build_date (void);
/* Copies the build date into a pre-allocated buffer. Returns a negative number
* on error and the number of copied bytes otherwise */
int halutils_copy_build_date (char *dest, size_t size);
/* Clone the build user name. Returns the cloned string is successfully or NULL
* on error */
char *halutils_clone_build_user_name (void);
/* Copies the build user name into a pre-allocated buffer. Returns a negative number
* on error and the number of copied bytes otherwise */
int halutils_copy_build_user_name (char *dest, size_t size);
/* Clone the build user email. Returns the cloned string is successfully or NULL
* on error */
char *halutils_clone_build_user_email (void);
/* Copies the build user email into a pre-allocated buffer. Returns a negative number
* on error and the number of copied bytes otherwise */
int halutils_copy_build_user_email (char *dest, size_t size);
int hutils_wait_chld (void);
/* Clones a str NULL terminated string and return it to the called. Returns NULL
* in case of error */
char *hutils_clone_str (const char *str);
/* Copies a src NULL terminated string into a dest pre-allocated buffer, up to
* a maximum of size bytes */
int hutils_copy_str (char *dest, const char *src, size_t size);
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment