void l_key_file_free(LKeyFile *key_file) { if(!key_file) return; l_free(key_file->file); l_slist_free(key_file->line,l_keyvalue_free); l_free(key_file); }
static void fitem_free(FITEM *p) { if(!p) return; l_free(p->file); l_free(p->md5); l_free(p); }
static void l_keyvalue_free(void *data) { KeyValue *k=data; l_free(k->key); l_free(k->value); l_free(k); }
static void l_free_matrix3(void *vptr, int nxmin, int nxmax, int nymin, int nymax, int nzmin, size_t el_size) { /* Frees 3D matrix created by l_malloc_matrix3(). */ int i, j; char ***cptr; if (vptr == NULL) { return; } cptr = (char ***)vptr; for (i = nxmin; i <= nxmax; i++) { for (j = nymin; j <= nymax; j++) { cptr[i][j] += nzmin * el_size / sizeof (char); l_free(cptr[i][j]); } cptr[i] += nymin; l_free(cptr[i]); } cptr += nxmin; l_free(cptr); }
int main(int argc, char* argv[]) { int *a = (int*) l_malloc(sizeof(int)); l_free(a); l_free(a); return 0; }
static void l_free_matrix(void *vptr, int nxmin, int nxmax, int nymin, size_t el_size) { /* Frees matrix created by l_malloc_matrix(). */ int i; char **cptr; if (vptr == NULL) { return; } cptr = (char **)vptr; for (i = nxmin; i <= nxmax; ++i) { cptr[i] += nymin * el_size / sizeof(char); l_free(cptr[i]); } cptr += nxmin; l_free(cptr); }
static void dl_of_il_test(void) { printsln((String)__func__); List il = il_of_string("-20, -1, 0, 1, 20"); List ex = dl_of_string("-20, -1, 0, 1, 20"); List ac = dl_of_il(il); dl_check_within(ac, ex); l_free(il); l_free(ex); l_free(ac); }
static void dl_foldl_test(void) { printsln((String)__func__); List a = dl_of_string(""); check_within_d(dl_foldl(a, double_plus, 100), 100, EPSILON); check_within_d(dl_foldl(a, double_minus, 100), 100, EPSILON); l_free(a); a = dl_of_string("1, 2, 3, 4"); check_within_d(dl_foldl(a, double_plus, 0), (((0 + 1) + 2) + 3) + 4, EPSILON); check_within_d(dl_foldl(a, double_minus, 100), (((100 - 1) - 2) - 3) - 4, EPSILON); l_free(a); }
static void dl_foldr_test(void) { printsln((String)__func__); List a = dl_of_string(""); check_within_d(dl_foldr(a, double_plus, 100), 100, EPSILON); check_within_d(dl_foldr(a, double_minus, 100), 100, EPSILON); l_free(a); a = dl_of_string("1, 2, 3, 4"); check_within_d(dl_foldr(a, double_plus, 100), 1 + (2 + (3 + (4 + 100))), EPSILON); check_within_d(dl_foldr(a, double_minus, 0), 1 - (2 - (3 - (4 - 0))), EPSILON); l_free(a); }
static void dl_choose_test(void) { printsln((String)__func__); List a, ac, ex; a = dl_of_string("1, 2, 3, 4, 5, 6"); ac = dl_choose(a, gt3_times_x, 4); ex = dl_of_string("16, 20, 24"); dl_check_within(ac, ex); l_free(ac); l_free(ex); l_free(a); }
static void dl_fn_test(void) { printsln((String)__func__); List ac, ex; ac = dl_fn(3, two_d_plus_1, 0); ex = dl_of_string("1, 3, 5"); dl_check_within(ac, ex); l_free(ac); l_free(ex); ac = dl_fn(4, two_d_plus_1, 0); ex = dl_of_string("1, 3, 5, 7"); dl_check_within(ac, ex); l_free(ac); l_free(ex); ac = dl_fn(1, two_d_plus_1, 0); ex = dl_of_string("1"); dl_check_within(ac, ex); l_free(ac); l_free(ex); ac = dl_fn(0, two_d_plus_1, 0); ex = dl_create(); dl_check_within(ac, ex); l_free(ac); l_free(ex); }
static void dl_prepend_append_test(void) { printsln((String)__func__); List ac, ex; ac = dl_create(); dl_append(ac, 1); dl_append(ac, 2); dl_append(ac, 3); ex = dl_create(); dl_prepend(ex, 3); dl_prepend(ex, 2); dl_prepend(ex, 1); dl_check_within(ac, ex); l_free(ac); l_free(ex); }
static void pl_prepend_append_test(void) { printsln((Any)__func__); List ac, ex; ac = pl_create(); pl_append(ac, "11"); pl_append(ac, "22"); pl_append(ac, "33"); ex = pl_create(); pl_prepend(ex, "33"); pl_prepend(ex, "22"); pl_prepend(ex, "11"); pl_check_expect(ac, ex); l_free(ac); l_free(ex); }
static void dl_index_fn_test(void) { printsln((String)__func__); List a = dl_of_string("1 2 3 -4 5"); check_expect_i(dl_index_fn(a, dl_gt, 0.0), 0); check_expect_i(dl_index_fn(a, dl_lt, 0.0), 3); l_free(a); }
bool l_column_results_send(l_column_results_t *results, l_column_socket_info_t *sp, l_column_result_id_t id) { int res = pthread_rwlock_rdlock(&results->lock); if (res) { l_log_errno(L_ERR, res, "pthread_rwlock_rdlock() failed"); return 0; } l_column_result_t *result = l_map_remove(&results->result_map, &id, sizeof(l_column_result_id_t)); res = pthread_rwlock_unlock(&results->lock); if (res) { l_log_errno(L_ERR, res, "pthread_rwlock_unlock() failed"); return 0; } if (result) { result->send(sp, result->arg); if (result->free) { result->free(result->arg); } l_free(result); return true; } else { return false; } }
static void status(const char *fmt,...) { CUCtrl list,p; char temp[256]; va_list ap; char *fmt2; list=cu_ctrl_list_from_type(CU_LABEL); for(p=list;p!=NULL;p=p->tlist) { if(strcmp("status",p->group)) { continue; } break; } if(p==NULL) { return; } fmt2=cu_translate(fmt); va_start(ap,fmt); vsnprintf(temp,sizeof(temp),fmt2,ap); va_end(ap); cu_ctrl_set_self(p,temp); l_free(fmt2); cu_step(); }
static void pl_iterator_test(void) { printsln((Any)__func__); // various ways of iterating a list: List ac = pl_create(); pl_append(ac, "a"); pl_append(ac, "b"); pl_append(ac, "c"); ListIterator iter = l_iterator(ac); while (l_has_next(iter)) { Any s = pl_next(&iter); printsln(s); } // PointerListNode *first = (PointerListNode*)ac->first; for (PointerListNode *node = ac->first; node != NULL; node = node->next) { Any s = node->value; printsln(s); } #if 0 while (iter = pl_next(iter)) { Any d = pl_current(iter); printiln(d); } for (AnyListIterator iter = pl_iterator(ac); pl_has_current(iter); iter = pl_next(iter)) { Any d = pl_current(iter); printiln(d); } #endif l_free(ac); }
/* ** generic allocation routine. */ void *luaM_realloc (lua_State *L, void *block, lu_mem oldsize, lu_mem size) { lua_assert((oldsize == 0) == (block == NULL)); if (size == 0) { if (block != NULL) { l_free(block, oldsize); block = NULL; } else return NULL; /* avoid `nblocks' computations when oldsize==size==0 */ } else if (size >= MAX_SIZET) luaG_runerror(L, "memory allocation error: block too big"); else { block = l_realloc(block, oldsize, size); if (block == NULL) { if (L) luaD_throw(L, LUA_ERRMEM); else return NULL; /* error before creating state! */ } } if (L) { lua_assert(G(L) != NULL && G(L)->nblocks > 0); G(L)->nblocks -= oldsize; G(L)->nblocks += size; } return block; }
static void dl_iterator_test(void) { printsln((String)__func__); List ac; ac = dl_of_string("1, 2, 3, 4, 5"); // various ways of iterating a list: ListIterator iter = l_iterator(ac); while (l_has_next(iter)) { double i = dl_next(&iter); printiln(i); } // DoubleListNode *first = (DoubleListNode*)ac->first; for (DoubleListNode *node = ac->first; node != NULL; node = node->next) { double d = node->value; printiln(d); } #if 0 while (iter = dl_next(iter)) { double d = dl_current(iter); printiln(d); } for (DoubleListIterator iter = dl_iterator(ac); dl_has_current(iter); iter = dl_next(iter)) { double d = dl_current(iter); printiln(d); } #endif l_free(ac); }
static void dl_exists_test(void) { printsln((String)__func__); List a = dl_of_string("1, 2, 3, 4, 5, 6"); check_expect_b(dl_exists(a, dl_gt, 3), true); check_expect_b(dl_exists(a, dl_gt, 9), false); check_expect_b(dl_exists(a, dl_lt, -1), false); l_free(a); }
static void dl_contains_test(void) { printsln((String)__func__); List list = dl_of_string("10, 20, 30"); check_expect_b(dl_contains(list, 10, EPSILON), true); check_expect_b(dl_contains(list, 11, EPSILON), false); check_expect_b(dl_contains(list, 30, EPSILON), true); l_free(list); }
static void dl_forall_test(void) { printsln((String)__func__); List a = dl_of_string("1, 2, 3, 4, 5, 6"); check_expect_b(dl_forall(a, dl_gt, 0), true); check_expect_b(dl_forall(a, dl_gt, 1), false); check_expect_b(dl_forall(a, dl_lt, 7), true); l_free(a); }
static void l_column_results_map_destructor(void *arg) { l_column_result_t *result = (l_column_result_t *) arg; if (result->free) { result->free(result->arg); } l_free(result); }
void l_free(struct label *l) { if (!l) return; l_free(l->next); free(l->name); free(l); }
void pl_free_with_destructor(List list, AnyToVoid element_destructor) { if (list != NULL) { pl_assert_element_size(list); for (PointerListNode *node = list->first; node != NULL; node = node->next) { element_destructor(node->value); } l_free(list); } }
static void dl_each_test(void) { printsln((String)__func__); List ac, ex; ac = dl_of_string("1, 2, 3, 4"); dl_each(ac, dl_times, 2); ex = dl_of_string("2, 4, 6, 8"); dl_check_within(ac, ex); l_free(ac); l_free(ex); ac = dl_of_string(""); dl_each(ac, dl_times, 3); ex = dl_of_string(""); dl_check_within(ac, ex); l_free(ac); l_free(ex); }
void pl_free(List list) { if (list != NULL) { pl_assert_element_size(list); for (PointerListNode *node = list->first; node != NULL; node = node->next) { free(node->value); } l_free(list); } }
static void dl_repeat_test(void) { printsln((String)__func__); List list; list = dl_repeat(3, 0); check_within_d(dl_get(list, 0), 0, EPSILON); check_within_d(dl_get(list, 1), 0, EPSILON); check_within_d(dl_get(list, 2), 0, EPSILON); l_free(list); list = dl_repeat(2, -1); check_within_d(dl_get(list, 0), -1, EPSILON); check_within_d(dl_get(list, 1), -1, EPSILON); l_free(list); list = dl_repeat(0, 2); check_expect_i(l_length(list), 0); l_free(list); }
static void dl_index_from_test(void) { printsln((String)__func__); List a = dl_of_string("10 20 30 40 50"); check_expect_i(dl_index_from(a, 20, 0, EPSILON), 1); check_expect_i(dl_index_from(a, 20, 1, EPSILON), 1); check_expect_i(dl_index_from(a, 20, 2, EPSILON), -1); check_expect_i(dl_index_from(a, 30, -1, EPSILON), 2); check_expect_i(dl_index_from(a, 60, 0, EPSILON), -1); l_free(a); }
double dl_foldr(List list, DoubleDoubleIntToDouble f, double init) { assert_function_not_null(f); assert_argument_not_null(list); dl_assert_element_size(list); List rev = l_reverse(list); int i = l_length(list) - 1; for (DoubleListNode *node = rev->first; node != NULL; node = node->next, i--) { init = f(node->value, init, i); } l_free(rev); return init; }