/*! * \fn void startNewTurn(csuStruct *ptr_csu_struct,int index_player) * Reallocate the memory for the point to begin a new turn. * \param[in,out] *ptr_csu_struct a pointer on a csuStruct * \param[in,out] index_player the index of the player who begin a new turn, -1 if everybody begin a new turn */ void startNewTurn(csuStruct *ptr_csu_struct,int index_player) { int i; if (index_player == -1) { for (i=0 ; i<ptr_csu_struct->nb_player ; i++) myRealloc((void**)&(ptr_csu_struct->point[i]),ptr_csu_struct->nb_player*((ptr_csu_struct->nb_turn[0])+1)*sizeof(float)); } else myRealloc((void **)&(ptr_csu_struct->point[index_player]),ptr_csu_struct->nb_player*((ptr_csu_struct->nb_turn[index_player])+1)*sizeof(float)); }
/*--------------------------------------------------------------------------- // Add an array to this list //-------------------------------------------------------------------------*/ void myListAddArray(MYlist list, void *array, int num) { int size, capacity, elementSize, capacityIncrement, actualIncrement; void *data; size = myListSize(list); capacity = myListCapacity(list); elementSize = myListElementSize(list); data = myListData(list); if (size + num > capacity) { capacityIncrement = myListCapacityIncrement(list); actualIncrement = (capacityIncrement > num)? capacityIncrement: num; capacity += actualIncrement; myListSetCapacity(list, capacity); if (data == NULL) { /* initial list */ data = (void *)myMalloc(elementSize * capacity); } else { /* allocate a larger list */ data = (void *)myRealloc(data, elementSize*capacity); } myListSetData(list, data); } memcpy((char *)data+size*elementSize, (char *)array, num*elementSize); myListSetSize(list, size+num); }
/*--------------------------------------------------------------------------- // Add an integer to this list (must be a list consists of only integers) //-------------------------------------------------------------------------*/ void myListAddInt(MYlist list, int element) { int size, capacity, elementSize, capacityIncrement; int *data; size = myListSize(list); capacity = myListCapacity(list); elementSize = myListElementSize(list); data = myListData(list); if (size >= capacity) { capacityIncrement = myListCapacityIncrement(list); capacity += capacityIncrement; myListSetCapacity(list, capacity); if (data == NULL) { /* initial list */ data = (int *)myMalloc(elementSize * capacity); } else { /* allocate a larger list */ data = (int *)myRealloc(data, elementSize*capacity); } myListSetData(list, data); } data[size] = element; myListSetSize(list, size+1); }
/*--------------------------------------------------------------------------- // Add an element to this list //-------------------------------------------------------------------------*/ void myListAddElement(MYlist list, void *element) { int size, capacity, elementSize, capacityIncrement; void *data; size = myListSize(list); capacity = myListCapacity(list); elementSize = myListElementSize(list); data = myListData(list); if (size >= capacity) { capacityIncrement = myListCapacityIncrement(list); capacity += capacityIncrement; myListSetCapacity(list, capacity); if (data == NULL) { /* initial list */ data = (void *)myMalloc(elementSize * capacity); } else { /* allocate a larger list */ data = (void *)myRealloc(data, elementSize*capacity); } myListSetData(list, data); } memcpy((char *)data+size*elementSize, (char *)element, elementSize); myListSetSize(list, size+1); }
/*! * \fn bool deleteTurn(csuStruct *ptr_csu_struct, int player_index, int turn) * Delete a turn of a player or all of them * \param[in] *ptr_csu_struct a pointer on a csuStruct * \param[in] player_index the index of the player * \param[in] turn the turn * \return true if everything is OK, false otherwise */ bool deleteTurn(csuStruct *ptr_csu_struct, int player_index, int turn) { int i,j; // Test the turn if (ptr_csu_struct->nb_turn[player_index] < turn) { printf(_("\nError: %s only have %.0f turn but you ask the %d turn\n"),ptr_csu_struct->player_names[player_index],ptr_csu_struct->nb_turn[player_index],turn); return 0; } if (turn < 0) { printf(_("\nError: negative turns doesn't exist\n")); return 0; } // Turn based game if (ptr_csu_struct->config.turn_based) { // For all player delete the points and the turn for (i=0 ; i<ptr_csu_struct->nb_player ; i++) { (ptr_csu_struct->total_points[i]) -= ptr_csu_struct->point[i][turn]; for (j=turn+1 ; j<ptr_csu_struct->nb_turn[i] ; j++) { ptr_csu_struct->point[i][j-1] = ptr_csu_struct->point[i][j]; } myRealloc((void**)&(ptr_csu_struct->point[i]),((ptr_csu_struct->nb_turn[i])-1)*sizeof(float)); (ptr_csu_struct->nb_turn[i]) -= 1; } } else { (ptr_csu_struct->total_points[player_index]) -= ptr_csu_struct->point[player_index][turn]; for (j=turn+1 ; j<ptr_csu_struct->nb_turn[player_index] ; j++) ptr_csu_struct->point[player_index][j-1] = ptr_csu_struct->point[player_index][j]; myRealloc((void**)&(ptr_csu_struct->point[player_index]),((ptr_csu_struct->nb_turn[player_index])-1)*sizeof(float)); (ptr_csu_struct->nb_turn[player_index]) -= 1; } rankCalculation(ptr_csu_struct); return true; }
void recoverOldExtention(const char *fileName, secureHeader * sHeader){ char * newFilePath = get_only_path_copy(fileName); newFilePath = (char*) myRealloc(newFilePath, strlen(newFilePath) + strlen(sHeader->fileName) + 1); strcat(newFilePath, sHeader->fileName); printHeader(sHeader); fprintf(stderr, "newFile is %s", newFilePath); rename(fileName, newFilePath); myFree(newFilePath); }
/*--------------------------------------------------------------------------- // Trim this list to current size //-------------------------------------------------------------------------*/ void myListTrim(MYlist list) { void *data; int size, elementSize; size = myListSize(list); elementSize = myListElementSize(list); data = myListData(list); data = (void *)myRealloc(data, elementSize*size); myListSetData(list, data); myListSetCapacity(list, size); }
template<class VALUE> void ResizMemArena<VALUE>::resize() { ++numArrays; arrays = (VALUE**)myRealloc(arrays, numArrays * sizeof(VALUE*)); nat nextSize = initSize * POWER_2(numArrays-1); arrays[numArrays-1] = (VALUE*)calloc(nextSize, sizeof(VALUE)); currentArraySize = nextSize; currentArray++; currentIndex = 0; // cout << "RFR resize: allocating new array with size " << nextSize << endl; }
// Wrapper to call my free function void *realloc(void *ptr = NULL, size_t size) { // If ptr is NULL, realloc behaves as malloc if (ptr == NULL) { return malloc(size); } // If size is 0, realloc behaves as free if (size == 0) { myFree(ptr); return NULL; } return myRealloc(ptr, size + PADDING); }
/** Algorithm: * * two buffers, nodes are propagated according to survivor information * * when recombinations are introduced, the "directions" of these nodes have to alternate * * laying the mutations on top of the recombinations should not be a problem */ void Graph::hookup(const Survivors &survivors, const Ancestry &ancestry, const PopulationManager &popMan, Chromosome &chrom, Randomness &rng, nat startGen, nat endOfSection) { nat chromId = chrom.getId(); // set up both buffers Node **nodeBufferPrevGen = (Node**) malloc(previousState.size() * sizeof(Node*)), **nodeBufferNowGen = (Node**) malloc(popMan.getTotalNumHaploByGen(startGen) * sizeof(Node*)); nat i = 0; for(Node *node : previousState) nodeBufferPrevGen[i++] = node; AddrArrayBackwardIter<Node,true> mutBackIter; mutNodes.getEnd(mutBackIter); AddrArrayBackwardIter<Node,true> recBackIter; recNodes.getEnd(recBackIter); for(nat genC = startGen; genC < endOfSection; ++genC) { #ifdef DEBUG_HOOKUP cout << "======== hooking up generation " << genC << endl; #endif // resize buffer nodeBufferNowGen = (Node**) myRealloc(nodeBufferNowGen, popMan.getTotalNumHaploByGen(genC) * sizeof(Node*)); memset(nodeBufferNowGen,0, popMan.getTotalNumHaploByGen(genC) * sizeof(Node*)); propagateSurvivorNodes(genC, chromId, nodeBufferNowGen, nodeBufferPrevGen, ancestry, survivors); insertRecEvents(genC, chromId, recBackIter, nodeBufferNowGen, nodeBufferPrevGen, ancestry); insertMutEvents(genC, mutBackIter, nodeBufferNowGen, chrom, rng); std::swap(nodeBufferPrevGen, nodeBufferNowGen); } // we swapped once too much std::swap(nodeBufferNowGen, nodeBufferPrevGen); nat lastSize = popMan.getTotalNumHaploByGen(endOfSection-1); previousState.resize(lastSize); for(nat i = 0; i < lastSize; ++i) previousState[i] = nodeBufferNowGen[i]; free(nodeBufferPrevGen); free(nodeBufferNowGen); #ifdef DEBUG_HOOKUP cout << "================> hookup finished"<< endl; #endif }
static size_t WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data) { register size_t realsize = size * nmemb; memBuf_t *mp = (memBuf_t *)data; if (!mp->timeToFirstByte) mp->timeToFirstByte = time(NULL); mp->memory = (char *)myRealloc(mp->memory, mp->size + realsize + 1); mp->readptr = mp->memory; memcpy(&(mp->memory[mp->size]), ptr, realsize); mp->size += realsize; mp->memory[mp->size] = 0; return realsize; }
/* * Create a membuf from a file. */ memBuf_t * readFile(FILE *fp) { static memBuf_t membuf = { NULL, 0, NULL, 0 }; static const size_t BUFINC = 20 * 1024; size_t i = 0; int c; if (membuf.memory) free(membuf.memory); membuf.size = BUFINC; membuf.memory = (char *)myMalloc(membuf.size); while ((c = getc(fp)) != EOF) { if (i == membuf.size) { membuf.size += BUFINC; membuf.memory = (char *)myRealloc(membuf.memory, membuf.size); } membuf.memory[i++] = (char)c; } membuf.size = i; membuf.readptr = membuf.memory; return &membuf; }
/* get META refresh URL (if any) */ char * memGetMetaRefresh(memBuf_t *mp) { char *cp; static char *buf = NULL; char *bufptr; static size_t bufsize = 0; char *metaRefresh = NULL; if (!buf) { bufsize = 1024; buf = myMalloc(bufsize); } /* look for all "meta" tags until Refresh found */ while (!metaRefresh && (cp = memStr(mp, "<meta")) != NULL) { int c; bufptr = buf; /* copy whole tag to buffer for processing */ for (c = memGetc(mp); c != EOF && c != '>'; c = memGetc(mp)) { *bufptr++ = (char)c; if (bufptr > buf + (bufsize -1)) { bufsize += 1024; buf = myRealloc(buf, bufsize); } } /* terminate string */ *bufptr = '\0'; log(("found META tag: %s", buf)); cp = strstr(buf, "http-equiv="); if (!cp) { log(("no http-equiv, looking for next")); continue; } cp += 11; if (strncasecmp(cp, "\"Refresh\"", 9)) { log(("no Refresh, looking for next")); continue; } cp = strstr(buf, "content=\""); if (!cp) { log(("no content, looking for next")); continue; } cp += 9; /* skip delay value (everything until ';') */ while (*cp && *cp != ';') cp++; /* if not end of string skip ';' */ if (*cp) cp++; /* and skip whitespace */ while (*cp && isspace(*cp)) cp++; /* now there should be "url=" with optional whitespace around '=' */ if (strncasecmp(cp, "url", 3)) { log(("no url key, looking for next")); continue; } cp += 3; while (*cp && isspace(*cp)) cp++; if (*cp != '=') { log(("no = after url, looking for next")); continue; } cp++; while (*cp && isspace(*cp)) cp++; /* this is the beginning of the redirection URL */ bufptr = cp; cp = strchr(bufptr, '"'); if (!cp) { log(("no closing \", looking for next")); continue; } /* cut off terminating '"' and other trailing garbage */ *cp = '\0'; metaRefresh = bufptr; } if (metaRefresh) log(("found redirection")); else log(("no redirection found")); memReset(mp); return metaRefresh; }
int main(void) { void* myHeap = malloc(20000); if(myHeap == NULL) { printf("Unable to allocate memory."); exit (1); } double* k = myMalloc(sizeof(double), myHeap); if(k == NULL) { printf("Unable to allocate memory."); exit (1); } *k=1993; int* k2 = myMalloc(sizeof(int),myHeap); if(k2 == NULL) { printf("Unable to allocate memory."); exit (1); } *k2=28; show(30); printf("myHeap addr: %d\n",myHeap); printf("k addr: %d value: %f\n",k,*k); printf("k2 addr: %d value: %d\n",k2,*k2); double* k3 = myMalloc(sizeof(double),myHeap); if(k3 == NULL) { printf("Unable to allocate memory."); exit (1); } *k3=888; k2 = myRealloc(k2,myHeap,sizeof(int)/2); if(k2 == NULL) { printf("Unable to allocate memory."); exit (1); } show(30); printf("myHeap addr: %d \n",myHeap); printf("k addr: %d value: %f\n",k,*k); printf("k2 addr: %d value: %d\n",k2,*k2); printf("k3 addr: %d value: %f\n",k3,*k3); myFree(k2,myHeap); k2=NULL; show(30); printf("myHeap addr: %d \n",myHeap); printf("k addr: %d value: %f\n",k,*k); printf("k3 addr: %d value: %f\n",k3,*k3); free(myHeap); myHeap=NULL; return 0; }
char * recover_old_extention_copy(const char *fileName, secureHeader * sHeader){ char * newFilePath = get_only_path_copy(fileName); newFilePath = (char*) myRealloc(newFilePath, strlen(newFilePath) + strlen(sHeader->fileName) + 1); strcat(newFilePath, sHeader->fileName); return newFilePath; }
void displayMultilineText(const char *text) { int screenColumns = 16; int screenRows = 8; const char *beginningOfLine = text; int linesSize = 32; int linesIndex = -1; const char **lines = myRealloc(NULL, linesSize); for (int index = 0, currentColumn = 0; 1; ++index, ++currentColumn) { if ((index > 0 && (currentColumn % screenColumns) == 0) || text[index] == 0 || text[index] == 0x0A) { currentColumn = 0; ++linesIndex; if (linesIndex >= linesSize) { linesSize *= 2; lines = myRealloc(lines, linesSize); } lines[linesIndex] = beginningOfLine; if (text[index] == 0) break; beginningOfLine = text + index; if (*beginningOfLine == 0x0A) ++beginningOfLine; } } { int lineCount = linesIndex + 1; int currentLineOffset = 0; while (1) { for (int currentLine = currentLineOffset, screenLine = 0; screenLine < screenRows; ++currentLine, ++screenLine) { const char *line = lines[currentLine]; int column = 0; CTOS_LCDTGotoXY(1, screenLine + 1); if (currentLine < lineCount) { while (column < screenColumns && line[column] != 0 && line[column] != 0x0A) { const char ch = line[column]; if (ch >= 0x20 && ch < 127) CTOS_LCDTPutch(ch); else CTOS_LCDTPutch('_'); // just some random stuff to mark gibberish chars ++column; } } CTOS_LCDTClear2EOL(); } switch (getKey()) { case d_KBD_DOWN: if (currentLineOffset < (lineCount + screenRows)) ++currentLineOffset; break; case d_KBD_UP: if (currentLineOffset > 0) --currentLineOffset; break; case d_KBD_CANCEL: goto ret; break; } } } ret: free(lines); CTOS_LCDTClearDisplay(); }