MBArray * mbarray_Copy_Template(MBArray * src, char * filename, int perms) { int header_len = mbarray_HeaderLen(src); char * header; if (header_len < 0) { return NULL; } if (!strcmp(filename, src->filename)) { errno = EINVAL; return NULL; } header = (char *)malloc(header_len + 1); if (header == NULL) { errno = ENOMEM; return NULL; } if (mbarray_Header(header, src, header_len) == NULL) { free(header); return NULL; } return mbarray_Create( src->bits, filename, header, header_len, O_CREAT | O_RDWR, perms); }
int main(int argc, char ** argv) { BTYPE bit; int value; MBArray * array; int i; if (argc < 3) { fprintf(stderr, "Usage: %s FILE BIT [VALUE]\nValue is either 0 or 1 and will define a set/clear operation.\n", argv[0]); return 255; } /* Open file */ array = mbarray_Create( 0, argv[1], "", 0, O_RDWR, 0); if (!array) goto error; bit = atol(argv[2]); if (argc > 3) { value = atol(argv[3]); if (value) { if (mbarray_Set(array, bit)) goto error; } else { if (mbarray_Clear(array, bit)) goto error; } } for (i = 0; i < array->bits; i++) { mbarray_Set(array, i); mbarray_Test(array, i); } getc(stdin); bit = 1 - mbarray_Test(array, bit); mbarray_Destroy(array); return bit; error: fprintf(stderr, "Error: %s [%d]\n", strerror(errno), errno); return 255; }
BloomFilter *bloomfilter_Create(size_t max_num_elem, double error_rate, const char * file, BTYPE num_bits, int oflags, int perms, int *hash_seeds, int num_hashes) { BloomFilter * bf = (BloomFilter *)malloc(sizeof(BloomFilter)); MBArray * array; if (!bf) { return NULL; } bf->max_num_elem = max_num_elem; bf->error_rate = error_rate; bf->num_hashes = num_hashes; bf->count_correct = 1; bf->bf_version = BF_CURRENT_VERSION; bf->elem_count = 0; memset(bf->reserved, sizeof(uint32_t) * 32, 0); memset(bf->hash_seeds, sizeof(uint32_t) * 256, 0); memcpy(bf->hash_seeds, hash_seeds, sizeof(uint32_t) * num_hashes); array = mbarray_Create(num_bits, file, (char *)bf, sizeof(BloomFilter), oflags, perms); if (!array) { bloomfilter_Destroy(bf); return NULL; } /* After we create the new array object, this array may already have all of the bloom filter data from the file in the header info. By calling mbarray_Header, we copy that header data back into this BloomFilter object. */ if (mbarray_Header((char *)bf, array, sizeof(BloomFilter)) == NULL) { bloomfilter_Destroy(bf); mbarray_Destroy(array); return NULL; } /* Since we just initialized from a file, we have to fix our pointers */ bf->array = array; return bf; }
int main(int argc, char ** argv) { MBArray * array; if (argc < 3) { fprintf(stderr, "Usage: %s FILENAME SIZE\nCreate new mmap'd array file.\n", argv[0]); return 1; } array = mbarray_Create( atol(argv[2]), argv[1], "", 0, O_RDWR | O_CREAT, 0777); if (!array) goto error; mbarray_ClearAll(array); mbarray_Destroy(array); return 0; error: fprintf(stderr, "Error: %s [%d]\n", strerror(errno), errno); return 255; }