Example #1
0
void menu_destroy(menu* pMenu) {
	char* entry;
	for (int i = 0; i < pMenu->m_aEntries->m_len; i++) {
		table_get(pMenu->m_aEntries, i, &entry);
		free(entry);
		table_get(pMenu->m_aValues, i, &entry);
		free(entry);
	}
	table_free(pMenu->m_aEntries);
	table_free(pMenu->m_aValues);
	free(pMenu);
}
Example #2
0
File: app_pd.c Project: ArkShen/xcb
static int unload_module(void) {
	msgs_unhook(&default_msgs, pd_exec);
	dlist_free(&pairs);
	table_free(&contracts);
	config_destroy(cfg);
	return unregister_application(app);
}
Example #3
0
/*
 * Return code:
 *	0  - End Of Table
 * 	-1 - Error
 *	-2 - Last change changed - again
 *	+1 - ok, continue
 */
static int
table_check_response(struct tabwork *work, const struct snmp_pdu *resp)
{
	const struct snmp_value *b;
	struct entry *e;

	if (resp->error_status != SNMP_ERR_NOERROR) {
		if (snmp_client.version == SNMP_V1 &&
		    resp->error_status == SNMP_ERR_NOSUCHNAME &&
		    resp->error_index ==
		    (work->descr->last_change.len == 0) ? 1 : 2)
			/* EOT */
			return (0);
		/* Error */
		seterr(&snmp_client, "error fetching table: status=%d index=%d",
		    resp->error_status, resp->error_index);
		return (-1);
	}

	for (b = resp->bindings; b < resp->bindings + resp->nbindings; b++) {
		if (work->descr->last_change.len != 0 && b == resp->bindings) {
			if (!asn_is_suboid(&work->descr->last_change, &b->var) ||
			    b->var.len != work->descr->last_change.len + 1 ||
			    b->var.subs[work->descr->last_change.len] != 0) {
				seterr(&snmp_client,
				    "last_change: bad response");
				return (-1);
			}
			if (b->syntax != SNMP_SYNTAX_TIMETICKS) {
				seterr(&snmp_client,
				    "last_change: bad syntax %u", b->syntax);
				return (-1);
			}
			if (work->first) {
				work->last_change = b->v.uint32;
				work->first = 0;

			} else if (work->last_change != b->v.uint32) {
				if (++work->iter >= work->descr->max_iter) {
					seterr(&snmp_client,
					    "max iteration count exceeded");
					return (-1);
				}
				table_free(work, 1);
				return (-2);
			}
					
			continue;
		}
		if (!asn_is_suboid(&work->descr->table, &b->var) ||
		    b->syntax == SNMP_SYNTAX_ENDOFMIBVIEW)
			return (0);

		if ((e = table_find(work, &b->var)) == NULL)
			return (-1);
		if (table_value(work->descr, e, b))
			return (-1);
	}
	return (+1);
}
Example #4
0
int main(void) {

    Table *table = table_create(compareString);
    //table_setKeyMemHandler(table, free);
    //table_setValueMemHandler(table,free);
    //char *key1 = "key1";
    //char *key2 = "key2";
    //char *value1 = "value1";
    //char *value2 = "value2";

    if(table_isEmpty(table)){
        printf("The table is empty\n");
    } else {
        printf("The table is not empty\n");
    }

    //printf("key 101 has value %d\n", *((int*)table_lookup(table, intPtrFromInt(101))));
    //table_insert(table, key1, value1);
    //table_insert(table, key2, value2);
    //table_remove(table, key1);

    //if (table_isEmpty(table)) {
    //    printf("The table is empty\n");
    //} else {
    //    printf("The table is not empty\n");
    //}

    //printf("key 101 has value %s\n", ((char *) table_lookup(table, key2)));
    table_free(table);
    //free(table);

    return 0;
}
Example #5
0
static void db_free(struct DB *const db) {
    free(db->persist_dir);
    if (db->active_table[0]) {
        table_free(db->active_table[0]);
    }
    if (db->active_table[1]) {
        table_free(db->active_table[1]);
    }
    vc_recursive_free(db->vcroot);
    fclose(db->log);
    for (int i = 0; db->cms_dump[i]; i++) {
        containermap_destroy(db->cms_dump[i]);
    }
    free(db);
    return;
}
Example #6
0
int main (int argc, char *argv[]) {
  if (argc != 2) {
    usage();
    exit(1);
  }

  char *infile  = argv[1];

  TreeNode *tree = huffman_build_tree(infile);
  if (tree == NULL) {
    printf("Could not build the tree!");
    usage();
    exit(1);
  }

  EncodeTable *etab = table_build(tree);
  if (etab == NULL) {
    printf("Could not build the table!");
    usage();
    exit(1);
  }  

  table_print(etab);

  table_free(etab);
  tree_free(tree);

  return 0;
}
Example #7
0
/*
 *  deallocates the stroage for the configuration data
 */
void (conf_free)(void)
{
    table_map(section, tabfree, section);
    table_free(&section);
    current = NULL;
    preset = 0;
    errcode = CONF_ERR_OK;
    control = 0;
}
Example #8
0
static int unload_module(void) {
	int res;

	if (check_msgs(impvbaw_msgs) == -1)
		return MODULE_LOAD_FAILURE;
	stop_msgs(impvbaw_msgs);
	msgs_unhook(&default_msgs, impvbaw_exec);
	msgs_unhook(impvbaw_msgs, mm_impvbaw_exec);
	msgs_free(impvbaw_msgs);
	config_destroy(cfg);
	table_free(&contracts);
	table_free(&expiries);
	table_free(&optns);
	table_free(&spots);
	res  = unregister_application(app);
	res |= unregister_application(app2);
	return res;
}
Example #9
0
/* =============================================================================
 * sequencer_free
 * =============================================================================
 */
void
sequencer_free (sequencer_t* sequencerPtr)
{
    long i;

    table_free(sequencerPtr->hashToConstructEntryTable);
    free(sequencerPtr->constructEntries);
    for (i = 1; i < sequencerPtr->segmentLength; i++) {
        table_free(sequencerPtr->startHashToConstructEntryTables[i]);
    }
    free(sequencerPtr->startHashToConstructEntryTables);
    free(sequencerPtr->endInfoEntries);

    hashtable_free(sequencerPtr->uniqueSegmentsPtr);
    if (sequencerPtr->sequence != NULL) {
        free(sequencerPtr->sequence);
    }
    free(sequencerPtr);
}
Example #10
0
static int unload_module(void) {
	if (check_msgs(vxo_msgs) == -1)
		return MODULE_LOAD_FAILURE;
	msgs_unhook_name(inmsg, vxo_exec);
	stop_msgs(vxo_msgs);
	msgs_free(vxo_msgs);
	config_destroy(cfg);
	table_free(&spots);
	return unregister_application(app);
}
Example #11
0
void table_stack_destroy (table_stack_t *stack)
{
	table_t *tmp1, *tmp2;
	assert(stack != NULL);
	tmp1 = stack->tables;
	while (tmp1 != NULL)
	{
		tmp2 = tmp1;
		tmp1 = tmp1->next;
		table_free(tmp2);
	}
}
Example #12
0
/* =============================================================================
 * sequencer_free
 * =============================================================================
 */
void
sequencer_free (sequencer_t* sequencerPtr)
{
    long i;

    table_free(sequencerPtr->hashToConstructEntryTable);
    free(sequencerPtr->constructEntries);
    for (i = 1; i < sequencerPtr->segmentLength; i++) {
        table_free(sequencerPtr->startHashToConstructEntryTables[i]);
    }
    free(sequencerPtr->startHashToConstructEntryTables);
    free(sequencerPtr->endInfoEntries);
#if 0
    /* TODO: fix mixed sequential/parallel allocation */
    hashtable_free(sequencerPtr->uniqueSegmentsPtr);
    if (sequencerPtr->sequence != NULL) {
        free(sequencerPtr->sequence);
    }
#endif
    free(sequencerPtr);
}
Example #13
0
static void free_field(SField *field)
{
    if (! field)
        return;
    if (field->name)
        free(field->name);
    switch (field->type)
    {
        case TYPE_STRING: free(field->value.str_value); break;
        case TYPE_TABLE: table_free(field->value.table_value); break;
    }
    free(field);
}
Example #14
0
void free_crack_4test(ophcrack_t *crack) {
  for (list_nd_t *nd = crack->hashes->head; nd != NULL; nd = nd->next) {
    hash_t *hash = nd->data;
    hash_free(hash);
  }

  for (list_nd_t *nd = crack->tables->head; nd != NULL; nd = nd->next) {
    table_t *tbl = nd->data;
    table_free(tbl);
  }

  arg_free(crack->arg);
  ophcrack_free(crack);
}
Example #15
0
/*
 *  provides a callback for conf_free()
 */
static void tabfree(const void *key, void **value, void *cl)
{
    UNUSED(key);

    if (cl == section) {    /* for section table */
        table_t *t = *value;
        table_map(t, tabfree, *value);
        table_free(&t);
    } else {    /* for var=val tables */
        struct valnode_t *pnode = *value;
        assert(pnode);
        MEM_FREE(pnode->freep);
        MEM_FREE(pnode);
    }
}
Example #16
0
static void compaction_free_old(struct Compaction *const comp) {
    // free n
    for (uint64_t i = 0; i < comp->nr_feed; i++) {
        containermap_release(comp->db->cms[comp->start_bit / 3],
                             comp->mts_old[i]->mfh.off);
        metatable_free(comp->mts_old[i]);
        db_destory_metatable(comp->db, comp->mtids_old[i]);
    }

    // free n+1
    for (uint64_t i = 0; i < 8; i++) {
        if (comp->mbcs_old[i]) {
            containermap_release(comp->db->cm_bc, comp->mbcs_old[i]->off_raw);
            bloomcontainer_free(comp->mbcs_old[i]);
        }
        if (!comp->gen_bc) {  // keep bloomtable
            comp->tables[i]->bt = NULL;
        }
        table_free(comp->tables[i]);
    }
}
Example #17
0
HTable table_read(const char *filename)
{
    long int len, pos;
    char *buf;
    HTable table;
    int res;

    buf = read_file(filename, &len);
    if (! buf) return NULL;

    pos = 0;
    table = table_create();
    res = parse_table(table, buf, &pos, 1);
    if (res)
    {
        table_free(table);
        table = NULL;
    }

    free(buf);
    return table;
}
Example #18
0
int
main ()
{
    table_t* tablePtr;
    long hash[] = {3, 1, 4, 1, 5, 9, 2, 6, 8, 7, -1};
    long i;

    bool_t status = memory_init(1, 4, 2);
    assert(status);

    puts("Starting...");

    tablePtr = table_alloc(8, NULL);

    for (i = 0; hash[i] >= 0; i++ ) {
        bool_t status = table_insert(tablePtr,
                                     (ulong_t)hash[i],
                                     (void*)&hash[i])
        assert(status);
        printTable(tablePtr);
        puts("");
    }

    for (i = 0; hash[i] >= 0; i++ ) {
        bool_t status = table_remove(tablePtr,
                                     (ulong_t)hash[i],
                                     (void*)&hash[i])
        assert(status);
        printTable(tablePtr);
        puts("");
    }

    table_free(tablePtr);

    puts("Done.");

    return 0;
}
Example #19
0
static int add_field(HTable table, char *name, char *value, char *buf, long int *pos)
{
    int value_type;
    char *s;
    HTable tbl;

    value_type = get_value_type(value);
    switch (value_type)
    {
        case TYPE_INT:
            table_set_int(table, name, atoi(value));
            break;
        case TYPE_STRING:
            s = unescape_string(value);
            if (! s)
                return -1;
            table_set_str(table, name, s);
            free(s);
            break;
        case TYPE_FLOAT:
            table_set_double(table, name, atof(value));
            break;
        case TYPE_TABLE:
            tbl = table_create();
            if (table_set_table(table, name, tbl))
            {
                table_free(tbl);
                return -1;
            }
            if (parse_table(tbl, buf, pos, 0))
                return -1;
            break;
        default:
            return -1;
    }
    return 0;
}
Example #20
0
void wf(const char *name, FILE *fp) 
{
    table_t table = table_new(0, NULL, NULL);
    char buf[BUFSIZ];

    while (getword(fp, buf, sizeof(buf), first, rest)) {
        const char *word;
        int i, *count;
        for (i=0; buf[i] != '\0'; i++)
            buf[i] = tolower(buf[i]);
        word = atom_string(buf);
        count = table_get(table, word);
        if (count) {
            (*count) ++;
        } else {
            count = (int *)zalloc(sizeof (*count));
            *count = 1;
            table_put(table, word, count);
        }
    }

    if (name) {
        printf("%s:\n", name);
    }
    /* print the words */
    int i;
    void **array = table_to_array(table, NULL);
    qsort(array, table_length(table), 2*sizeof(*array), cmp);
    for (i = 0; array[i]; i+=2) {
        printf("%d\t%s\n", *(int *)array[i+1], (char *)array[i]);
    }
    zfree(array);

    /* destroy the table */
    table_free(&table, vfree);
}
Example #21
0
/*
 *  This function print the results of the command "btrfs fi usage"
 *  in tabular format
 */
static void _cmd_filesystem_usage_tabular(unsigned unit_mode,
					struct btrfs_ioctl_space_args *sargs,
					struct chunk_info *chunks_info_ptr,
					int chunks_info_count,
					struct device_info *device_info_ptr,
					int device_info_count)
{
	int i;
	u64 total_unused = 0;
	struct string_table *matrix = 0;
	int  ncols, nrows;

	ncols = sargs->total_spaces + 2;
	nrows = 2 + 1 + device_info_count + 1 + 2;

	matrix = table_create(ncols, nrows);
	if (!matrix) {
		fprintf(stderr, "ERROR: not enough memory\n");
		return;
	}

	/* header */
	for (i = 0; i < sargs->total_spaces; i++) {
		const char *description;
		u64 flags = sargs->spaces[i].flags;

		if (flags & BTRFS_SPACE_INFO_GLOBAL_RSV)
			continue;

		description = btrfs_group_type_str(flags);

		table_printf(matrix, 1+i, 0, "<%s", description);
	}

	for (i = 0; i < sargs->total_spaces; i++) {
		const char *r_mode;

		u64 flags = sargs->spaces[i].flags;
		r_mode = btrfs_group_profile_str(flags);

		table_printf(matrix, 1+i, 1, "<%s", r_mode);
	}

	table_printf(matrix, 1+sargs->total_spaces, 1, "<Unallocated");

	/* body */
	for (i = 0; i < device_info_count; i++) {
		int k, col;
		char *p;

		u64  total_allocated = 0, unused;

		p = strrchr(device_info_ptr[i].path, '/');
		if (!p)
			p = device_info_ptr[i].path;
		else
			p++;

		table_printf(matrix, 0, i + 3, "<%s", device_info_ptr[i].path);

		for (col = 1, k = 0 ; k < sargs->total_spaces ; k++)  {
			u64	flags = sargs->spaces[k].flags;
			u64 devid = device_info_ptr[i].devid;
			int	j;
			u64 size = 0;

			for (j = 0 ; j < chunks_info_count ; j++) {
				if (chunks_info_ptr[j].type != flags )
						continue;
				if (chunks_info_ptr[j].devid != devid)
						continue;

				size += calc_chunk_size(chunks_info_ptr+j);
			}

			if (size)
				table_printf(matrix, col, i+3,
					">%s", pretty_size_mode(size, unit_mode));
			else
				table_printf(matrix, col, i+3, ">-");

			total_allocated += size;
			col++;
		}

		unused = get_partition_size(device_info_ptr[i].path)
				- total_allocated;

		table_printf(matrix, sargs->total_spaces + 1, i + 3,
			       ">%s", pretty_size_mode(unused, unit_mode));
		total_unused += unused;

	}

	for (i = 0; i <= sargs->total_spaces; i++)
		table_printf(matrix, i + 1, device_info_count + 3, "=");

	/* footer */
	table_printf(matrix, 0, device_info_count + 4, "<Total");
	for (i = 0; i < sargs->total_spaces; i++)
		table_printf(matrix, 1 + i, device_info_count + 4, ">%s",
			pretty_size_mode(sargs->spaces[i].total_bytes, unit_mode));

	table_printf(matrix, sargs->total_spaces + 1, device_info_count + 4,
			">%s", pretty_size_mode(total_unused, unit_mode));

	table_printf(matrix, 0, device_info_count + 5, "<Used");
	for (i = 0; i < sargs->total_spaces; i++)
		table_printf(matrix, 1 + i, device_info_count+5, ">%s",
			pretty_size_mode(sargs->spaces[i].used_bytes, unit_mode));

	table_dump(matrix);
	table_free(matrix);
}
Example #22
0
/*
 * Fetch a table. Returns 0 if ok, -1 on errors.
 * This is the synchronous variant.
 */
int
snmp_table_fetch(const struct snmp_table *descr, void *list)
{
	struct snmp_pdu resp;
	struct tabwork work;
	int ret;

	work.descr = descr;
	work.table = (struct table *)list;
	work.iter = 0;
	TAILQ_INIT(work.table);
	TAILQ_INIT(&work.worklist);
	work.callback = NULL;
	work.arg = NULL;

  again:
	/*
	 * We come to this label when the code detects that the table
	 * has changed while fetching it.
	 */
	work.first = 1;
	work.last_change = 0;
	table_init_pdu(descr, &work.pdu);

	for (;;) {
		if (snmp_dialog(&work.pdu, &resp)) {
			table_free(&work, 1);
			return (-1);
		}
		if ((ret = table_check_response(&work, &resp)) == 0) {
			snmp_pdu_free(&resp);
			break;
		}
		if (ret == -1) {
			snmp_pdu_free(&resp);
			table_free(&work, 1);
			return (-1);
		}
		if (ret == -2) {
			snmp_pdu_free(&resp);
			goto again;
		}

		work.pdu.bindings[work.pdu.nbindings - 1].var =
		    resp.bindings[resp.nbindings - 1].var;

		snmp_pdu_free(&resp);
	}

	if ((ret = table_check_cons(&work)) == -1) {
		table_free(&work, 1);
		return (-1);
	}
	if (ret == -2) {
		table_free(&work, 1);
		goto again;
	}
	/*
	 * Free index list
	 */
	table_free(&work, 0);
	return (0);
}
Example #23
0
// pthread
static void *thread_active_dumper(void *ptr) {
    struct DB *const db = (typeof(db))ptr;

    conc_set_affinity_n(2);
    while (db->active_table[0]) {
        // active
        pthread_mutex_lock(&(db->mutex_active));
        if ((db->active_table[0]->volume == 0) && db->closing) {
            pthread_mutex_unlock(&(db->mutex_active));
            table_free(db->active_table[0]);
            db->active_table[0] = NULL;
            break;
        }
        while ((!table_full(db->active_table[0])) && (!db->closing)) {
            pthread_cond_wait(&(db->cond_active), &(db->mutex_active));
        }
        // shift active table
        const uint64_t ticket1 = rwlock_writer_lock(&(db->rwlock));
        db->active_table[1] = db->active_table[0];
        if (db->closing) {
            db->active_table[0] = NULL;
        } else {
            db->active_table[0] = table_alloc_default(15.0);
        }
        rwlock_writer_unlock(&(db->rwlock), ticket1);
        // notify writers
        pthread_cond_broadcast(&(db->cond_writer));
        pthread_mutex_unlock(&(db->mutex_active));

        struct Table *const table1 = db->active_table[1];
        if (containermap_unused(db->cms[0]) < 8u) {
            db_log(db,
                   "ContainerMap is near full, dropping current active-table");
            sleep(10);
            const uint64_t ticket2 = rwlock_writer_lock(&(db->rwlock));
            db->active_table[1] = NULL;
            rwlock_writer_unlock(&(db->rwlock), ticket2);
        } else if (table1->volume > 0) {
            const bool rbt = table_build_bloomtable(table1);
            assert(rbt);

            const uint64_t mtid = db_table_dump(db, table1, 0);
            struct MetaTable *const mt =
                db_load_metatable(db, mtid, db->cms[0]->raw_fd, false);
            assert(mt);
            stat_inc_n(&(db->stat.nr_write[0]), TABLE_NR_BARRELS);
            mt->bt = table1->bt;
            // mark !active_table[1]->bt before free it

            // wait for room
            pthread_mutex_lock(&(db->mutex_current));
            while (db->vcroot->cc.count == DB_CONTAINER_NR) {
                pthread_cond_wait(&(db->cond_root_producer),
                                  &(db->mutex_current));
            }
            pthread_mutex_unlock(&(db->mutex_current));

            // insert
            const uint64_t ticket2 = rwlock_writer_lock(&(db->rwlock));
            const bool ri = vc_insert_internal(db->vcroot, mt, NULL);
            assert(ri);
            stat_inc(&(db->stat.nr_active_dumped));
            // alert compaction thread if have work to be done
            if (db->vcroot->cc.count >= 8) {
                pthread_mutex_lock(&(db->mutex_current));
                pthread_cond_broadcast(&(db->cond_root_consumer));
                pthread_mutex_unlock(&(db->mutex_current));
            }
            db->active_table[1] = NULL;
            rwlock_writer_unlock(&(db->rwlock), ticket2);

            // post process
            table1->bt = NULL;
        }
        table_free(table1);
    }
    pthread_exit(NULL);
    return NULL;
}
Example #24
0
/* Convert table into a grided form, taking into account symmetries
 *
 * table must have at least 3 columns
 * n number of points on circle for ecliptic longitude
 * m number of points on circle for hour angle
 */
int grid_transform(Table *table, size_t n, size_t m, const char *sym)
{
	Table tab; // working table for grided data
	size_t i;
	size_t j;
	size_t r;
	size_t nr = n + 1; // extra rows for hour angles
	size_t nc = m + 1; // extra cols for eclip longs
	double el_step = 360./n; // ecliptic longitude grid step
	double ha_step = 360./m; // hour angle grid step

	int east_only = !strcmp(sym, "E");

	if (table_init(&tab, nr, nc))
		return -1;

	// Setting values for grid axes
	tab.v[0][0] = 0.; // top left point not used
	for (i=1; i<nr; i++)
		tab.v[i][0] = -180.0 + (i-1)*el_step;
	for (j=1; j<nc; j++)
		tab.v[0][j] = -180.0 +(j-1)*ha_step;

	// Some positions don't get written to below.  Initialising the table so
	// that these values end up as zero.
	for (i=1; i<nr; i++)
		for (j=1; j<nc; j++)
			tab.v[i][j] = 0.;
	
	for (r=0; r<table->nr; r++) {
		double el = table->v[r][0];
		double ha = table->v[r][1];
		double val = table->v[r][2];
		size_t i = (size_t) roundf((180. + el)/el_step) % n;
		size_t j = (size_t) roundf((180. + ha)/ha_step) % m;

		// direct
		tab.v[i+1][j+1] = val; // direct

		// spring-autumn
		size_t i_sa = (3*n/2 - i) % n;
		tab.v[i_sa+1][j+1] = val;

		// day-night
		size_t i_dn = (n - i) % n;
		size_t j_dn = (3*m/2 - j) % m;
		tab.v[i_dn+1][j_dn+1] = -val;
		size_t i_sadn = (n - i_sa) % n;
		tab.v[i_sadn+1][j_dn+1] = -val;

		// east-west
		if (east_only) {
			size_t j_ew = (m - j) % m;
			tab.v[i+1][j_ew+1] = val;
			tab.v[i_sa+1][j_ew+1] = val;
			size_t j_ewdn = (3*m/2 - j_ew) % m;
			tab.v[i_dn+1][j_ewdn+1] = -val;
			tab.v[i_sadn+1][j_ewdn+1] = -val;
		}
	}

	table_free(table);
	*table = tab;
	return 0;
}
Example #25
0
int main() {
    table_t tab = table_new(1024, cmp, dictGenHashFunction);

    table_put(tab, "append", "cmd_append");
    table_put(tab, "bitcount", "cmd_bitcount");
    table_put(tab, "brpop", "cmd_brpop");
    table_put(tab, "brpoplpush", "cmd_brpoplpush");
    table_put(tab, "decr", "cmd_decr");
    table_put(tab, "decrby", "cmd_decrby");
    table_put(tab, "del", "cmd_del");
    table_put(tab, "exists", "cmd_exists");
    table_put(tab, "get", "cmd_get");
    table_put(tab, "getbit", "cmd_getbit");
    table_put(tab, "getrange", "cmd_getrange");
    table_put(tab, "incr", "cmd_incr");
    table_put(tab, "incrby", "cmd_incrby");
    table_put(tab, "keys", "cmd_keys");
    table_put(tab, "lindex", "cmd_lindex");
    table_put(tab, "linsert", "cmd_linsert");
    table_put(tab, "llen", "cmd_llen");
    table_put(tab, "lpop", "cmd_lpop");
    table_put(tab, "lpush", "cmd_lpush");
    table_put(tab, "lpushx", "cmd_lpushx");
    table_put(tab, "lrange", "cmd_lrange");
    table_put(tab, "lrem", "cmd_lrem");
    table_put(tab, "lset", "cmd_lset");
    table_put(tab, "ltrim", "cmd_ltrim");
    table_put(tab, "mget", "cmd_mget");
    table_put(tab, "msetnx", "cmd_msetnx");
    table_put(tab, "randomkey", "cmd_randomkey");
    table_put(tab, "rename", "cmd_rename");
    table_put(tab, "rpop", "cmd_rpop");
    table_put(tab, "rpoplpush", "cmd_rpoplpush");
    table_put(tab, "rpush", "cmd_rpush");
    table_put(tab, "rpushx", "cmd_rpushx");
    table_put(tab, "set", "cmd_set");
    table_put(tab, "setbit", "cmd_setbit");
    table_put(tab, "setrange", "cmd_setrange");
    table_put(tab, "strlen", "cmd_strlen");
    table_put(tab, "type", "cmd_type");

    table_map(tab, apply);
    printf("len = %d\n", table_length(tab));

    char key[32];
    scanf("%s", key);
    printf("%s\n", (char *)table_get(tab, key));

    printf("remove set\n");
    table_remove(tab, "set");

    table_map(tab, apply);
    printf("len = %d\n", table_length(tab));

    scanf("%s", key);
    printf("%s\n", (char *)table_get(tab, key));

    table_free(&tab);

    return 0;
}
Example #26
0
/*
 * perform some basic tests
 */
static	void	io_test(table_t *tab_p)
{
  int		ret, bucket_n, entry_n;
  table_t	*tab2_p;
  
  (void)printf("Performing I/O tests:\n");
  (void)fflush(stdout);
  
#if 0
  {
    long	key, data;
    (void)table_clear(tab_p);
    key = 1;
    data = 2;
    (void)table_insert(tab_p, &key, sizeof(key), &data, sizeof(data), NULL, 0);
    key = 3;
    data = 4;
    (void)table_insert(tab_p, &key, sizeof(key), &data, sizeof(data), NULL, 0);
    key = 5;
    data = 6;
    (void)table_insert(tab_p, &key, sizeof(key), &data, sizeof(data), NULL, 0);
    (void)table_adjust(tab_p, 0);
    dump_table(tab_p);
  }
#endif
  
  ret = table_info(tab_p, &bucket_n, &entry_n);
  if (ret != TABLE_ERROR_NONE) {
    (void)fprintf(stderr, "could not get info of table: %s\n",
		  table_strerror(ret));
    exit(1);
  }
  (void)printf("Table we are writing has %d buckets and %d entries\n",
	       bucket_n, entry_n);
  
  /*
   * dump the table to disk
   */
  int pmode = 0640;
#ifdef win32
  pmode = _S_IREAD | _S_IWRITE;
#endif
  (void)unlink(TABLE_FILE);
  ret = table_write(tab_p, TABLE_FILE, pmode);
  if (ret != TABLE_ERROR_NONE) {
    (void)fprintf(stderr, "could not write table to '%s': %s\n",
		  TABLE_FILE, table_strerror(ret));
    exit(1);
  }
  
#if 0
  dump_table(tab_p);
#endif
  
  /*
   * now read back in the table
   */
  tab2_p = table_read(TABLE_FILE, &ret);
  if (tab2_p == NULL) {
    (void)fprintf(stderr, "could not read in file '%s': %s\n",
		  TABLE_FILE, table_strerror(ret));
    exit(1);
  }
  
  (void)printf("Testing table-read...\n");
  if (test_eq(tab_p, tab2_p, 0)) {
    (void)printf("  equal.\n");
  }
  else {
    (void)printf("  NOT equal.\n");
  }
  
  ret = table_free(tab2_p);
  if (ret != TABLE_ERROR_NONE) {
    (void)fprintf(stderr, "could not free read table: %s\n",
		  table_strerror(ret));
    exit(1);
  }
  
  /*
   * mmap in the table
   */
  tab2_p = table_mmap(TABLE_FILE, &ret);
  if (tab2_p == NULL) {
    (void)fprintf(stderr, "could not mmap file '%s': %s\n",
		  TABLE_FILE, table_strerror(ret));
    exit(1);
  }
  
  (void)printf("Testing table-mmap...\n");
  if (test_eq(tab2_p, tab_p, 0)) {
    (void)printf("  equal.\n");
  }
  else {
    (void)printf("  NOT equal.\n");
  }
  
  ret = table_munmap(tab2_p);
  if (ret != TABLE_ERROR_NONE) {
    (void)fprintf(stderr, "could not munmap file '%s': %s\n",
		  TABLE_FILE, table_strerror(ret));
    exit(1);
  }
}
Example #27
0
int	main(int argc, char ** argv)
{
  table_t	*tab;
  int		ret, iter_n = ITERATIONS, alignment = 0;
  long		seed;
  
  argc--, argv++;
  
  /* set default seed */
  seed = time(NULL);
  
  /* process the args */
  for (; *argv != NULL; argv++, argc--) {
    if (**argv != '-') {
      continue;
    }
    
    switch (*(*argv + 1)) {
      
    case 'a':
      auto_adjust_b = 1;
      break;
      
    case 'A':
      if (argc > 0) {
	argv++, argc--;
	if (argc == 0) {
	  usage();
	}
	alignment = atoi(*argv);
      }
      break;
      
    case 'i':
      if (argc > 0) {
	argv++, argc--;
	if (argc == 0) {
	  usage();
	}
	iter_n = atoi(*argv);
      }
      break;
      
    case 'l':
      large_b = 1;
      break;
      
    case 's':
      if (argc > 0) {
	argv++, argc--;
	if (argc == 0) {
	  usage();
	}
	seed = atol(*argv);
      }
      break;
      
    case 'v':
      verbose_b = 1;
      break;
      
    default:
      usage();
      break;
    }
  }
  
  if (argc > 0) {
    usage();
  }
  
  (void)srandom(seed);
  (void)printf("Seed for random is %ld\n", seed);
  
  /* alloc the test table */
  call_c++;
  tab = table_alloc(0, &ret);
  if (tab == NULL) {
    (void)printf("table_alloc returned: %s\n", table_strerror(ret));
    exit(1);
  }
  
  if (auto_adjust_b) {
    ret = table_attr(tab, TABLE_FLAG_AUTO_ADJUST);
    if (ret != TABLE_ERROR_NONE) {
      (void)fprintf(stderr, "ERROR: could not set table for auto-adjust: %s\n",
		    table_strerror(ret));
      exit(1);
    }
  }
  
  if (alignment > 0) {
    ret = table_set_data_alignment(tab, alignment);
    if (ret != TABLE_ERROR_NONE) {
      (void)fprintf(stderr, "ERROR: could not set data alignment to %d: %s\n",
		    alignment, table_strerror(ret));
      exit(1);
    }
  }
  
  basic(tab);
  stress(tab, iter_n, 0);
  io_test(tab);
  order_test(tab);
  
  call_c++;
  ret = table_free(tab);
  if (ret != TABLE_ERROR_NONE) {
    (void)fprintf(stderr, "ERROR in table_free: %s\n", table_strerror(ret));
  }
  
  (void)fputc('\n', stdout);
  (void)printf("Test program performed %d table calls\n", call_c);
  
  exit(0);
}
/*
 *  This function print the results of the command "btrfs fi usage"
 *  in tabular format
 */
static void _cmd_filesystem_usage_tabular(unsigned unit_mode,
					struct btrfs_ioctl_space_args *sargs,
					struct chunk_info *chunks_info_ptr,
					int chunks_info_count,
					struct device_info *device_info_ptr,
					int device_info_count)
{
	int i;
	u64 total_unused = 0;
	struct string_table *matrix = NULL;
	int  ncols, nrows;
	int col;
	int unallocated_col;
	int spaceinfos_col;
	const int vhdr_skip = 3;	/* amount of vertical header space */

	/* id, path, unallocated */
	ncols = 3;
	spaceinfos_col = 2;
	/* Properly count the real space infos */
	for (i = 0; i < sargs->total_spaces; i++) {
		if (sargs->spaces[i].flags & BTRFS_SPACE_INFO_GLOBAL_RSV)
			continue;
		ncols++;
	}

	/* 2 for header, empty line, devices, ===, total, used */
	nrows = vhdr_skip + device_info_count + 1 + 2;

	matrix = table_create(ncols, nrows);
	if (!matrix) {
		error("not enough memory");
		return;
	}

	/*
	 * We have to skip the global block reserve everywhere as it's an
	 * artificial blockgroup
	 */

	/* header */
	for (i = 0, col = spaceinfos_col; i < sargs->total_spaces; i++) {
		u64 flags = sargs->spaces[i].flags;

		if (flags & BTRFS_SPACE_INFO_GLOBAL_RSV)
			continue;

		table_printf(matrix, col, 0, "<%s",
				btrfs_group_type_str(flags));
		table_printf(matrix, col, 1, "<%s",
				btrfs_group_profile_str(flags));
		col++;
	}
	unallocated_col = col;

	table_printf(matrix, 0, 1, "<Id");
	table_printf(matrix, 1, 1, "<Path");
	table_printf(matrix, unallocated_col, 1, "<Unallocated");

	/* body */
	for (i = 0; i < device_info_count; i++) {
		int k;
		char *p;

		u64  total_allocated = 0, unused;

		p = strrchr(device_info_ptr[i].path, '/');
		if (!p)
			p = device_info_ptr[i].path;
		else
			p++;

		table_printf(matrix, 0, vhdr_skip + i, ">%llu",
				device_info_ptr[i].devid);
		table_printf(matrix, 1, vhdr_skip + i, "<%s",
				device_info_ptr[i].path);

		for (col = spaceinfos_col, k = 0; k < sargs->total_spaces; k++) {
			u64	flags = sargs->spaces[k].flags;
			u64 devid = device_info_ptr[i].devid;
			int	j;
			u64 size = 0;

			if (flags & BTRFS_SPACE_INFO_GLOBAL_RSV)
				continue;

			for (j = 0 ; j < chunks_info_count ; j++) {
				if (chunks_info_ptr[j].type != flags )
						continue;
				if (chunks_info_ptr[j].devid != devid)
						continue;

				size += calc_chunk_size(chunks_info_ptr+j);
			}

			if (size)
				table_printf(matrix, col, vhdr_skip+ i,
					">%s", pretty_size_mode(size, unit_mode));
			else
				table_printf(matrix, col, vhdr_skip + i, ">-");

			total_allocated += size;
			col++;
		}

		unused = get_partition_size(device_info_ptr[i].path)
				- total_allocated;

		table_printf(matrix, unallocated_col, vhdr_skip + i,
			       ">%s", pretty_size_mode(unused, unit_mode));
		total_unused += unused;

	}

	for (i = 0; i < spaceinfos_col; i++) {
		table_printf(matrix, i, vhdr_skip - 1, "*-");
		table_printf(matrix, i, vhdr_skip + device_info_count, "*-");
	}

	for (i = 0, col = spaceinfos_col; i < sargs->total_spaces; i++) {
		if (sargs->spaces[i].flags & BTRFS_SPACE_INFO_GLOBAL_RSV)
			continue;

		table_printf(matrix, col, vhdr_skip - 1, "*-");
		table_printf(matrix, col, vhdr_skip + device_info_count, "*-");
		col++;
	}
	/* One for Unallocated */
	table_printf(matrix, col, vhdr_skip - 1, "*-");
	table_printf(matrix, col, vhdr_skip + device_info_count, "*-");

	/* footer */
	table_printf(matrix, 1, vhdr_skip + device_info_count + 1, "<Total");
	for (i = 0, col = spaceinfos_col; i < sargs->total_spaces; i++) {
		if (sargs->spaces[i].flags & BTRFS_SPACE_INFO_GLOBAL_RSV)
			continue;

		table_printf(matrix, col++, vhdr_skip + device_info_count + 1,
			">%s",
			pretty_size_mode(sargs->spaces[i].total_bytes, unit_mode));
	}

	table_printf(matrix, unallocated_col, vhdr_skip + device_info_count + 1,
			">%s", pretty_size_mode(total_unused, unit_mode));

	table_printf(matrix, 1, vhdr_skip + device_info_count + 2, "<Used");
	for (i = 0, col = spaceinfos_col; i < sargs->total_spaces; i++) {
		if (sargs->spaces[i].flags & BTRFS_SPACE_INFO_GLOBAL_RSV)
			continue;

		table_printf(matrix, col++, vhdr_skip + device_info_count + 2,
			">%s",
			pretty_size_mode(sargs->spaces[i].used_bytes, unit_mode));
	}

	table_dump(matrix);
	table_free(matrix);
}
Example #29
0
/* prepare a list of pkgs to display */
static void _display_targets(alpm_list_t *targets, int verbose)
{
	char *str;
	off_t isize = 0, rsize = 0, dlsize = 0;
	unsigned short cols;
	alpm_list_t *i, *names = NULL, *header = NULL, *rows = NULL;

	if(!targets) {
		return;
	}

	/* gather package info */
	for(i = targets; i; i = alpm_list_next(i)) {
		pm_target_t *target = i->data;

		if(target->install) {
			dlsize += alpm_pkg_download_size(target->install);
			isize += alpm_pkg_get_isize(target->install);
		}
		if(target->remove) {
			/* add up size of all removed packages */
			rsize += alpm_pkg_get_isize(target->remove);
		}
	}

	/* form data for both verbose and non-verbose display */
	for(i = targets; i; i = alpm_list_next(i)) {
		pm_target_t *target = i->data;

		if(verbose) {
			rows = alpm_list_add(rows, create_verbose_row(target));
		}

		if(target->install) {
			pm_asprintf(&str, "%s-%s", alpm_pkg_get_name(target->install),
					alpm_pkg_get_version(target->install));
		} else if(isize == 0) {
			pm_asprintf(&str, "%s-%s", alpm_pkg_get_name(target->remove),
					alpm_pkg_get_version(target->remove));
		} else {
			pm_asprintf(&str, "%s-%s [%s]", alpm_pkg_get_name(target->remove),
					alpm_pkg_get_version(target->remove), _("removal"));
		}
		names = alpm_list_add(names, str);
	}

	/* print to screen */
	pm_asprintf(&str, "%s (%zd)", _("Packages"), alpm_list_count(targets));
	printf("\n");

	cols = getcols(fileno(stdout));
	if(verbose) {
		header = create_verbose_header(alpm_list_count(targets));
		if(table_display(header, rows, cols) != 0) {
			/* fallback to list display if table wouldn't fit */
			list_display(str, names, cols);
		}
	} else {
		list_display(str, names, cols);
	}
	printf("\n");

	table_free(header, rows);
	FREELIST(names);
	free(str);
	rows = NULL;

	if(dlsize > 0 || config->op_s_downloadonly) {
		add_transaction_sizes_row(&rows, _("Total Download Size:"), dlsize);
	}
	if(!config->op_s_downloadonly) {
		if(isize > 0) {
			add_transaction_sizes_row(&rows, _("Total Installed Size:"), isize);
		}
		if(rsize > 0 && isize == 0) {
			add_transaction_sizes_row(&rows, _("Total Removed Size:"), rsize);
		}
		/* only show this net value if different from raw installed size */
		if(isize > 0 && rsize > 0) {
			add_transaction_sizes_row(&rows, _("Net Upgrade Size:"), isize - rsize);
		}
	}
	table_display(NULL, rows, cols);
	table_free(NULL, rows);
}