Waitrud Weber’s blog

things and reminders for memories

Garbage Collection

Garbage Collection

Basically, We use malloc inside scope like function and then free memories like the following:

void Scope()
{
 char* dummu;
 dummy = malloc ( sizeof(char)* 10 );
 free(dummy);
}

sub lootin has usually sub lootin.
on the following case, we must keep memories a and b before call of m_concat and also after call of m_concat.

void Scope()
{
 char* a, b;
 ...
 a = m_concat( a, (char*)b );
}

So, I created garbage collection.

1. Execute winmain_001.exe.
2. Press Ctl and then v about 10 times.
3. Press Ctl and then r.

You could see garbage collection.
Qhen changing stage or before start of process, you free garbage collecyion, which get system stable.

//
//
//
//
//
void put_garbage_collection (char* put_char_string ) {

 char** g_dummy;

 if ( garbage.stock_index == 0 ) {
  g_dummy = (char **) malloc( sizeof(char*) * garbage.stock_max );
  garbage.stock = g_dummy;
 }

 garbage.stock_index++;
 if ( garbage.stock_index >= garbage.stock_max ) {
  garbage.stock_max *=2;
  garbage.stock = (char**) realloc( garbage.stock, sizeof(char*) * garbage.stock_max );
 }

 garbage.stock[garbage.stock_index] = put_char_string;
}

//
//
//
//
//
char* m_concat( char *head, char *tail )
{
 int nh, nt;
 static int alloc = 0;
 char* c_head;

 nh = array_count( head );
 nt = array_count( tail );

 for ( int j=0; j< 1000; j++ ) {
  if ( alloc == 1 ) {
   sleep(1000);
  }
  break;
 }

 if ( alloc == 0 ) {
  alloc = 1;
  dummy_allocation = (char *) malloc( sizeof(char) * ( nh + nt + 1 ) );
  alloc = 0;
 }

 for( int i=0; i< nh; i++ )
  *( dummy_allocation + i ) = *(head + i);

 for( int i=0; i< nt; i++ )
  *( dummy_allocation + nh + i ) = *(tail + i);

 *( dummy_allocation + nh + nt ) = '\0';

 put_garbage_collection(head);
 put_garbage_collection(tail);
 put_garbage_collection(dummy_allocation);

 return dummy_allocation;
}

I'm pleasure if you download the below.
https://github.com/WaitrudWeber/source_zip/blob/master/winmain-20190113.zip


Please reffer to the below for compilation.
https://waitrudweber.hatenablog.com/entry/2018/05/01/005938