wi_timer_t * wi_timer_init_with_function(wi_timer_t *timer, wi_timer_func_t *func, wi_time_interval_t interval, wi_boolean_t repeats) { timer->func = func; timer->interval = WI_MAX(interval, _WI_TIMER_MINIMUM_INTERVAL); timer->repeats = repeats; return timer; }
wi_data_t * wi_data_init_with_capacity(wi_data_t *data, wi_uinteger_t capacity) { data->capacity = WI_MAX(wi_exp2m1(wi_log2(capacity) + 1), _WI_DATA_MIN_SIZE); data->bytes = wi_malloc(data->capacity); data->free = true; return data; }
wi_string_t * wi_string_init_with_capacity(wi_string_t *string, wi_uinteger_t capacity) { string->capacity = WI_MAX(wi_exp2m1(wi_log2(capacity) + 1), _WI_STRING_MIN_SIZE); string->string = wi_malloc(string->capacity); string->length = 0; string->free = true; return string; }
wi_dictionary_t * wi_dictionary_init_with_capacity_and_callbacks(wi_dictionary_t *dictionary, wi_uinteger_t capacity, wi_dictionary_key_callbacks_t key_callbacks, wi_dictionary_value_callbacks_t value_callbacks) { dictionary->key_callbacks = key_callbacks; dictionary->value_callbacks = value_callbacks; dictionary->bucket_chunks_offset = _wi_dictionary_buckets_per_page; dictionary->min_count = WI_MAX(wi_exp2m1(wi_log2(capacity) + 1), _WI_DICTIONARY_MIN_COUNT); dictionary->buckets_count = dictionary->min_count; dictionary->buckets = wi_malloc(dictionary->buckets_count * sizeof(_wi_dictionary_bucket_t *)); return dictionary; }
wi_set_t * wi_set_init_with_capacity_and_callbacks(wi_set_t *set, wi_uinteger_t capacity, wi_set_callbacks_t callbacks) { set->callbacks = callbacks; set->bucket_chunks_offset = _wi_set_buckets_per_page; set->min_count = WI_MAX(wi_exp2m1(wi_log2(capacity) + 1), _WI_SET_MIN_COUNT); set->buckets_count = set->min_count; set->buckets = wi_malloc(set->buckets_count * sizeof(_wi_set_bucket_t *)); set->lock = wi_rwlock_init(wi_rwlock_alloc()); return set; }
wi_array_t * wi_array_init_with_capacity_and_callbacks(wi_array_t *array, wi_uinteger_t capacity, wi_array_callbacks_t callbacks) { array->callbacks = callbacks; array->item_chunks_offset = _wi_array_items_per_page; array->items_count = WI_MAX(wi_exp2m1(wi_log2(capacity) + 1), _WI_ARRAY_MIN_COUNT); array->min_count = array->items_count; array->items = wi_malloc(array->items_count * sizeof(_wi_array_item_t *)); array->lock = wi_rwlock_init(wi_rwlock_alloc()); return array; }
static void _wi_string_grow(wi_string_t *string, wi_uinteger_t capacity) { capacity = WI_MAX(wi_exp2m1(wi_log2(capacity) + 1), _WI_STRING_MIN_SIZE); if(string->free) { string->string = wi_realloc(string->string, capacity); } else { string->string = wi_malloc(capacity); string->free = true; } string->capacity = capacity; }
void wr_print_users(void) { wi_list_node_t *node; wr_user_t *user; unsigned int max_length = 0; WI_LIST_FOREACH(wr_users, node, user) max_length = WI_MAX(max_length, wi_string_length(user->nick)); wr_printf_prefix(WI_STR("Users currently online:")); WI_LIST_FOREACH(wr_users, node, user) wr_print_user(user, max_length); }
void wi_timer_reschedule(wi_timer_t *timer, wi_time_interval_t interval) { timer->interval = WI_MAX(interval, _WI_TIMER_MINIMUM_INTERVAL); if(timer->scheduled) _wi_timer_invalidate(timer); if(!timer->incallback) wi_condition_lock_lock(_wi_timer_lock); _wi_timer_schedule(timer); if(!timer->incallback) wi_condition_lock_unlock_with_condition(_wi_timer_lock, 1); }
void wr_print_files(void) { wi_enumerator_t *enumerator; wr_file_t *file; wi_uinteger_t max_length = 0; enumerator = wi_array_data_enumerator(wr_files); while((file = wi_enumerator_next_data(enumerator))) max_length = WI_MAX(max_length, wi_string_length(wr_file_name(file))); enumerator = wi_array_data_enumerator(wr_files); while((file = wi_enumerator_next_data(enumerator))) wr_print_file(file, false, max_length); }
static void wr_command_help(wi_array_t *arguments) { wi_mutable_string_t *string; wi_size_t size; wi_uinteger_t i, x, max, length, max_length; if(wi_array_count(arguments) > 0) { wr_commands_print_usage_for_command(WI_ARRAY(arguments, 0)); } else { wr_printf_prefix(WI_STR("Commands:")); max = WI_ARRAY_SIZE(wr_commands) - 1; max_length = 0; for(i = 0; i < max; i++) { if(wr_commands[i].help) { length = strlen(wr_commands[i].name); max_length = WI_MAX(length, max_length); } } size = wi_terminal_size(wr_terminal); string = wi_mutable_string(); for(i = 0, x = 0; i < max; i++) { if(wr_commands[i].help) { if(!string) string = wi_string_init(wi_string_alloc()); wi_mutable_string_append_format(string, WI_STR(" %s%*s"), wr_commands[i].name, max_length - strlen(wr_commands[i].name) + 1, " "); if(wi_terminal_width_of_string(wr_terminal, string) >= size.width - max_length - max_length) { wr_printf(WI_STR("%@"), string); wi_mutable_string_set_string(string, WI_STR("")); } } } if(wi_string_length(string) > 0) wr_printf(WI_STR("%@"), string); } }
void wr_print_users(wr_window_t *window) { wi_enumerator_t *enumerator; wi_array_t *users; wr_user_t *user; wi_uinteger_t max_length = 0; if(window->chat == wr_public_chat) wr_printf_prefix(WI_STR("Users currently online:")); else wr_printf_prefix(WI_STR("Users in chat:")); users = wr_chat_users(window->chat); enumerator = wi_array_data_enumerator(users); while((user = wi_enumerator_next_data(enumerator))) max_length = WI_MAX(max_length, wi_string_length(wr_user_nick(user))); enumerator = wi_array_data_enumerator(users); while((user = wi_enumerator_next_data(enumerator))) wr_print_user(user, max_length); }
static wi_boolean_t _wi_date_is_equal(wi_runtime_instance_t *instance1, wi_runtime_instance_t *instance2) { wi_date_t *date1 = instance1; wi_date_t *date2 = instance2; return (WI_MAX(date1, date2) - WI_MIN(date1, date2) < _WI_DATE_EPSILON); }
WI_FTS * wi_fts_open(char * const *argv, int options, int (*compar)(const WI_FTSENT **, const WI_FTSENT **)) { WI_FTS *sp; WI_FTSENT *p, *root; int nitems; WI_FTSENT *parent, *tmp; size_t len; /* Options check. */ if (options & ~WI_FTS_OPTIONMASK) { errno = EINVAL; return (NULL); } /* Allocate/initialize the stream */ if ((sp = malloc(sizeof(WI_FTS))) == NULL) return (NULL); memset(sp, 0, sizeof(WI_FTS)); sp->fts_compar = (int (*)(const void *, const void *)) compar; sp->fts_options = options; /* Logical walks turn on NOCHDIR; symbolic links are too hard. */ if (ISSET(WI_FTS_LOGICAL)) SET(WI_FTS_NOCHDIR); /* * Start out with 1K of path space, and enough, in any case, * to hold the user's paths. */ if (wi_fts_palloc(sp, WI_MAX(wi_fts_maxarglen(argv), MAXPATHLEN))) goto mem1; /* Allocate/initialize root's parent. */ if ((parent = wi_fts_alloc(sp, "", 0)) == NULL) goto mem2; parent->fts_level = WI_FTS_ROOTPARENTLEVEL; /* Allocate/initialize root(s). */ for (root = tmp = NULL, nitems = 0; *argv; ++argv, ++nitems) { /* Don't allow zero-length paths. */ if ((len = strlen(*argv)) == 0) { errno = ENOENT; goto mem3; } if ((p = wi_fts_alloc(sp, *argv, len)) == NULL) goto mem3; p->fts_level = WI_FTS_ROOTLEVEL; p->fts_parent = parent; p->fts_accpath = p->fts_name; p->fts_info = wi_fts_stat(sp, p, ISSET(WI_FTS_COMFOLLOW)); /* Command-line "." and ".." are real directories. */ if (p->fts_info == WI_FTS_DOT) p->fts_info = WI_FTS_D; /* * If comparison routine supplied, traverse in sorted * order; otherwise traverse in the order specified. */ if (compar) { p->fts_link = root; root = p; } else { p->fts_link = NULL; if (root == NULL) tmp = root = p; else { tmp->fts_link = p; tmp = p; } } } if (compar && nitems > 1) root = wi_fts_sort(sp, root, nitems); /* * Allocate a dummy pointer and make fts_read think that we've just * finished the node before the root(s); set p->fts_info to FTS_INIT * so that everything about the "current" node is ignored. */ if ((sp->fts_cur = wi_fts_alloc(sp, "", 0)) == NULL) goto mem3; sp->fts_cur->fts_link = root; sp->fts_cur->fts_info = WI_FTS_INIT; /* * If using chdir(2), grab a file descriptor pointing to dot to ensure * that we can get back here; this could be avoided for some paths, * but almost certainly not worth the effort. Slashes, symbolic links, * and ".." are all fairly nasty problems. Note, if we can't get the * descriptor we run anyway, just more slowly. */ if (!ISSET(WI_FTS_NOCHDIR) && (sp->fts_rfd = open(".", O_RDONLY, 0)) < 0) SET(WI_FTS_NOCHDIR); return (sp); mem3: wi_fts_lfree(root); free(parent); mem2: free(sp->fts_path); mem1: free(sp); return (NULL); }