Gamestudio Links
Zorro Links
Newest Posts
loading historical data 1st time
by AndrewAMD. 04/14/23 12:54
Trade at bar open
by juanex. 04/13/23 19:43
Bug in Highpass2 filter
by rki. 04/13/23 09:54
Adding Limit Orders For IB
by scatters. 04/11/23 16:16
FisherN
by rki. 04/11/23 08:38
AUM Magazine
Latest Screens
SHADOW (2014)
DEAD TASTE
Tactics of World War I
Hecknex World
Who's Online Now
1 registered members (Grant), 999 guests, and 2 spiders.
Key: Admin, Global Mod, Mod
Newest Members
rki, FranzIII, indonesiae, The_Judge, storrealba
18919 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Malloc -ish memory managment #227995
09/17/08 07:51
09/17/08 07:51
Joined: Feb 2008
Posts: 3,232
Australia
EvilSOB Offline OP
Expert
EvilSOB  Offline OP
Expert

Joined: Feb 2008
Posts: 3,232
Australia
Its pretty self-descriptive. A managed malloc handling system.

I am open for suggestions as to problems, bugs, improvements, etc.

Any queries, feel free to reply or PM.

Code:
///////////////////////////////////////////////////////////////
// Memory Management
//
typedef struct { void* addr;  long size; } T_BLOCK;
T_BLOCK* t_alloc_index=0;   long t_alloc_length=0;   long t_alloc_size=0;
//
void* t_malloc(long);            // malloc()  replacement (identical usage)
void* t_mrealloc(void*,long);    // realloc() replacement (identical usage)
long   t_msize(void*);           // _msize()  replacement (identical usage)
void   t_mfree(void*);           // free()    replacement (identical usage)
void   t_mflush();               // free() ALL entires in table
void   t_mdiag(char*);           // send list of ALL entries to acklog.txt
//

...

//
//
void* t_malloc(long size)
{   if(t_alloc_index==NULL)   t_alloc_index=malloc((long)sizeof(T_BLOCK));
   long idx;
   for(idx=0; idx<t_alloc_length; idx++)   
      if(t_alloc_index[idx].addr==NULL)   
      break;   //look for unused index
   if(idx>=t_alloc_length)               //any unused ?
   {   t_alloc_index = realloc(t_alloc_index,t_alloc_size+(long)sizeof(T_BLOCK));         //append unused index 
       t_alloc_length++;      t_alloc_size += (long)sizeof(T_BLOCK);                  }      // if none empty
   t_alloc_index[idx].addr = (void*)malloc(size);      //create 
   t_alloc_index[idx].size = size;                     // memory block
   return(t_alloc_index[idx].addr);      //return pointer to block
}
//
//
long   t_msize(void* block)
{  if(t_alloc_index==NULL)   return(0);   //no allocated blocks, so nothing to find.
   long idx;
   for(idx=0; idx<t_alloc_length; idx++)   if(t_alloc_index[idx].addr==block)   break;   //look for matching index
   if(t_alloc_index[idx].addr!=block)   return(0);   //no matching block
   return(t_alloc_index[idx].size);         //return block size
}
//
//
void* t_mrealloc(void* block,long size)
{   if(t_alloc_index==NULL)   {   block=t_malloc(size);    return(block);   }
   long idx;
   for(idx=0; idx<t_alloc_length; idx++)   if(t_alloc_index[idx].addr==block)   break;   //look for matching index
   if(t_alloc_index[idx].addr!=block)   {   block=t_malloc(size);    return(block);   }   //no matching block
   t_alloc_index[idx].addr = realloc(t_alloc_index[idx].addr,size);   //re-create 
   t_alloc_index[idx].size = size;                                    // memory block
   return(t_alloc_index[idx].addr);      //return pointer to block
}
//
//
void   t_mfree(void* block)
{   if(t_alloc_index==NULL)   return;   //no allocated blocks, so nothing to free.
   long idx;
   for(idx=0; idx<t_alloc_length; idx++)   if(t_alloc_index[idx].addr==block)   break;   //look for matching index
   if(t_alloc_index[idx].addr!=block)   return;   //no matching block
   free(t_alloc_index[idx].addr);   //free block
   t_alloc_index[idx].addr   = 0;   // and clear index
   t_alloc_index[idx].size   = 0;   // *
   return;         //return, block freed
}
//
//
void   t_mdiag(char* header)
{   diag(header);   //'print' user supplied test heading
   if(t_alloc_index==NULL)   {   diag("Table Empty\n\n");   return;   }   //very empty
   long idx;   long count=0;
   for(idx=0; idx<t_alloc_length; idx++)   
   {   if(t_alloc_index[idx].addr!=NULL)      //Only print valid entries
      {   diag_var("\nAddress = %-10.0f,\t",    (unsigned long)t_alloc_index[idx].addr);
         diag_var("Size = %6.0f",              (long)t_alloc_index[idx].size);
         count++;         }   }         // count valid entries
   diag_var("\nCounted %.0f valid entries ", count);
   diag_var("out of %.0f slots\n\n", t_alloc_length);
   return;   
}
//
//
void   t_mflush()
{   if(t_alloc_index==NULL)   return;   //no allocated blocks, so nothing to free.
   long idx;
   for(idx=0; idx<t_alloc_length; idx++)   
      if(t_alloc_index[idx].addr!=NULL)      
         free(t_alloc_index[idx].addr);   //free valid block
   t_alloc_length=0;      //all blocks freed
   t_alloc_size=0;      // so reset counters
   free(t_alloc_index);      //and free 
   t_alloc_index=0;         // empty table
   return;         
}
//
//



"There is no fate but what WE make." - CEO Cyberdyne Systems Corp.
A8.30.5 Commercial
Re: Malloc -ish memory managment [Re: EvilSOB] #228046
09/17/08 12:14
09/17/08 12:14
Joined: Dec 2006
Posts: 1,086
Queensland - Australia
Nidhogg Offline
Serious User
Nidhogg  Offline
Serious User

Joined: Dec 2006
Posts: 1,086
Queensland - Australia
Looks like a great contrib EvilSOB, Is it for pure or legacy lite-c?


Windows XP SP3
Intel Dual Core CPU: E5200 @ 2.5GHz
4.00GB DDR3 Ram
ASUS P5G41T-M LX
PCIE x16 GeForce GTS 450 1Gb
SB Audigy 4
Spyware Doctor with AntiVirus
Re: Malloc -ish memory managment [Re: Nidhogg] #228433
09/19/08 13:27
09/19/08 13:27
Joined: Feb 2008
Posts: 3,232
Australia
EvilSOB Offline OP
Expert
EvilSOB  Offline OP
Expert

Joined: Feb 2008
Posts: 3,232
Australia
Believe it or not, I dunno. I havent figured out the difference yet.

I THINK its lite-c Pure.
It only needs acknex.h as an include to function fully. No API calls required.
Thats 'Pure' isnt it?


"There is no fate but what WE make." - CEO Cyberdyne Systems Corp.
A8.30.5 Commercial
Re: Malloc -ish memory managment [Re: EvilSOB] #231544
10/15/08 12:15
10/15/08 12:15
Joined: Oct 2004
Posts: 4,134
Netherlands
Joozey Offline
Expert
Joozey  Offline
Expert

Joined: Oct 2004
Posts: 4,134
Netherlands
I'm sorry if I missed the point, but managed how? As in a linked list?


Click and join the 3dgs irc community!
Room: #3dgs
Re: Malloc -ish memory managment [Re: Joozey] #231552
10/15/08 12:46
10/15/08 12:46
Joined: Feb 2008
Posts: 3,232
Australia
EvilSOB Offline OP
Expert
EvilSOB  Offline OP
Expert

Joined: Feb 2008
Posts: 3,232
Australia
It keeps track 'internally' of all memory allocated through it,
wherethat memory is, how big each block is, allows block to re-sized,
and can be freed through it.

I feel its greatest feature is that you no longer need to mentally track
if a certain block has been free'ed yet or not. Multiple free's of the
same block DO NO HARM as it does with real malloc finctions.

I still dont grasp the buzzword 'linked lists' yet, so it may be using one,
its up to you to figure out if it is or not though...


"There is no fate but what WE make." - CEO Cyberdyne Systems Corp.
A8.30.5 Commercial
Re: Malloc -ish memory managment [Re: EvilSOB] #231664
10/16/08 00:51
10/16/08 00:51
Joined: Oct 2004
Posts: 4,134
Netherlands
Joozey Offline
Expert
Joozey  Offline
Expert

Joined: Oct 2004
Posts: 4,134
Netherlands
Ok that made it clear, thanks smile And thanks for this contribution too. It's handy indeed.
Linked list is just a data managing method: a chain of data by using pointers pointing to eachother. Hence the name smile.


Click and join the 3dgs irc community!
Room: #3dgs
Re: Malloc -ish memory managment [Re: Joozey] #231675
10/16/08 05:17
10/16/08 05:17
Joined: Feb 2008
Posts: 3,232
Australia
EvilSOB Offline OP
Expert
EvilSOB  Offline OP
Expert

Joined: Feb 2008
Posts: 3,232
Australia
Aha, like the c_link structure is used by the
acknex structures object.next to drive ent_next, ptr_first etc.

I will probably add that to mine later after a bit
more experience with it. It looks to be a more efficient way to simulate
variable length arrays.

Thanks.


"There is no fate but what WE make." - CEO Cyberdyne Systems Corp.
A8.30.5 Commercial

Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1