#+TITLE: Chapter 6: Memory Management and Data Storage #+AUTHOR: Hermes & Claude #+DATE: 2026-10-07 #+PARENT: [[file:index.org][Index]] #+INCLUDE: goback.org * The Dual-Heap Philosophy Palm OS handles memory in a way that surprises almost every newcomer: 1. *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. 2. *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*. 3. *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. * Memory Allocations in the Dynamic Heap The *Dynamic Heap* is allocated at boot time for operating system runtime data and transient application needs: ** Pointers (~MemPtr~) vs. Movable Handles (~MemHandle~) To combat memory fragmentation in small RAM spaces, Palm OS supports two allocation types: 1. *Non-movable Chunks (~MemPtr~)*: #+BEGIN_SRC cpp void* ptr = MemPtrNew(size); // Address is fixed until freed MemPtrFree(ptr); #+END_SRC Use sparingly! Non-movable pointers fragment the limited Dynamic Heap. 2. *Movable Chunks (~MemHandle~)*: #+BEGIN_SRC cpp 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(MemHandleLock(handle)); // Use buffer... MemHandleUnlock(handle); // Release as soon as finished! MemHandleFree(handle); #+END_SRC * 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. ** Database Lifecycle 1. *Creating a Database*: #+BEGIN_SRC cpp DmCreateDatabase(0, "MyNotesDB", 'NOTE', 'appl', false); #+END_SRC 2. *Opening a Database*: #+BEGIN_SRC cpp DmOpenRef dbRef = DmOpenDatabaseByTypeCreator('appl', 'NOTE', dmModeReadWrite); #+END_SRC 3. *Adding a Record*: #+BEGIN_SRC cpp UInt16 recordIndex = dmMaxRecordIndex; // Append to end MemHandle recHandle = DmNewRecord(dbRef, &recordIndex, sizeof(MyRecordStruct)); #+END_SRC 4. *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: #+BEGIN_SRC cpp MyRecordStruct* recP = static_cast(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 #+END_SRC 5. *Closing the Database*: #+BEGIN_SRC cpp DmCloseDatabase(dbRef); #+END_SRC * 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: #+BEGIN_SRC cpp template 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(src); MemHandleUnlock(handle); return true; } }; #+END_SRC With this class: #+BEGIN_SRC cpp struct HighScore { UInt32 score; char initials[4]; }; PalmRecordStore store("ScoresDB", 'SCRS'); if (store.OpenOrCreate()) { HighScore entry{ 99500, "JDM" }; store.Append(entry); } #+END_SRC All lock/unlock operations, storage protection routines, and database handle cleanups are handled deterministically at compile-time with zero leak potential! #+LF * 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.