I remem, uh, rem, uh, recall myself

This is my choice for memory allocation centralization.


#include "HelloMe.h" struct node { void *Allocated; struct node *next; }; typedef struct node node; /* Allocate an address as a primary node and place as first element. Next element is NULL or "nil" as printf calls it for pointer. Allocate an address with a length. Allocate an address as a secondary node and place as next element. Place Allocated address in list. Next element is NULL or "nil" as printf calls it for pointer. Return allocated memory address. When program exits: Free Allocated addresses. */ int initializeMemory(sourceStructure *SourceStructure){ SourceStructure->memoryList = malloc(sizeof(struct node)); SourceStructure->memoryElement=0; SourceStructure->memoryElements=1; SourceStructure->memoryElementP=SourceStructure->memoryList; SourceStructure->memoryList->next=NULL; SourceStructure->memoryList->Allocated=SourceStructure->memoryList; return 0; } int allocateMemory(sourceStructure *SourceStructure){ SourceStructure->memoryElement++; SourceStructure->memoryElements++; SourceStructure->newMemory=malloc(sizeof(struct node)); SourceStructure->memoryElementP->next=SourceStructure->newMemory; SourceStructure->newMemory=malloc(SourceStructure->memoryRequest); SourceStructure->memoryElementP=SourceStructure->memoryElementP->next; SourceStructure->memoryElementP->next=NULL; SourceStructure->memoryElementP->Allocated=SourceStructure->newMemory; return 0; } int freeMemory(sourceStructure *SourceStructure){ int i; SourceStructure->memoryElementP=SourceStructure->memoryList; SourceStructure->memoryElementP=SourceStructure->memoryElementP->next; for(i=1;i<SourceStructure->memoryElements;i++) { free(SourceStructure->memoryElementP->Allocated); SourceStructure->memoryElementP->Allocated=NULL; SourceStructure->memoryElementP=SourceStructure->memoryElementP->next; } return 0; } int freeList(sourceStructure *SourceStructure){ int i; node *memoryElementPl2; SourceStructure->memoryElementP=SourceStructure->memoryList; for(i=1;i<SourceStructure->memoryElements;i++) { memoryElementPl2=SourceStructure->memoryElementP->next; free(SourceStructure->memoryElementP->next); SourceStructure->memoryElementP=memoryElementPl2; } free(SourceStructure->memoryList); return 0; } int showMemory(sourceStructure *SourceStructure){ int i; SourceStructure->memoryElementP=SourceStructure->memoryList; for(i=0;i<SourceStructure->memoryElements;i++) { if(SourceStructure->memoryElementP==NULL){break;} printf("Allocated address %d = %p Next is %p\n",i, SourceStructure->memoryElementP->Allocated, (void *)SourceStructure->memoryElementP->next); SourceStructure->memoryElementP=SourceStructure->memoryElementP->next; } return 0; }

Roughly speaking. ADDED: the code which disposes of the memory for the linked list also. I put this code up before I do my usual testing. I was playing with gprof again and looking at an old post about profiling serial program killers. As time goes by, I have a better grasp of this stuff and I am calling bull shit on code I see on the internet quite regularly now. Most of it really is helpful but students must surpass their teachers eventually, unless we are going down hill, which is a possibility.. I was reading some explanation of code profiling and I looked at the article date, 1998, eeek , that is the stone age. Even 2007 is a bit dated when it comes to code analysis. I rarely trust SIMD,CISC, or MIMD computer articles dated before 1900.

This code was checked with `valgrind` and the results are below.


==11811== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 8 from 1) ==11811== malloc/free: in use at exit: 0 bytes in 0 blocks. ==11811== malloc/free: 4 allocs, 4 frees, 320 bytes allocated. ==11811== For counts of detected errors, rerun with: -v ==11811== All heap blocks were freed -- no leaks are possible.

I am integrating this within the code generator, so that it has one more parameter to utilize in qualifying code it generates. I eventually use all the profiling and checking methods on my code. So this way I get clean code from the start. This is not the cleanest code, but this is the code that makes the code and will eventually be recreated by the code generator at a higher quality and consistency level. And the command I ran was this:


valgrind --tool=memcheck --leak-check=yes --show-reachable=yes --num-callers=20 --track-fds=yes ./HelloMe

0 comments:

Contributors

Automated Intelligence

Automated Intelligence
Auftrag der unendlichen LOL katzen