A customer asked me an interesting question today. He wanted to know how to know how much space his system needed for the Object Store. That had me thinking that there could be a way to monitor the size and automatically adjust it, Windows Mobile does that. Of course Windows Mobile probably has a better way to manage the Object Store size than this, I am guessing that it monitors the size within FileSys and adjusts it as requests come in.
I wrote the following code just to see what could be done. I don’t necessary think that this is a good way to do it, but it is something that could be used to monitor the Object Store.
CheckObjectStoreSize() will check the current number of pages being used by the Object Store and will adjust the size to the maximum of:
-          The documented minimum size of 128K
o   #define DOCUMENTED_MIN_OS_PAGES( PageSize ) (( 128 * 1024 )/PageSize )

-          Current number of pages being used plus MIN_FREE_PAGES, which is a random number that I picked
o   #define MIN_FREE_PAGES( PageSize )          ((100 * 1024)/PageSize)
Because the number of pages being used is obtained using GetSStoreInformation() this function will both increase and decrease the size of the Object Store.
void CheckObjectStoreSize()
                DWORD StorePages,
                DWORD RamPages
                DWORD PageSize;
                DWORD CurrentNeededSize;
                DWORD Status;
                DWORD ObjectStorePages;
                STORE_INFORMATION StoreInfo;
                GetSystemMemoryDivision(&StorePages, &RamPages, &PageSize);
                CurrentNeededSize = ((StoreInfo.dwStoreSize - StoreInfo.dwFreeSize)/PageSize) + 1 ;
                ObjectStorePages = DOCUMENTED_MIN_OS_PAGES(PageSize);
                // Check to see if the current Object Store needs to be bigger   
                if( ObjectStorePages < (CurrentNeededSize + MIN_FREE_PAGES( PageSize) ))
                                ObjectStorePages = (CurrentNeededSize + MIN_FREE_PAGES( PageSize));
                // Do we have enough free space to increase the Object Store?               
                if (ObjectStorePages >= (StorePages + RamPages))
                                RETAILMSG(1,(TEXT("Out of memory to increase Object Store\n")));
                Status = SetSystemMemoryDivision (ObjectStorePages);
                if (Status)
                                RETAILMSG(1,(TEXT("Failed to set Object Store: ")));
                                                case SYSMEM_MUSTREBOOT:
                                                                RETAILMSG(1,(TEXT(" SYSMEM_MUSTREBOOT\n")));
                                                case SYSMEM_REBOOTPENDING:
                                                                RETAILMSG(1,(TEXT(" SYSMEM_REBOOTPENDING\n")));
                                                case SYSMEM_FAILED:
                                                                RETAILMSG(1,(TEXT(" SYSMEM_FAILED (%d)\n", GetLastError())));
I then tested the CheckObjectStoreSize() by calling it in a loop from WinMain(). The loop copies a file from the storage card to the Object Store 50 times followed by deleting the files on the next 40 times through the loop.
int WINAPI WinMain(
                HINSTANCE hInstance,
                HINSTANCE hPrevInstance,
                LPTSTR    lpCmdLine,
                int       nCmdShow
                DWORD Counter = 0;
                TCHAR FileName[ MAX_PATH ];
                BOOL Toggle = FALSE;
                while( 1 )
                                Sleep( 5000 );
                                wsprintf( FileName, TEXT("\\File%d.txt"), Counter++ );
                                RETAILMSG( 1, (TEXT("Copy to %s\n"), FileName ));
                                if( Counter > 50 )
                                                Toggle = !Toggle;
                                                Counter = 0;
                                if( Toggle )
                                                if( !DeleteFile( FileName ))
                                                                RETAILMSG( 1, (TEXT("File Delete failed %d\n"), GetLastError() ));
                                                if( !CopyFile( TEXT("\\Storage Card\\a.txt"), FileName, FALSE ))
                                                                RETAILMSG( 1, (TEXT("File Copy failed %d\n"), GetLastError() ));
Copyright © 2008 – Bruce Eitman
All Rights Reserved