本文整理汇总了C++中MALLOCATE函数的典型用法代码示例。如果您正苦于以下问题:C++ MALLOCATE函数的具体用法?C++ MALLOCATE怎么用?C++ MALLOCATE使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了MALLOCATE函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: S_final_method_def
/* Create a macro definition that aliases to a function name directly, since
* this method may not be overridden. */
static char*
S_final_method_def(CFCMethod *method, CFCClass *klass) {
const char *self_type = CFCType_to_c(CFCMethod_self_type(method));
const char *full_func_sym = CFCMethod_implementing_func_sym(method);
const char *arg_names
= CFCParamList_name_list(CFCMethod_get_param_list(method));
size_t meth_sym_size = CFCMethod_full_method_sym(method, klass, NULL, 0);
char *full_meth_sym = (char*)MALLOCATE(meth_sym_size);
CFCMethod_full_method_sym(method, klass, full_meth_sym, meth_sym_size);
size_t offset_sym_size = CFCMethod_full_offset_sym(method, klass, NULL, 0);
char *full_offset_sym = (char*)MALLOCATE(offset_sym_size);
CFCMethod_full_offset_sym(method, klass, full_offset_sym, offset_sym_size);
const char pattern[] = "extern size_t %s;\n#define %s(%s) \\\n %s((%s)%s)\n";
size_t size = sizeof(pattern)
+ strlen(full_offset_sym)
+ strlen(full_meth_sym)
+ strlen(arg_names)
+ strlen(full_func_sym)
+ strlen(self_type)
+ strlen(arg_names)
+ 20;
char *method_def = (char*)MALLOCATE(size);
sprintf(method_def, pattern, full_offset_sym, full_meth_sym, arg_names,
full_func_sym, self_type, arg_names);
FREEMEM(full_offset_sym);
FREEMEM(full_meth_sym);
return method_def;
}
示例2: TV_Deserialize_IMP
TermVector*
TV_Deserialize_IMP(TermVector *self, InStream *instream) {
String *field = Freezer_read_string(instream);
String *text = Freezer_read_string(instream);
size_t num_pos = InStream_Read_C64(instream);
// Read positional data.
int32_t *posits = (int32_t*)MALLOCATE(num_pos * sizeof(int32_t));
int32_t *starts = (int32_t*)MALLOCATE(num_pos * sizeof(int32_t));
int32_t *ends = (int32_t*)MALLOCATE(num_pos * sizeof(int32_t));
for (size_t i = 0; i < num_pos; i++) {
posits[i] = InStream_Read_C32(instream);
starts[i] = InStream_Read_C32(instream);
ends[i] = InStream_Read_C32(instream);
}
I32Array *positions = I32Arr_new_steal(posits, num_pos);
I32Array *start_offsets = I32Arr_new_steal(starts, num_pos);
I32Array *end_offsets = I32Arr_new_steal(ends, num_pos);
TV_init(self, field, text, positions, start_offsets, end_offsets);
DECREF(positions);
DECREF(start_offsets);
DECREF(end_offsets);
DECREF(text);
DECREF(field);
return self;
}
示例3: TV_deserialize
TermVector*
TV_deserialize(TermVector *self, InStream *instream)
{
u32_t i;
CharBuf *field = (CharBuf*)CB_deserialize(NULL, instream);
CharBuf *text = (CharBuf*)CB_deserialize(NULL, instream);
u32_t num_pos = InStream_Read_C32(instream);
i32_t *posits, *starts, *ends;
I32Array *positions, *start_offsets, *end_offsets;
/* Read positional data. */
posits = MALLOCATE(num_pos, i32_t);
starts = MALLOCATE(num_pos, i32_t);
ends = MALLOCATE(num_pos, i32_t);
for (i = 0; i < num_pos; i++) {
posits[i] = InStream_Read_C32(instream);
starts[i] = InStream_Read_C32(instream);
ends[i] = InStream_Read_C32(instream);
}
positions = I32Arr_new_steal(posits, num_pos);
start_offsets = I32Arr_new_steal(starts, num_pos);
end_offsets = I32Arr_new_steal(ends, num_pos);
self = self ? self : (TermVector*)VTable_Make_Obj(&TERMVECTOR);
self = TV_init(self, field, text, positions, start_offsets, end_offsets);
DECREF(positions);
DECREF(start_offsets);
DECREF(end_offsets);
DECREF(text);
DECREF(field);
return self;
}
示例4: IxManager_Recycle_IMP
Vector*
IxManager_Recycle_IMP(IndexManager *self, PolyReader *reader,
DeletionsWriter *del_writer, int64_t cutoff,
bool optimize) {
Vector *seg_readers = PolyReader_Get_Seg_Readers(reader);
size_t num_seg_readers = Vec_Get_Size(seg_readers);
SegReader **candidates
= (SegReader**)MALLOCATE(num_seg_readers * sizeof(SegReader*));
size_t num_candidates = 0;
for (size_t i = 0; i < num_seg_readers; i++) {
SegReader *seg_reader = (SegReader*)Vec_Fetch(seg_readers, i);
if (SegReader_Get_Seg_Num(seg_reader) > cutoff) {
candidates[num_candidates++] = seg_reader;
}
}
Vector *recyclables = Vec_new(num_candidates);
if (optimize) {
for (size_t i = 0; i < num_candidates; i++) {
Vec_Push(recyclables, INCREF(candidates[i]));
}
FREEMEM(candidates);
return recyclables;
}
// Sort by ascending size in docs, choose sparsely populated segments.
qsort(candidates, num_candidates, sizeof(SegReader*), S_compare_doc_count);
int32_t *counts = (int32_t*)MALLOCATE(num_candidates * sizeof(int32_t));
for (uint32_t i = 0; i < num_candidates; i++) {
counts[i] = SegReader_Doc_Count(candidates[i]);
}
I32Array *doc_counts = I32Arr_new_steal(counts, num_candidates);
uint32_t threshold = IxManager_Choose_Sparse(self, doc_counts);
DECREF(doc_counts);
// Move SegReaders to be recycled.
for (uint32_t i = 0; i < threshold; i++) {
Vec_Store(recyclables, i, INCREF(candidates[i]));
}
// Find segments where at least 10% of all docs have been deleted.
for (uint32_t i = threshold; i < num_candidates; i++) {
SegReader *seg_reader = candidates[i];
String *seg_name = SegReader_Get_Seg_Name(seg_reader);
double doc_max = SegReader_Doc_Max(seg_reader);
double num_deletions = DelWriter_Seg_Del_Count(del_writer, seg_name);
double del_proportion = num_deletions / doc_max;
if (del_proportion >= 0.1) {
Vec_Push(recyclables, INCREF(seg_reader));
}
}
FREEMEM(candidates);
return recyclables;
}
示例5: CFCParcel_init
CFCParcel*
CFCParcel_init(CFCParcel *self, const char *name, const char *cnick,
CFCVersion *version) {
// Validate name.
if (!name || !S_validate_name_or_cnick(name)) {
CFCUtil_die("Invalid name: '%s'", name ? name : "[NULL]");
}
self->name = CFCUtil_strdup(name);
// Validate or derive cnick.
if (cnick) {
if (!S_validate_name_or_cnick(cnick)) {
CFCUtil_die("Invalid cnick: '%s'", cnick);
}
self->cnick = CFCUtil_strdup(cnick);
}
else {
// Default cnick to name.
self->cnick = CFCUtil_strdup(name);
}
// Default to version v0.
if (version) {
self->version = (CFCVersion*)CFCBase_incref((CFCBase*)version);
}
else {
self->version = CFCVersion_new("v0");
}
// Derive prefix, Prefix, PREFIX.
size_t cnick_len = strlen(self->cnick);
size_t prefix_len = cnick_len ? cnick_len + 1 : 0;
size_t amount = prefix_len + 1;
self->prefix = (char*)MALLOCATE(amount);
self->Prefix = (char*)MALLOCATE(amount);
self->PREFIX = (char*)MALLOCATE(amount);
memcpy(self->Prefix, self->cnick, cnick_len);
if (cnick_len) {
self->Prefix[cnick_len] = '_';
self->Prefix[cnick_len + 1] = '\0';
}
else {
self->Prefix[cnick_len] = '\0';
}
for (size_t i = 0; i < amount; i++) {
self->prefix[i] = tolower(self->Prefix[i]);
self->PREFIX[i] = toupper(self->Prefix[i]);
}
self->prefix[prefix_len] = '\0';
self->Prefix[prefix_len] = '\0';
self->PREFIX[prefix_len] = '\0';
return self;
}
示例6: S_set_prereqs
static void
S_set_prereqs(CFCParcel *self, CFCJson *node, const char *path) {
size_t num_prereqs = CFCJson_get_num_children(node) / 2;
CFCJson **children = CFCJson_get_children(node);
CFCPrereq **prereqs
= (CFCPrereq**)MALLOCATE((num_prereqs + 1) * sizeof(CFCPrereq*));
for (size_t i = 0; i < num_prereqs; ++i) {
const char *name = CFCJson_get_string(children[2*i]);
CFCJson *value = children[2*i+1];
int value_type = CFCJson_get_type(value);
CFCVersion *version = NULL;
if (value_type == CFCJSON_STRING) {
version = CFCVersion_new(CFCJson_get_string(value));
}
else if (value_type != CFCJSON_NULL) {
CFCUtil_die("Invalid prereq value (filepath '%s')", path);
}
prereqs[i] = CFCPrereq_new(name, version);
CFCBase_decref((CFCBase*)version);
}
prereqs[num_prereqs] = NULL;
// Assume that prereqs are empty.
FREEMEM(self->prereqs);
self->prereqs = prereqs;
self->num_prereqs = num_prereqs;
}
示例7: PolySearcher_init
PolySearcher*
PolySearcher_init(PolySearcher *self, Schema *schema, VArray *searchers) {
const uint32_t num_searchers = VA_Get_Size(searchers);
int32_t *starts_array = (int32_t*)MALLOCATE(num_searchers * sizeof(int32_t));
int32_t doc_max = 0;
Searcher_init((Searcher*)self, schema);
PolySearcherIVARS *const ivars = PolySearcher_IVARS(self);
ivars->searchers = (VArray*)INCREF(searchers);
ivars->starts = NULL; // Safe cleanup.
for (uint32_t i = 0; i < num_searchers; i++) {
Searcher *searcher
= (Searcher*)CERTIFY(VA_Fetch(searchers, i), SEARCHER);
Schema *candidate = Searcher_Get_Schema(searcher);
Class *orig_class = Schema_Get_Class(schema);
Class *candidate_class = Schema_Get_Class(candidate);
// Confirm that searchers all use the same schema.
if (orig_class != candidate_class) {
THROW(ERR, "Conflicting schemas: '%o', '%o'",
Schema_Get_Class_Name(schema),
Schema_Get_Class_Name(candidate));
}
// Derive doc_max and relative start offsets.
starts_array[i] = (int32_t)doc_max;
doc_max += Searcher_Doc_Max(searcher);
}
ivars->doc_max = doc_max;
ivars->starts = I32Arr_new_steal(starts_array, num_searchers);
return self;
}
示例8: Folder_slurp_file
ByteBuf*
Folder_slurp_file(Folder *self, const CharBuf *path) {
InStream *instream = Folder_Open_In(self, path);
ByteBuf *retval = NULL;
if (!instream) {
RETHROW(INCREF(Err_get_error()));
}
else {
uint64_t length = InStream_Length(instream);
if (length >= SIZE_MAX) {
InStream_Close(instream);
DECREF(instream);
THROW(ERR, "File %o is too big to slurp (%u64 bytes)", path,
length);
}
else {
size_t size = (size_t)length;
char *ptr = (char*)MALLOCATE((size_t)size + 1);
InStream_Read_Bytes(instream, ptr, size);
ptr[size] = '\0';
retval = BB_new_steal_bytes(ptr, size, size + 1);
InStream_Close(instream);
DECREF(instream);
}
}
return retval;
}
示例9: Freezer_deserialize_blob
Blob*
Freezer_deserialize_blob(Blob *blob, InStream *instream) {
size_t size = InStream_Read_C32(instream);
char *buf = (char*)MALLOCATE(size);
InStream_Read_Bytes(instream, buf, size);
return Blob_init_steal(blob, buf, size);
}
示例10: S_flattened_but_empty_spans
// Create all the spans needed by HeatMap_Flatten_Spans, based on the source
// offsets and lengths... but leave the scores at 0.
static Vector*
S_flattened_but_empty_spans(Vector *spans) {
const size_t num_spans = Vec_Get_Size(spans);
int32_t *bounds = (int32_t*)MALLOCATE((num_spans * 2) * sizeof(int32_t));
// Assemble a list of all unique start/end boundaries.
for (size_t i = 0; i < num_spans; i++) {
Span *span = (Span*)Vec_Fetch(spans, i);
bounds[i] = Span_Get_Offset(span);
bounds[i + num_spans] = Span_Get_Offset(span) + Span_Get_Length(span);
}
qsort(bounds, num_spans * 2, sizeof(int32_t), S_compare_i32);
size_t num_bounds = 0;
int32_t last = INT32_MAX;
for (size_t i = 0; i < num_spans * 2; i++) {
if (bounds[i] != last) {
bounds[num_bounds++] = bounds[i];
last = bounds[i];
}
}
// Create one Span for each zone between two bounds.
Vector *flattened = Vec_new(num_bounds - 1);
for (size_t i = 0; i < num_bounds - 1; i++) {
int32_t start = bounds[i];
int32_t length = bounds[i + 1] - start;
Vec_Push(flattened, (Obj*)Span_new(start, length, 0.0f));
}
FREEMEM(bounds);
return flattened;
}
示例11: S_init_arena
static void
S_init_arena(MemoryPool *self, size_t amount) {
ByteBuf *bb;
// Indicate which arena we're using at present.
self->tick++;
if (self->tick < (int32_t)VA_Get_Size(self->arenas)) {
// In recycle mode, use previously acquired memory.
bb = (ByteBuf*)VA_Fetch(self->arenas, self->tick);
if (amount >= BB_Get_Size(bb)) {
BB_Grow(bb, amount);
BB_Set_Size(bb, amount);
}
}
else {
// In add mode, get more mem from system.
size_t buf_size = (amount + 1) > self->arena_size
? (amount + 1)
: self->arena_size;
char *ptr = (char*)MALLOCATE(buf_size);
bb = BB_new_steal_bytes(ptr, buf_size - 1, buf_size);
VA_Push(self->arenas, (Obj*)bb);
}
// Recalculate consumption to take into account blocked off space.
self->consumed = 0;
for (int32_t i = 0; i < self->tick; i++) {
ByteBuf *bb = (ByteBuf*)VA_Fetch(self->arenas, i);
self->consumed += BB_Get_Size(bb);
}
self->buf = BB_Get_Buf(bb);
self->limit = self->buf + BB_Get_Size(bb);
}
示例12: CFCPerlSub_init
CFCPerlSub*
CFCPerlSub_init(CFCPerlSub *self, CFCParamList *param_list,
const char *class_name, const char *alias,
int use_labeled_params) {
CFCUTIL_NULL_CHECK(param_list);
CFCUTIL_NULL_CHECK(class_name);
CFCUTIL_NULL_CHECK(alias);
self->param_list = (CFCParamList*)CFCBase_incref((CFCBase*)param_list);
self->class_name = CFCUtil_strdup(class_name);
self->alias = CFCUtil_strdup(alias);
self->use_labeled_params = use_labeled_params;
self->perl_name = CFCUtil_sprintf("%s::%s", class_name, alias);
size_t c_name_len = strlen(self->perl_name) + sizeof("XS_") + 1;
self->c_name = (char*)MALLOCATE(c_name_len);
int j = 3;
memcpy(self->c_name, "XS_", j);
for (int i = 0, max = (int)strlen(self->perl_name); i < max; i++) {
char c = self->perl_name[i];
if (c == ':') {
while (self->perl_name[i + 1] == ':') { i++; }
self->c_name[j++] = '_';
}
else {
self->c_name[j++] = c;
}
}
self->c_name[j] = 0; // NULL-terminate.
return self;
}
示例13: S_camel_to_lower
static char*
S_camel_to_lower(const char *camel) {
if (camel[0] == '\0') { return CFCUtil_strdup(""); }
size_t alloc = 1;
for (size_t i = 1; camel[i]; i++) {
if (CFCUtil_isupper(camel[i]) && CFCUtil_islower(camel[i+1])) {
alloc += 1;
}
alloc += 1;
}
char *lower = (char*)MALLOCATE(alloc + 1);
lower[0] = CFCUtil_tolower(camel[0]);
size_t j = 1;
for (size_t i = 1; camel[i]; i++) {
// Only insert underscore if next char is lowercase.
if (CFCUtil_isupper(camel[i]) && CFCUtil_islower(camel[i+1])) {
lower[j++] = '_';
}
lower[j++] = CFCUtil_tolower(camel[i]);
}
lower[j] = '\0';
return lower;
}
示例14: 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;
}
示例15: S_flattened_but_empty_spans
// Create all the spans needed by HeatMap_Flatten_Spans, based on the source
// offsets and lengths... but leave the scores at 0.
static VArray*
S_flattened_but_empty_spans(VArray *spans)
{
const uint32_t num_spans = VA_Get_Size(spans);
int32_t *bounds = (int32_t*)MALLOCATE((num_spans * 2) * sizeof(int32_t));
// Assemble a list of all unique start/end boundaries.
for (uint32_t i = 0; i < num_spans; i++) {
Span *span = (Span*)VA_Fetch(spans, i);
bounds[i] = span->offset;
bounds[i + num_spans] = span->offset + span->length;
}
Sort_quicksort(bounds, num_spans * 2, sizeof(uint32_t),
S_compare_i32, NULL);
uint32_t num_bounds = 0;
int32_t last = I32_MAX;
for (uint32_t i = 0; i < num_spans * 2; i++) {
if (bounds[i] != last) {
bounds[num_bounds++] = bounds[i];
last = bounds[i];
}
}
// Create one Span for each zone between two bounds.
VArray *flattened = VA_new(num_bounds - 1);
for (uint32_t i = 0; i < num_bounds - 1; i++) {
int32_t start = bounds[i];
int32_t length = bounds[i + 1] - start;
VA_Push(flattened, (Obj*)Span_new(start, length, 0.0f));
}
FREEMEM(bounds);
return flattened;
}