static bool *uf_string_fill_bool(t_string *v_this, const char *charset) { size_t i; size_t size; bool *active; i = 0; if ((active = uf_malloc_s(v_this->v_size, sizeof(*active))) == NULL) return ((bool *)M_ERROR((size_t)NULL, "Bad alloc")); uf_memset(active, false, v_this->v_size * sizeof(*active)); size = uf_str_len(charset); if (size == 0) i = v_this->v_size; while (i < v_this->v_size) { if (uf_strncmp(v_this->v_str + i, charset, size) == 0) { uf_memset(active + i, true, size * sizeof(*active)); i = i + size; } else i = i + 1; } return (active); }
bool f_arqueue_init(t_arqueue *v_this, size_t elem_size, size_t nb_elem) { v_this->v_start = 0; v_this->v_end = 0; v_this->v_size = 0; if ((v_this->v_array = uf_malloc_s(nb_elem, elem_size)) == NULL) return (M_ERROR(false, "Bad alloc")); v_this->v_capacity = nb_elem; v_this->v_sizeof = elem_size; return (true); }
t_list_cell *f_list_cell_create(t_list_cell *prev, t_list_cell *next, void *data) { t_list_cell *cell; if ((cell = uf_malloc_s(1, sizeof(*cell))) != NULL) { cell->v_next = next; cell->v_prev = prev; cell->v_data = data; } return (cell); }
static bool uf_string_dump_word(const char *str, char **tab, size_t size, size_t *word) { if ((tab[*word] = uf_malloc_s(size + 1, sizeof(*tab[*word]))) == NULL) { uf_free_tab_fail((void **)tab, *word); return (false); } uf_memcpy(tab[*word], str, size * sizeof(*tab[*word])); tab[*word][size] = '\0'; *word = *word + 1; tab[*word] = NULL; return (true); }
static t_unit_test *uf_unit_alloc_test(const char *name, void (*test)(t_unit_test *)) { t_unit_test *t; if ((t = uf_malloc_s(1, sizeof(*t))) == NULL) return ((t_unit_test*)M_ERROR((size_t)NULL, "Bad alloc")); t->v_name = name; t->f_func = test; t->v_failed = false; t->v_active = true; t->v_tested = false; D_LIST(init)(&t->v_assert, free); return (t); }
bool f_string_init(t_string *v_this, size_t (*uf_realloc)(size_t size)) { size_t size; size = 2; v_this->v_size = 0; if ((v_this->v_str = uf_malloc_s(size, sizeof(*v_this->v_str))) == NULL) return (M_ERROR(false, "Bad alloc")); uf_memset(v_this->v_str, 0, size * sizeof(*v_this->v_str)); v_this->f_realloc = uf_string_realloc_value; if (uf_realloc != NULL) v_this->f_realloc = uf_realloc; v_this->v_capacity = size; v_this->v_hex = "0123456789ABCDEF"; return (true); }
bool f_array_init(t_array *v_this, size_t (*uf_realloc)(size_t size), void (*uf_delete)(void *ptr), size_t type_size) { v_this->v_size = 0; v_this->v_capacity = 0; v_this->f_realloc = uf_realloc; if (uf_realloc == NULL) v_this->f_realloc = uf_array_realloc; v_this->f_delete = uf_delete; if (uf_delete == NULL) v_this->f_delete = uf_array_delete; if ((v_this->v_data = uf_malloc_s(2, type_size)) == NULL) return (M_ERROR(false, "Bad alloc")); uf_memset(v_this->v_data, 0, 2 * type_size); v_this->v_capacity = 2; v_this->v_type_size = type_size; return (true); }
bool f_unit_add_context(t_unit *v_this, const char *name, bool (*init)(void *), bool (*destroy)(void *)) { t_unit_context *context; if ((context = uf_malloc_s(1, sizeof(*context))) == NULL) return (M_ERROR(false, "Bad alloc")); context->v_name = name; context->f_init = init; context->f_destroy = destroy; context->v_id = D_LIST(size)(&v_this->v_context) + 1; D_LIST(init)(&context->v_test, uf_unit_destroy_test); if (D_LIST(push_back)(&v_this->v_context, context) == false) { uf_free_s((void **)&context); return (false); } return (true); }
bool f_threadpool_init(t_threadpool *v_this, size_t nb_thread) { t_threadpool_data *data; v_this->pv_data.v_run = false; if ((v_this->v_id = uf_malloc_s(nb_thread, sizeof(*v_this->v_id))) == NULL) return (M_ERROR(false, "Bad alloc")); D_QUEUE(init)(&v_this->pv_data.v_tasks, free); if (D_LOCK(init)(&v_this->v_data, &v_this->pv_data, e_lock_default) == false) { uf_free_s((void **)&v_this->v_id); return (M_ERROR(false, "Couldn't initialize lock")); } if (D_LOCK(lock)(&v_this->v_data, (void **)&data) == true) return (D_THREADPOOL(create)(v_this, data, nb_thread)); D_LOCK(destroy)(&v_this->v_data); uf_free_s((void **)&v_this->v_id); return (M_ERROR(false, "An error has occured")); }
bool f_threadpool_add_task(t_threadpool *v_this, t_threadpool_task *task) { t_threadpool_task *add; t_threadpool_data *data; if ((add = uf_malloc_s(1, sizeof(*add))) == NULL) return (M_ERROR(false, "Bad alloc")); uf_memcpy(add, task, sizeof(*add)); if (D_LOCK(lock)(&v_this->v_data, (void **)&data) == true) { if (D_QUEUE(push)(&data->v_tasks, add) == false) { uf_free_s((void **)&add); D_LOCK(release)(&v_this->v_data, (void **)&data); return (M_ERROR(false, "Couldn't add tasks")); } D_LOCK(release)(&v_this->v_data, (void **)&data); return (true); } uf_free_s((void **)&add); return (false); }