#include #include #include "cookie.h" using namespace std; char cookie::buffer[n * sizeof (cookie)]; bool cookie::b[n]; void *cookie::operator new(size_t) { for (size_t i = 0; i < n; ++i) { if (!b[i]) { b[i] = true; cookie *const p = reinterpret_cast(buffer) + i; cout << "cookie::operator new() returns " << p << " [block " << i << "].\n"; return p; } } cerr << "cookie::operator new out of store\n"; exit(EXIT_FAILURE); } void cookie::operator delete(void *p) { if (p < buffer || p >= buffer + sizeof buffer || (static_cast(p) - buffer) % sizeof (cookie) != 0) { cerr << "cookie::operator delete: " << p << " not from previous cookie::operator new.\n"; exit(EXIT_FAILURE); } const size_t i = static_cast(p) - reinterpret_cast(buffer); if (!b[i]) { cerr << "cookie::operator delete: " << p << " [block " << i << "] not currently allocated.\n"; exit(EXIT_FAILURE); } cout << "cookie::operator delete(" << p << ") [block " << i << "].\n"; b[i] = false; }