Advertisement
tyler569

malloc tests

Apr 5th, 2019
400
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.56 KB | None | 0 0
  1. #define ALLN 1000000
  2. void* allocations[ALLN] = {0};
  3. size_t max_alloc = 1;
  4.  
  5. void insert_alloc(void* alloc) {
  6.     for (size_t i=0; i<ALLN; i++) {
  7.         if (!allocations[i]) {
  8.             allocations[i] = alloc;
  9.             if (i > max_alloc) {
  10.                 max_alloc = i;
  11.             }
  12.             return;
  13.         }
  14.     }
  15. }
  16.  
  17. #define ITERATIONS 100000
  18.  
  19. int test_with_random() {
  20.     srand(time(0));
  21.  
  22.     for (int iteration=0; iteration<ITERATIONS; iteration++) {
  23.         int op = (rand() % 10) > 4 ? 0 : 1; // mmalloc : mfree
  24.         int len = (rand() % 10) > 8 ? 0 : 1; // large : small
  25.  
  26.         if (op == 0) {
  27.             size_t alen = rand() % (len == 0 ? 1000000 : 1000);
  28.             void* alloc = mmalloc(alen);
  29.             // printf("mmalloc(%zu) -> %p\n", alen, alloc);
  30.             if (alloc)
  31.                 memset(alloc, 'a', alen);
  32.             insert_alloc(alloc);
  33.         }
  34.         if (op == 1) {
  35.             size_t allocations_ix = rand() % max_alloc;
  36.             void* to_free = allocations[allocations_ix];
  37.             // printf("mfree(%p)\n", to_free);
  38.             mfree(to_free);
  39.             allocations[allocations_ix] = NULL;
  40.         }
  41.     }
  42.  
  43.     print_pool();
  44.     summarize_pool();
  45. }
  46.  
  47. int test_with_specific() {
  48.     void* alloc = mmalloc((1<<24) - sizeof(mregion));
  49.  
  50.     print_pool();
  51.     summarize_pool();
  52.  
  53.     mfree(alloc);
  54.  
  55.     print_pool();
  56.     summarize_pool();
  57. }
  58.  
  59. int main() {
  60.     pool = malloc(POOL_LENGTH);
  61.     region_0 = pool;
  62.     initialize();
  63.  
  64.     // test_with_specific();
  65.     test_with_random();
  66.  
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement