/*********************************************************** * Name: software_fifo_create * * Arguments: const uint32_t size - size of FIFO (in bytes) * int alignment - required alignment (bytes) * SOFTWARE_FIFO_HANDLE_T *handle - handle to this FIFO * * Description: Allocates space for a FIFO and initialises it * * Returns: 0 if successful, any other value is failure * ***********************************************************/ int32_t software_fifo_create( const uint32_t size, int alignment, SOFTWARE_FIFO_HANDLE_T *handle ) { int32_t success = -1; // check that the alignment is a power of 2 or 0 os_assert((OS_COUNT(alignment) == 1) || (alignment == 0)); // put the alignment into a form we can use (must be power of 2) alignment = alignment ? alignment-1 : 0; // check we have a valid pointer if(handle) { memset(handle,0,sizeof(SOFTWARE_FIFO_HANDLE_T)); // allocate slightly more space than we need so that we can get the required alignment handle->malloc_address = (uint8_t *)os_malloc(size+alignment, OS_ALIGN_DEFAULT, ""); if(handle->malloc_address) { handle->base_address = (uint8_t *)(((uint32_t)handle->malloc_address + alignment) & ~alignment); handle->read_ptr = handle->base_address; handle->write_ptr = handle->base_address; handle->size = size; handle->bytes_read = 0; handle->bytes_written = 0; success = os_semaphore_create(&handle->software_fifo_semaphore,OS_SEMAPHORE_TYPE_SUSPEND); // oops, unable to create the semaphore os_assert(success == 0); } } // oops, looks like you've run out of memory os_assert(success == 0); return(success); }
/* ---------------------------------------------------------------------- * allocate space for a new non-wrap fifo, and return pointer to * opaque handle. returns NULL on failure * -------------------------------------------------------------------- */ VCHI_NWFIFO_T * non_wrap_fifo_create( const char *name, const uint32_t size, int alignment, int no_of_slots ) { int32_t success = -1; NON_WRAP_FIFO_HANDLE_T *fifo; // check that the alignment is a power of 2 or 0 #ifndef WIN32 os_assert((OS_COUNT(alignment) == 1) || (alignment == 0)); #endif // put the alignment into a form we can use (must be power of 2) alignment = alignment ? alignment - 1 : 0; // create new non-wrap fifo struct fifo = (NON_WRAP_FIFO_HANDLE_T *)os_malloc( sizeof(NON_WRAP_FIFO_HANDLE_T), OS_ALIGN_DEFAULT, "" ); os_assert( fifo ); if (!fifo) return NULL; memset( fifo, 0, sizeof(NON_WRAP_FIFO_HANDLE_T) ); fifo->name = name; // allocate slightly more space than we need so that we can get the required alignment fifo->malloc_address = (uint8_t *)os_malloc(size+alignment, OS_ALIGN_DEFAULT, ""); if ( fifo->malloc_address ) { fifo->base_address = (uint8_t *)(((size_t)fifo->malloc_address + alignment) & ~alignment); fifo->read_ptr = fifo->base_address; fifo->write_ptr = fifo->base_address; // now allocate the space for the slot_info structures fifo->slot_info = (NON_WRAP_SLOT_T *)os_malloc(no_of_slots*sizeof(NON_WRAP_SLOT_T), OS_ALIGN_DEFAULT, ""); memset( fifo->slot_info, 0, no_of_slots * sizeof(NON_WRAP_SLOT_T) ); fifo->size = size; fifo->num_of_slots = no_of_slots; success = os_semaphore_create( &fifo->sem, OS_SEMAPHORE_TYPE_SUSPEND ); os_assert(success == 0); success = os_cond_create( &fifo->space_available_cond, &fifo->sem ); os_assert(success == 0); } else { os_free(fifo); fifo = NULL; } return (VCHI_NWFIFO_T *)fifo; }