コード例 #1
0
ファイル: ORMatcher.c プロジェクト: apache/lucy
static ORMatcher*
S_ormatcher_init2(ORMatcher *self, ORMatcherIVARS *ivars, Vector *children,
                  Similarity *sim) {
    // Init.
    PolyMatcher_init((PolyMatcher*)self, children, sim);
    ivars->size = 0;

    // Derive.
    ivars->max_size = (uint32_t)Vec_Get_Size(children);

    // Allocate.
    ivars->heap = (HeapedMatcherDoc**)CALLOCATE(ivars->max_size + 1, sizeof(HeapedMatcherDoc*));

    // Create a pool of HMDs.  Encourage CPU cache hits by using a single
    // allocation for all of them.
    size_t amount_to_malloc = (ivars->max_size + 1) * sizeof(HeapedMatcherDoc);
    ivars->blob = (char*)MALLOCATE(amount_to_malloc);
    ivars->pool = (HeapedMatcherDoc**)CALLOCATE(ivars->max_size + 1, sizeof(HeapedMatcherDoc*));
    for (uint32_t i = 1; i <= ivars->max_size; i++) {
        size_t offset = i * sizeof(HeapedMatcherDoc);
        HeapedMatcherDoc *hmd = (HeapedMatcherDoc*)(ivars->blob + offset);
        ivars->pool[i] = hmd;
    }

    // Prime queue.
    for (uint32_t i = 0; i < ivars->max_size; i++) {
        Matcher *matcher = (Matcher*)Vec_Fetch(children, i);
        if (matcher) {
            S_add_element(self, ivars, (Matcher*)INCREF(matcher), 0);
        }
    }

    return self;
}
コード例 #2
0
ファイル: cfc.c プロジェクト: leckie711/lucy-clownfish
/* Parse command line arguments. */
static void
S_parse_arguments(int argc, char **argv, CFCArgs *args) {
    int i;

    memset(args, 0, sizeof(CFCArgs));
    args->source_dirs  = (char**)CALLOCATE(1, sizeof(char*));
    args->include_dirs = (char**)CALLOCATE(1, sizeof(char*));
    args->parcels      = (char**)CALLOCATE(1, sizeof(char*));

    for (i = 1; i < argc; i++) {
        char *arg = argv[i];

        if (S_parse_string_argument(arg, "--dest", &args->dest)) {
            continue;
        }
        if (S_parse_string_argument(arg, "--header", &args->header_filename)) {
            continue;
        }
        if (S_parse_string_argument(arg, "--footer", &args->footer_filename)) {
            continue;
        }
        if (S_parse_string_array_argument(arg, "--source",
                                          &args->num_source_dirs,
                                          &args->source_dirs)
           ) {
            continue;
        }
        if (S_parse_string_array_argument(arg, "--include",
                                          &args->num_include_dirs,
                                          &args->include_dirs)
           ) {
            continue;
        }
        if (S_parse_string_array_argument(arg, "--parcel",
                                          &args->num_parcels,
                                          &args->parcels)
           ) {
            continue;
        }

        fprintf(stderr, "Invalid argument '%s'\n", arg);
        exit(EXIT_FAILURE);
    }

    if (!args->dest) {
        fprintf(stderr, "Mandatory argument --dest missing\n");
        exit(EXIT_FAILURE);
    }
}
コード例 #3
0
ファイル: TestNumberUtils.c プロジェクト: theory/lucy
static void
test_bigend_f32(TestBatchRunner *runner) {
    float    source[]  = { -1.3f, 0.0f, 100.2f };
    size_t   count     = 3;
    size_t   amount    = (count + 1) * sizeof(float);
    uint8_t *allocated = (uint8_t*)CALLOCATE(amount, sizeof(uint8_t));
    uint8_t *encoded   = allocated + 1; // Intentionally misaligned.
    uint8_t *target    = encoded;

    for (size_t i = 0; i < count; i++) {
        NumUtil_encode_bigend_f32(source[i], &target);
        target += sizeof(float);
    }
    target = encoded;
    for (size_t i = 0; i < count; i++) {
        float got = NumUtil_decode_bigend_f32(target);
        TEST_TRUE(runner, got == source[i], "bigend f32");
        target += sizeof(float);
    }

    target = encoded;
    NumUtil_encode_bigend_f32(-2.0f, &target);
    TEST_INT_EQ(runner, (encoded[0] & 0x80), 0x80,
                "Truly big-endian (IEEE 754 sign bit set for negative number)");
    TEST_INT_EQ(runner, encoded[0], 0xC0,
                "IEEE 754 representation of -2.0f, byte 0");
    for (size_t i = 1; i < sizeof(float); i++) {
        TEST_INT_EQ(runner, encoded[i], 0,
                    "IEEE 754 representation of -2.0f, byte %d", (int)i);
    }

    FREEMEM(allocated);
}
コード例 #4
0
ファイル: TestNumberUtils.c プロジェクト: theory/lucy
static void
test_bigend_u32(TestBatchRunner *runner) {
    size_t    count     = 32;
    uint64_t *ints      = TestUtils_random_u64s(NULL, count, 0, UINT64_C(1) + UINT32_MAX);
    size_t    amount    = (count + 1) * sizeof(uint32_t);
    char     *allocated = (char*)CALLOCATE(amount, sizeof(char));
    char     *encoded   = allocated + 1; // Intentionally misaligned.
    char     *target    = encoded;

    for (size_t i = 0; i < count; i++) {
        NumUtil_encode_bigend_u32((uint32_t)ints[i], &target);
        target += sizeof(uint32_t);
    }
    target = encoded;
    for (size_t i = 0; i < count; i++) {
        uint32_t got = NumUtil_decode_bigend_u32(target);
        TEST_INT_EQ(runner, got, (long)ints[i], "bigend u32");
        target += sizeof(uint32_t);
    }

    target = encoded;
    NumUtil_encode_bigend_u32(1, &target);
    TEST_INT_EQ(runner, encoded[0], 0, "Truly big-endian u32");
    TEST_INT_EQ(runner, encoded[3], 1, "Truly big-endian u32");

    FREEMEM(allocated);
    FREEMEM(ints);
}
コード例 #5
0
ファイル: Hash.c プロジェクト: pavansondur/lucy
Hash*
Hash_init(Hash *self, uint32_t capacity) {
    // Allocate enough space to hold the requested number of elements without
    // triggering a rebuild.
    uint32_t requested_capacity = capacity < I32_MAX ? capacity : I32_MAX;
    uint32_t threshold;
    capacity = 16;
    while (1) {
        threshold = (capacity / 3) * 2;
        if (threshold > requested_capacity) {
            break;
        }
        capacity *= 2;
    }

    // Init.
    self->size         = 0;
    self->iter_tick    = -1;

    // Derive.
    self->capacity     = capacity;
    self->entries      = (HashEntry*)CALLOCATE(capacity, sizeof(HashEntry));
    self->threshold    = threshold;

    return self;
}
コード例 #6
0
ファイル: TestNumberUtils.c プロジェクト: pavansondur/lucy
static void
test_bigend_u64(TestBatch *batch) {
    size_t    count     = 32;
    uint64_t *ints      = TestUtils_random_u64s(NULL, count, 0, U64_MAX);
    size_t    amount    = (count + 1) * sizeof(uint64_t);
    char     *allocated = (char*)CALLOCATE(amount, sizeof(char));
    char     *encoded   = allocated + 1; // Intentionally misaligned.
    char     *target    = encoded;

    for (size_t i = 0; i < count; i++) {
        NumUtil_encode_bigend_u64(ints[i], &target);
        target += sizeof(uint64_t);
    }
    target = encoded;
    for (size_t i = 0; i < count; i++) {
        uint64_t got = NumUtil_decode_bigend_u64(target);
        TEST_TRUE(batch, got == ints[i], "bigend u64");
        target += sizeof(uint64_t);
    }

    target = encoded;
    NumUtil_encode_bigend_u64(1, &target);
    TEST_INT_EQ(batch, encoded[0], 0, "Truly big-endian");
    TEST_INT_EQ(batch, encoded[7], 1, "Truly big-endian");

    FREEMEM(allocated);
    FREEMEM(ints);
}
コード例 #7
0
ファイル: db.c プロジェクト: BPotato/fluffos
int create_db_conn (void)
{
	int i;

	/* allocate more slots if we need them */
	if (dbConnAlloc == dbConnUsed) {
		i = dbConnAlloc;
		dbConnAlloc += 10;
		if (!dbConnList) {
			dbConnList = CALLOCATE(dbConnAlloc, db_t, TAG_DB, "create_db_conn");
		} else {
			pthread_mutex_lock(db_mut);
			dbConnList = RESIZE(dbConnList, dbConnAlloc, db_t, TAG_DB, "create_db_conn");
			pthread_mutex_unlock(db_mut);
		}
		while (i < dbConnAlloc) {
			dbConnList[i++].flags = DB_FLAG_EMPTY;
		}
	}

	for (i = 0;  i < dbConnAlloc;  i++) {
		if (dbConnList[i].flags & DB_FLAG_EMPTY) {
			dbConnList[i].flags = 0;
			dbConnList[i].type = &no_db;
			dbConnUsed++;
			return i + 1;
		}
	}

	fatal("dbConnAlloc != dbConnUsed, but no empty slots");
}
コード例 #8
0
I32Array*
SegReader_offsets(SegReader *self)
{
    i32_t *ints = CALLOCATE(1, i32_t);
    UNUSED_VAR(self);
    return I32Arr_new_steal(ints, 1);
}
コード例 #9
0
ファイル: CFCPerl.c プロジェクト: rlugojr/lucy-clownfish
static CFCPerlPodFile*
S_write_class_pod(CFCPerl *self) {
    CFCPerlClass **registry  = CFCPerlClass_registry();
    size_t num_registered = 0;
    while (registry[num_registered] != NULL) { num_registered++; }
    CFCPerlPodFile *pod_files
        = (CFCPerlPodFile*)CALLOCATE(num_registered + 1,
                                     sizeof(CFCPerlPodFile));
    size_t count = 0;

    // Generate POD, but don't write.  That way, if there's an error while
    // generating pod, we leak memory but don't clutter up the file system.
    for (size_t i = 0; i < num_registered; i++) {
        const char *class_name = CFCPerlClass_get_class_name(registry[i]);
        char *raw_pod = CFCPerlClass_create_pod(registry[i]);
        if (!raw_pod) { continue; }
        char *pod = CFCUtil_sprintf("%s\n%s%s", self->pod_header, raw_pod,
                                    self->pod_footer);
        char *pod_path = CFCUtil_sprintf("%s" CHY_DIR_SEP "%s.pod",
                                         self->lib_dir, class_name);
        S_replace_double_colons(pod_path, CHY_DIR_SEP_CHAR);

        pod_files[count].contents = pod;
        pod_files[count].path     = pod_path;
        count++;

        FREEMEM(raw_pod);
    }
    pod_files[count].contents = NULL;
    pod_files[count].path     = NULL;

    return pod_files;
}
コード例 #10
0
ファイル: TestNumberUtils.c プロジェクト: pavansondur/lucy
static void
test_u1(TestBatch *batch) {
    size_t    count   = 64;
    uint64_t *ints    = TestUtils_random_u64s(NULL, count, 0, 2);
    size_t    amount  = count / 8;
    uint8_t  *bits    = (uint8_t*)CALLOCATE(amount, sizeof(uint8_t));

    for (size_t i = 0; i < count; i++) {
        if (ints[i]) { NumUtil_u1set(bits, i); }
    }
    for (size_t i = 0; i < count; i++) {
        TEST_INT_EQ(batch, NumUtil_u1get(bits, i), (long)ints[i],
                    "u1 set/get");
    }

    for (size_t i = 0; i < count; i++) {
        NumUtil_u1flip(bits, i);
    }
    for (size_t i = 0; i < count; i++) {
        TEST_INT_EQ(batch, NumUtil_u1get(bits, i), !ints[i], "u1 flip");
    }

    FREEMEM(bits);
    FREEMEM(ints);
}
コード例 #11
0
ファイル: pcre.c プロジェクト: BPotato/fluffos
static int pcre_match_single(svalue_t *str, svalue_t *pattern)
{
	pcre_t *run;
	int ret;

	run = CALLOCATE(1, pcre_t, TAG_TEMPORARY, "pcre_match_single : run");
	run->ovector = NULL;
	run->ovecsize = 0;
	assign_svalue_no_free(&run->pattern, pattern);
	run->subject  = str->u.string;
	run->s_length = SVALUE_STRLEN(str);


	if(pcre_magic(run) < 0)
	{
		error("PCRE compilation failed at offset %d: %s\n", run->erroffset,
				run->error);
		pcre_free_memory(run);
		return 0;
	}

	ret = pcre_query_match(run);

	/* Free memory */
	pcre_free_memory(run);
	return ret;
}
コード例 #12
0
ファイル: TestNumberUtils.c プロジェクト: pavansondur/lucy
static void
test_bigend_f64(TestBatch *batch) {
    double   source[]  = { -1.3, 0.0, 100.2 };
    size_t   count     = 3;
    size_t   amount    = (count + 1) * sizeof(double);
    uint8_t *allocated = (uint8_t*)CALLOCATE(amount, sizeof(uint8_t));
    uint8_t *encoded   = allocated + 1; // Intentionally misaligned.
    uint8_t *target    = encoded;

    for (size_t i = 0; i < count; i++) {
        NumUtil_encode_bigend_f64(source[i], &target);
        target += sizeof(double);
    }
    target = encoded;
    for (size_t i = 0; i < count; i++) {
        double got = NumUtil_decode_bigend_f64(target);
        TEST_TRUE(batch, got == source[i], "bigend f64");
        target += sizeof(double);
    }

    target = encoded;
    NumUtil_encode_bigend_f64(-2.0, &target);
    TEST_INT_EQ(batch, (encoded[0] & 0x80), 0x80,
                "Truly big-endian (IEEE 754 sign bit set for negative number)");
    TEST_INT_EQ(batch, encoded[0], 0xC0,
                "IEEE 754 representation of -2.0, byte 0");
    for (size_t i = 1; i < sizeof(double); i++) {
        TEST_INT_EQ(batch, encoded[i], 0,
                    "IEEE 754 representation of -2.0, byte %d", (int)i);
    }

    FREEMEM(allocated);
}
コード例 #13
0
ファイル: TestNumberUtils.c プロジェクト: pavansondur/lucy
static void
test_c32(TestBatch *batch) {
    uint64_t  mins[]   = { 0,   0x4000 - 100, (uint32_t)I32_MAX - 100, U32_MAX - 10 };
    uint64_t  limits[] = { 500, 0x4000 + 100, (uint32_t)I32_MAX + 100, U32_MAX      };
    uint32_t  set_num;
    uint32_t  num_sets  = sizeof(mins) / sizeof(uint64_t);
    size_t    count     = 64;
    uint64_t *ints      = NULL;
    size_t    amount    = count * C32_MAX_BYTES;
    char     *encoded   = (char*)CALLOCATE(amount, sizeof(char));
    char     *target    = encoded;
    char     *limit     = target + amount;

    for (set_num = 0; set_num < num_sets; set_num++) {
        char *skip;
        ints = TestUtils_random_u64s(ints, count,
                                     mins[set_num], limits[set_num]);
        target = encoded;
        for (size_t i = 0; i < count; i++) {
            NumUtil_encode_c32((uint32_t)ints[i], &target);
        }
        target = encoded;
        skip   = encoded;
        for (size_t i = 0; i < count; i++) {
            TEST_INT_EQ(batch, NumUtil_decode_c32(&target), (long)ints[i],
                        "c32 %lu", (long)ints[i]);
            NumUtil_skip_cint(&skip);
            if (target > limit) { THROW(ERR, "overrun"); }
        }
        TEST_TRUE(batch, skip == target, "skip %lu == %lu",
                  (unsigned long)skip, (unsigned long)target);

        target = encoded;
        for (size_t i = 0; i < count; i++) {
            NumUtil_encode_padded_c32((uint32_t)ints[i], &target);
        }
        TEST_TRUE(batch, target == limit,
                  "padded c32 uses 5 bytes (%lu == %lu)", (unsigned long)target,
                  (unsigned long)limit);
        target = encoded;
        skip   = encoded;
        for (size_t i = 0; i < count; i++) {
            TEST_INT_EQ(batch, NumUtil_decode_c32(&target), (long)ints[i],
                        "padded c32 %lu", (long)ints[i]);
            NumUtil_skip_cint(&skip);
            if (target > limit) { THROW(ERR, "overrun"); }
        }
        TEST_TRUE(batch, skip == target, "skip padded %lu == %lu",
                  (unsigned long)skip, (unsigned long)target);
    }

    target = encoded;
    NumUtil_encode_c32(U32_MAX, &target);
    target = encoded;
    TEST_INT_EQ(batch, NumUtil_decode_c32(&target), U32_MAX, "c32 U32_MAX");

    FREEMEM(encoded);
    FREEMEM(ints);
}
コード例 #14
0
ファイル: TestUtils.c プロジェクト: kstarsinic/lucy
double*
TestUtils_random_f64s(double *buf, size_t count) {
    double *f64s = buf ? buf : (double*)CALLOCATE(count, sizeof(double));
    for (size_t i = 0; i < count; i++) {
        uint64_t num = TestUtils_random_u64();
        f64s[i] = U64_TO_DOUBLE(num) / UINT64_MAX;
    }
    return f64s;
}
コード例 #15
0
ファイル: CFCC.c プロジェクト: leckie711/lucy-clownfish
void
CFCC_write_man_pages(CFCC *self) {
    CFCHierarchy  *hierarchy = self->hierarchy;
    CFCClass     **ordered   = CFCHierarchy_ordered_classes(hierarchy);

    size_t num_classes = 0;
    for (size_t i = 0; ordered[i] != NULL; i++) {
        CFCClass *klass = ordered[i];
        if (!CFCClass_included(klass)) { ++num_classes; }
    }
    char **man_pages = (char**)CALLOCATE(num_classes, sizeof(char*));

    // Generate man pages, but don't write.  That way, if there's an error
    // while generating the pages, we leak memory but don't clutter up the file 
    // system.
    for (size_t i = 0, j = 0; ordered[i] != NULL; i++) {
        CFCClass *klass = ordered[i];
        if (CFCClass_included(klass)) { continue; }

        char *man_page = CFCCMan_create_man_page(klass);
        man_pages[j++] = man_page;
    }

    const char *dest = CFCHierarchy_get_dest(hierarchy);
    char *man3_path
        = CFCUtil_sprintf("%s" CHY_DIR_SEP "man" CHY_DIR_SEP "man3", dest);
    if (!CFCUtil_is_dir(man3_path)) {
        CFCUtil_make_path(man3_path);
        if (!CFCUtil_is_dir(man3_path)) {
            CFCUtil_die("Can't make path %s", man3_path);
        }
    }

    // Write out any man pages that have changed.
    for (size_t i = 0, j = 0; ordered[i] != NULL; i++) {
        CFCClass *klass = ordered[i];
        if (CFCClass_included(klass)) { continue; }

        char *raw_man_page = man_pages[j++];
        if (!raw_man_page) { continue; }
        char *man_page = CFCUtil_sprintf("%s%s%s", self->man_header,
                                         raw_man_page, self->man_footer);

        const char *full_struct_sym = CFCClass_full_struct_sym(klass);
        char *filename = CFCUtil_sprintf("%s" CHY_DIR_SEP "%s.3", man3_path,
                                         full_struct_sym);
        CFCUtil_write_if_changed(filename, man_page, strlen(man_page));
        FREEMEM(filename);
        FREEMEM(man_page);
        FREEMEM(raw_man_page);
    }

    FREEMEM(man3_path);
    FREEMEM(man_pages);
    FREEMEM(ordered);
}
コード例 #16
0
ファイル: TestUtils.c プロジェクト: kstarsinic/lucy
uint64_t*
TestUtils_random_u64s(uint64_t *buf, size_t count, uint64_t min,
                      uint64_t limit) {
    uint64_t  range = min < limit ? limit - min : 0;
    uint64_t *ints = buf ? buf : (uint64_t*)CALLOCATE(count, sizeof(uint64_t));
    for (size_t i = 0; i < count; i++) {
        ints[i] = min + TestUtils_random_u64() % range;
    }
    return ints;
}
コード例 #17
0
ファイル: VArray.c プロジェクト: timwilkens/lucy-clownfish
VArray*
VA_init(VArray *self, uint32_t capacity) {
    // Init.
    self->size = 0;

    // Assign.
    self->cap = capacity;

    // Derive.
    self->elems = (Obj**)CALLOCATE(capacity, sizeof(Obj*));

    return self;
}
コード例 #18
0
ファイル: CFCHierarchy.c プロジェクト: apache/lucy-clownfish
static void
S_parse_cf_files(CFCHierarchy *self, const char *source_dir, int is_included) {
    CFCFindFilesContext context;
    context.ext       = ".cfh";
    context.paths     = (char**)CALLOCATE(1, sizeof(char*));
    context.num_paths = 0;
    CFCUtil_walk(source_dir, S_find_files, &context);

    // Process any file that has at least one class declaration.
    for (int i = 0; context.paths[i] != NULL; i++) {
        // Derive the name of the class that owns the module file.
        char *source_path = context.paths[i];
        char *path_part = S_extract_path_part(source_path, source_dir, ".cfh");

        // Ignore hidden files.
        if (path_part[0] == '.'
            || strstr(path_part, CHY_DIR_SEP ".") != NULL) {
            continue;
        }

        CFCFileSpec *file_spec = CFCFileSpec_new(source_dir, path_part, ".cfh",
                                                 is_included);

        // Slurp and parse file.
        size_t unused;
        char *content = CFCUtil_slurp_text(source_path, &unused);
        CFCFile *file = CFCParser_parse_file(self->parser, content, file_spec);
        FREEMEM(content);
        if (!file) {
            int lineno = CFCParser_get_lineno(self->parser);
            CFCUtil_die("%s:%d: parser error", source_path, lineno);
        }

        // Make sure path_part is unique because the name of the generated
        // C header is derived from it.
        CFCFile *existing = S_fetch_file(self, path_part);
        if (existing) {
            CFCUtil_die("File %s.cfh found twice in %s and %s",
                        path_part, CFCFile_get_source_dir(existing),
                        source_dir);
        }

        S_add_file(self, file);

        CFCBase_decref((CFCBase*)file);
        CFCBase_decref((CFCBase*)file_spec);
        FREEMEM(path_part);
    }

    CFCUtil_free_string_array(context.paths);
}
コード例 #19
0
ファイル: CFCHierarchy.c プロジェクト: apache/lucy-clownfish
CFCHierarchy*
CFCHierarchy_init(CFCHierarchy *self, const char *dest) {
    if (!dest || !strlen(dest)) {
        CFCUtil_die("'dest' is required");
    }
    self->sources      = (char**)CALLOCATE(1, sizeof(char*));
    self->num_sources  = 0;
    self->includes     = (char**)CALLOCATE(1, sizeof(char*));
    self->num_includes = 0;
    self->prereqs      = (char**)CALLOCATE(1, sizeof(char*));
    self->num_prereqs  = 0;
    self->dest         = CFCUtil_strdup(dest);
    self->trees        = (CFCClass**)CALLOCATE(1, sizeof(CFCClass*));
    self->num_trees    = 0;
    self->files        = (CFCFile**)CALLOCATE(1, sizeof(CFCFile*));
    self->num_files    = 0;
    self->parser       = CFCParser_new();

    self->inc_dest = CFCUtil_sprintf("%s" CHY_DIR_SEP "include", self->dest);
    self->src_dest = CFCUtil_sprintf("%s" CHY_DIR_SEP "source", self->dest);

    return self;
}
コード例 #20
0
ファイル: BitVector.c プロジェクト: pavansondur/lucy
BitVector*
BitVec_init(BitVector *self, uint32_t capacity) {
    const uint32_t byte_size = (uint32_t)ceil(capacity / 8.0);

    // Derive.
    self->bits = capacity
                 ? (uint8_t*)CALLOCATE(byte_size, sizeof(uint8_t))
                 : NULL;

    // Assign.
    self->cap = byte_size * 8;

    return self;
}
コード例 #21
0
ファイル: BitVector.c プロジェクト: unlobe/lucy
BitVector*
BitVec_init(BitVector *self, size_t capacity) {
    BitVectorIVARS *const ivars = BitVec_IVARS(self);
    const size_t byte_size = SI_octet_size(capacity);

    // Derive.
    ivars->bits = capacity
                 ? (uint8_t*)CALLOCATE(byte_size, sizeof(uint8_t))
                 : NULL;

    // Assign.
    ivars->cap = byte_size * 8;

    return self;
}
コード例 #22
0
ファイル: TestNumberUtils.c プロジェクト: pavansondur/lucy
static void
test_u4(TestBatch *batch) {
    size_t    count = 128;
    uint64_t *ints  = TestUtils_random_u64s(NULL, count, 0, 16);
    uint8_t  *bits  = (uint8_t*)CALLOCATE((count / 2), sizeof(uint8_t));

    for (size_t i = 0; i < count; i++) {
        NumUtil_u4set(bits, i, (uint8_t)ints[i]);
    }
    for (size_t i = 0; i < count; i++) {
        TEST_INT_EQ(batch, NumUtil_u4get(bits, i), (long)ints[i], "u4");
    }

    FREEMEM(bits);
    FREEMEM(ints);
}
コード例 #23
0
ファイル: TestNumberUtils.c プロジェクト: theory/lucy
static void
test_u2(TestBatchRunner *runner) {
    size_t    count = 32;
    uint64_t *ints = TestUtils_random_u64s(NULL, count, 0, 4);
    uint8_t  *bits = (uint8_t*)CALLOCATE((count / 4), sizeof(uint8_t));

    for (size_t i = 0; i < count; i++) {
        NumUtil_u2set(bits, i, (uint8_t)ints[i]);
    }
    for (size_t i = 0; i < count; i++) {
        TEST_INT_EQ(runner, NumUtil_u2get(bits, i), (long)ints[i], "u2");
    }

    FREEMEM(bits);
    FREEMEM(ints);
}
コード例 #24
0
ファイル: CFCGoClass.c プロジェクト: nkurz/lucy-clownfish
static void
S_lazy_init_method_bindings(CFCGoClass *self) {
    if (self->method_bindings) {
        return;
    }
    CFCUTIL_NULL_CHECK(self->client);
    size_t        num_bound     = 0;
    CFCMethod   **fresh_methods = CFCClass_fresh_methods(self->client);
    CFCGoMethod **bound
        = (CFCGoMethod**)CALLOCATE(1, sizeof(CFCGoMethod*));

     // Iterate over the class's fresh methods.
    for (size_t i = 0; fresh_methods[i] != NULL; i++) {
        CFCMethod *method = fresh_methods[i];

        // Skip methods which have been explicitly excluded.
        if (CFCMethod_excluded_from_host(method)) {
            continue;
        }

        // Skip methods that shouldn't be bound.
        if (!CFCMethod_can_be_bound(method)) {
            continue;
        }

        // Only include novel methods.
        if (!CFCMethod_novel(method)) {
            continue;
        }
        const char *sym = CFCMethod_get_name(method);
        if (!CFCClass_fresh_method(self->client, sym)) {
            continue;
        }

        /* Create the binding, add it to the array.
         */
        CFCGoMethod *meth_binding = CFCGoMethod_new(method);
        size_t size = (num_bound + 2) * sizeof(CFCGoMethod*);
        bound = (CFCGoMethod**)REALLOCATE(bound, size);
        bound[num_bound] = meth_binding;
        num_bound++;
        bound[num_bound] = NULL;
    }

    self->method_bindings = bound;
    self->num_bound       = num_bound;
}
コード例 #25
0
ファイル: pcre.c プロジェクト: BPotato/fluffos
void f_pcre_extract(void)
{
	pcre_t *run;
	array_t *ret;

	run = CALLOCATE(1, pcre_t, TAG_TEMPORARY, "f_pcre_extract : run");
	assign_svalue_no_free(&run->pattern, sp);
	run->subject  = (sp - 1)->u.string;
	run->s_length = SVALUE_STRLEN(sp - 1);
	run->ovector = NULL;
	run->ovecsize = 0;

	if(pcre_magic(run) < 0)
	{
		error("PCRE compilation failed at offset %d: %s\n", run->erroffset,
				run->error);
		pop_2_elems();
		pcre_free_memory(run);
		return;
	}

	/* Pop the 2 arguments from the stack */

	if (run->rc < 0) /* No match. could do handling of matching errors if wanted */
	{
		pop_2_elems();
		pcre_free_memory(run);
		push_refed_array(&the_null_array);
		return;
	}
	else if (run->rc > (run->ovecsize/3 - 1))
	{
		pop_2_elems();
		pcre_free_memory(run);
		error("Too many substrings.\n");
		return;
	}


	ret = pcre_get_substrings(run);
	pop_2_elems();

	push_refed_array(ret);

	pcre_free_memory(run);
	return;
}
コード例 #26
0
/*
 * Get more LPC sockets structures if we run out
 */
static int more_lpc_sockets()
{
    int i;

    max_lpc_socks += 10;

    if (!lpc_socks)
        lpc_socks = CALLOCATE(10, lpc_socket_t, TAG_SOCKETS, "more_lpc_sockets");
    else
        lpc_socks = RESIZE(lpc_socks, max_lpc_socks, lpc_socket_t, TAG_SOCKETS, "more_lpc_sockets");

    i = max_lpc_socks;
    while (--i >= max_lpc_socks - 10)
        clear_socket(i, 0);

    return max_lpc_socks - 10;
}
コード例 #27
0
ファイル: CFCParcel.c プロジェクト: pavansondur/lucy
void
CFCParcel_register(CFCParcel *self) {
    CFCParcel *existing = CFCParcel_fetch(self->name);
    if (existing) {
        CFCUtil_die("Parcel '%s' already registered", self->name);
    }
    if (!num_registered) {
        // Init default parcel as first.
        registry = (CFCParcel**)CALLOCATE(3, sizeof(CFCParcel*));
        CFCParcel *def = CFCParcel_default_parcel();
        registry[0] = (CFCParcel*)CFCBase_incref((CFCBase*)def);
        num_registered++;
    }
    size_t size = (num_registered + 2) * sizeof(CFCParcel*);
    registry = (CFCParcel**)REALLOCATE(registry, size);
    registry[num_registered++] = (CFCParcel*)CFCBase_incref((CFCBase*)self);
    registry[num_registered]   = NULL;
}
コード例 #28
0
ファイル: TestNumberUtils.c プロジェクト: pavansondur/lucy
static void
test_c64(TestBatch *batch) {
    uint64_t  mins[]    = { 0,   0x4000 - 100, (uint64_t)U32_MAX - 100,  U64_MAX - 10 };
    uint64_t  limits[]  = { 500, 0x4000 + 100, (uint64_t)U32_MAX + 1000, U64_MAX      };
    uint32_t  set_num;
    uint32_t  num_sets  = sizeof(mins) / sizeof(uint64_t);
    size_t    count     = 64;
    uint64_t *ints      = NULL;
    size_t    amount    = count * C64_MAX_BYTES;
    char     *encoded   = (char*)CALLOCATE(amount, sizeof(char));
    char     *target    = encoded;
    char     *limit     = target + amount;

    for (set_num = 0; set_num < num_sets; set_num++) {
        char *skip;
        ints = TestUtils_random_u64s(ints, count,
                                     mins[set_num], limits[set_num]);
        target = encoded;
        for (size_t i = 0; i < count; i++) {
            NumUtil_encode_c64(ints[i], &target);
        }
        target = encoded;
        skip   = encoded;
        for (size_t i = 0; i < count; i++) {
            uint64_t got = NumUtil_decode_c64(&target);
            TEST_TRUE(batch, got == ints[i],
                      "c64 %" U64P " == %" U64P, got, ints[i]);
            if (target > limit) { THROW(ERR, "overrun"); }
            NumUtil_skip_cint(&skip);
        }
        TEST_TRUE(batch, skip == target, "skip %lu == %lu",
                  (unsigned long)skip, (unsigned long)target);
    }

    target = encoded;
    NumUtil_encode_c64(U64_MAX, &target);
    target = encoded;

    uint64_t got = NumUtil_decode_c64(&target);
    TEST_TRUE(batch, got == U64_MAX, "c64 U64_MAX");

    FREEMEM(encoded);
    FREEMEM(ints);
}
コード例 #29
0
ファイル: CFCHierarchy.c プロジェクト: apache/lucy-clownfish
static void
S_find_doc_files(const char *source_dir) {
    CFCFindFilesContext context;
    context.ext       = ".md";
    context.paths     = (char**)CALLOCATE(1, sizeof(char*));
    context.num_paths = 0;
    CFCUtil_walk(source_dir, S_find_files, &context);

    for (int i = 0; context.paths[i] != NULL; i++) {
        char *path = context.paths[i];
        char *path_part = S_extract_path_part(path, source_dir, ".md");
        CFCDocument *doc = CFCDocument_create(path, path_part);

        CFCBase_decref((CFCBase*)doc);
        FREEMEM(path_part);
    }

    CFCUtil_free_string_array(context.paths);
}
コード例 #30
0
I32Array*
DelWriter_Generate_Doc_Map_IMP(DeletionsWriter *self, Matcher *deletions,
                               int32_t doc_max, int32_t offset) {
    int32_t *doc_map = (int32_t*)CALLOCATE(doc_max + 1, sizeof(int32_t));
    int32_t  next_deletion = deletions ? Matcher_Next(deletions) : INT32_MAX;
    UNUSED_VAR(self);

    // 0 for a deleted doc, a new number otherwise
    for (int32_t i = 1, new_doc_id = 1; i <= doc_max; i++) {
        if (i == next_deletion) {
            next_deletion = Matcher_Next(deletions);
        }
        else {
            doc_map[i] = offset + new_doc_id++;
        }
    }

    return I32Arr_new_steal(doc_map, doc_max + 1);
}