static blob_list_ty * ifndef(blob_list_ty *blp, string_list_ty *ref) { blob_ty *arg; blob_list_ty *result; blob_ty *bp; arg = blp->list[0]; result = ifdef(blp, ref); bp = blob_alloc(str_from_c("[not"), arg->file_name, arg->line_number); blob_list_prepend(result, bp); bp = blob_alloc(str_from_c("]"), arg->file_name, arg->line_number); blob_list_append(result, bp); return result; }
int sys_malloc(int wantreturn, userptr_t retval, int nargs, primitive_val* args){ if (nargs != 1){ say(BAD_SYSCALL_ARGS, "malloc called with wrong number of arguments"); return 0; } //FIXME: strictly speaking, malloc is called with a size_t, not an int //so the following is wrong if (args[0].type != PS_INT){ say(BAD_SYSCALL_ARGS, "malloc called with non-int argument"); return 0; } if (!args[0].valid){ say(DUBIOUS_SYSCALL_INVOCATION, "argument to malloc appears invalid - unknown amount of memory allocated"); } REPTYPE(PS_INT) bytes = USERDATA_PART(args[0].value, PS_INT); if (!wantreturn){ sayf(DUBIOUS_SYSCALL_INVOCATION, "return value of malloc ignored - memory leak of %d bytes", (int)bytes); return 1;//this is OK, but stupid }else if (bytes < 0){ say(BAD_SYSCALL_ARGS, "malloc called with negative argument"); return 0; }else{ sayf(DEBUG, "mallocing %d bytes", bytes); primitive_val newmem; newmem.valid = 1; USERDATA_PART(newmem.value,PT_PTR) = pointer_to_blob(blob_alloc(bytes,0,NULL),0); newmem.type = PT_PTR; return pointer_assign(retval, newmem); } }
/* * Convert a string variable, in the format of blob2string(), to a blob. * Return NULL when conversion failed. */ blob_T * string2blob(char_u *str) { blob_T *blob = blob_alloc(); char_u *s = str; if (*s != '[') goto failed; s = skipwhite(s + 1); while (*s != ']') { if (s[0] != '0' || s[1] != 'x' || !vim_isxdigit(s[2]) || !vim_isxdigit(s[3])) goto failed; ga_append(&blob->bv_ga, (hex2nr(s[2]) << 4) + hex2nr(s[3])); s += 4; if (*s == ',') s = skipwhite(s + 1); else if (*s != ']') goto failed; } s = skipwhite(s + 1); if (*s != NUL) goto failed; // text after final ']' ++blob->bv_refcount; return blob; failed: blob_free(blob); return NULL; }
char *buffer_read_string(uint32_t len) { char *s; blob_free(blob_pool.size); s = blob_pointer(blob_alloc(len + 1)); s[fread(s, 1, len, infile)] = '\0'; return ferror(infile) ? NULL : s; }
/* * Allocate an empty blob for a return value, with reference count set. * Returns OK or FAIL. */ int rettv_blob_alloc(typval_T *rettv) { blob_T *b = blob_alloc(); if (b == NULL) return FAIL; rettv_blob_set(rettv, b); return OK; }
void *malloc(size_t size) { void *ptr = NULL; if (size != 0) { if (size <= bucket_get_record_size(BUCKET_TYPE_MAX)) { ptr = record_alloc(size); } else { ptr = blob_alloc(size); } } dprint("DEBUG: malloc(%zu) = %p\n", size, ptr); return ptr; }
static blob_list_ty * ifdef(blob_list_ty *blp, string_list_ty *ref) { blob_ty *bp; blob_list_ty *result; size_t j; bp = blp->list[0]; result = blob_list_alloc(); blob_list_append ( result, blob_alloc(str_from_c("[defined"), bp->file_name, bp->line_number) ); for (j = 1; j < blp->length; ++j) variable_rename(blp->list[j], result, ref, VAREN_QUOTE_SPACES); blob_list_append ( result, blob_alloc(str_from_c("]"), bp->file_name, bp->line_number) ); return result; }
blob_ty * lex_blob(string_ty *s) { return blob_alloc(s, input_filename(input), line_number - first); }
int gram_lex(void) { static char *paren; static long paren_max; long paren_depth; int c; long linum; int bol_was; int first_was; string_ty *s; int token; int start_of_line = 0; trace(("gram_lex()\n{\n")); for (;;) { linum = line_number; bol_was = bol; first_was = first; c = byte(); switch (c) { case INPUT_EOF: token = 0; goto done; case '\t': if (!bol_was || within_define) continue; sa_open(); for (;;) { c = byte(); switch (c) { case INPUT_EOF: case '\n': break; case ' ': case '\t': case '\f': #if __STDC__ >= 1 case '\v': #endif if (sa_data_length) sa_char(c); continue; default: sa_char(c); continue; } break; } gram_lval.lv_line = blob_alloc(sa_close(), input_filename(input), linum); token = COMMAND; goto done; case '#': sa_open(); more_comment: start_of_line = 1; for (;;) { c = byte(); switch (c) { case INPUT_EOF: case '\n': break; case '#': if (!start_of_line) sa_char(c); continue; case ' ': case '\t': case '\f': #if __STDC__ >= 1 case '\v': #endif if (!start_of_line) sa_char(' '); continue; default: sa_char(c); start_of_line = 0; continue; } break; } if (!first_was) { /* * If the comment did not start at the * beginning of the line, throw it away. */ byte_undo('\n'); continue; } token = COMMENT; if (c == '\n') { /* * Take a peek at the next character. * If it is '#', we have more comment. * If it is '\t', we have a code comment. */ c = byte(); if (c == '#') { sa_char('\n'); goto more_comment; } if (c == '\t') token = COMMAND_COMMENT; byte_undo(c); /* need to restore this state, too */ bol = 1; first = 1; colon_special = 1; } gram_lval.lv_line = blob_alloc(sa_close(), input_filename(input), linum); goto done; case ' ': case '\f': #if __STDC__ >= 1 case '\v': #endif break; case '\n': token = EOLN; goto done; case ';': if (!colon_special) goto normal; byte_undo('\t'); bol = 1; first = 1; colon_special = 1; token = EOLN; goto done; case ':': if (!colon_special) goto normal; c = byte(); if (c == ':') { token = COLON_COLON; goto done; } if (c == '=') { token = COLON_EQUALS; colon_special = 0; goto done; } byte_undo(c); token = COLON; goto done; case '=': token = EQUALS; colon_special = 0; goto done; case '+': c = byte(); if (c == '=') { token = PLUS_EQUALS; colon_special = 0; goto done; } byte_undo(c); c = '+'; /* fall through... */ default: normal: sa_open(); paren_depth = 0; for (;;) { switch (c) { case INPUT_EOF: case '\n': break; case ' ': case '\t': case '\f': #if __STDC__ >= 1 case '\v': #endif if (!within_define && !paren_depth) break; sa_char(c); c = byte(); continue; case ';': case ':': case '=': if (colon_special && !within_define && !paren_depth) break; sa_char(c); c = byte(); continue; default: sa_char(c); c = byte(); continue; case '(': sa_char(c); if (paren_depth >= paren_max) { paren_max = paren_max * 2 + 16; paren = mem_change_size(paren, paren_max); } paren[paren_depth++] = ')'; c = byte(); continue; case ')': case '}': sa_char(c); if (paren_depth && c == paren[paren_depth - 1]) --paren_depth; c = byte(); continue; case '{': sa_char(c); if (paren_depth >= paren_max) { paren_max = paren_max * 2 + 16; paren = mem_change_size(paren, paren_max); } paren[paren_depth++] = '}'; c = byte(); continue; } break; } byte_undo(c); s = sa_close(); if (first_was && (token = reserved(s)) != 0) { switch (token) { case DEFINE: str_free(s); ++within_define; break; case ENDDEF: str_free(s); --within_define; break; case IF: gram_lval.lv_line = blob_alloc(s, input_filename(input), linum); break; case VPATH: colon_special = 0; break; default: str_free(s); break; } goto done; } gram_lval.lv_line = blob_alloc(s, input_filename(input), linum); token = WORD; goto done; } } /* * here for all exits */ done: #ifdef DEBUG if (token == WORD || token == COMMENT || token == COMMAND) trace(("text = \"%s\";\n", gram_lval.lv_line->text->str_text)); #endif trace(("return %d;\n", token)); trace(("}\n")); return token; }
static blob_list_ty * ifeq(blob_list_ty *blp, string_list_ty *ref) { blob_ty *arg; blob_ty *bp; blob_list_ty *result; string_ty *s; string_ty *s2; char *cp; size_t j; /* * allocate the result list */ trace(("ifeq()\n{\n")); result = blob_list_alloc(); /* * make sure we were given enough arguments */ if (blp->length < 2) { arg = blp->list[0]; blob_list_append ( result, blob_alloc(str_from_c("0"), arg->file_name, arg->line_number) ); trace(("}\n")); return result; } /* * turn the list of arguments into a single string */ arg = blp->list[1]; s = str_copy(blp->list[1]->text); for (j = 2; j < blp->length; ++j) { s2 = str_format("%s %s", s->str_text, blp->list[j]->text->str_text); str_free(s); s = s2; } bp = blob_alloc(s, arg->file_name, arg->line_number); /* * rename the variables * and reform to be a single string, again. */ variable_rename(bp, result, ref, VAREN_NO_QUOQUO); blob_free(bp); s = result->length ? str_copy(result->list[0]->text) : str_from_c("0"); for (j = 1; j < result->length; ++j) { s2 = str_format("%s %s", s->str_text, result->list[j]->text->str_text); str_free(s); s = s2; } blob_list_free(result); /* * construct the result */ result = blob_list_alloc(); switch (s->str_text[0]) { case '(': /* * ifeq (xxx,yyy) */ if (s->str_length < 3) goto useless; cp = strchr(s->str_text, ','); if (cp == 0 || s->str_text[s->str_length - 1] != ')') goto useless; blob_list_append ( result, blob_alloc(str_from_c("[in"), arg->file_name, arg->line_number) ); s2 = str_n_from_c(s->str_text + 1, cp - s->str_text - 1); if (s2->str_length == 0) s2 = str_from_c("\"\""); bp = blob_alloc(s2, arg->file_name, arg->line_number); blob_list_append(result, bp); s2 = str_n_from_c(cp + 1, s->str_text + s->str_length - cp - 2); if (s2->str_length == 0) s2 = str_from_c("\"\""); bp = blob_alloc(s2, arg->file_name, arg->line_number); blob_list_append(result, bp); blob_list_append ( result, blob_alloc(str_from_c("]"), arg->file_name, arg->line_number) ); break; case '\'': case '"': /* * ifeq "xxx" "yyy" */ if (s->str_length < 5) goto useless; cp = strchr(s->str_text + 1, s->str_text[0]); if ( !cp || cp[1] != ' ' || cp[2] != s->str_text[0] || s->str_text[s->str_length - 1] != s->str_text[0] ) goto useless; blob_list_append ( result, blob_alloc(str_from_c("[in"), arg->file_name, arg->line_number) ); s2 = str_n_from_c(s->str_text + 1, cp - s->str_text - 1); if (s2->str_length == 0) s2 = str_from_c("\"\""); bp = blob_alloc(s2, arg->file_name, arg->line_number); blob_list_append(result, bp); s2 = str_n_from_c(cp + 3, s->str_text + s->str_length - cp - 4); if (s2->str_length == 0) s2 = str_from_c("\"\""); bp = blob_alloc(s2, arg->file_name, arg->line_number); blob_list_append(result, bp); blob_list_append ( result, blob_alloc(str_from_c("]"), arg->file_name, arg->line_number) ); break; default: /* * We were given some useless thing, just rename the * variables and copy it through. */ useless: bp = blob_alloc(str_copy(s), arg->file_name, arg->line_number); blob_list_append(result, bp); break; } str_free(s); trace(("}\n")); return result; }
int jadvacOpenFile_cue(jadvacContext* ctx) { Blob* currBlob = NULL; int retval = JAD_ERROR; int c = 0, ret, digits; int pregap_lba = -1; CueLexer _L = {0}, *L = &_L; struct { StringBuffer performer, songwriter, title, catalog, isrc, cdtextfile; } meta; //this might be getting a little large for a stack so... TrackInfo* tracks = ctx->allocator->alloc(ctx->allocator,sizeof(TrackInfo)*100); int tracknum = -1; L->allocator = ctx->allocator; L->lastc = '\n'; L->stream = ctx->stream; sb_init(L,&L->buffer); sb_init(L,&meta.performer); sb_init(L,&meta.songwriter); sb_init(L,&meta.title); sb_init(L,&meta.catalog); sb_init(L,&meta.isrc); sb_init(L,&meta.cdtextfile); memset(tracks,0,sizeof(struct TrackInfo)*100); LOOP: READ(); READTOKEN(); //commands that dont matter much if(CHECKTOKEN("PERFORMER")) { READSTRING(); sb_clone(L, &L->buffer, &meta.performer); goto ENDLINE; } if(CHECKTOKEN("SONGWRITER")) { READSTRING(); sb_clone(L, &L->buffer, &meta.songwriter); goto ENDLINE; } if(CHECKTOKEN("TITLE")) { READSTRING(); sb_clone(L, &L->buffer, &meta.title); goto ENDLINE; } if(CHECKTOKEN("CATALOG")) { READSTRING(); sb_clone(L, &L->buffer, &meta.catalog); goto ENDLINE; } if(CHECKTOKEN("ISRC")) { READSTRING(); sb_clone(L, &L->buffer, &meta.isrc); goto ENDLINE; } if(CHECKTOKEN("CDTEXTFILE")) { READSTRING(); sb_clone(L, &L->buffer, &meta.cdtextfile); goto ENDLINE; } if(CHECKTOKEN("REM")) { READLINE(); goto LOOP; } if(CHECKTOKEN("FILE")) { jadStream stream; READSTRING(); ret = ctx->fs->open(ctx->fs,&stream,L->buffer.buf); if(ret) { retval = ret; goto EXIT; } READTOKEN(); //file type if(CHECKTOKEN("BINARY")) {} else BOMB("Invalid file type"); //currently only binary files are allowed, since we don't have the audio decoding framework in //ok, startup the new file currBlob = blob_alloc(ctx->allocator); currBlob->stream = stream; currBlob->type = FileType_Binary; goto ENDLINE; } if(CHECKTOKEN("PREGAP")) { int m, s, f; if(tracknum == -1) BOMB("Invalid INDEX command\n"); READMSF(); tracks[tracknum].pregap = m*60*75+s*75+f; } if(CHECKTOKEN("POSTGAP")) { int m, s, f; if(tracknum == -1) BOMB("Invalid INDEX command\n"); READMSF(); tracks[tracknum].postgap = m*60*75+s*75+f; } if(CHECKTOKEN("INDEX")) { int indexnum, m, s, f; if(tracknum == -1) BOMB("Invalid INDEX command\n"); READDIGITS(); indexnum = digits; READMSF(); tracks[tracknum].indexes[indexnum].exists = 1; tracks[tracknum].lba = m*60*75+s*75+f; } if(CHECKTOKEN("TRACK")) { READDIGITS(); tracknum = digits; READTOKEN(); //track type if(CHECKTOKEN("AUDIO")) tracks[tracknum].type = TrackType_Audio; else if(CHECKTOKEN("MODE1/2352")) tracks[tracknum].type = TrackType_Mode1_2352; else BOMB("Invalid track type"); //if we have an unassociated blob, now's the time to associte it tracks[tracknum].newBlob = currBlob; currBlob = NULL; goto ENDLINE; } ENDLINE: READLINE(); goto LOOP; retval = JAD_OK; EXIT: //todo: unassociated blob sb_finalize(L,&meta.performer); sb_finalize(L,&meta.songwriter); sb_finalize(L,&meta.title); sb_finalize(L,&meta.catalog); sb_finalize(L,&meta.isrc); sb_finalize(L,&meta.cdtextfile); sb_finalize(L,&L->buffer); ctx->allocator->free(ctx->allocator,tracks); return retval; ERROR: retval = JAD_ERROR; goto EXIT; }