static int count_word(char *str, char c) { char *s; int count; s = get_begin(str, c); count = 0; while (*s != '\0') { s = get_end(s, c); s = get_begin(s, c); count++; } return (count); }
bool memcache::get(const char* key, size_t klen, string& out, unsigned short* flags) { out.clear(); int len = get_begin(key, klen, flags); if (len <= 0) return false; // 得需要保证足够的空间能容纳读取的数据,该种方式 // 可能会造成数据量非常大时的缓冲区溢出! char buf[4096]; int n; while (true) { n = get_data(buf, sizeof(buf)); if (n < 0) return false; else if (n == 0) break; out.append(buf, n); } return !out.empty(); }
static void stats_print_diff(struct stats_file *file) { struct stats_record *begin, *end; begin = get_begin(file); begin = get_next(file, begin); end = get_end(file); printf("\n(begin + 1)\n"); printf("\t[%04d] ", get_index(file, begin)); stats_print_record(begin); printf("end\n"); printf("\t[%04d] ", get_index(file, end)); stats_print_record(end); if (file->home_first && get_home(file)) { printf("\nhome\n"); stats_print_rec_diff(file->home_first, get_home(file)); } if (file->roaming_first && get_roaming(file)) { printf("\roaming\n"); stats_print_rec_diff(file->roaming_first, get_roaming(file)); } }
static void stats_hdr_info(struct stats_file *file) { struct stats_file_header *hdr; struct stats_record *begin, *end, *home, *roaming; unsigned int home_idx, roaming_idx; hdr = get_hdr(file); begin = get_begin(file); end = get_end(file); home = get_home(file); if (!home) home_idx = UINT_MAX; else home_idx = get_index(file, home); roaming = get_roaming(file); if (!roaming) roaming_idx = UINT_MAX; else roaming_idx = get_index(file, roaming); printf("Data Structure Sizes\n"); printf(" sizeof header %zd/0x%02zx\n", sizeof(struct stats_file_header), sizeof(struct stats_file_header)); printf(" sizeof entry %zd/0%02zx\n\n", sizeof(struct stats_record), sizeof(struct stats_record)); printf("File\n"); printf(" addr %p\n", file->addr); printf(" len %zd\n", file->len); printf(" max nr entries %d\n", file->max_nr); printf(" nr entries %d\n\n", file->nr); printf("Header\n"); printf(" magic 0x%08x\n", hdr->magic); printf(" begin [%d] 0x%08x\n", get_index(file, begin), hdr->begin); printf(" end [%d] 0x%08x\n", get_index(file, end), hdr->end); printf(" home [%d] 0x%08x\n", home_idx, hdr->home); printf(" roaming [%d] 0x%08x\n\n", roaming_idx, hdr->roaming); printf("Pointers\n"); printf(" hdr %p\n", hdr); printf(" begin %p\n", begin); printf(" end %p\n", end); printf(" home %p\n", home); printf(" romaing %p\n", roaming); printf(" first %p\n", file->first); printf(" last %p\n\n", file->last); }
void find_next_valid_item() { // if low_itr is at the end of current container, go to next container while (m_low_itr == get_end(*m_high_itr)) { ++m_high_itr; if (m_high_itr == m_high_end_itr) { // We reached the end! Set low_itr to invalid and return m_low_itr = LowLevelItrType(); return; } m_low_itr = get_begin(*m_high_itr); } }
static void update_nr_entries(struct stats_file *file) { struct stats_record *begin, *end; int nr; begin = get_begin(file); end = get_end(file); nr = get_index(file, end) - get_index(file, begin); if (nr < 0) nr += file->max_nr; file->nr = nr; }
int memcache::get_begin(const char* key, unsigned short* flags /* = NULL */) { return get_begin(key, strlen(key), flags); }
static struct stats_record *get_iterator_begin(struct stats_file *file) { return get_next(file, get_begin(file)); }
static void stats_convert(struct stats_file *file) { struct stats_file temp_file; int err; DBG("converting data file %s", file->name); stats_file_update_cache32(file); bzero(&temp_file, sizeof(struct stats_file)); err = stats_open_temp(&temp_file); if (err < 0) { connman_error("failed to open temporary file during data conversion"); return; } stats_file_setup(&temp_file); struct stats_iter32 data_iter; struct stats_record32 *record; data_iter.file = file; data_iter.begin = get_iterator_begin32(data_iter.file); data_iter.end = get_iterator_end32(data_iter.file); data_iter.it = data_iter.begin; record = get_next_record32(&data_iter); while (record) { struct stats_record *next; if (temp_file.last == get_end(&temp_file)) { err = stats_file_remap(&temp_file, temp_file.len + sysconf(_SC_PAGESIZE)); if (err < 0) { connman_error("failed to extend file %s", temp_file.name); unlink(temp_file.name); stats_file_unmap(&temp_file); TFR(close(temp_file.fd)); stats_file_cleanup(&temp_file); return; } stats_file_update_cache(&temp_file); } next = get_next(&temp_file, get_end(&temp_file)); if (next == get_begin(&temp_file)) { connman_error("ring buffer is full"); unlink(temp_file.name); stats_file_unmap(&temp_file); TFR(close(temp_file.fd)); stats_file_cleanup(&temp_file); return; } next->ts = record->ts; next->roaming = record->roaming; next->data.rx_packets = record->data.rx_packets; next->data.tx_packets = record->data.tx_packets; next->data.rx_bytes = record->data.rx_bytes; next->data.tx_bytes = record->data.tx_bytes; next->data.rx_errors = record->data.rx_errors; next->data.tx_errors = record->data.tx_errors; next->data.rx_dropped = record->data.rx_dropped; next->data.tx_dropped = record->data.tx_dropped; next->data.time = record->data.time; if (next->roaming) set_roaming(&temp_file, next); else set_home(&temp_file, next); set_end(&temp_file, next); record = get_next_record32(&data_iter); } // close and swap stats_file_unmap(file); TFR(close(file->fd)); err = rename(temp_file.name, file->name); if (err < 0) connman_error("failed to rename converted data file %s to %s", temp_file.name, file->name); g_free(temp_file.name); temp_file.name = file->name; memcpy(file, &temp_file, sizeof(struct stats_file)); }