Bool bfqCreate ( BufferQueue_Handle queue, BufferQueue_Frame * frames, Int BufCount, Int BufSize, Int SegID) { Int i; Ptr p; // Acquire the frames queue->Frames = frames; queue->bDynamicFrames = FALSE; // Init the QUE and SEM objects. SEM_new(&(queue->semFullBuffers), 0); SEM_new(&(queue->semEmptyBuffers), BufCount); QUE_new( &(queue->queFullBuffers) ); QUE_new( &(queue->queEmptyBuffers) ); QUE_new( &(queue->queEmptyFrames) ); for (i=0; i<BufCount; i++) { // TODO: handle errors. // If the _BFQ_INIT_BUFFERS is defined, fill the buffers with 0xFF, // leave them uninitialized, otherwise. #ifdef _BFQ_INIT_BUFFERS p = MEM_valloc( SegID, BufSize, 0, 0xFF ); queue->Frames[i].Buffer = p; assertLog( p != MEM_ILLEGAL); #else p = MEM_alloc( SegID, BufSize, 0 ); queue->Frames[i].Buffer = p; assertLog( p != MEM_ILLEGAL); #endif QUE_put(&(queue->queEmptyBuffers), &(queue->Frames[i])); } queue->BufSize = BufSize; queue->BufCount = BufCount; queue->SegId = SegID; return TRUE; }
/* * ======== MEM_alloc ======== */ Void * MEM_alloc(Int segid, size_t size, size_t align) { return (MEM_valloc(segid, size, align, 0)); }
/* * ======== DOV_open ======== */ static Int DOV_open(DEV_Handle device, String name) { DOV_CopyObj *copy; DEV_Device *entry; Int status = SYS_EALLOC; DEV_Frame *frame; size_t size; if (device->mode != DEV_INPUT) { return (SYS_EINVAL); } /* * If devid is nonzero, it holds the 'size' of the overlap buffer. */ if (device->devid > 0) { size = device->devid; } else { size = atoi(name); /* * Skip the numeric characters to get to the underlying * device's name. */ while (isdigit(*name)) { name++; } } if (size <= 0 || size >= device->bufsize) { return (SYS_EINVAL); } /* * find underlying device in device table */ name = DEV_match(name, &entry); if (entry == NULL) { return (SYS_ENODEV); } /* allocate copy object */ if ((copy = MEM_alloc(0, sizeof(DOV_CopyObj), 0)) == MEM_ILLEGAL) { return (SYS_EALLOC); } copy->size = size; /* allocate and initialize overlap buffer */ if ((copy->overlap = MEM_valloc(0, size, 0, DOV->INITIAL)) == MEM_ILLEGAL) { goto e1; } copy->dobj = *device; /* copy descriptor fields */ copy->dobj.fxns = *(DEV_Fxns *)(entry->fxns); copy->dobj.devid = entry->devid; copy->dobj.params = entry->params; /* size of underlying buffers */ copy->dobj.bufsize = device->bufsize - size; /* * create queues and frames for underlying device. */ if ((copy->dobj.todevice = QUE_create(NULL)) == NULL) { goto e2; } if ((copy->dobj.fromdevice = QUE_create(NULL)) == NULL) { goto e3; } /* * adjust frame size and address according to the overlap size before * copying frames to underlying device's 'todevice' queue */ while (!QUE_empty(device->todevice)) { frame = QUE_get(device->todevice); frame->size = frame->size - size; frame->addr = (Char *)frame->addr + size; QUE_put(copy->dobj.todevice, frame); } /* open underlying device */ if ((status = DEV_open((©->dobj), name)) != SYS_OK) { goto e4; } device->object = (Ptr)copy; return (SYS_OK); /* all is well */ /* free memory and return error code */ e4: QUE_delete(copy->dobj.fromdevice); e3: QUE_delete(copy->dobj.todevice); e2: MEM_free(0, copy->overlap, copy->size); e1: MEM_free(0, copy, sizeof(DOV_CopyObj)); return (status); }
/* * ======== MEM_calloc ======== */ Ptr MEM_calloc(Int id, size_t size, size_t align) { return (MEM_valloc(id, size, align, 0)); }