00001 /* 00002 * PROPRIETARY INFORMATION. This software is proprietary to 00003 * Side Effects Software Inc., and is not to be reproduced, 00004 * transmitted, or disclosed in any way without written permission. 00005 * 00006 * Produced by: 00007 * Side Effects Software Inc 00008 * 477 Richmond Street West 00009 * Toronto, Ontario 00010 * Canada M5V 3E7 00011 * 416-504-9876 00012 * 00013 * NAME: UT_FMalloc.h ( UT Library, C++) 00014 * 00015 * COMMENTS: Disk file based malloc. 00016 * 00017 * Memory is allocated and freed using fmalloc/fcalloc and ffree. 00018 * In order to read from the memory, you must call access(). This locks 00019 * the memory pages in RAM. 00020 * When the data has been modified, it should be flushed to disk using the 00021 * write() method. 00022 * When you no longer need to access the memory, close() should be called. 00023 * This will possibly free up RAM so that it can be used by other blocks. 00024 * 00025 */ 00026 00027 #ifndef __UT_FMalloc__ 00028 #define __UT_FMalloc__ 00029 00030 #include "UT_API.h" 00031 00032 #include <stdio.h> 00033 #include <SYS/SYS_Types.h> 00034 00035 class ut_FMemoryPage; 00036 00037 #define UT_FMALLOC_NBUCKETS 39 00038 00039 class UT_API UT_FMalloc { 00040 public: 00041 UT_FMalloc(const char *filename); 00042 ~UT_FMalloc(); 00043 00044 /// Allocate memory from the disk. This creates a memory block which can 00045 /// be accessed at a later time by locking the page in memory. 00046 ut_FMemoryPage *fmalloc(size_t size); 00047 00048 /// Free a block of memory. This allows the block to be re-used. If a 00049 /// locked page is freed, it's contents will not be written to disk 00050 /// regardless of it's read/write status. 00051 void ffree(ut_FMemoryPage *mem); 00052 00053 /// Lock the memory in RAM. If the page is marked as read-write, when it's 00054 /// unlocked, it will be re-flushed to disk (regardless of whether the 00055 /// block was actually modified). If it's not flagged as read-write, the 00056 /// in-core memory can be modified without changing the state of the disk 00057 /// contents. 00058 void *lock(ut_FMemoryPage *mem, bool readwrite=true); 00059 00060 /// If the page was locked as read-only, you can change the status to write 00061 /// by calling modify. Alternatively, you can abort the write to disk by 00062 /// passing readwrite=false. 00063 void modify(ut_FMemoryPage *mem, bool readwrite=true); 00064 00065 /// Unlocking the page will release the memory associated with the page. 00066 /// If the memory has been modified, the page will be written to disk 00067 /// (unless the modified flag has been turned off). 00068 void unlock(ut_FMemoryPage *mem); 00069 00070 /// Query methods for fmalloc. In theory, you probably don't need these, 00071 /// but it might be handy. 00072 /// @{ 00073 /// The file may not be allocated until a page is actually written to disk, 00074 /// so querying the file pointer may return a null pointer. 00075 FILE *getFile() { return myFile; } 00076 size_t getSize(ut_FMemoryPage *mem); 00077 int64 getSeek(ut_FMemoryPage *mem); 00078 /// This method will return a null ptr if there is no memory locked to the 00079 /// current page. This can be used to find out the state of the page. 00080 void *getLockedMemory(ut_FMemoryPage *mem); 00081 /// @} 00082 00083 /// This will garbage collect the disk file. All used pages will be 00084 /// compacted to a better memory layout. 00085 void compactTrash(); 00086 00087 /// Get the state of memory fragmentation. There's no metric defined for 00088 /// this yet. 00089 int getEntropy() const { return myEntropy; } 00090 00091 /// Debug builds only to dump statistics on memory usage. Please see 00092 /// UT_Memory for additional information. 00093 void dumpStats(); 00094 00095 private: 00096 // The free memory bins 00097 ut_FMemoryPage *myBins[UT_FMALLOC_NBUCKETS]; 00098 00099 // Pointers to used memory blocks (required during compacting) 00100 char *myPath; 00101 FILE *myFile; 00102 00103 // The entropy is a measure of how much fragmentation there is in the disk 00104 // file. This can be used for garbage collection of the used pages. 00105 int myEntropy; 00106 int64 myFileSize; 00107 int myBlockCount; 00108 }; 00109 00110 #endif
1.5.9