本文整理汇总了C++中CALLOCATE函数的典型用法代码示例。如果您正苦于以下问题:C++ CALLOCATE函数的具体用法?C++ CALLOCATE怎么用?C++ CALLOCATE使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了CALLOCATE函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: S_ormatcher_init2
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: S_parse_arguments
/* 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: test_bigend_f32
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: test_bigend_u32
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: Hash_init
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: test_bigend_u64
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: create_db_conn
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: SegReader_offsets
I32Array*
SegReader_offsets(SegReader *self)
{
i32_t *ints = CALLOCATE(1, i32_t);
UNUSED_VAR(self);
return I32Arr_new_steal(ints, 1);
}
示例9: S_write_class_pod
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: test_u1
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: pcre_match_single
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: test_bigend_f64
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: test_c32
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: TestUtils_random_f64s
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: CFCC_write_man_pages
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);
}