Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version

Undo and Redo the "Easy" Way

, 20 Jun 2004
This article introduces a simple approach to in-memory transactions that can be used to implement Undo and Redo. The technique uses SEH and Virtual Memory and requires only STL and Win32.
transactions_complete.zip
Common
CER
Curve Simplification
Transactions
Utilities
DrawIt
res
DrawIt.ico
DrawItDoc.ico
Toolbar.bmp
vssver.scc
DrawItMDI
res
DrawItMDI.ico
DrawItMDI.manifest
DrawItMDIDoc.ico
mainfram.bmp
Toolbar.bmp
MemTest
Bin
release
DrawFunc.dll
DrawIt.exe
DrawItMDI.exe
XCrashReport.exe
XCrashReport.ini
DrawFunc
transactions_demo.zip
transactions_mfc_demo.zip
DrawIt.ico
DrawItDoc.ico
Toolbar.bmp
vssver.scc
transactions_mfc_mdi_demo.zip
DrawItMDI.ico
DrawItMDI.manifest
DrawItMDIDoc.ico
mainfram.bmp
Toolbar.bmp
transactions_src.zip
Transactions
#ifndef MEM_MANAGER_H_INCLUDED
#define MEM_MANAGER_H_INCLUDED

#include "Utilities\Result.h"

namespace Mm
{
	// types
	typedef unsigned long TXNID;
	typedef unsigned long SPACEID;

	// setup
	bool InstallExceptionFilter();
	bool RemoveExceptionFilter();
	int ExceptionFilter(void* e);

	// space (heap) managment
	Result	InitSpaceZero(); // read the documentation

	SPACEID CreateSpace(size_t initial_size);
	Result	Destroy(SPACEID sid);
	Result 	Clear(SPACEID sid);

	Result ClearAll();

	// transactions
	Result OpenTransaction(SPACEID sid);
	Result CancelTransaction(SPACEID sid);
	Result CommitTransaction(SPACEID sid);

	TXNID  GetLastTransactionId(SPACEID sid);
	TXNID  GetNextTransactionId(SPACEID sid);
	TXNID  GetOpenTransactionId(SPACEID sid);

	Result Undo(SPACEID sid);
	Result Redo(SPACEID sid);

	Result TruncateUndo(SPACEID sid);
	Result TruncateRedo(SPACEID sid);

	// allocation
	void* Allocate(size_t size, SPACEID sid);
	void* Allocate(void* hint, size_t size); // does space lookup, slower, used by Mm::Allocator
	
	void  Deallocate(void* p); // does space lookup, slower
	void  Deallocate(void* p, SPACEID sid);
	void  Deallocate(void* p, size_t size); // used by Mm::Allocator
	
	void* Reallocate(void* p, size_t size); // don't pass null p
	void* Reallocate(void* p, size_t size, SPACEID sid); // null p pkay

	// debug
	size_t FreeSpace(SPACEID sid);
	size_t FreeCount(SPACEID sid);

//	void DumpStats(std::ostream& o);

	class AutoTransaction {
	private:
		AutoTransaction(const AutoTransaction& );
		operator=(const AutoTransaction&);

	public:
		AutoTransaction(SPACEID s) : sid(s), txnid(NULL), cancel(false) 
		{ 
			if (Mm::GetOpenTransactionId(sid) == NULL && !Mm::OpenTransaction(sid).IsError()) 
				txnid = Mm::GetOpenTransactionId(sid); 
		}
		
		~AutoTransaction()
		{
			if (txnid == NULL) return;
			if (txnid != Mm::GetOpenTransactionId(sid)) throw;

			if (cancel) Mm::CancelTransaction(sid);
			else Mm::CommitTransaction(sid);
		}

		Mm::SPACEID sid;
		Mm::TXNID txnid;
		bool cancel;
	};
};

#endif //

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

compiler

United States United States
A compiler warns of bogasity, ignore it at your peril. Unless you've done the compiler's job yourself, don't criticize it.

| Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 21 Jun 2004
Article Copyright 2002 by compiler
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid