_memory.c
Upload User: gzelex
Upload Date: 2007-01-07
Package Size: 707k
Code Size: 5k
Development Platform:

MultiPlatform

  1. /*******************************************************************************
  2. +
  3. +  LEDA-R  3.2.3
  4. +
  5. +  _memory.c
  6. +
  7. +  Copyright (c) 1995  by  Max-Planck-Institut fuer Informatik
  8. +  Im Stadtwald, 66123 Saarbruecken, Germany     
  9. +  All rights reserved.
  10. *******************************************************************************/
  11. #include <LEDA/basic.h>
  12. #include <stdio.h>
  13. //------------------------------------------------------------------------------
  14. // Memory Management
  15. //------------------------------------------------------------------------------
  16. //const int memory_block_bytes = 4092; // size of block 2^k - 4  bytes
  17. const int memory_block_bytes = 8188; // size of block 2^13 - 4  bytes
  18. const int memory_max_size    = 256;  // maximal size of structures in bytes
  19. memory_elem_ptr        memory_free_list[257];     //memory_max_size + 1
  20. static long int        memory_total_count[257]; 
  21. static memory_elem_ptr memory_block_list[257];
  22. static int             memory_block_count[257];
  23. static int             memory_initialized = 0;
  24. static void memory_init()
  25. { for(int i = 0; i <= memory_max_size; i++)
  26.   { memory_free_list[i]   = 0;
  27.     memory_total_count[i] = 0;
  28.     memory_block_list[i]  = 0;
  29.     memory_block_count[i] = 0;
  30.    }
  31.   memory_initialized = 1;
  32.  }
  33. memory_elem_ptr memory_allocate_block(int b)
  34.   if (memory_initialized == 0) memory_init();
  35.   if (b > memory_max_size) 
  36.      error_handler(1,string("allocate_block: size (%d bytes) too big",b));
  37.   //allocate new block and slice it into chunks of size b bytes
  38.   register memory_elem_ptr p;
  39.   register memory_elem_ptr stop;
  40.   register int words = (b + sizeof(void*) - 1)/sizeof(void*);
  41.   int bytes = words * sizeof(void*);
  42.   int num   = memory_block_bytes/bytes - 2;
  43.   memory_block_count[b]++;
  44.   memory_total_count[b] += num;
  45.   if ((p=memory_elem_ptr(malloc(memory_block_bytes))) == 0 )
  46.    { cout << "memory allocation: out of memoryn";
  47.      print_statistics();
  48.      exit(1);
  49.     }
  50.   //insert block into list of used blocks
  51.   p->next = memory_block_list[b];
  52.   memory_block_list[b] = p;
  53.   p += words;
  54.   memory_free_list[b] = p;
  55.   stop = p + (num-1)*words;
  56.   stop->next = 0;
  57.   while (p < stop) p = (p->next = p+words);
  58.   return memory_free_list[b];
  59. }
  60. void memory_clear()
  61.   register memory_elem_ptr p;
  62.   int i;
  63.   long used;
  64.   for (i=1;i<=memory_max_size;i++)
  65.   { p = memory_free_list[i];
  66.     used = memory_total_count[i];
  67.     while (p) { used--; p = p->next; }
  68.     if (used==0)
  69.     { while (memory_block_list[i])
  70.       { p = memory_block_list[i];
  71.         memory_block_list[i] = p->next;
  72.         memory_block_count[i]--;
  73.         free((char*)p);
  74.        }
  75.       memory_free_list[i] = 0;
  76.       memory_total_count[i] = 0;
  77.      }
  78.    }
  79. }
  80. void memory_kill()
  81. { register memory_elem_ptr p;
  82.   int i;
  83.   for (i=1;i<=memory_max_size;i++)
  84.   { while (memory_block_list[i])
  85.     { p = memory_block_list[i];
  86.       memory_block_list[i] = p->next;
  87.       free((char*)p);
  88.      }
  89.     memory_free_list[i] = 0;
  90.     memory_total_count[i] = 0;
  91.     memory_block_count[i] = 0;
  92.    }
  93. }
  94. memory_elem_ptr allocate_bytes_with_check(int bytes)
  95. { memory_elem_ptr p = memory_free_list[bytes];
  96.   if (p==0) p = memory_allocate_block(bytes);
  97.   memory_free_list[bytes] = p->next;
  98.   //printf("allocate(%d):   %xn",bytes,p);
  99.   //fflush(stdout);
  100.   return p;
  101. }
  102. void deallocate_bytes_with_check(void* p, int bytes)
  103. { //printf("deallocate(%d): %xn",bytes,p);
  104.   //fflush(stdout);
  105.   if (memory_block_count[bytes] == 0) 
  106.      error_handler(999,"no block allocated");
  107.   memory_elem_ptr q = memory_free_list[bytes];
  108.   while(q && q != memory_elem_ptr(p)) q = q->next;
  109.   if (q) error_handler(999,string("LEDA memory: pointer %d deleted twice",p));
  110.   memory_elem_ptr(p)->next = memory_free_list[bytes];
  111.   memory_free_list[bytes] = memory_elem_ptr(p);
  112.  }
  113. memory_elem_ptr allocate_words(int words)
  114. { int bytes = words * sizeof(void*);
  115.   memory_elem_ptr p = memory_free_list[bytes];
  116.   if (p==0) p = memory_allocate_block(bytes);
  117.   memory_free_list[bytes] = p->next;
  118.   return p;
  119. }
  120. void deallocate_words(void* p, int words)
  121. { int bytes = words * sizeof(void*);
  122.   memory_elem_ptr(p)->next = memory_free_list[bytes];
  123.   memory_free_list[bytes] = memory_elem_ptr(p);
  124.  }
  125. int used_memory()
  126. { long int total_bytes=0;
  127.   for (int i=1;i<=memory_max_size;i++)
  128.      total_bytes += i*memory_total_count[i];
  129.   return total_bytes;
  130.  }
  131. void print_statistics()
  132. {
  133.   cout.flush();
  134.   long int total,free,used;
  135.   long int total_bytes=0, free_bytes=0, used_bytes=0, b;
  136.   printf("n");
  137.   printf("t+--------------------------------------------------+n");
  138.   printf("t|   size     used     free     blocks     bytes    |n"); 
  139.   printf("t+--------------------------------------------------+n");
  140.   for (int i=1;i<=memory_max_size;i++)
  141.     if ((total = memory_total_count[i]) > 0 || memory_free_list[i])
  142.     { printf("t|   %3d    ",i);
  143.       fflush(stdout);
  144.       memory_elem_ptr p = memory_free_list[i];
  145.       free = 0;
  146.       while (p) { free++; p = p->next; }
  147.       b = total*i; 
  148.       used = total - free;
  149.       free_bytes  += free*i;
  150.       used_bytes  += used*i;
  151.       total_bytes += b;
  152.       printf("%6ld   %6ld    %6d   %8ld    |n",
  153.               used,free,memory_block_count[i],b);
  154.      }
  155.  float kb = float(total_bytes)/1024;
  156.  float sec = used_time();
  157.  printf("t+--------------------------------------------------+n");
  158.  printf("t|   time:%6.2f sec              space:%8.2f kb |n",sec,kb);
  159.  printf("t+--------------------------------------------------+n");
  160.  printf("n");
  161.  fflush(stdout);
  162. }