tb_void_t tb_queue_buffer_push_exit(tb_queue_buffer_t* buffer, tb_size_t size) { // check tb_assert_and_check_return(buffer && buffer->head && buffer->size + size <= buffer->maxn); buffer->size += size; }
static __tb_inline__ tb_void_t g2_gl_draw_stencil_clip_path(g2_gl_draw_t* draw, g2_clipper_item_t const* item) { // check tb_assert_and_check_return(item->type == G2_CLIPPER_ITEM_PATH); // the clip path g2_gl_path_t* path = (g2_gl_path_t*)item->u.path; tb_assert_and_check_return(path); // null? tb_check_return(!g2_path_null(path)); // like g2_gl_path_make_like(path); // like rect? if (path->like == G2_GL_PATH_LIKE_RECT) { // clip bounds g2_gl_draw_stencil_clip_bounds(draw, &path->rect); // ok return ; } // like triangle? else if (path->like == G2_GL_PATH_LIKE_TRIG) { // clip triangle g2_clipper_item_t clip = {0}; clip.type = G2_CLIPPER_ITEM_TRIANGLE; clip.mode = item->mode; clip.u.triangle = path->trig; g2_gl_draw_stencil_clip_triangle(draw, &clip); // ok return ; } // make draw if (!g2_gl_path_make_fill(path)) return ; // check tb_assert(path->fill.data && tb_vector_size(path->fill.data)); tb_assert(path->fill.size && tb_vector_size(path->fill.size)); tb_check_return(path->fill.rect.x1 < path->fill.rect.x2 && path->fill.rect.y1 < path->fill.rect.y2); // init vertices if (draw->context->version < 0x20) g2_glVertexPointer(2, G2_GL_FLOAT, 0, tb_vector_data(path->fill.data)); else g2_glVertexAttribPointer(g2_gl_program_location(draw->program, G2_GL_PROGRAM_LOCATION_VERTICES), 2, G2_GL_FLOAT, G2_GL_FALSE, 0, tb_vector_data(path->fill.data)); // clip path tb_size_t head = 0; tb_size_t size = 0; tb_size_t itor = tb_iterator_head(path->fill.size); tb_size_t tail = tb_iterator_tail(path->fill.size); for (; itor != tail; itor++) { size = tb_iterator_item(path->fill.size, itor); g2_glDrawArrays(G2_GL_TRIANGLE_FAN, (g2_GLint_t)head, (g2_GLint_t)size); head += size; } }
/* ////////////////////////////////////////////////////////////////////////////////////// * modifiors */ tb_void_t tb_queue_buffer_clear(tb_queue_buffer_t* buffer) { tb_assert_and_check_return(buffer); buffer->size = 0; buffer->head = buffer->data; }
static tb_void_t gb_device_skia_apply_paint(gb_skia_device_ref_t impl) { // check tb_assert_and_check_return(impl && impl->paint && impl->base.paint); // init paint impl->paint->reset(); // init style tb_size_t mode = gb_paint_mode(impl->base.paint); switch (mode) { case GB_PAINT_MODE_FILL: impl->paint->setStyle(SkPaint::kFill_Style); break; case GB_PAINT_MODE_STROKE: impl->paint->setStyle(SkPaint::kStroke_Style); break; case GB_PAINT_MODE_FILL_STROKE: impl->paint->setStyle(SkPaint::kStrokeAndFill_Style); break; default: tb_trace_e("invalid paint mode: %lu", gb_paint_mode(impl->base.paint)); break; } // init paint for shader if (gb_paint_shader(impl->base.paint)) { } // init paint for solid else impl->paint->setColor(gb_color_pixel(gb_paint_color(impl->base.paint))); // apply alpha impl->paint->setAlpha(gb_paint_alpha(impl->base.paint)); // init paint for stroking if (mode & GB_PAINT_MODE_STROKE) { // init stroke width impl->paint->setStrokeWidth(gb_float_to_sk(gb_paint_stroke_width(impl->base.paint))); // init cap switch (gb_paint_stroke_cap(impl->base.paint)) { case GB_PAINT_STROKE_CAP_BUTT: impl->paint->setStrokeCap(SkPaint::kButt_Cap); break; case GB_PAINT_STROKE_CAP_ROUND: impl->paint->setStrokeCap(SkPaint::kRound_Cap); break; case GB_PAINT_STROKE_CAP_SQUARE: impl->paint->setStrokeCap(SkPaint::kSquare_Cap); break; default: break; } // init join switch (gb_paint_stroke_join(impl->base.paint)) { case GB_PAINT_STROKE_JOIN_MITER: impl->paint->setStrokeJoin(SkPaint::kMiter_Join); break; case GB_PAINT_STROKE_JOIN_ROUND: impl->paint->setStrokeJoin(SkPaint::kRound_Join); break; case GB_PAINT_STROKE_JOIN_BEVEL: impl->paint->setStrokeJoin(SkPaint::kBevel_Join); break; default: break; } } // init antialiasing tb_size_t flag = gb_paint_flag(impl->base.paint); if (flag & GB_PAINT_FLAG_ANTIALIASING) impl->paint->setFlags(impl->paint->getFlags() | SkPaint::kAntiAlias_Flag); else impl->paint->setFlags(impl->paint->getFlags() & ~SkPaint::kAntiAlias_Flag); // init filter bitmap if (flag & GB_PAINT_FLAG_FILTER_BITMAP) impl->paint->setFlags(impl->paint->getFlags() | SkPaint::kFilterBitmap_Flag); else impl->paint->setFlags(impl->paint->getFlags() & ~SkPaint::kFilterBitmap_Flag); }
static tb_void_t tb_ifaddrs_interface_load6(tb_list_ref_t interfaces) { // check tb_assert_and_check_return(interfaces); // done PIP_ADAPTER_ADDRESSES addresses = tb_null; do { // make the addresses addresses = (PIP_ADAPTER_ADDRESSES)tb_malloc0_type(IP_ADAPTER_ADDRESSES); tb_assert_and_check_break(addresses); // get the real adapter info size ULONG size = sizeof(IP_ADAPTER_ADDRESSES); if (tb_iphlpapi()->GetAdaptersAddresses(AF_INET6, GAA_FLAG_SKIP_DNS_SERVER, tb_null, addresses, &size) == ERROR_BUFFER_OVERFLOW) { // grow the adapter info buffer addresses = (PIP_ADAPTER_ADDRESSES)tb_ralloc(addresses, size); tb_assert_and_check_break(addresses); // reclear it tb_memset(addresses, 0, size); } // get the addresses if (tb_iphlpapi()->GetAdaptersAddresses(AF_INET6, GAA_FLAG_SKIP_DNS_SERVER, tb_null, addresses, &size) != NO_ERROR) break; // done PIP_ADAPTER_ADDRESSES address = addresses; while (address) { // check tb_assert_abort(address->AdapterName); /* attempt to get the interface from the cached interfaces * and make a new interface if no the cached interface */ tb_ifaddrs_interface_t interface_new = {0}; tb_ifaddrs_interface_ref_t interface = tb_ifaddrs_interface_find((tb_iterator_ref_t)interfaces, address->AdapterName); if (!interface) interface = &interface_new; // check tb_assert_abort(interface == &interface_new || interface->name); // save flags if (address->IfType == IF_TYPE_SOFTWARE_LOOPBACK) interface->flags |= TB_IFADDRS_INTERFACE_FLAG_IS_LOOPBACK; // save hwaddr if (address->PhysicalAddressLength == sizeof(interface->hwaddr.u8)) { interface->flags |= TB_IFADDRS_INTERFACE_FLAG_HAVE_HWADDR; tb_memcpy(interface->hwaddr.u8, address->PhysicalAddress, sizeof(interface->hwaddr.u8)); } // save ipaddrs PIP_ADAPTER_UNICAST_ADDRESS ipAddress = address->FirstUnicastAddress; while (ipAddress && (interface->flags & TB_IFADDRS_INTERFACE_FLAG_HAVE_IPADDR) != TB_IFADDRS_INTERFACE_FLAG_HAVE_IPADDR) { // done tb_ipaddr_t ipaddr; struct sockaddr_storage* saddr = (struct sockaddr_storage*)ipAddress->Address.lpSockaddr; if (saddr && tb_sockaddr_save(&ipaddr, saddr)) { if (ipaddr.family == TB_IPADDR_FAMILY_IPV4) { interface->flags |= TB_IFADDRS_INTERFACE_FLAG_HAVE_IPADDR4; interface->ipaddr4 = ipaddr.u.ipv4; } else if (ipaddr.family == TB_IPADDR_FAMILY_IPV6) { interface->flags |= TB_IFADDRS_INTERFACE_FLAG_HAVE_IPADDR6; interface->ipaddr6 = ipaddr.u.ipv6; } } // the next ipAddress = ipAddress->Next; } // new interface? save it if ( interface == &interface_new && interface->flags) { // save interface name interface->name = tb_strdup(address->AdapterName); tb_assert_abort(interface->name); // save interface tb_list_insert_tail(interfaces, interface); } // the next address address = address->Next; } } while (0); // exit the addresses if (addresses) tb_free(addresses); addresses = tb_null; }
tb_void_t tb_fixed_pool_clear(tb_fixed_pool_ref_t pool) { // check tb_fixed_pool_impl_t* impl = (tb_fixed_pool_impl_t*)pool; tb_assert_and_check_return(impl); // exit items if (impl->func_exit) tb_fixed_pool_walk(pool, tb_fixed_pool_item_exit, (tb_pointer_t)impl); // exit the current slot first if (impl->current_slot) tb_fixed_pool_slot_exit(impl, impl->current_slot); impl->current_slot = tb_null; // exit the partial slots tb_iterator_ref_t partial_iterator = tb_list_entry_itor(&impl->partial_slots); if (partial_iterator) { // walk it tb_size_t itor = tb_iterator_head(partial_iterator); while (itor != tb_iterator_tail(partial_iterator)) { // the slot tb_fixed_pool_slot_t* slot = (tb_fixed_pool_slot_t*)tb_iterator_item(partial_iterator, itor); tb_assert_and_check_break(slot); // save next tb_size_t next = tb_iterator_next(partial_iterator, itor); // exit data tb_fixed_pool_slot_exit(impl, slot); // next itor = next; } } // exit the full slots tb_iterator_ref_t full_iterator = tb_list_entry_itor(&impl->full_slots); if (full_iterator) { // walk it tb_size_t itor = tb_iterator_head(full_iterator); while (itor != tb_iterator_tail(full_iterator)) { // the slot tb_fixed_pool_slot_t* slot = (tb_fixed_pool_slot_t*)tb_iterator_item(full_iterator, itor); tb_assert_and_check_break(slot); // save next tb_size_t next = tb_iterator_next(full_iterator, itor); // exit data tb_fixed_pool_slot_exit(impl, slot); // next itor = next; } } // clear item count impl->item_count = 0; // clear current slot impl->current_slot = tb_null; // clear partial slots tb_list_entry_clear(&impl->partial_slots); // clear full slots tb_list_entry_clear(&impl->full_slots); }
tb_void_t tb_aicp_loop_util(tb_aicp_ref_t aicp, tb_bool_t (*stop)(tb_cpointer_t priv), tb_cpointer_t priv) { // check tb_aicp_impl_t* impl = (tb_aicp_impl_t*)aicp; tb_assert_and_check_return(impl); // the ptor tb_aicp_ptor_impl_t* ptor = impl->ptor; tb_assert_and_check_return(ptor && ptor->loop_spak); // the loop spak tb_long_t (*loop_spak)(tb_aicp_ptor_impl_t* , tb_handle_t, tb_aice_ref_t , tb_long_t ) = ptor->loop_spak; // worker++ tb_atomic_fetch_and_inc(&impl->work); // init loop tb_handle_t loop = ptor->loop_init? ptor->loop_init(ptor) : tb_null; // trace tb_trace_d("loop[%p]: init", loop); // spak ctime tb_cache_time_spak(); // loop while (1) { // spak tb_aice_t resp = {0}; tb_long_t ok = loop_spak(ptor, loop, &resp, -1); // spak ctime tb_cache_time_spak(); // failed? tb_check_break(ok >= 0); // timeout? tb_check_continue(ok); // check aico tb_aico_impl_t* aico = (tb_aico_impl_t*)resp.aico; tb_assert_and_check_continue(aico); // trace tb_trace_d("loop[%p]: spak: code: %lu, aico: %p, state: %s: %ld", loop, resp.code, aico, aico? tb_state_cstr(tb_atomic_get(&aico->state)) : "null", ok); // pending? clear state if be not accept or accept failed tb_size_t state = TB_STATE_OPENED; state = (resp.code != TB_AICE_CODE_ACPT || resp.state != TB_STATE_OK)? tb_atomic_fetch_and_pset(&aico->state, TB_STATE_PENDING, state) : tb_atomic_get(&aico->state); // killed or killing? if (state == TB_STATE_KILLED || state == TB_STATE_KILLING) { // update the aice state resp.state = TB_STATE_KILLED; // killing? update to the killed state tb_atomic_fetch_and_pset(&aico->state, TB_STATE_KILLING, TB_STATE_KILLED); } // done func, @note maybe the aico exit will be called if (resp.func && !resp.func(&resp)) { // trace #ifdef __tb_debug__ tb_trace_e("loop[%p]: done aice func failed with code: %lu at line: %lu, func: %s, file: %s!", loop, resp.code, aico->line, aico->func, aico->file); #else tb_trace_e("loop[%p]: done aice func failed with code: %lu!", loop, resp.code); #endif } // killing? update to the killed state tb_atomic_fetch_and_pset(&aico->state, TB_STATE_KILLING, TB_STATE_KILLED); // stop it? if (stop && stop(priv)) tb_aicp_kill(aicp); } // exit loop if (ptor->loop_exit) ptor->loop_exit(ptor, loop); // worker-- tb_atomic_fetch_and_dec(&impl->work); // trace tb_trace_d("loop[%p]: exit", loop); }
static tb_void_t tb_ifaddrs_interface_done_hwaddr(tb_list_ref_t interfaces, tb_hash_map_ref_t names, struct nlmsghdr* response) { // check tb_assert_and_check_return(interfaces && names && response); // the info struct ifaddrmsg* info = (struct ifaddrmsg *)NLMSG_DATA(response); // attempt to find the interface name tb_bool_t owner = tb_false; tb_char_t* name = (tb_char_t*)tb_hash_map_get(names, tb_u2p(info->ifa_index)); if (!name) { // get the interface name struct rtattr* rta = tb_null; tb_size_t rta_size = NLMSG_PAYLOAD(response, sizeof(struct ifaddrmsg)); for(rta = IFLA_RTA(info); RTA_OK(rta, rta_size); rta = RTA_NEXT(rta, rta_size)) { // done tb_pointer_t rta_data = RTA_DATA(rta); tb_size_t rta_data_size = RTA_PAYLOAD(rta); switch(rta->rta_type) { case IFLA_IFNAME: { // make name name = (tb_char_t*)tb_ralloc(name, rta_data_size + 1); tb_assert_and_check_break(name); // copy name tb_strlcpy(name, rta_data, rta_data_size + 1); // save name tb_hash_map_insert(names, tb_u2p(info->ifa_index), name); owner = tb_true; } break; default: break; } } } // check tb_check_return(name); // done struct rtattr* rta = tb_null; tb_size_t rta_size = NLMSG_PAYLOAD(response, sizeof(struct ifaddrmsg)); for(rta = IFLA_RTA(info); RTA_OK(rta, rta_size); rta = RTA_NEXT(rta, rta_size)) { /* attempt to get the interface from the cached interfaces * and make a new interface if no the cached interface */ tb_ifaddrs_interface_t interface_new = {0}; tb_ifaddrs_interface_ref_t interface = tb_ifaddrs_interface_find((tb_iterator_ref_t)interfaces, name); if (!interface) interface = &interface_new; // check tb_assert(interface == &interface_new || interface->name); // done tb_pointer_t rta_data = RTA_DATA(rta); tb_size_t rta_data_size = RTA_PAYLOAD(rta); switch(rta->rta_type) { case IFLA_ADDRESS: { // no hwaddr? if (!(interface->flags & TB_IFADDRS_INTERFACE_FLAG_HAVE_HWADDR)) { // check tb_check_break(rta_data_size == sizeof(interface->hwaddr.u8)); // save flags interface->flags |= TB_IFADDRS_INTERFACE_FLAG_HAVE_HWADDR; if (info->ifa_flags & IFF_LOOPBACK) interface->flags |= TB_IFADDRS_INTERFACE_FLAG_IS_LOOPBACK; // save hwaddr tb_memcpy(interface->hwaddr.u8, rta_data, sizeof(interface->hwaddr.u8)); // trace tb_trace_d("name: %s, hwaddr: %{hwaddr}", name, &interface->hwaddr); // new interface? save it if (interface == &interface_new) { // save interface name interface->name = tb_strdup(name); tb_assert(interface->name); // save interface tb_list_insert_tail(interfaces, interface); } } } break; case IFLA_IFNAME: case IFLA_BROADCAST: case IFLA_STATS: break; default: break; } } // exit name if (name && owner) tb_free(name); name = tb_null; }
static tb_void_t tb_demo_http_session_head_parse(tb_demo_http_session_ref_t session) { // check tb_assert_and_check_return(session); // the first line? tb_char_t const* p = session->line; if (!session->line_index) { // parse get if (!tb_strnicmp(p, "GET", 3)) { session->code = TB_HTTP_CODE_OK; session->method = TB_HTTP_METHOD_GET; p += 3; } // parse post else if (!tb_strnicmp(p, "POST", 4)) { session->code = TB_HTTP_CODE_NOT_IMPLEMENTED; session->method = TB_HTTP_METHOD_POST; p += 4; } // other method is not implemented else session->code = TB_HTTP_CODE_NOT_IMPLEMENTED; // get or post? parse the path if ( session->method == TB_HTTP_METHOD_GET || session->method == TB_HTTP_METHOD_POST) { // skip space while (*p && tb_isspace(*p)) p++; // append path tb_size_t i = 0; while (*p && !tb_isspace(*p) && i < sizeof(session->path) - 1) session->path[i++] = *p++; session->path[i] = '\0'; } } // key: value? else { // seek to value while (*p && *p != ':') p++; tb_assert_and_check_return(*p); p++; while (*p && tb_isspace(*p)) p++; // no value tb_check_return(*p); // parse content-length if (!tb_strnicmp(session->line, "Content-Length", 14)) session->content_size = tb_stou64(p); // parse connection else if (!tb_strnicmp(session->line, "Connection", 10)) session->keep_alive = !tb_stricmp(p, "keep-alive"); // parse range else if (!tb_strnicmp(session->line, "Range", 5)) session->code = TB_HTTP_CODE_NOT_IMPLEMENTED; } }
tb_void_t tb_dump_data_from_stream(tb_stream_ref_t stream) { // check tb_assert_and_check_return(stream); // init tb_size_t offset = 0; // dump head tb_trace_i(""); // done tb_char_t info[8192]; while (!tb_stream_beof(stream)) { // read line tb_size_t i = 0; tb_size_t n = 147; tb_long_t read = 0; tb_byte_t line[0x20]; while (read < 0x20) { // read data tb_long_t real = tb_stream_read(stream, line + read, 0x20 - read); // has data? if (real > 0) read += real; // no data? else if (!real) { // wait tb_long_t e = tb_stream_wait(stream, TB_AIOE_CODE_RECV, tb_stream_timeout(stream)); tb_assert_and_check_break(e >= 0); // timeout? tb_check_break(e); // has read? tb_assert_and_check_break(e & TB_AIOE_CODE_RECV); } else break; } // full line? tb_char_t* p = info; tb_char_t* e = info + sizeof(info); if (read == 0x20) { // dump offset if (p < e) p += tb_snprintf(p, e - p, "%08X ", offset); // dump data for (i = 0; i < 0x20; i++) { if (!(i & 3) && p < e) p += tb_snprintf(p, e - p, " "); if (p < e) p += tb_snprintf(p, e - p, " %02X", line[i]); } // dump spaces if (p < e) p += tb_snprintf(p, e - p, " "); // dump characters for (i = 0; i < 0x20; i++) { if (p < e) p += tb_snprintf(p, e - p, "%c", tb_isgraph(line[i])? line[i] : '.'); } // dump it if (p < e) { // end *p = '\0'; // trace tb_trace_i("%s", info); } // update offset offset += 0x20; } // has left? else if (read) { // init padding tb_size_t padding = n - 0x20; // dump offset if (p < e) p += tb_snprintf(p, e - p, "%08X ", offset); if (padding >= 9) padding -= 9; // dump data for (i = 0; i < read; i++) { if (!(i & 3)) { if (p < e) p += tb_snprintf(p, e - p, " "); if (padding) padding--; } if (p < e) p += tb_snprintf(p, e - p, " %02X", line[i]); if (padding >= 3) padding -= 3; } // dump spaces while (padding--) if (p < e) p += tb_snprintf(p, e - p, " "); // dump characters for (i = 0; i < read; i++) { if (p < e) p += tb_snprintf(p, e - p, "%c", tb_isgraph(line[i])? line[i] : '.'); } // dump it if (p < e) { // end *p = '\0'; // trace tb_trace_i("%s", info); } // update offset offset += read; } // end else break; } }
static tb_void_t tb_ifaddrs_interface_done_ipaddr(tb_list_ref_t interfaces, tb_hash_map_ref_t names, struct nlmsghdr* response) { // check tb_assert_and_check_return(interfaces && names && response); // the info struct ifaddrmsg* info = (struct ifaddrmsg *)NLMSG_DATA(response); // must be not link tb_assert_and_check_return(info->ifa_family != AF_PACKET); // attempt to find the interface name tb_bool_t owner = tb_false; tb_char_t* name = (tb_char_t*)tb_hash_map_get(names, tb_u2p(info->ifa_index)); if (!name) { // get the interface name struct rtattr* rta = tb_null; tb_size_t rta_size = NLMSG_PAYLOAD(response, sizeof(struct ifaddrmsg)); for(rta = IFA_RTA(info); RTA_OK(rta, rta_size); rta = RTA_NEXT(rta, rta_size)) { // done tb_pointer_t rta_data = RTA_DATA(rta); tb_size_t rta_data_size = RTA_PAYLOAD(rta); switch(rta->rta_type) { case IFA_LABEL: { // make name name = (tb_char_t*)tb_ralloc(name, rta_data_size + 1); tb_assert_and_check_break(name); // copy name tb_strlcpy(name, rta_data, rta_data_size + 1); // save name tb_hash_map_insert(names, tb_u2p(info->ifa_index), name); owner = tb_true; } break; default: break; } } } // check tb_check_return(name); // done struct rtattr* rta = tb_null; tb_size_t rta_size = NLMSG_PAYLOAD(response, sizeof(struct ifaddrmsg)); for(rta = IFA_RTA(info); RTA_OK(rta, rta_size); rta = RTA_NEXT(rta, rta_size)) { /* attempt to get the interface from the cached interfaces * and make a new interface if no the cached interface */ tb_ifaddrs_interface_t interface_new = {0}; tb_ifaddrs_interface_ref_t interface = tb_ifaddrs_interface_find((tb_iterator_ref_t)interfaces, name); if (!interface) interface = &interface_new; // check tb_assert(interface == &interface_new || interface->name); // done tb_pointer_t rta_data = RTA_DATA(rta); switch(rta->rta_type) { case IFA_LOCAL: case IFA_ADDRESS: { // make ipaddr tb_ipaddr_t ipaddr; if (!tb_ifaddrs_netlink_ipaddr_save(&ipaddr, info->ifa_family, info->ifa_index, rta_data)) break; // save flags if ((info->ifa_flags & IFF_LOOPBACK) || tb_ipaddr_ip_is_loopback(&ipaddr)) interface->flags |= TB_IFADDRS_INTERFACE_FLAG_IS_LOOPBACK; // save ipaddr switch (tb_ipaddr_family(&ipaddr)) { case TB_IPADDR_FAMILY_IPV4: { interface->flags |= TB_IFADDRS_INTERFACE_FLAG_HAVE_IPADDR4; interface->ipaddr4 = ipaddr.u.ipv4; } break; case TB_IPADDR_FAMILY_IPV6: { interface->flags |= TB_IFADDRS_INTERFACE_FLAG_HAVE_IPADDR6; interface->ipaddr6 = ipaddr.u.ipv6; } break; default: break; } // trace tb_trace_d("name: %s, ipaddr: %{ipaddr}", name, &ipaddr); // new interface? save it if (tb_ipaddr_family(&ipaddr) && interface == &interface_new) { // save interface name interface->name = tb_strdup(name); tb_assert(interface->name); // save interface tb_list_insert_tail(interfaces, interface); } } break; case IFA_LABEL: case IFA_BROADCAST: break; default: break; } } // exit name if (name && owner) tb_free(name); name = tb_null; }
static tb_void_t tb_test_heap_min_func() { // init heap tb_heap_ref_t heap = tb_heap_init(16, tb_element_uint32()); tb_assert_and_check_return(heap); // clear rand tb_random_clear(tb_null); // make heap tb_size_t i = 0; for (i = 0; i < 100; i++) { // the value tb_uint32_t val = tb_random_range(tb_null, 0, 50); // trace // tb_trace_i("heap_min: put: %u", val); // put it tb_heap_put(heap, tb_u2p(val)); } // clear rand tb_random_clear(tb_null); // remove some values for (i = 0; i < 100; i++) { // the value tb_uint32_t val = tb_random_range(tb_null, 0, 50); // remove it? if (!(i & 3)) { tb_size_t itor = tb_find_all(heap, tb_u2p(val)); if (itor != tb_iterator_tail(heap)) tb_heap_remove(heap, itor); } } // append heap for (i = 0; i < 30; i++) { // the value tb_uint32_t val = tb_random_range(tb_null, 0, 50); // put it tb_heap_put(heap, tb_u2p(val)); } // trace tb_trace_i(""); // dump heap while (tb_heap_size(heap)) { // put it tb_uint32_t val = (tb_uint32_t)(tb_size_t)tb_heap_top(heap); // trace tb_trace_i("heap_min: pop: %u", val); // pop it tb_heap_pop(heap); } // exit heap tb_heap_exit(heap); }
tb_void_t tb_dns_cache_set(tb_char_t const* name, tb_ipv4_t const* addr) { // check tb_assert_and_check_return(name && addr && addr->u32); // trace tb_trace_d("set: %s => %u.%u.%u.%u", name, addr->u8[0], addr->u8[1], addr->u8[2], addr->u8[3]); // init addr tb_dns_cache_addr_t caddr; caddr.ipv4 = *addr; caddr.time = tb_dns_cache_now(); // enter tb_spinlock_enter(&g_lock); // done do { // check tb_assert_and_check_break(g_cache.hash); // remove the expired items if full if (tb_hash_size(g_cache.hash) >= TB_DNS_CACHE_MAXN) { // the expired time g_cache.expired = ((tb_size_t)(g_cache.times / tb_hash_size(g_cache.hash)) + 1); // check tb_assert_and_check_break(g_cache.expired); // trace tb_trace_d("expired: %lu", g_cache.expired); // remove the expired times tb_remove_if(g_cache.hash, tb_dns_cache_cler, tb_null); } // check tb_assert_and_check_break(tb_hash_size(g_cache.hash) < TB_DNS_CACHE_MAXN); // save addr tb_hash_set(g_cache.hash, name, &caddr); // update times g_cache.times += caddr.time; // trace tb_trace_d("set: %s => %u.%u.%u.%u, time: %u, size: %u", name , caddr.ipv4.u8[0] , caddr.ipv4.u8[1] , caddr.ipv4.u8[2] , caddr.ipv4.u8[3] , caddr.time , tb_hash_size(g_cache.hash)); } while (0); // leave tb_spinlock_leave(&g_lock); }
tb_void_t tb_fixed_pool_clear(tb_fixed_pool_ref_t self) { // check tb_fixed_pool_t* pool = (tb_fixed_pool_t*)self; tb_assert_and_check_return(pool); // exit items if (pool->func_exit) tb_fixed_pool_walk(self, tb_fixed_pool_item_exit, (tb_pointer_t)pool); // exit the partial slots tb_iterator_ref_t partial_iterator = tb_list_entry_itor(&pool->partial_slots); if (partial_iterator) { // walk it tb_size_t itor = tb_iterator_head(partial_iterator); while (itor != tb_iterator_tail(partial_iterator)) { // the slot tb_fixed_pool_slot_t* slot = (tb_fixed_pool_slot_t*)tb_iterator_item(partial_iterator, itor); tb_assert_and_check_break(slot); // check tb_assert(slot != pool->current_slot); // save next tb_size_t next = tb_iterator_next(partial_iterator, itor); // exit slot tb_fixed_pool_slot_exit(pool, slot); // next itor = next; } } // exit the full slots tb_iterator_ref_t full_iterator = tb_list_entry_itor(&pool->full_slots); if (full_iterator) { // walk it tb_size_t itor = tb_iterator_head(full_iterator); while (itor != tb_iterator_tail(full_iterator)) { // the slot tb_fixed_pool_slot_t* slot = (tb_fixed_pool_slot_t*)tb_iterator_item(full_iterator, itor); tb_assert_and_check_break(slot); // check tb_assert(slot != pool->current_slot); // save next tb_size_t next = tb_iterator_next(full_iterator, itor); // exit slot tb_fixed_pool_slot_exit(pool, slot); // next itor = next; } } // clear current slot if (pool->current_slot && pool->current_slot->pool) tb_static_fixed_pool_clear(pool->current_slot->pool); // clear item count pool->item_count = 0; // clear partial slots tb_list_entry_clear(&pool->partial_slots); // clear full slots tb_list_entry_clear(&pool->full_slots); }
static tb_void_t tb_pool_data_dump_data(tb_byte_t const* data, tb_size_t size) { // check tb_assert_and_check_return(data && size); // dump head tb_trace_i(""); // walk tb_size_t i = 0; tb_size_t n = 147; tb_byte_t const* p = data; tb_byte_t const* e = data + size; tb_char_t info[8192]; while (p < e) { // full line? tb_char_t* q = info; tb_char_t* d = info + sizeof(info); if (p + 0x20 <= e) { // dump offset if (q < d) q += tb_snprintf(q, d - q, "%08X ", p - data); // dump data for (i = 0; i < 0x20; i++) { if (!(i & 3) && q < d) q += tb_snprintf(q, d - q, " "); if (q < d) q += tb_snprintf(q, d - q, " %02X", p[i]); } // dump spaces if (q < d) q += tb_snprintf(q, d - q, " "); // dump characters for (i = 0; i < 0x20; i++) { if (q < d) q += tb_snprintf(q, d - q, "%c", tb_isgraph(p[i])? p[i] : '.'); } // dump it if (q < d) { // end *q = '\0'; // trace tb_trace_i("%s", info); } // update p p += 0x20; } // has left? else if (p < e) { // init padding tb_size_t padding = n - 0x20; // dump offset if (q < d) q += tb_snprintf(q, d - q, "%08X ", p - data); if (padding >= 9) padding -= 9; // dump data tb_size_t left = e - p; for (i = 0; i < left; i++) { if (!(i & 3)) { if (q < d) q += tb_snprintf(q, d - q, " "); if (padding) padding--; } if (q < d) q += tb_snprintf(q, d - q, " %02X", p[i]); if (padding >= 3) padding -= 3; } // dump spaces while (padding--) if (q < d) q += tb_snprintf(q, d - q, " "); // dump characters for (i = 0; i < left; i++) { if (q < d) q += tb_snprintf(q, d - q, "%c", tb_isgraph(p[i])? p[i] : '.'); } // dump it if (q < d) { // end *q = '\0'; // trace tb_trace_i("%s", info); } // update p p += left; } // end else break; } }
static tb_void_t gb_window_sdl_fullscreen(gb_window_ref_t window, tb_bool_t fullscreen) { // check gb_window_sdl_impl_t* impl = (gb_window_sdl_impl_t*)window; tb_assert_and_check_return(impl); // the pixmap gb_pixmap_ref_t pixmap = gb_pixmap(impl->base.pixfmt, 0xff); tb_assert_and_check_return(pixmap); // fullscreen? tb_size_t changed = tb_false; if (fullscreen && !(impl->base.flag & GB_WINDOW_FLAG_FULLSCREEN)) { // exit surface if (impl->surface) SDL_FreeSurface(impl->surface); // init mode tb_size_t mode = SDL_DOUBLEBUF | SDL_FULLSCREEN; // TODO // the screen width and height tb_uint16_t screen_width = 0;//tb_screen_width(); tb_uint16_t screen_height = 0;//tb_screen_height(); tb_assert(screen_width && screen_height && screen_width <= GB_WIDTH_MAXN && screen_height <= GB_HEIGHT_MAXN); // init surface impl->surface = SDL_SetVideoMode(screen_width, screen_height, pixmap->bpp, mode | SDL_HWSURFACE); if (!impl->surface) impl->surface = SDL_SetVideoMode(screen_width, screen_height, pixmap->bpp, mode | SDL_SWSURFACE); tb_assert(impl->surface && impl->surface->pixels); // update flag impl->base.flag |= GB_WINDOW_FLAG_FULLSCREEN; // save the normal width and height impl->normal_width = (tb_uint16_t)gb_window_width(window); impl->normal_height = (tb_uint16_t)gb_window_height(window); // update the window width and height impl->base.width = screen_width; impl->base.height = screen_height; // changed changed = tb_true; } else if (impl->base.flag & GB_WINDOW_FLAG_FULLSCREEN) { // exit surface if (impl->surface) SDL_FreeSurface(impl->surface); // init mode tb_size_t mode = SDL_DOUBLEBUF; if (impl->base.flag & GB_WINDOW_FLAG_HIHE_TITLEBAR) mode |= SDL_NOFRAME; if (impl->base.flag & GB_WINDOW_FLAG_NOT_REISZE) mode &= ~SDL_RESIZABLE; else mode |= SDL_RESIZABLE; // init surface impl->surface = SDL_SetVideoMode(impl->normal_width, impl->normal_height, pixmap->bpp, mode | SDL_HWSURFACE); if (!impl->surface) impl->surface = SDL_SetVideoMode(impl->normal_width, impl->normal_height, pixmap->bpp, mode | SDL_SWSURFACE); tb_assert(impl->surface && impl->surface->pixels); // update flag impl->base.flag &= ~GB_WINDOW_FLAG_FULLSCREEN; // update the window width and height impl->base.width = impl->normal_width; impl->base.height = impl->normal_height; // changed changed = tb_true; } // ok? if (changed) { // exit canvas first if (impl->canvas) gb_canvas_exit(impl->canvas); // exit bitmap first if (impl->base.bitmap) tb_free(impl->base.bitmap); // init bitmap impl->base.bitmap = gb_bitmap_init(impl->surface->pixels, impl->base.pixfmt, impl->base.width, impl->base.height, impl->surface->pitch, tb_false); tb_assert(impl->base.bitmap); // init canvas impl->canvas = gb_canvas_init_from_window(window); tb_assert(impl->canvas); // done resize if (impl->base.info.resize) impl->base.info.resize((gb_window_ref_t)impl, impl->canvas, impl->base.info.priv); } }
tb_void_t tb_object_setp(tb_object_ref_t object, tb_cpointer_t priv) { // check tb_assert_and_check_return(object); object->priv = priv; }
static tb_void_t gb_window_sdl_loop(gb_window_ref_t window) { // check gb_window_sdl_impl_t* impl = (gb_window_sdl_impl_t*)window; tb_assert_and_check_return(impl); // init canvas if (!impl->canvas) impl->canvas = gb_canvas_init_from_window(window); tb_assert(impl->canvas); // done init if (impl->base.info.init && !impl->base.info.init((gb_window_ref_t)impl, impl->canvas, impl->base.info.priv)) return ; // loop SDL_Event evet; tb_hong_t time; tb_bool_t stop = tb_false; tb_size_t delay = 1000 / (impl->base.info.framerate? impl->base.info.framerate : GB_WINDOW_DEFAULT_FRAMERATE); while (!stop) { // spak time = gb_window_impl_spak((gb_window_ref_t)impl); // lock the surface SDL_LockSurface(impl->surface); // draw gb_window_impl_draw((gb_window_ref_t)impl, impl->canvas); // unlock the surface SDL_UnlockSurface(impl->surface); // flip if (SDL_Flip(impl->surface) < 0) stop = tb_true; // poll while (SDL_PollEvent(&evet)) { // done switch (evet.type) { case SDL_MOUSEMOTION: { // init event gb_event_t event = {0}; event.type = GB_EVENT_TYPE_MOUSE; event.u.mouse.code = GB_MOUSE_MOVE; event.u.mouse.button = impl->button; gb_point_imake(&event.u.mouse.cursor, evet.motion.x, evet.motion.y); // done event gb_window_impl_event((gb_window_ref_t)impl, &event); } break; case SDL_MOUSEBUTTONUP: case SDL_MOUSEBUTTONDOWN: { // init event gb_event_t event = {0}; event.type = GB_EVENT_TYPE_MOUSE; event.u.mouse.code = evet.type == SDL_MOUSEBUTTONDOWN? GB_MOUSE_DOWN : GB_MOUSE_UP; gb_point_imake(&event.u.mouse.cursor, evet.button.x, evet.button.y); // init button switch (evet.button.button) { case SDL_BUTTON_LEFT: event.u.mouse.button = GB_MOUSE_BUTTON_LEFT; break; case SDL_BUTTON_RIGHT: event.u.mouse.button = GB_MOUSE_BUTTON_RIGHT; break; case SDL_BUTTON_MIDDLE: event.u.mouse.button = GB_MOUSE_BUTTON_MIDDLE; break; default: event.u.mouse.button = GB_MOUSE_BUTTON_NONE; break; } // save button impl->button = evet.type == SDL_MOUSEBUTTONDOWN? event.u.mouse.button : GB_MOUSE_BUTTON_NONE; // done event gb_window_impl_event((gb_window_ref_t)impl, &event); } break; case SDL_KEYDOWN: case SDL_KEYUP: { // init event gb_event_t event = {0}; event.type = GB_EVENT_TYPE_KEYBOARD; event.u.keyboard.pressed = evet.type == SDL_KEYDOWN? tb_true : tb_false; // init code switch ((tb_size_t)evet.key.keysym.sym) { case SDLK_F1: event.u.keyboard.code = GB_KEY_F1; break; case SDLK_F2: event.u.keyboard.code = GB_KEY_F2; break; case SDLK_F3: event.u.keyboard.code = GB_KEY_F3; break; case SDLK_F4: event.u.keyboard.code = GB_KEY_F4; break; case SDLK_F5: event.u.keyboard.code = GB_KEY_F5; break; case SDLK_F6: event.u.keyboard.code = GB_KEY_F6; break; case SDLK_F7: event.u.keyboard.code = GB_KEY_F7; break; case SDLK_F8: event.u.keyboard.code = GB_KEY_F8; break; case SDLK_F9: event.u.keyboard.code = GB_KEY_F9; break; case SDLK_F10: event.u.keyboard.code = GB_KEY_F10; break; case SDLK_F11: event.u.keyboard.code = GB_KEY_F11; break; case SDLK_F12: event.u.keyboard.code = GB_KEY_F12; break; case SDLK_LEFT: event.u.keyboard.code = GB_KEY_LEFT; break; case SDLK_UP: event.u.keyboard.code = GB_KEY_UP; break; case SDLK_RIGHT: event.u.keyboard.code = GB_KEY_RIGHT; break; case SDLK_DOWN: event.u.keyboard.code = GB_KEY_DOWN; break; case SDLK_HOME: event.u.keyboard.code = GB_KEY_HOME; break; case SDLK_END: event.u.keyboard.code = GB_KEY_END; break; case SDLK_INSERT: event.u.keyboard.code = GB_KEY_INSERT; break; case SDLK_PAGEUP: event.u.keyboard.code = GB_KEY_PAGEUP; break; case SDLK_PAGEDOWN: event.u.keyboard.code = GB_KEY_PAGEDOWN; break; case SDLK_HELP: event.u.keyboard.code = GB_KEY_HELP; break; case SDLK_PRINT: event.u.keyboard.code = GB_KEY_PRINT; break; case SDLK_SYSREQ: event.u.keyboard.code = GB_KEY_SYSREQ; break; case SDLK_BREAK: event.u.keyboard.code = GB_KEY_BREAK; break; case SDLK_MENU: event.u.keyboard.code = GB_KEY_MENU; break; case SDLK_POWER: event.u.keyboard.code = GB_KEY_POWER; break; case SDLK_EURO: event.u.keyboard.code = GB_KEY_EURO; break; case SDLK_UNDO: event.u.keyboard.code = GB_KEY_UNDO; break; case SDLK_NUMLOCK: event.u.keyboard.code = GB_KEY_NUMLOCK; break; case SDLK_CAPSLOCK: event.u.keyboard.code = GB_KEY_CAPSLOCK; break; case SDLK_SCROLLOCK: event.u.keyboard.code = GB_KEY_SCROLLLOCK; break; case SDLK_RSHIFT: event.u.keyboard.code = GB_KEY_RSHIFT; break; case SDLK_LSHIFT: event.u.keyboard.code = GB_KEY_LSHIFT; break; case SDLK_RCTRL: event.u.keyboard.code = GB_KEY_RCTRL; break; case SDLK_LCTRL: event.u.keyboard.code = GB_KEY_LCTRL; break; case SDLK_RALT: event.u.keyboard.code = GB_KEY_RALT; break; case SDLK_LALT: event.u.keyboard.code = GB_KEY_LALT; break; case 0x136: event.u.keyboard.code = GB_KEY_RCMD; break; case 0x135: event.u.keyboard.code = GB_KEY_LCMD; break; case SDLK_PAUSE: event.u.keyboard.code = GB_KEY_PAUSE; break; default : if (evet.key.keysym.sym < 256) { // the char code event.u.keyboard.code = evet.key.keysym.sym; } break; } // done event if (event.u.keyboard.code) gb_window_impl_event((gb_window_ref_t)impl, &event); } break; case SDL_VIDEORESIZE: { // trace tb_trace_d("resize: type: %d, %dx%d", evet.resize.type, evet.resize.w, evet.resize.h); // TODO // ... } break; case SDL_ACTIVEEVENT: { // trace tb_trace_d("active: type: %d, gain: %d, state: %d", evet.active.type, evet.active.gain, evet.active.state); // active? if (evet.active.state == SDL_APPACTIVE) { // init event gb_event_t event = {0}; event.type = GB_EVENT_TYPE_ACTIVE; event.u.active.code = evet.active.gain? GB_ACTIVE_FOREGROUND : GB_ACTIVE_BACKGROUND; // done event gb_window_impl_event((gb_window_ref_t)impl, &event); } } break; case SDL_QUIT: { // stop it stop = tb_true; } break; default: // trace tb_trace_e("unknown event: %x", evet.type); break; } } // compute the delta time time = tb_cache_time_spak() - time; // wait if (delay > (tb_size_t)time) SDL_Delay(delay - (tb_size_t)time); } // done exit if (impl->base.info.exit) impl->base.info.exit((gb_window_ref_t)impl, impl->canvas, impl->base.info.priv); }
/* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ tb_void_t tb_remove_if(tb_iterator_ref_t iterator, tb_iterator_comp_t comp, tb_cpointer_t priv) { // check tb_assert_and_check_return(iterator && comp); // the iterator mode tb_size_t mode = tb_iterator_mode(iterator); tb_assert_and_check_return((mode & TB_ITERATOR_MODE_FORWARD)); tb_assert_and_check_return(!(mode & TB_ITERATOR_MODE_READONLY)); // done tb_long_t ok = 1; tb_size_t size = 0; tb_bool_t stop = tb_false; tb_bool_t need = tb_false; tb_size_t prev = tb_iterator_tail(iterator); tb_size_t itor = tb_iterator_head(iterator); tb_size_t base = tb_iterator_tail(iterator); tb_bool_t bmutable = (mode & TB_ITERATOR_MODE_MUTABLE)? tb_true : tb_false; while (itor != tb_iterator_tail(iterator)) { // save next tb_size_t next = tb_iterator_next(iterator, itor); // done func if ((ok = comp(iterator, tb_iterator_item(iterator, itor), priv)) < 0) stop = tb_true; // remove it? if (!ok) { // is the first removed item? if (!need) { // save the removed range base base = prev; // need remove items need = tb_true; } // update size size++; } // the removed range have been passed or stop or end? if (ok || next == tb_iterator_tail(iterator)) { // need remove items? if (need) { // check tb_assert_abort(size); // the previous tail tb_size_t prev_tail = tb_iterator_tail(iterator); // remove items tb_iterator_remove_range(iterator, base, !ok? next : itor, size); // reset state need = tb_false; size = 0; // is the mutable iterator? if (bmutable) { // update itor prev = base; // the body items are removed? if (base != prev_tail) { // the next itor itor = tb_iterator_next(iterator, base); // the last item be not removed? skip the last walked item if (ok) { prev = itor; itor = tb_iterator_next(iterator, itor); } } // the head items are removed? else itor = tb_iterator_head(iterator); // stop? tb_check_break(!stop); // continue? continue ; } } // stop? tb_check_break(!stop); } // next prev = itor; itor = next; } }
tb_void_t tb_hash_map_dump(tb_hash_map_ref_t hash_map) { // check tb_hash_map_impl_t* impl = (tb_hash_map_impl_t*)hash_map; tb_assert_and_check_return(impl && impl->hash_list); // the step tb_size_t step = impl->element_name.size + impl->element_data.size; tb_assert_and_check_return(step); // trace tb_trace_i(""); tb_trace_i("hash_map: size: %lu", tb_hash_map_size(hash_map)); // done tb_size_t i = 0; tb_char_t name[4096]; tb_char_t data[4096]; for (i = 0; i < impl->hash_size; i++) { // the list tb_hash_map_item_list_t* list = impl->hash_list[i]; if (list) { // trace tb_trace_i("buck[%u]: size: %u, maxn: %u", i, list->size, list->maxn); // done tb_size_t j = 0; for (j = 0; j < list->size; j++) { // the item tb_byte_t const* item = ((tb_byte_t*)&list[1]) + j * step; // the item name tb_pointer_t element_name = impl->element_name.data(&impl->element_name, item); // the item data tb_pointer_t element_data = impl->element_data.data(&impl->element_data, item + impl->element_name.size); // trace if (impl->element_name.cstr && impl->element_data.cstr) { tb_trace_i(" %s => %s", impl->element_name.cstr(&impl->element_name, element_name, name, sizeof(name)), impl->element_data.cstr(&impl->element_data, element_data, data, sizeof(data))); } else if (impl->element_name.cstr) { tb_trace_i(" %s => %p", impl->element_name.cstr(&impl->element_name, element_name, name, sizeof(name)), element_data); } else if (impl->element_data.cstr) { tb_trace_i(" %x => %p", element_name, impl->element_data.cstr(&impl->element_data, element_data, data, sizeof(data))); } else { tb_trace_i(" %p => %p", element_name, element_data); } } } } }
static tb_void_t gb_device_skia_draw_polygon(gb_device_impl_t* device, gb_polygon_ref_t polygon, gb_shape_ref_t hint, gb_rect_ref_t bounds) { // check gb_skia_device_ref_t impl = (gb_skia_device_ref_t)device; tb_assert_and_check_return(impl && impl->canvas && impl->path && polygon); // the points gb_point_ref_t points = polygon->points; tb_uint16_t const* counts = polygon->counts; tb_assert_and_check_return(points && counts); // apply matrix gb_device_skia_apply_matrix(impl); // apply paint gb_device_skia_apply_paint(impl); // draw hint? if (hint && gb_device_skia_draw_hint(device, hint)) return ; // clear path impl->path->reset(); // init path gb_point_ref_t first = tb_null; gb_point_ref_t point = tb_null; tb_uint16_t count = *counts++; tb_size_t index = 0; while (index < count) { // the point point = points++; // first point? if (!index) { impl->path->moveTo(gb_float_to_sk(point->x), gb_float_to_sk(point->y)); first = point; } else impl->path->lineTo(gb_float_to_sk(point->x), gb_float_to_sk(point->y)); // next point index++; // next polygon if (index == count) { // close path if (first && first->x == point->x && first->y == point->y) impl->path->close(); // next count = *counts++; index = 0; } } // mark convex if (polygon->convex) impl->path->setIsConvex(true); // draw it impl->canvas->drawPath(*impl->path, *impl->paint); }
static tb_void_t tb_demo_spider_parser_task_done(tb_thread_pool_worker_ref_t worker, tb_cpointer_t priv) { // check tb_demo_spider_task_t* task = (tb_demo_spider_task_t*)priv; tb_assert_and_check_return(worker && task && task->spider); // init parser tb_demo_spider_parser_t* parser = tb_demo_spider_parser_init(worker); tb_assert_and_check_return(parser && parser->stream && parser->reader && parser->cache); // open stream if (tb_demo_spider_parser_open_html(parser->stream, task->ourl)) { // open reader if (tb_xml_reader_open(parser->reader, parser->stream, tb_false)) { // trace tb_trace_d("parser: open: %s", task->ourl); // init url tb_url_set(&parser->iurl, task->iurl); // parse url while ( TB_STATE_OK == tb_atomic_get(&task->spider->state) && tb_demo_spider_parser_get_url(parser->reader, &parser->iurl)) { // trace tb_trace_d("parser: done: %s", tb_url_get(&parser->iurl)); // done task tb_bool_t full = tb_false; if (!tb_demo_spider_task_done(task->spider, tb_url_get(&parser->iurl), &full)) { // full? tb_assert_and_check_break(full); // cache url if (!tb_circle_queue_full(parser->cache)) tb_circle_queue_put(parser->cache, tb_url_get(&parser->iurl)); // trace tb_trace_d("parser: cache: save: %s, size: %lu", tb_url_get(&parser->iurl), tb_circle_queue_size(parser->cache)); } } // clos reader tb_xml_reader_clos(parser->reader); } // clos stream tb_stream_clos(parser->stream); } // done task from the cache while (!tb_circle_queue_null(parser->cache)) { // the url tb_char_t const* url = (tb_char_t const*)tb_circle_queue_get(parser->cache); tb_assert_and_check_break(url); // done task if (!tb_demo_spider_task_done(task->spider, url, tb_null)) break; // trace tb_trace_d("parser: cache: load: %s, size: %lu", url, tb_circle_queue_size(parser->cache)); // pop it tb_circle_queue_pop(parser->cache); } }
// basic md5 step. the sp based on ip static tb_void_t tb_md5_transform(tb_uint32_t* sp, tb_uint32_t* ip) { // check tb_assert_and_check_return(sp && ip); // init tb_uint32_t a = sp[0], b = sp[1], c = sp[2], d = sp[3]; // round 1 TB_MD5_FF ( a, b, c, d, ip[ 0], TB_MD5_S11, (tb_uint32_t) 3614090360u); /* 1 */ TB_MD5_FF ( d, a, b, c, ip[ 1], TB_MD5_S12, (tb_uint32_t) 3905402710u); /* 2 */ TB_MD5_FF ( c, d, a, b, ip[ 2], TB_MD5_S13, (tb_uint32_t) 606105819u); /* 3 */ TB_MD5_FF ( b, c, d, a, ip[ 3], TB_MD5_S14, (tb_uint32_t) 3250441966u); /* 4 */ TB_MD5_FF ( a, b, c, d, ip[ 4], TB_MD5_S11, (tb_uint32_t) 4118548399u); /* 5 */ TB_MD5_FF ( d, a, b, c, ip[ 5], TB_MD5_S12, (tb_uint32_t) 1200080426u); /* 6 */ TB_MD5_FF ( c, d, a, b, ip[ 6], TB_MD5_S13, (tb_uint32_t) 2821735955u); /* 7 */ TB_MD5_FF ( b, c, d, a, ip[ 7], TB_MD5_S14, (tb_uint32_t) 4249261313u); /* 8 */ TB_MD5_FF ( a, b, c, d, ip[ 8], TB_MD5_S11, (tb_uint32_t) 1770035416u); /* 9 */ TB_MD5_FF ( d, a, b, c, ip[ 9], TB_MD5_S12, (tb_uint32_t) 2336552879u); /* 10 */ TB_MD5_FF ( c, d, a, b, ip[10], TB_MD5_S13, (tb_uint32_t) 4294925233u); /* 11 */ TB_MD5_FF ( b, c, d, a, ip[11], TB_MD5_S14, (tb_uint32_t) 2304563134u); /* 12 */ TB_MD5_FF ( a, b, c, d, ip[12], TB_MD5_S11, (tb_uint32_t) 1804603682u); /* 13 */ TB_MD5_FF ( d, a, b, c, ip[13], TB_MD5_S12, (tb_uint32_t) 4254626195u); /* 14 */ TB_MD5_FF ( c, d, a, b, ip[14], TB_MD5_S13, (tb_uint32_t) 2792965006u); /* 15 */ TB_MD5_FF ( b, c, d, a, ip[15], TB_MD5_S14, (tb_uint32_t) 1236535329u); /* 16 */ // round 2 TB_MD5_GG ( a, b, c, d, ip[ 1], TB_MD5_S21, (tb_uint32_t) 4129170786u); /* 17 */ TB_MD5_GG ( d, a, b, c, ip[ 6], TB_MD5_S22, (tb_uint32_t) 3225465664u); /* 18 */ TB_MD5_GG ( c, d, a, b, ip[11], TB_MD5_S23, (tb_uint32_t) 643717713u); /* 19 */ TB_MD5_GG ( b, c, d, a, ip[ 0], TB_MD5_S24, (tb_uint32_t) 3921069994u); /* 20 */ TB_MD5_GG ( a, b, c, d, ip[ 5], TB_MD5_S21, (tb_uint32_t) 3593408605u); /* 21 */ TB_MD5_GG ( d, a, b, c, ip[10], TB_MD5_S22, (tb_uint32_t) 38016083u); /* 22 */ TB_MD5_GG ( c, d, a, b, ip[15], TB_MD5_S23, (tb_uint32_t) 3634488961u); /* 23 */ TB_MD5_GG ( b, c, d, a, ip[ 4], TB_MD5_S24, (tb_uint32_t) 3889429448u); /* 24 */ TB_MD5_GG ( a, b, c, d, ip[ 9], TB_MD5_S21, (tb_uint32_t) 568446438u); /* 25 */ TB_MD5_GG ( d, a, b, c, ip[14], TB_MD5_S22, (tb_uint32_t) 3275163606u); /* 26 */ TB_MD5_GG ( c, d, a, b, ip[ 3], TB_MD5_S23, (tb_uint32_t) 4107603335u); /* 27 */ TB_MD5_GG ( b, c, d, a, ip[ 8], TB_MD5_S24, (tb_uint32_t) 1163531501u); /* 28 */ TB_MD5_GG ( a, b, c, d, ip[13], TB_MD5_S21, (tb_uint32_t) 2850285829u); /* 29 */ TB_MD5_GG ( d, a, b, c, ip[ 2], TB_MD5_S22, (tb_uint32_t) 4243563512u); /* 30 */ TB_MD5_GG ( c, d, a, b, ip[ 7], TB_MD5_S23, (tb_uint32_t) 1735328473u); /* 31 */ TB_MD5_GG ( b, c, d, a, ip[12], TB_MD5_S24, (tb_uint32_t) 2368359562u); /* 32 */ // round 3 TB_MD5_HH ( a, b, c, d, ip[ 5], TB_MD5_S31, (tb_uint32_t) 4294588738u); /* 33 */ TB_MD5_HH ( d, a, b, c, ip[ 8], TB_MD5_S32, (tb_uint32_t) 2272392833u); /* 34 */ TB_MD5_HH ( c, d, a, b, ip[11], TB_MD5_S33, (tb_uint32_t) 1839030562u); /* 35 */ TB_MD5_HH ( b, c, d, a, ip[14], TB_MD5_S34, (tb_uint32_t) 4259657740u); /* 36 */ TB_MD5_HH ( a, b, c, d, ip[ 1], TB_MD5_S31, (tb_uint32_t) 2763975236u); /* 37 */ TB_MD5_HH ( d, a, b, c, ip[ 4], TB_MD5_S32, (tb_uint32_t) 1272893353u); /* 38 */ TB_MD5_HH ( c, d, a, b, ip[ 7], TB_MD5_S33, (tb_uint32_t) 4139469664u); /* 39 */ TB_MD5_HH ( b, c, d, a, ip[10], TB_MD5_S34, (tb_uint32_t) 3200236656u); /* 40 */ TB_MD5_HH ( a, b, c, d, ip[13], TB_MD5_S31, (tb_uint32_t) 681279174u); /* 41 */ TB_MD5_HH ( d, a, b, c, ip[ 0], TB_MD5_S32, (tb_uint32_t) 3936430074u); /* 42 */ TB_MD5_HH ( c, d, a, b, ip[ 3], TB_MD5_S33, (tb_uint32_t) 3572445317u); /* 43 */ TB_MD5_HH ( b, c, d, a, ip[ 6], TB_MD5_S34, (tb_uint32_t) 76029189u); /* 44 */ TB_MD5_HH ( a, b, c, d, ip[ 9], TB_MD5_S31, (tb_uint32_t) 3654602809u); /* 45 */ TB_MD5_HH ( d, a, b, c, ip[12], TB_MD5_S32, (tb_uint32_t) 3873151461u); /* 46 */ TB_MD5_HH ( c, d, a, b, ip[15], TB_MD5_S33, (tb_uint32_t) 530742520u); /* 47 */ TB_MD5_HH ( b, c, d, a, ip[ 2], TB_MD5_S34, (tb_uint32_t) 3299628645u); /* 48 */ // round 4 TB_MD5_II ( a, b, c, d, ip[ 0], TB_MD5_S41, (tb_uint32_t) 4096336452u); /* 49 */ TB_MD5_II ( d, a, b, c, ip[ 7], TB_MD5_S42, (tb_uint32_t) 1126891415u); /* 50 */ TB_MD5_II ( c, d, a, b, ip[14], TB_MD5_S43, (tb_uint32_t) 2878612391u); /* 51 */ TB_MD5_II ( b, c, d, a, ip[ 5], TB_MD5_S44, (tb_uint32_t) 4237533241u); /* 52 */ TB_MD5_II ( a, b, c, d, ip[12], TB_MD5_S41, (tb_uint32_t) 1700485571u); /* 53 */ TB_MD5_II ( d, a, b, c, ip[ 3], TB_MD5_S42, (tb_uint32_t) 2399980690u); /* 54 */ TB_MD5_II ( c, d, a, b, ip[10], TB_MD5_S43, (tb_uint32_t) 4293915773u); /* 55 */ TB_MD5_II ( b, c, d, a, ip[ 1], TB_MD5_S44, (tb_uint32_t) 2240044497u); /* 56 */ TB_MD5_II ( a, b, c, d, ip[ 8], TB_MD5_S41, (tb_uint32_t) 1873313359u); /* 57 */ TB_MD5_II ( d, a, b, c, ip[15], TB_MD5_S42, (tb_uint32_t) 4264355552u); /* 58 */ TB_MD5_II ( c, d, a, b, ip[ 6], TB_MD5_S43, (tb_uint32_t) 2734768916u); /* 59 */ TB_MD5_II ( b, c, d, a, ip[13], TB_MD5_S44, (tb_uint32_t) 1309151649u); /* 60 */ TB_MD5_II ( a, b, c, d, ip[ 4], TB_MD5_S41, (tb_uint32_t) 4149444226u); /* 61 */ TB_MD5_II ( d, a, b, c, ip[11], TB_MD5_S42, (tb_uint32_t) 3174756917u); /* 62 */ TB_MD5_II ( c, d, a, b, ip[ 2], TB_MD5_S43, (tb_uint32_t) 718787259u); /* 63 */ TB_MD5_II ( b, c, d, a, ip[ 9], TB_MD5_S44, (tb_uint32_t) 3951481745u); /* 64 */ sp[0] += a; sp[1] += b; sp[2] += c; sp[3] += d; }
tb_void_t tb_option_help(tb_option_ref_t option) { // check tb_option_impl_t* impl = (tb_option_impl_t*)option; tb_assert_and_check_return(impl && impl->opts); // dump usage head tb_printf("======================================================================\n"); tb_printf("[usage]: %s", impl->name); // dump usage item tb_bool_t bopt = tb_false; tb_option_item_t const* item = impl->opts; while (item) { // dump options if (bopt && item->mode != TB_OPTION_MODE_KEY && item->mode != TB_OPTION_MODE_KEY_VAL) { tb_printf(" [options]"); bopt = tb_false; } // dump item switch (item->mode) { case TB_OPTION_MODE_KEY: case TB_OPTION_MODE_KEY_VAL: { bopt = tb_true; item++; } break; case TB_OPTION_MODE_VAL: { tb_printf(" %s", item->lname); item++; } break; case TB_OPTION_MODE_MORE: tb_printf(" ..."); case TB_OPTION_MODE_END: default: item = tb_null; break; } } // dump usage tail tb_printf("\n\n"); // dump help if (tb_string_size(&impl->help)) tb_printf("[help]: %s\n\n", tb_string_cstr(&impl->help)); // dump options head tb_printf("[options]: \n"); for (item = impl->opts; item; ) { // dump item tb_size_t spaces = 32; switch (item->mode) { case TB_OPTION_MODE_KEY: case TB_OPTION_MODE_KEY_VAL: { // dump spaces tb_printf(" "); spaces -= 3; // has short name? if (tb_isalpha(item->sname)) { // dump short name tb_printf("-%c", item->sname); spaces -= 2; // dump long name if (item->lname) { tb_printf(", --%s", item->lname); spaces -= 4; if (tb_strlen(item->lname) <= spaces) spaces -= tb_strlen(item->lname); } } // dump long name else if (item->lname) { tb_printf(" --%s", item->lname); spaces -= 6; if (tb_strlen(item->lname) <= spaces) spaces -= tb_strlen(item->lname); } // dump value if (item->mode == TB_OPTION_MODE_KEY_VAL) { switch (item->type) { case TB_OPTION_TYPE_BOOL: tb_printf("=BOOL"); spaces -= 5; break; case TB_OPTION_TYPE_CSTR: tb_printf("=STRING"); spaces -= 7; break; case TB_OPTION_TYPE_INTEGER: tb_printf("=INTEGER"); spaces -= 8; break; case TB_OPTION_TYPE_FLOAT: tb_printf("=FLOAT"); spaces -= 6; break; default: break; } } // dump help if (item->help) { tb_char_t line[8192] = {0}; tb_char_t const* pb = item->help; tb_char_t* qb = line; tb_char_t* qe = line + 8192; while (qb < qe) { if (*pb != '\n' && *pb) *qb++ = *pb++; else { // strip line and next *qb = '\0'; qb = line; // dump spaces while (spaces--) tb_printf(" "); // dump help line tb_printf("%s", line); // reset spaces spaces = 32; // next or end? if (*pb) { // dump new line tb_printf("\n"); pb++; } else break; } } } // dump newline tb_printf("\n"); // next item++; } break; case TB_OPTION_MODE_VAL: item++; break; case TB_OPTION_MODE_MORE: case TB_OPTION_MODE_END: default: item = tb_null; break; } } // dump options tail tb_printf("\n\n"); // dump values head tb_printf("[values]: \n"); for (item = impl->opts; item; ) { // dump item tb_size_t spaces = 32; switch (item->mode) { case TB_OPTION_MODE_KEY: case TB_OPTION_MODE_KEY_VAL: item++; break; case TB_OPTION_MODE_VAL: { // dump spaces tb_printf(" "); spaces -= 3; // dump long name if (item->lname) { tb_printf("%s", item->lname); if (tb_strlen(item->lname) <= spaces) spaces -= tb_strlen(item->lname); } // dump help if (item->help) { tb_char_t line[8192] = {0}; tb_char_t const* pb = item->help; tb_char_t* qb = line; tb_char_t* qe = line + 8192; while (qb < qe) { if (*pb != '\n' && *pb) *qb++ = *pb++; else { // strip line and next *qb = '\0'; qb = line; // dump spaces while (spaces--) tb_printf(" "); // dump help line tb_printf("%s", line); // reset spaces spaces = 32; // next or end? if (*pb) { // dump new line tb_printf("\n"); pb++; } else break; } } } // dump newline tb_printf("\n"); // next item++; } break; case TB_OPTION_MODE_MORE: tb_printf(" ...\n"); case TB_OPTION_MODE_END: default: item = tb_null; break; } } // dump values tail tb_printf("\n"); }
static tb_void_t tb_ifaddrs_interface_load4(tb_list_ref_t interfaces) { // check tb_assert_and_check_return(interfaces); // done PIP_ADAPTER_INFO adapter_info = tb_null; do { // make the adapter info adapter_info = tb_malloc0_type(IP_ADAPTER_INFO); tb_assert_and_check_break(adapter_info); // get the real adapter info size ULONG size = sizeof(IP_ADAPTER_INFO); if (tb_iphlpapi()->GetAdaptersInfo(adapter_info, &size) == ERROR_BUFFER_OVERFLOW) { // grow the adapter info buffer adapter_info = (PIP_ADAPTER_INFO)tb_ralloc(adapter_info, size); tb_assert_and_check_break(adapter_info); // reclear it tb_memset(adapter_info, 0, size); } // get the adapter info if (tb_iphlpapi()->GetAdaptersInfo(adapter_info, &size) != NO_ERROR) break; // done PIP_ADAPTER_INFO adapter = adapter_info; while (adapter) { // check tb_assert_abort(adapter->AdapterName); /* attempt to get the interface from the cached interfaces * and make a new interface if no the cached interface */ tb_ifaddrs_interface_t interface_new = {0}; tb_ifaddrs_interface_ref_t interface = tb_ifaddrs_interface_find((tb_iterator_ref_t)interfaces, adapter->AdapterName); if (!interface) interface = &interface_new; // check tb_assert_abort(interface == &interface_new || interface->name); // save flags if (adapter->Type == MIB_IF_TYPE_LOOPBACK) interface->flags |= TB_IFADDRS_INTERFACE_FLAG_IS_LOOPBACK; // save hwaddr if (adapter->AddressLength == sizeof(interface->hwaddr.u8)) { interface->flags |= TB_IFADDRS_INTERFACE_FLAG_HAVE_HWADDR; tb_memcpy(interface->hwaddr.u8, adapter->Address, sizeof(interface->hwaddr.u8)); } // save ipaddrs PIP_ADDR_STRING ipAddress = &adapter->IpAddressList; while (ipAddress && (interface->flags & TB_IFADDRS_INTERFACE_FLAG_HAVE_IPADDR) != TB_IFADDRS_INTERFACE_FLAG_HAVE_IPADDR) { // done tb_ipaddr_t ipaddr; if ( ipAddress->IpAddress.String && tb_ipaddr_ip_cstr_set(&ipaddr, ipAddress->IpAddress.String, TB_IPADDR_FAMILY_NONE)) { if (ipaddr.family == TB_IPADDR_FAMILY_IPV4) { interface->flags |= TB_IFADDRS_INTERFACE_FLAG_HAVE_IPADDR4; interface->ipaddr4 = ipaddr.u.ipv4; } else if (ipaddr.family == TB_IPADDR_FAMILY_IPV6) { interface->flags |= TB_IFADDRS_INTERFACE_FLAG_HAVE_IPADDR6; interface->ipaddr6 = ipaddr.u.ipv6; } } // the next ipAddress = ipAddress->Next; } // new interface? save it if ( interface == &interface_new && interface->flags) { // save interface name interface->name = tb_strdup(adapter->AdapterName); tb_assert_abort(interface->name); // save interface tb_list_insert_tail(interfaces, interface); } // the next adapter adapter = adapter->Next; } } while (0); // exit the adapter info if (adapter_info) tb_free(adapter_info); adapter_info = tb_null; }
tb_void_t g2_gl_draw_path(g2_gl_painter_t* painter, tb_size_t mode, g2_gl_path_t const* path) { // check tb_assert_and_check_return(painter && path); tb_assert_and_check_return((mode == G2_STYLE_MODE_FILL) || (mode == G2_STYLE_MODE_STOK)); // null? tb_check_return(!g2_path_null(path)); // make like g2_gl_path_make_like((g2_gl_path_t*)path); // make draw if (!g2_gl_path_make_fill((g2_gl_path_t*)path)) return ; // check tb_assert(path->fill.data && tb_vector_size(path->fill.data)); tb_assert(path->fill.size && tb_vector_size(path->fill.size)); tb_check_return(path->fill.rect.x1 < path->fill.rect.x2 && path->fill.rect.y1 < path->fill.rect.y2); // like rect? if (path->like == G2_GL_PATH_LIKE_RECT) { // draw bounds g2_gl_draw_bounds(painter, mode, &path->rect); // ok return ; } // like triangle? else if (path->like == G2_GL_PATH_LIKE_TRIG) { // draw triangle g2_gl_draw_triangle(painter, mode, &path->trig); // ok return ; } // init draw g2_gl_draw_t draw = {0}; if (!g2_gl_draw_init(&draw, painter, mode, path->like == G2_GL_PATH_LIKE_CONX? G2_GL_DRAW_FLAG_CONVEX : G2_GL_DRAW_FLAG_NONE)) return ; // init bounds g2_gl_rect_t bounds = path->fill.rect; if (draw.mode == G2_STYLE_MODE_STOK) g2_gl_bounds_stok(&bounds, 1); // clip draw g2_gl_draw_clip(&draw, &bounds); // init vertices if (draw.context->version < 0x20) g2_glVertexPointer(2, G2_GL_FLOAT, 0, tb_vector_data(path->fill.data)); else g2_glVertexAttribPointer(g2_gl_program_location(draw.program, G2_GL_PROGRAM_LOCATION_VERTICES), 2, G2_GL_FLOAT, G2_GL_FALSE, 0, tb_vector_data(path->fill.data)); // draw vertices tb_size_t head = 0; tb_size_t size = 0; tb_size_t itor = tb_iterator_head(path->fill.size); tb_size_t tail = tb_iterator_tail(path->fill.size); g2_GLenum_t gmode = (draw.mode == G2_STYLE_MODE_FILL)? G2_GL_TRIANGLE_FAN : G2_GL_LINE_STRIP; for (; itor != tail; itor++) { size = tb_iterator_item(path->fill.size, itor); g2_glDrawArrays(gmode, (g2_GLint_t)head, (g2_GLint_t)size); head += size; } // draw fill g2_gl_draw_fill(&draw, &bounds); // exit draw g2_gl_draw_exit(&draw); }