[A] Added memory test and fixed err. treatment errors
This commit is contained in:
10
src/fort.c
10
src/fort.c
@@ -515,13 +515,12 @@ const char *ft_to_string(const FTABLE *table)
|
||||
size_t *col_width_arr = NULL;
|
||||
size_t *row_height_arr = NULL;
|
||||
status = table_rows_and_cols_geometry(table, &col_width_arr, &cols, &row_height_arr, &rows);
|
||||
if (IS_ERROR(status))
|
||||
return NULL;
|
||||
|
||||
if (rows == 0)
|
||||
return cur_F_STRDUP(empty_string);
|
||||
|
||||
if (IS_ERROR(status))
|
||||
return NULL;
|
||||
|
||||
int written = 0;
|
||||
int tmp = 0;
|
||||
size_t i = 0;
|
||||
@@ -871,3 +870,8 @@ FT_EXTERN int ft_set_tbl_option(FTABLE *table, uint32_t option, int value)
|
||||
}
|
||||
return set_entire_table_option(table->options, option, value);
|
||||
}
|
||||
|
||||
FT_EXTERN void ft_set_memory_funcs(void *(*f_malloc)(size_t size), void (*f_free)(void *ptr))
|
||||
{
|
||||
set_memory_funcs(f_malloc, f_free);
|
||||
}
|
||||
|
@@ -6,6 +6,62 @@
|
||||
/*****************************************************************************
|
||||
* LIBFORT helpers
|
||||
*****************************************************************************/
|
||||
|
||||
void *(*fort_malloc)(size_t size) = &malloc;
|
||||
void (*fort_free)(void *ptr) = &free;
|
||||
void *(*fort_calloc)(size_t nmemb, size_t size) = &calloc;
|
||||
void *(*fort_realloc)(void *ptr, size_t size) = &realloc;
|
||||
|
||||
|
||||
static void *custom_fort_calloc(size_t nmemb, size_t size)
|
||||
{
|
||||
size_t total_size = nmemb * size;
|
||||
void *result = F_MALLOC(total_size);
|
||||
if (result != NULL)
|
||||
memset(result, 0, total_size);
|
||||
return result;
|
||||
}
|
||||
|
||||
static void *custom_fort_realloc(void *ptr, size_t size)
|
||||
{
|
||||
if (ptr == NULL)
|
||||
return F_MALLOC(size);
|
||||
if (size == 0) {
|
||||
F_FREE(ptr);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void *new_chunk = F_MALLOC(size);
|
||||
if (new_chunk == NULL)
|
||||
return NULL;
|
||||
|
||||
/*
|
||||
* In theory we should copy MIN(size, size allocated for ptr) bytes,
|
||||
* but this is rather dummy implementation so we don't care about it
|
||||
*/
|
||||
memcpy(new_chunk, ptr, size);
|
||||
F_FREE(ptr);
|
||||
return new_chunk;
|
||||
}
|
||||
|
||||
void set_memory_funcs(void *(*f_malloc)(size_t size), void (*f_free)(void *ptr))
|
||||
{
|
||||
assert((f_malloc == NULL && f_free == NULL) /* Use std functions */
|
||||
|| (f_malloc != NULL && f_free != NULL) /* Use custom functions */);
|
||||
fort_malloc = f_malloc;
|
||||
fort_free = f_free;
|
||||
|
||||
if (fort_malloc == NULL) {
|
||||
fort_calloc = &calloc;
|
||||
fort_realloc = &realloc;
|
||||
} else {
|
||||
fort_calloc = &custom_fort_calloc;
|
||||
fort_realloc = &custom_fort_realloc;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
char *fort_strdup(const char *str)
|
||||
{
|
||||
if (str == NULL)
|
||||
|
@@ -12,10 +12,10 @@
|
||||
|
||||
#define FORT_UNUSED __attribute__((unused))
|
||||
|
||||
#define F_CALLOC calloc
|
||||
#define F_MALLOC malloc
|
||||
#define F_REALLOC realloc
|
||||
#define F_FREE free
|
||||
#define F_MALLOC fort_malloc
|
||||
#define F_FREE fort_free
|
||||
#define F_CALLOC fort_calloc
|
||||
#define F_REALLOC fort_realloc
|
||||
#define F_STRDUP fort_strdup
|
||||
#define F_WCSDUP fort_wcsdup
|
||||
|
||||
@@ -109,6 +109,14 @@ typedef struct separator separator_t;
|
||||
/*****************************************************************************
|
||||
* LIBFORT helpers
|
||||
*****************************************************************************/
|
||||
|
||||
extern void *(*fort_malloc)(size_t size);
|
||||
extern void (*fort_free)(void *ptr);
|
||||
extern void *(*fort_calloc)(size_t nmemb, size_t size);
|
||||
extern void *(*fort_realloc)(void *ptr, size_t size);
|
||||
void set_memory_funcs(void *(*f_malloc)(size_t size), void (*f_free)(void *ptr));
|
||||
|
||||
|
||||
char *fort_strdup(const char* str);
|
||||
wchar_t *fort_wcsdup(const wchar_t* str);
|
||||
size_t number_of_columns_in_format_string(const char *fmt);
|
||||
|
@@ -374,7 +374,7 @@ fort_table_options_t *create_table_options()
|
||||
options->cell_options = create_cell_opt_container();
|
||||
if (options->cell_options == NULL) {
|
||||
destroy_table_options(options);
|
||||
options = NULL;
|
||||
return NULL;
|
||||
}
|
||||
memcpy(&options->entire_table_options, &g_entire_table_options, sizeof(fort_entire_table_options_t));
|
||||
return options;
|
||||
|
Reference in New Issue
Block a user