Exemplo n.º 1
0
int
buddy_alloc(struct buddy * self , int s) {
	int size;
	if (s==0) {
		size = 1;
	} else {
		size = (int)next_pow_of_2(s);
	}
	int length = 1 << self->level;

	if (size > length)
		return -1;

	int index = 0;
	int level = 0;

	while (index >= 0) {
		if (size == length) {
			if (self->tree[index] == NODE_UNUSED) {
				self->tree[index] = NODE_USED;
				_mark_parent(self, index);
				return _index_offset(index, level, self->level);
			}
		} else {
			// size < length
			switch (self->tree[index]) {
			case NODE_USED:
			case NODE_FULL:
				break;
			case NODE_UNUSED:
				// split first
				self->tree[index] = NODE_SPLIT;
				self->tree[index*2+1] = NODE_UNUSED;
				self->tree[index*2+2] = NODE_UNUSED;
			default:
				index = index * 2 + 1;
				length /= 2;
				level++;
				continue;
			}
		}
		if (index & 1) {
			++index;
			continue;
		}
		for (;;) {
			level--;
			length *= 2;
			index = (index+1)/2 -1;
			if (index < 0)
				return -1;
			if (index & 1) {
				++index;
				break;
			}
		}
	}

	return -1;
}
Exemplo n.º 2
0
int cuckoo_filter_init(void *s)
{
    int i, hash_size;

    hash_size = next_pow_of_2(getSize(s));

    /* Allocate hash slots */
    cuckoo_filter.slot_num = hash_size;
    cuckoo_filter.slots = calloc(cuckoo_filter.slot_num, sizeof(struct hash_slot_cache));
    if (cuckoo_filter.slots == NULL) {
        return -1;
    }

    /* Allocate hash buckets associated with slots */
    cuckoo_filter.bucket_num = cuckoo_filter.slot_num / ASSOC_WAY;
    cuckoo_filter.buckets = malloc(cuckoo_filter.bucket_num * sizeof(struct hash_slot_cache *));
    if (cuckoo_filter.buckets == NULL) {
        free(cuckoo_filter.slots);
        return -1;
    }
    for (i = 0; i < cuckoo_filter.bucket_num; i++) {
        cuckoo_filter.buckets[i] = &cuckoo_filter.slots[i * ASSOC_WAY];
    }

    cuckoo_filter.data = s;

    return 0;
}
Exemplo n.º 3
0
int main(int argc, char **argv)
{
        SHA_CTX c;
        struct stat st;
        uint32_t key_num;
        uint8_t *keys;
        uint8_t **sha1_key;
        uint8_t value[DAT_LEN], *v;
        int bytes, i, j;
        FILE *f1, *f2;

        if (argc < 3) {
                fprintf(stderr, "usage: ./cuckoo_filter read_file write_file\n");
                exit(-1);
        }

        --argc;
        ++argv;

        f1 = fopen(argv[0], "rb");
        if (f1 == NULL) {
                fprintf(stderr, "Fail to open %s!\n", argv[0]);
                exit(-1);
        }
        stat(argv[0], &st);

        f2 = fopen(argv[1], "wb+");
        if (f2 == NULL) {
                fprintf(stderr, "Fail to open %s!\n", argv[1]);
                exit(-1);
        }

        /* Initialization */
        cuckoo_filter_init(st.st_size);

        /* Allocate SHA1 key space */
        key_num = next_pow_of_2(st.st_size) / DAT_LEN;
        keys = malloc(key_num * 20);
        sha1_key = malloc(key_num * sizeof(void *));
        if (!keys || !sha1_key) {
                fprintf(stderr, "Out of memory!\n");
                exit(-1);
        }
        for (i = 0; i < key_num; i++) {
                sha1_key[i] = keys + i * 20;
        }

        /* Put read_file into log on flash. */
        i = 0;
        do {
                memset(value, 0, DAT_LEN);
                bytes = fread(value, 1, DAT_LEN, f1);
                SHA1_Init(&c);
                SHA1_Update(&c, value, bytes);
                SHA1_Final(sha1_key[i], &c);
                cuckoo_filter_put(sha1_key[i], value);
                i++;
        } while (bytes == DAT_LEN);

        /* Real key number */
        key_num = i;
        printf("Total %u records.\n", key_num);

        /* Deletion test */
        for (i = 0; i < key_num; i += 2) {
                cuckoo_filter_put(sha1_key[i], NULL);
        }
        for (i = 0; i < key_num; i++) {
                memset(value, 0, DAT_LEN);
                bytes = fread(value, 1, DAT_LEN, f1);
                if (!(i & 0x1)) {
                        cuckoo_filter_put(sha1_key[i], value);
                }
        }

        /* Get logs on flash and write them into a new file. */
        for (j = 0; j < key_num; j++) {
                v = cuckoo_filter_get(sha1_key[j]);
                if (v != NULL) {
                        memcpy(value, v, DAT_LEN);
                        fwrite(value, 1, DAT_LEN, f2);
                }
        }

        fclose(f1);
        fclose(f2);

        return 0;
}