MASA-Core
SpecialRowRAM.cpp
Go to the documentation of this file.
00001 /*******************************************************************************
00002  *
00003  * Copyright (c) 2010-2015   Edans Sandes
00004  *
00005  * This file is part of MASA-Core.
00006  * 
00007  * MASA-Core is free software: you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation, either version 3 of the License, or
00010  * (at your option) any later version.
00011  * 
00012  * MASA-Core is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU General Public License
00018  * along with MASA-Core.  If not, see <http://www.gnu.org/licenses/>.
00019  *
00020  ******************************************************************************/
00021 
00022 #include "SpecialRowRAM.hpp"
00023 
00024 #include <stdlib.h>
00025 
00026 /** Initial row size in number of elements */
00027 #define INITIAL_LENGTH          (1048*1048)
00028 
00029 /** Multiplier of the length whenever the row is full */
00030 #define LENGTH_MULTIPLIER       (1.5) // 50%
00031 
00032 static int sum = 0;
00033 
00034 /*
00035  * @see description on header file
00036  */
00037 SpecialRowRAM::SpecialRowRAM(int id)
00038 {
00039         length = 0;
00040         row = NULL;
00041         setId(id);
00042 }
00043 
00044 /*
00045  * @see description on header file
00046  */
00047 SpecialRowRAM::~SpecialRowRAM() {
00048         if (row != NULL) {
00049                 //printf("Closed %p\n", row);
00050                 free(row);
00051                 row = NULL;
00052         }
00053 }
00054 
00055 /*
00056  * @see description on header file
00057  */
00058 void SpecialRowRAM::close() {
00059 }
00060 
00061 /*
00062  * @see description on header file
00063  */
00064 void SpecialRowRAM::truncateRow(int size) {
00065 }
00066 
00067 /*
00068  * @see description on header file
00069  */
00070 void SpecialRowRAM::initialize(bool readOnly, int length) {
00071         if (row == NULL) {
00072                 if (length == 0) {
00073                         this->length = INITIAL_LENGTH;
00074                 } else {
00075                         this->length = length;
00076                 }
00077                 row = (cell_t*)malloc(length*sizeof(cell_t));
00078                 if (row == NULL) {
00079                         fprintf(stderr, "Out of memory (%d %d)\n", sum, length*sizeof(cell_t));
00080                         exit(1);
00081                 }
00082                 sum += length;
00083         }
00084 
00085         //printf("%p: init(%d, %d) %d\n", row, 0, length, length);
00086 }
00087 
00088 /*
00089  * @see description on header file
00090  */
00091 int SpecialRowRAM::write(const cell_t* buf, int offset, int len) {
00092         if (offset + len > length) {
00093                 sum -= length;
00094                 length *= LENGTH_MULTIPLIER;
00095                 sum += length;
00096                 row = (cell_t*)realloc(row, length*sizeof(cell_t));
00097                 printf("%p: Up\n", row);
00098         }
00099         //printf("%p: Write(%d, %d) %d\n", row, offset, len, length);
00100         memcpy(row+offset, buf, len*sizeof(cell_t));
00101         return len;
00102 }
00103 
00104 /*
00105  * @see description on header file
00106  */
00107 int SpecialRowRAM::read(cell_t* buf, int offset, int len) {
00108         memcpy(buf, row+offset, len*sizeof(cell_t));
00109         return len;
00110 }
00111 
00112