int main(int argc, char *argv[]) { const int numWrites = 4, numReads = 10, numBufs = 3, maxN = 20; int i,j; data_t datum; bool_t shouldReset; bool_t datumOut; buf_t **bufs = (buf_t **)malloc(numBufs * sizeof(buf_t *)); for (i=0; i<numBufs; i++) bufs[i] = bufAlloc(maxN); for (i=0; i<numWrites; i++) { for (j=0; j<numBufs; j++) bufWrite(bufs[j], randomIdx(bufs[j]), (data_t)__VERIFIER_nondet_int()); } for (i=0; i<numReads; i++) { for (j=0; j<numBufs; j++) { datum = (data_t) __VERIFIER_nondet_int(); shouldReset = __VERIFIER_nondet_int(); datumOut = (data_t)0; if (shouldReset) bufReset(bufs[j], datum); else datumOut = bufRead(bufs[j], randomIdx(bufs[j])); } } return 1; }
bool BufAllocCmd::Stage2() { SndBuf *buf = World_GetNRTBuf(mWorld, mBufIndex); mFreeData = buf->data; bufAlloc(buf, mNumChannels, mNumFrames, mWorld->mFullRate.mSampleRate); mSndBuf = *buf; return true; }
static void httpRead(App *app, HTTP *http, int sock) { struct timeval theTime; app->status(app, "httpRead", __FILE__, __LINE__); gettimeofday(&theTime, NULL); http->in = bufAlloc(sock); httpParseStream(http, app, http->url->url); app->time(app, appTimeReadStream, &theTime); app->status(app, "httpRead done", __FILE__, __LINE__); }
int main(int argc, char *argv[]) { const int numWrites = 4, numReads = 10, numBufs = 3, maxN = 20; int i,j; data_t datum; bool_t shouldReset; bool_t datumOut; buf_t **bufs = (buf_t **)malloc(numBufs * sizeof(buf_t *)); for (i=0; i<numBufs; i++) bufs[i] = bufAlloc(maxN); return 1; }
bool BufAllocReadCmd::Stage2() { #ifdef NO_LIBSNDFILE SendFailure(&mReplyAddress, "/b_allocRead", "scsynth compiled without libsndfile\n"); scprintf("scsynth compiled without libsndfile\n"); return false; #else SndBuf *buf = World_GetNRTBuf(mWorld, mBufIndex); SF_INFO fileinfo; memset(&fileinfo, 0, sizeof(fileinfo)); SNDFILE* sf = sf_open(mFilename, SFM_READ, &fileinfo); if (!sf) { char str[512]; sprintf(str, "File '%s' could not be opened: %s\n", mFilename, sf_strerror(NULL)); SendFailureWithIntValue(&mReplyAddress, "/b_allocRead", str, mBufIndex); //SendFailure(&mReplyAddress, "/b_allocRead", str); scprintf(str); return false; } if (mFileOffset < 0) mFileOffset = 0; else if (mFileOffset > fileinfo.frames) mFileOffset = fileinfo.frames; if (mNumFrames <= 0 || mNumFrames + mFileOffset > fileinfo.frames) mNumFrames = fileinfo.frames - mFileOffset; // alloc data size mFreeData = buf->data; SCErr err = bufAlloc(buf, fileinfo.channels, mNumFrames, fileinfo.samplerate); if (err) goto leave; sf_seek(sf, mFileOffset, SEEK_SET); sf_readf_float(sf, buf->data, mNumFrames); leave: mSndBuf = *buf; sf_close(sf); return true; #endif }
/* ARGSUSED */ static IRES_Handle getBuf(IALG_Handle alg, IRES_ResourceDescriptor *resDesc, Int scratchId, IRES_Status *pStatus) { BUFRES_ConstructArgs cArgs; BUFRES_Handle res = NULL; IALG_MemRec memRecs[MAXRECS]; Int nRecs; Int length; Int align; Void *base = NULL; IRES_RequestMode mode; IRES_Status status = IRES_OK; IRES_Status retVal; IRES_ProtocolArgs *args = resDesc->protocolArgs; Log_print0(Diags_ENTRY, "[+E] BUFRES getBuf> entered>"); Assert_isTrue(pStatus != NULL, (Assert_Id)NULL); Assert_isTrue(args != NULL, (Assert_Id)NULL); length = ((BUFRES_Args *)args)->length; align = ((BUFRES_Args *)args)->align; mode = args->mode; /* IRES_SCRATCH or IRES_PERSISTENT */ Log_print3(Diags_ENTRY, "[+E] BUFRES getBuf> length [0x%x], " "align [0x%x], mode = %s>", (IArg)length, (IArg)align, (IArg)((mode == IRES_PERSISTENT) ? "IRES_PERSISTENT" : "IRES_SCRATCH")); if (curInit == 0) { *pStatus = IRES_ENOINIT; return (NULL); } /* TODO: acquire a lock */ /* * The buffer can be used for the resource request if it is currently * being used by another algorithm with the same scratchId, or if * the buffer is not currently being used. The memory chunk of the * requested size and alignment must also fit in the buffer. */ if (((mode == IRES_SCRATCH) && (mode == buffer.mode) && (scratchId == buffer.scratchId)) || (buffer.refCount == 0)) { if ((base = bufAlloc(0, length, align)) == NULL) { /* The requested memory will not fit in the buffer */ Log_print0(Diags_USER6, "[+6] BUFRES getBuf> buffer resource" " allocation failed."); status = IRES_ENORESOURCE; } } else { /* Buffer is in use */ status = IRES_ENORESOURCE; } if (status == IRES_OK) { /* * Found available resource, now allocate memory to construct * a BUFRES handle to pass back to the algorithm. */ /* Number of buffers needed to construct a BUFRES object */ nRecs = getNumRecs(args); if (nRecs > MAXRECS) { /* Error: Weren't expecting to allocate this many buffers */ status = IRES_EFAIL; } else { /* * Fill in memRecs[] with memory requirements to allocate the * BUFRES object. */ retVal = getMemRecs(NULL, args, memRecs); if (retVal != IRES_OK) { status = IRES_ENOMEM; } else { /* Now allocate memory used to construct the BUFRES object */ if (!allocFxn(memRecs, nRecs)) { status = IRES_ENOMEM; } } } } if (status == IRES_OK) { /* Init cArgs with whatever is necessary to construct BUFRES object */ cArgs.size = sizeof(BUFRES_ConstructArgs); cArgs.base = base; /* Initialize the buffers */ res = (BUFRES_Handle)constructHandle(args, memRecs, (IRESMAN_ConstructArgs *)&cArgs, &retVal); if (retVal != IRES_OK) { freeFxn(memRecs, nRecs); status = IRES_EFAIL; } else { /* * Increment the reference count on the buffer and set * scratchId, mode */ buffer.refCount++; buffer.scratchId = scratchId; buffer.mode = mode; resDesc->handle = (IRES_Handle)res; } } /* TODO: release lock */ *pStatus = status; Log_print1(Diags_EXIT, "[+X] BUFRES getBuf> returning handle 0x%x", (IArg)res); resDesc->handle = (IRES_Handle)res; (resDesc->revision)->Major = 1; (resDesc->revision)->Source = 0; (resDesc->revision)->Radius = 0; return ((IRES_Handle)res); }
bool BufAllocReadChannelCmd::Stage2() { #ifdef NO_LIBSNDFILE SendFailure(&mReplyAddress, "/b_allocReadChannel", "scsynth compiled without libsndfile\n"); scprintf("scsynth compiled without libsndfile\n"); return false; #else SndBuf *buf = World_GetNRTBuf(mWorld, mBufIndex); SF_INFO fileinfo; memset(&fileinfo, 0, sizeof(fileinfo)); SNDFILE* sf = sf_open(mFilename, SFM_READ, &fileinfo); if (!sf) { char str[512]; sprintf(str, "File '%s' could not be opened: %s\n", mFilename, sf_strerror(NULL)); SendFailureWithIntValue(&mReplyAddress, "/b_allocReadChannel", str, mBufIndex); //SendFailure(&mReplyAddress, "/b_allocRead", str); scprintf(str); return false; } if (mFileOffset < 0) mFileOffset = 0; else if (mFileOffset > fileinfo.frames) mFileOffset = fileinfo.frames; if (mNumFrames <= 0 || mNumFrames + mFileOffset > fileinfo.frames) mNumFrames = fileinfo.frames - mFileOffset; if (mNumChannels == 0) { // alloc data size mFreeData = buf->data; SCErr err = bufAlloc(buf, fileinfo.channels, mNumFrames, fileinfo.samplerate); if (err) goto leave; // read all channels sf_seek(sf, mFileOffset, SEEK_SET); sf_readf_float(sf, buf->data, mNumFrames); } else { // verify channel indexes if (!CheckChannels(fileinfo.channels)) { const char* str = "Channel index out of range.\n"; SendFailureWithIntValue(&mReplyAddress, "/b_allocReadChannel", str, mBufIndex); //SendFailure(&mReplyAddress, "/b_allocRead", str); scprintf(str); sf_close(sf); return false; } // alloc data size mFreeData = buf->data; SCErr err = bufAlloc(buf, mNumChannels, mNumFrames, fileinfo.samplerate); if (err) goto leave; // alloc temp buffer float* data = (float*)malloc(mNumFrames*fileinfo.channels*sizeof(float)); if (data == 0) goto leave; // read some channels sf_seek(sf, mFileOffset, SEEK_SET); sf_readf_float(sf, data, mNumFrames); CopyChannels(buf->data, data, fileinfo.channels, mNumFrames); // free temp buffer free(data); } leave: mSndBuf = *buf; sf_close(sf); return true; #endif }
static void httpGetObject(App *app, HTTP *http, int sock) { Buf *buf; HTTPNameValue *h; app->printHTML(app, "<h4>Request</h4>"); app->printHTML(app, "<pre>"); buf = bufAlloc(sock); bufMark(buf, 0); bufPutString(buf, (unsigned char *) "GET "); if (http->url->path) { bufPutString(buf, http->url->path); } if (http->url->params) { bufPutString(buf, http->url->params); } if (http->url->query) { bufPutString(buf, http->url->query); } bufPutChar(buf, ' '); bufPutString(buf, http->version); bufPutString(buf, (unsigned char *) "\r\n"); bufMark(buf, 0); app->httpRequest(app, buf); h = http->headers; if (h) { while (h->name) { httpPutHeader(app, buf, (char *) h->name, (char *) h->value); h++; } } httpPutHeader(app, buf, "Connection", "close"); httpPutHeader(app, buf, "Host", (char *) http->url->host); bufPutString(buf, (unsigned char *) "\r\n"); bufMark(buf, 0); app->httpRequest(app, buf); app->printHTML(app, "</pre>"); if (bufError(buf)) { return; } bufSend(buf); bufFree(buf); httpRead(app, http, sock); }
void httpParseStream(HTTP *http, App *app, unsigned char *url) { Buf *buf; unsigned short c; unsigned char *contentType; int chunked; int i; unsigned char *line; unsigned int size; chunked = 0; c = httpReadHeaders(http, app, http->in, url, &contentType, &chunked); if (chunked) { buf = bufAlloc(-1); while (1) { bufMark(http->in, 0); c = bufGetByte(http->in); c = readLine(http->in, c); bufMark(http->in, -1); line = bufCopy(http->in); size = 0; sscanf((char *) line, "%x", &size); free(line); if (!size) { break; } bufUnGetByte(http->in); for (i = 0; i < size; i++) { c = bufGetByte(http->in); if (c == 256) { break; } else { bufPutChar(buf, c); } } c = bufGetByte(http->in); if (c != '\r') { break; } c = bufGetByte(http->in); if (c != '\n') { break; } } bufSet(buf, 0); bufMark(buf, 0); } else { buf = http->in; } http->body = bufCurrent(buf); if (contentType) { if ( (contentType != emptyHTTPResponse) && (contentType != http09Response) && (contentType != locationURLWasAdded) ) { app->contentType(app, contentType); if (!strcasecmp((char *) contentType, "text/html")) { htmlRead(app, buf, url); } else { httpDefaultType(http, app); } free(contentType); } } else { httpDefaultType(http, app); } if (chunked) { bufFree(buf); } app->printHTML(app, "</pre>"); }