pelib
2.0.0
|
00001 /* 00002 * fifo_array.c 00003 * 00004 * Created on: 20 Feb 2012 00005 * Copyright 2012 Nicolas Melot 00006 * 00007 * This file is part of pelib. 00008 * 00009 * pelib is free software: you can redistribute it and/or modify 00010 * it under the terms of the GNU General Public License as published by 00011 * the Free Software Foundation, either version 3 of the License, or 00012 * (at your option) any later version. 00013 * 00014 * pelib is distributed in the hope that it will be useful, 00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 * GNU General Public License for more details. 00018 * 00019 * You should have received a copy of the GNU General Public License 00020 * along with pelib. If not, see <http://www.gnu.org/licenses/>. 00021 * 00022 */ 00023 00024 #include <stdlib.h> 00025 #include <math.h> 00026 00027 cfifo_t(CFIFO_ARRAY_T)* 00028 pelib_cfifo_from_array(CFIFO_ARRAY_T)(array_t(CFIFO_ARRAY_T) *array) 00029 { 00030 00031 cfifo_t(CFIFO_T) *fifo; 00032 fifo = pelib_alloc_struct(cfifo_t(CFIFO_ARRAY_T))(); 00033 fifo->buffer = array->data; 00034 if(array->data == NULL) exit(81); 00035 fifo->capacity = pelib_array_capacity(CFIFO_ARRAY_T)(array); 00036 fifo->read = 0; 00037 fifo->write = pelib_array_length(CFIFO_ARRAY_T)(array) % fifo->capacity; 00038 00039 if (pelib_array_length(CFIFO_ARRAY_T)(array) > 0) 00040 { 00041 fifo->last_op = PELIB_CFIFO_PUSH; 00042 } 00043 else 00044 { 00045 fifo->last_op = PELIB_CFIFO_POP; 00046 } 00047 00048 pelib_free_struct(array_t(CFIFO_ARRAY_T))(array); 00049 00050 return fifo; 00051 } 00052 00053 array_t(CFIFO_ARRAY_T)* 00054 pelib_array_from_cfifo(CFIFO_ARRAY_T)(cfifo_t(CFIFO_ARRAY_T) *cfifo) 00055 { 00056 array_t(CFIFO_T) *array; 00057 array = pelib_alloc_collection(array_t(CFIFO_ARRAY_T))(pelib_cfifo_length(CFIFO_ARRAY_T)(cfifo)); 00058 pelib_init(array_t(CFIFO_ARRAY_T))(array); 00059 00060 array->length = pelib_cfifo_popmem(CFIFO_ARRAY_T)(cfifo, array->data, pelib_array_capacity(CFIFO_ARRAY_T)(array)); 00061 00062 pelib_free(cfifo_t(CFIFO_ARRAY_T))(cfifo); 00063 00064 return array; 00065 }