Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void* operator new(sequence::size_type size)
- {
- char *whole_block; // Pointer to the entire block that we get from heap
- sequence::size_type *size_spot; // Spot in the block where to store a copy of size
- char *front_border; // The border bytes in front of the user's memory
- char *middle; // The memory to be given to the calling program
- char *back_border; // The border bytes at the back of the user's memory
- sequence::size_type i; // Loop control variable
- // Allocate the block of memory for the user and for the two borders.
- whole_block = (char *) malloc(2*BORDER_SIZE + size);
- if (whole_block == NULL)
- {
- cout << "Insufficient memory for a call to the new operator." << endl;
- exit(0);
- }
- // Figure out the start points of the various pieces of the block.
- size_spot = (sequence::size_type *) whole_block;
- front_border = (char *) (whole_block + sizeof(sequence::size_type));
- middle = (char *) (whole_block + BORDER_SIZE);
- back_border = middle + size;
- // Put a copy of the size at the start of the block.
- *size_spot = size;
- // Fill the borders and the middle section.
- for (i = 0; i < BORDER_SIZE - sizeof(sequence::size_type); i++)
- front_border[i] = BORDER;
- for (i = 0; i < size; i++)
- middle[i] = GARBAGE;
- for (i = 0; i < BORDER_SIZE; i++)
- back_border[i] = BORDER;
- // Update the global static variable showing how much memory is now used.
- memory_used_now += size;
- return middle;
- }
- void operator delete(void* p)
- {
- char *whole_block; // Pointer to the entire block that we get from heap
- sequence::size_type *size_spot; // Spot in the block where to store a copy of size
- char *front_border; // The border bytes in front of the user's memory
- char *middle; // The memory to be given to the calling program
- char *back_border; // The border bytes at the back of the user's memory
- sequence::size_type i; // Loop control variable
- sequence::size_type size; // Size of the block being returned
- bool corrupt; // Set to true if the border was corrupted
- // Figure out the start of the pieces of the block, and the size.
- whole_block = ((char *) (p)) - BORDER_SIZE;
- size_spot = (sequence::size_type *) whole_block;
- size = *size_spot;
- front_border = (char *) (whole_block + sizeof(sequence::size_type));
- middle = (char *) (whole_block + BORDER_SIZE);
- back_border = middle + size;
- // Check the borders for the BORDER character.
- corrupt = false;
- for (i = 0; i < BORDER_SIZE - sizeof(sequence::size_type); i++)
- if (front_border[i] != BORDER)
- corrupt = true;
- for (i = 0; i < BORDER_SIZE; i++)
- if (back_border[i] != BORDER)
- corrupt = true;
- if (corrupt)
- {
- cout << "The delete operator has detected that the program wrote\n";
- cout << "beyond the ends of a block of memory that was allocated\n";
- cout << "by the new operator. Program will be halted." << endl;
- exit(0);
- }
- else
- {
- // Fill memory with garbage in case program tries to use it
- // even after the delete.
- for (i = 0; i < size + 2*BORDER_SIZE; i++)
- whole_block[i] = GARBAGE;
- free(whole_block);
- memory_used_now -= size;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement