Open chalenge -- 1 ... its about memory management Vs performance in terms of New/Delete system calls

here is actual code

#include
using namespace std;
#define MAX_NUM_TINY_CHUNKS 100
#define MAX_SIZE_TINY_CHUNKS 50

#define Byte unsigned char
struct type_bit{unsigned short type:2;};
class ArrayMemMgrC
{
private:
Byte **amm_tinyChunk_b;
unsigned long *amm_tinyFlags_p;
unsigned long amm_tinyFreeChunk_ul;
public:
//Variables for allocating and deallocating tiny chunks of memory
Byte **amm_tinyChunk_b; //This double pointer points to an array of pointers to array of Byte for which memory will be alloca
ted during initialization

unsigned long *amm_tinyFlags_p; //value of each bit of array will say whether corresponding element of amm_tinyChunk_b is al
located are free
unsigned long tinyFreeChunk_ul; //points to a free chunk in amm_tinychunk_b;

};

ArrayMemMgrC::ArrayMemMgrC()
{
//Allocate memory to tiny, small and big memory chunks according to defined macros
int i;
amm_tinyChunk_b = new Byte *[MAX_NUM_TINY_CHUNKS];
for(i=1;i<=MAX_NUM_TINY_CHUNKS;i++) amm_tinyChunk_b[i] = new unsigned char[MAX_SIZE_TINY_CHUNKS]; float RoundOFF = MAX_NUM_TINY_CHUNKS/32.0; if(RoundOFF > (MAX_NUM_TINY_CHUNKS/32))
{ RoundOFF = (MAX_NUM_TINY_CHUNKS/32)+1;
amm_tinyFlags_p = (unsigned long*)malloc(sizeof(unsigned long)*RoundOFF);
}
*(amm_tinyFlags_p) = 0;
amm_tinyFreeChunk_ul = 0;
}
Byte* ArrayMemMgrC::amm_allocMem(unsigned long *index,unsigned short type)
{ unsigned long unity = 0x10000000;
int which_i;
if(type == 1)
{
unity = unity >> amm_tinyFreeChunk_ul;
which_i = amm_tinyFreeChunk_ul/(sizeof(long) * 8);

amm_tinyFreeChunk_ul++;
while(((amm_tinyFlags_p[which_i] & unity)) != 0)
{ if(amm_tinyFreeChunk_ul > 101)
{
amm_tinyFreeChunk_ul = 0;
unity = 0x10000000;
}
amm_tinyFreeChunk_ul=amm_tinyFreeChunk_ul+1 ;
unity = unity>>1;
}
(*index)= amm_tinyFreeChunk_ul;
Byte *retVal = amm_tinyChunk_b[amm_tinyFreeChunk_ul];
amm_tinyFlags_p[which_i] = amm_tinyFlags_p[which_i] | unity;
return retVal;
}
}
void ArrayMemMgrC::amm_deAllocmem(unsigned long *index)
{ struct type_bit bit3;
unsigned long unity_temp = 0x10000000;
bit3.type = (*index)/(sizeof(long)*8);
amm_tinyFlags_p[bit3.type] = amm_tinyFlags_p[bit3.type] ^ (unity_temp>>((*index)-1));
}
int main()
{
struct type_bit bit2;
bit2.type = 1;
ArrayMemMgrC Obj;
unsigned char *Space_tiny = NULL,*Space_tiny1 = NULL;
unsigned long tiny_index,tiny_index1;
Space_tiny = Obj.amm_allocMem(&tiny_index,bit2.type);
if(Space_tiny!=NULL){
strcpy((char*)Space_tiny,"COMMUNICATIONS SOFTWARE");
cout<<<
}else cout<<"Unable to allocate memory needed";

Space_tiny1 = Obj.amm_allocMem(&tiny_index1,bit2.type);
if(Space_tiny1!=NULL){
strcpy((char*)Space_tiny1,"TECHNOLOGIES");
cout<<
}else cout<<"Unable to allocate memory needed";

Obj.amm_deAllocmem(&tiny_index);
Obj.amm_deAllocmem(&tiny_index1);
return 0;
}

This code is a memory management function in terms of reducing time to calling ‘new’ and ‘delete ‘ Function calls. Using this no need to delete variables, just we can re-use them, for this one array implemented to record the information about variable are in use or not. And start time itself we are defining a bunch of bytes (blocks in space), it may waste RAM size, but will give time difference (saving) in program execution.

The actual problem here .....

HERE WE ARE DEFINING A POINTER TO A MEMORY SPACE,*space_tiny AND ALSO tiny_index VARIABLE TO KEEP ITS PLACE IN amm_tinyFlags_p FLAGS AS BIT.

CAN ANYBODY SUGGEST A METHOD TO AVOID tiny_index VARIABLE USAGE IN THIS PROGRAMME .

1 comment :

  1. #include "iostream"
    using namespace std;
    #define MAX_NUM_TINY_CHUNKS 100
    #define MAX_SIZE_TINY_CHUNKS 50
    #define Byte unsigned char
    long Space[MAX_NUM_TINY_CHUNKS];
    class ArrayMemMgrC
    {
    private:
    Byte **amm_tinyChunk_b;
    unsigned long *amm_tinyFlags_p;
    unsigned long amm_tinyFreeChunk_ul;
    public:
    ArrayMemMgrC();
    Byte* amm_allocMem();
    void amm_deAllocmem(long);
    };

    ArrayMemMgrC::ArrayMemMgrC()
    {
    int i;
    amm_tinyChunk_b = new Byte *[MAX_NUM_TINY_CHUNKS];
    for(i=1;i<=MAX_NUM_TINY_CHUNKS;i++)
    amm_tinyChunk_b[i] = new unsigned char[MAX_SIZE_TINY_CHUNKS];

    float RoundOFF = MAX_NUM_TINY_CHUNKS/32.0;
    if(RoundOFF > (MAX_NUM_TINY_CHUNKS/32))
    { RoundOFF = (MAX_NUM_TINY_CHUNKS/32)+1;
    amm_tinyFlags_p = (unsigned long*)malloc(sizeof(unsigned long)*RoundOFF);
    }
    *(amm_tinyFlags_p) = 0;
    amm_tinyFreeChunk_ul = 0;
    }
    Byte* ArrayMemMgrC::amm_allocMem()
    {
    unsigned long unity = 0x10000000;
    int which_i;
    unity = unity >> amm_tinyFreeChunk_ul;
    which_i = amm_tinyFreeChunk_ul/(sizeof(long) * 8);

    amm_tinyFreeChunk_ul++;
    while(((amm_tinyFlags_p[which_i] & unity)) != 0)
    { if(amm_tinyFreeChunk_ul > 101)
    {
    amm_tinyFreeChunk_ul = 0;
    unity = 0x10000000;
    }
    amm_tinyFreeChunk_ul=amm_tinyFreeChunk_ul+1 ;
    unity = unity>>1;
    }
    Byte *retVal = amm_tinyChunk_b[amm_tinyFreeChunk_ul];
    amm_tinyFlags_p[which_i] = amm_tinyFlags_p[which_i] | unity;
    Space[amm_tinyFreeChunk_ul] = (long)retVal;
    return retVal;
    }
    void ArrayMemMgrC::amm_deAllocmem(long Space_place)
    { unsigned int bit3,index;
    unsigned long unity_temp = 0x10000000;
    for(int i_for =1 ;i_for<=MAX_NUM_TINY_CHUNKS;i_for++)
    {
    if(Space_place == Space[i_for])
    index = i_for;
    }
    bit3 = index/(sizeof(long)*8);
    amm_tinyFlags_p[bit3] = amm_tinyFlags_p[bit3] ^ (unity_temp>>((index%32)-1));
    }
    int main()
    {
    ArrayMemMgrC Obj;
    unsigned char *Space_tiny = NULL,*Space_tiny1 = NULL;
    Space_tiny = Obj.amm_allocMem();
    if(Space_tiny!=NULL){
    strcpy((char*)Space_tiny,"ARICENT COMMUNICATIONS SOFTWARE");
    printf("%s:",Space_tiny);
    }else cout<<"Unable to allocate memory needed";

    Space_tiny1 = Obj.amm_allocMem();
    if(Space_tiny1!=NULL){
    strcpy((char*)Space_tiny1,"aricent technologies");
    printf("%s",Space_tiny1);
    }else cout<<"Unable to allocate memory needed";

    Obj.amm_deAllocmem((long)Space_tiny);

    Obj.amm_deAllocmem((long)Space_tiny1);
    return 0;
    }

    ReplyDelete