Advertisement
AbdRoufBUET

delete new overload

Jul 13th, 2020
1,047
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.42 KB | None | 0 0
  1. void* operator new(sequence::size_type size)
  2. {
  3.     char   *whole_block;   // Pointer to the entire block that we get from heap
  4.     sequence::size_type *size_spot;     // Spot in the block where to store a copy of size
  5.     char   *front_border;  // The border bytes in front of the user's memory
  6.     char   *middle;        // The memory to be given to the calling program
  7.     char   *back_border;   // The border bytes at the back of the user's memory
  8.     sequence::size_type i;              // Loop control variable
  9.  
  10.     // Allocate the block of memory for the user and for the two borders.
  11.     whole_block = (char *) malloc(2*BORDER_SIZE + size);
  12.     if (whole_block == NULL)
  13.     {
  14.         cout << "Insufficient memory for a call to the new operator." << endl;
  15.         exit(0);
  16.     }
  17.  
  18.     // Figure out the start points of the various pieces of the block.
  19.     size_spot = (sequence::size_type *) whole_block;
  20.     front_border = (char *) (whole_block + sizeof(sequence::size_type));
  21.     middle = (char *) (whole_block + BORDER_SIZE);
  22.     back_border = middle + size;
  23.  
  24.     // Put a copy of the size at the start of the block.
  25.     *size_spot = size;
  26.  
  27.     // Fill the borders and the middle section.
  28.     for (i = 0; i < BORDER_SIZE - sizeof(sequence::size_type); i++)
  29.         front_border[i] = BORDER;
  30.     for (i = 0; i < size; i++)
  31.         middle[i] = GARBAGE;
  32.     for (i = 0; i < BORDER_SIZE; i++)
  33.         back_border[i] = BORDER;
  34.  
  35.     // Update the global static variable showing how much memory is now used.
  36.     memory_used_now += size;
  37.  
  38.     return middle;
  39. }
  40.  
  41. void operator delete(void* p)
  42. {
  43.     char   *whole_block;   // Pointer to the entire block that we get from heap
  44.     sequence::size_type *size_spot;     // Spot in the block where to store a copy of size
  45.     char   *front_border;  // The border bytes in front of the user's memory
  46.     char   *middle;        // The memory to be given to the calling program
  47.     char   *back_border;   // The border bytes at the back of the user's memory
  48.     sequence::size_type i;              // Loop control variable
  49.     sequence::size_type size;           // Size of the block being returned
  50.     bool   corrupt;        // Set to true if the border was corrupted
  51.  
  52.     // Figure out the start of the pieces of the block, and the size.
  53.     whole_block = ((char *) (p)) - BORDER_SIZE;
  54.     size_spot = (sequence::size_type *) whole_block;
  55.     size = *size_spot;
  56.     front_border = (char *) (whole_block + sizeof(sequence::size_type));
  57.     middle = (char *) (whole_block + BORDER_SIZE);
  58.     back_border = middle + size;
  59.  
  60.     // Check the borders for the BORDER character.
  61.     corrupt = false;
  62.     for (i = 0; i < BORDER_SIZE - sizeof(sequence::size_type); i++)
  63.         if (front_border[i] != BORDER)
  64.             corrupt = true;
  65.     for (i = 0; i < BORDER_SIZE; i++)
  66.         if (back_border[i] != BORDER)
  67.             corrupt = true;
  68.  
  69.     if (corrupt)
  70.     {
  71.         cout << "The delete operator has detected that the program wrote\n";
  72.         cout << "beyond the ends of a block of memory that was allocated\n";
  73.         cout << "by the new operator. Program will be halted." << endl;
  74.         exit(0);
  75.     }
  76.     else
  77.     {
  78.         // Fill memory with garbage in case program tries to use it
  79.         // even after the delete.
  80.         for (i = 0; i < size + 2*BORDER_SIZE; i++)
  81.             whole_block[i] = GARBAGE;
  82.         free(whole_block);
  83.         memory_used_now -= size;
  84.     }
  85.  
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement