void* corto_string_deserAllocElem(void *ptr, corto_string_deser_t *data) { corto_collection t = corto_collection(data->allocUdata); corto_int32 size = corto_type_sizeof(t->elementType); void *result = NULL; if (ptr) { switch(t->kind) { case CORTO_SEQUENCE: { corto_objectseq *seq = ptr; /* Use random built-in sequence type */ seq->buffer = corto_realloc(seq->buffer, (data->current + 1) * size); seq->length = data->current + 1; ptr = seq->buffer; memset(CORTO_OFFSET(ptr, size * data->current), 0, size); } case CORTO_ARRAY: result = CORTO_OFFSET(ptr, size * data->current); break; case CORTO_LIST: { corto_ll list = *(corto_ll*)ptr; if (corto_collection_requiresAlloc(t->elementType)) { result = corto_calloc(size); corto_ll_append(list, result); } else { corto_ll_append(list, NULL); result = corto_ll_getPtr(list, corto_ll_size(list) - 1); } break; default: break; } } } return result; }
void cdiff_file_write( cdiff_file file, char *fmt, ...) { corto_assert(file != NULL, "NULL specified for file"); if (file->newLine && file->indent) { int i; for (i = 0; i < file->indent * 4; i++) { corto_buffer_appendstrn(&file->writeBuffer, " ", 1); } } va_list args; va_start(args, fmt); corto_buffer_vappend(&file->writeBuffer, fmt, args); va_end(args); /* If no element specified for writing, write to new text element. */ if (!file->cur && file->isNew) { cdiff_elem *el = corto_calloc(sizeof(cdiff_elem)); el->kind = CDIFF_TEXT; el->body = corto_buffer_str(&file->writeBuffer); if (!file->elements) { file->elements = corto_ll_new(); } corto_ll_append(file->elements, el); file->writeBuffer = CORTO_BUFFER_INIT; } file->newLine = fmt[strlen(fmt) - 1] == '\n'; }
void* json_deser_allocElem(void *ptr, corto_collection t, corto_int32 i) { corto_int32 size = corto_type_sizeof(t->elementType); void *result = NULL; switch(t->kind) { case CORTO_SEQUENCE: { corto_objectseq *seq = ptr; /* Use random built-in sequence type */ seq->buffer = corto_realloc(seq->buffer, (i + 1) * size); seq->length = i + 1; ptr = seq->buffer; memset(CORTO_OFFSET(ptr, size * i), 0, size); } case CORTO_ARRAY: result = CORTO_OFFSET(ptr, size * i); break; case CORTO_LIST: { corto_ll list = *(corto_ll*)ptr; if (corto_collection_requiresAlloc(t->elementType)) { result = corto_calloc(size); corto_llAppend(list, result); } else { corto_llAppend(list, NULL); result = corto_llGetPtr(list, corto_llSize(list) - 1); } break; default: break; } } return result; }
/* This mount implements custom next and hasNext functions to ensure that only * the record that is iterated over is stored in memory. */ corto_iter onRequest(corto_object mount, corto_request *request) { corto_iter result; /* Create context data for the iterator */ struct iterData *ctx = corto_calloc(sizeof(struct iterData)); /* Support queries for a single number or starting from an offset */ if (isnumber(request->expr)) ctx->current = atoi(request->expr); else ctx->current = request->offset; /* Preset id and value to reusable buffers in the iterData structure */ ctx->result.id = ctx->id; /* Set values that will be the same for every result */ ctx->result.parent = "."; ctx->result.type = "uint64"; ctx->result.leaf = TRUE; result.udata = ctx; /* Set iterator callbacks */ result.hasNext = hasNext; result.next = next; result.release = release; return result; }
/* Add file */ static struct corto_loadedAdmin* corto_loadedAdminAdd(corto_string library) { struct corto_loadedAdmin *lib = corto_calloc(sizeof(struct corto_loadedAdmin)); lib->name = corto_strdup(library); lib->loading = corto_threadSelf(); if (!loadedAdmin) { loadedAdmin = corto_ll_new(); } corto_ll_insert(loadedAdmin, lib); return lib; }
static corto_selectData* corto_selectDataGet(void) { corto_selectData* result; result = corto_threadTlsGet(CORTO_KEY_SELECT); if (!result) { result = corto_calloc(sizeof(corto_selectData)); } return result; }
void* corto_string_deserAllocAny(void *ptr, corto_string_deser_t *data) { void *result = NULL; if (!data->current) { result = ptr; } else { corto_any *a = ptr; corto_type t = a->type; struct corto_string_deserIndexInfo *anyNode = data->allocUdata; anyNode->type = t; a->owner = TRUE; if (t->kind == CORTO_PRIMITIVE) { a->value = corto_calloc(corto_type_sizeof(t)); result = a->value; } else { result = &a->value; } } return result; }
static corto_int16 json_deserComposite(void* p, corto_type t, JSON_Value *v) { corto_assert(t->kind == CORTO_COMPOSITE, "not deserializing composite"); if (json_value_get_type(v) != JSONObject) { corto_seterr("expected object, got %s", json_valueTypeToString(v)); goto error; } JSON_Object* o = json_value_get_object(v); size_t count = json_object_get_count(o); size_t i; corto_bool isUnion = corto_interface(t)->kind == CORTO_UNION; corto_int32 discriminator = 0; corto_member unionMember = NULL; for (i = 0; i < count; i++) { const char* memberName = json_object_get_name(o, i); corto_member member_o; if (!strcmp(memberName, "super")) { JSON_Value* value = json_object_get_value(o, memberName); if (json_deserItem(p, corto_type(corto_interface(t)->base), value)) { goto error; } } else if (!strcmp(memberName, "_d") && isUnion) { JSON_Value* value = json_object_get_value(o, memberName); if (json_deserPrimitive(&discriminator, corto_union(t)->discriminator, value)) { goto error; } unionMember = corto_union_findCase(t, discriminator); if (!unionMember) { corto_seterr("discriminator '%d' invalid for union '%s'", discriminator, corto_fullpath(NULL, t)); } } else { member_o = corto_interface_resolveMember(t, (char*)memberName); /* Ensure that we're not resolving members from a base type */ if (!member_o || (corto_parentof(member_o) != t)) { corto_seterr( "cannot find member '%s' in type '%s'", memberName, corto_fullpath(NULL, t)); goto error; } if (isUnion && (unionMember != member_o)) { corto_seterr( "member '%s' does not match discriminator '%d' (expected member '%s')", memberName, discriminator, corto_idof(unionMember)); goto error; } else if (isUnion) { corto_int32 prev = *(corto_int32*)p; if (prev != discriminator) { corto_member prevMember = corto_union_findCase(t, prev); corto_deinitp(CORTO_OFFSET(p, prevMember->offset), prevMember->type); memset(CORTO_OFFSET(p, member_o->offset), 0, member_o->type->size); } *(corto_int32*)p = discriminator; } if (!json_deserMustSkip(member_o, p)) { JSON_Value* value = json_object_get_value(o, memberName); void *offset = CORTO_OFFSET(p, member_o->offset); if (member_o->modifiers & CORTO_OBSERVABLE) { offset = *(void**)offset; if (json_deserType(offset, member_o->type, value)) { goto error; } } else { if (member_o->modifiers & CORTO_OPTIONAL) { if (*(void**)offset) { corto_deinitp(*(void**)offset, member_o->type); memset(*(void**)offset, 0, member_o->type->size); } else { *(void**)offset = corto_calloc(member_o->type->size); } offset = *(void**)offset; } if (json_deserItem(offset, member_o->type, value)) { goto error; } } } } } return 0; error: return -1; }