Chapter 6: Memory Management and Data Storage
This is an LLM ass-isted document, as are all in the ass/ subdirectory.
Of these, here in the dump/ subdirectory I keep the sloppy & doubtful ones.
I keep these as learning materials for myself, yet to be went thru.
1. The Dual-Heap Philosophy
Palm OS handles memory in a way that surprises almost every newcomer:
- Memory is non-volatile: The battery in a Palm device keeps the static RAM chips powered 24/7. When a user powers off their Palm, the memory contents remain completely intact.
- Files are records in RAM: There is no hard disk or flash file system. Databases reside directly in battery-backed RAM in the Storage Heap.
- Direct Memory Protection: Because all databases live in RAM, Palm OS hardware locks the Storage Heap against arbitrary memory writes to protect against errant wild pointers! You cannot simply write
*ptr = value;to a database record — doing so generates a fatal Memory Manager write-protect bus error.
2. Memory Allocations in the Dynamic Heap
The Dynamic Heap is allocated at boot time for operating system runtime data and transient application needs:
2.1. Pointers (MemPtr) vs. Movable Handles (MemHandle)
To combat memory fragmentation in small RAM spaces, Palm OS supports two allocation types:
Non-movable Chunks (
MemPtr):void* ptr = MemPtrNew(size); // Address is fixed until freed MemPtrFree(ptr);
Use sparingly! Non-movable pointers fragment the limited Dynamic Heap.
Movable Chunks (
MemHandle):void* handle = MemHandleNew(size); // The OS may shift the physical buffer in RAM to compact memory. // You must LOCK the handle to access it: char* buffer = static_cast<char*>(MemHandleLock(handle)); // Use buffer... MemHandleUnlock(handle); // Release as soon as finished! MemHandleFree(handle);
3. The Data Manager: Record Databases
A Palm OS record database (.pdb) is a named collection of numbered records identified by a 4-byte Creator ID and a 4-byte Type.
3.1. Database Lifecycle
Creating a Database:
DmCreateDatabase(0, "MyNotesDB", 'NOTE', 'appl', false);
Opening a Database:
DmOpenRef dbRef = DmOpenDatabaseByTypeCreator('appl', 'NOTE', dmModeReadWrite);
Adding a Record:
UInt16 recordIndex = dmMaxRecordIndex; // Append to end MemHandle recHandle = DmNewRecord(dbRef, &recordIndex, sizeof(MyRecordStruct));
Writing to a Record (The DmWrite Rule): Because the record lives in the write-protected Storage Heap, you must write through the Data Manager API:
MyRecordStruct* recP = static_cast<MyRecordStruct*>(MemHandleLock(recHandle)); MyRecordStruct data = { 42, "Note content" }; // Write safely past hardware memory write-protection: DmWrite(recP, 0, &data, sizeof(MyRecordStruct)); MemHandleUnlock(recHandle); DmReleaseRecord(dbRef, recordIndex, true); // true = mark record dirty
Closing the Database:
DmCloseDatabase(dbRef);
4. Building a C++20 Type-Safe Record Interface
Let us wrap the Data Manager in modern C++ abstractions to make database operations safe, expressive, and impossible to leak:
template <typename T> class PalmRecordStore { void* dbRef_ = nullptr; const char* name_; UInt32 creator_; public: PalmRecordStore(const char* name, UInt32 creator) : name_(name), creator_(creator) {} bool OpenOrCreate() { dbRef_ = DmOpenDatabaseByTypeCreator('DATA', creator_, 0x0002 /* dmModeReadWrite */); if (!dbRef_) { // Create database if not present DmCreateDatabase(0, name_, creator_, 'DATA', false); dbRef_ = DmOpenDatabaseByTypeCreator('DATA', creator_, 0x0002); } return dbRef_ != nullptr; } ~PalmRecordStore() { if (dbRef_) { DmCloseDatabase(dbRef_); } } UInt16 Count() const { return dbRef_ ? DmNumRecords(dbRef_) : 0; } bool Append(const T& record) { if (!dbRef_) return false; UInt16 idx = 0xFFFF; // dmMaxRecordIndex void* handle = DmNewRecord(dbRef_, &idx, sizeof(T)); if (!handle) return false; void* dest = MemHandleLock(handle); DmWrite(dest, 0, &record, sizeof(T)); MemHandleUnlock(handle); DmReleaseRecord(dbRef_, idx, true); return true; } bool Read(UInt16 index, T& outRecord) const { if (!dbRef_ || index >= Count()) return false; void* handle = DmQueryRecord(dbRef_, index); if (!handle) return false; const void* src = MemHandleLock(handle); outRecord = *static_cast<const T*>(src); MemHandleUnlock(handle); return true; } };
With this class:
struct HighScore { UInt32 score; char initials[4]; }; PalmRecordStore<HighScore> store("ScoresDB", 'SCRS'); if (store.OpenOrCreate()) { HighScore entry{ 99500, "JDM" }; store.Append(entry); }
All lock/unlock operations, storage protection routines, and database handle cleanups are handled deterministically at compile-time with zero leak potential!
5. Further Reading & Reference Resources
- Data Manager & Memory Architecture:
- PalmSource Palm OS Companion: Memory Architecture and Data Manager: https://palm.wiki/development/docs/601/PalmOSCompanion/DataAndMemory_Chapter.html
- Lonnon Foster: Inside Palm OS Databases: O'Reilly Palm Developer Center archive.
- Palm Database Tools & Formats:
- PDB and PRC File Format Specifications: https://wiki.mobileread.com/wiki/PDB
- Pilot-Link Suite (Unix tools for inspecting and syncing PDB/PRC databases): https://github.com/jichu4n/pilot-link
- Modern RAII & Zero-Leak Memory Idioms:
- Scott Meyers: Effective Modern C++. O'Reilly Media.
- Herb Sutter: Exceptional C++: 47 Engineering Puzzles, Programming Problems, and Solutions. Addison-Wesley.