HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_FMalloc.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT_FMalloc.h ( UT Library, C++)
7  *
8  * COMMENTS: Disk file based malloc.
9  *
10  * Memory is allocated and freed using fmalloc/fcalloc and ffree.
11  * In order to read from the memory, you must call access(). This locks
12  * the memory pages in RAM.
13  * When the data has been modified, it should be flushed to disk using the
14  * write() method.
15  * When you no longer need to access the memory, close() should be called.
16  * This will possibly free up RAM so that it can be used by other blocks.
17  *
18  */
19 
20 #ifndef __UT_FMalloc__
21 #define __UT_FMalloc__
22 
23 #include "UT_API.h"
24 #include "UT_NonCopyable.h"
25 #include <SYS/SYS_Types.h>
26 #include <stdio.h>
27 
28 class ut_FMemoryPage;
29 
30 #define UT_FMALLOC_NBUCKETS 39
31 
33 public:
34  UT_FMalloc(const char *filename);
35  ~UT_FMalloc();
36 
38 
39  /// Allocate memory from the disk. This creates a memory block which can
40  /// be accessed at a later time by locking the page in memory.
41  ut_FMemoryPage *fmalloc(size_t size);
42 
43  /// Free a block of memory. This allows the block to be re-used. If a
44  /// locked page is freed, it's contents will not be written to disk
45  /// regardless of it's read/write status.
46  void ffree(ut_FMemoryPage *mem);
47 
48  /// Lock the memory in RAM. If the page is marked as read-write, when it's
49  /// unlocked, it will be re-flushed to disk (regardless of whether the
50  /// block was actually modified). If it's not flagged as read-write, the
51  /// in-core memory can be modified without changing the state of the disk
52  /// contents.
53  void *lock(ut_FMemoryPage *mem, bool readwrite=true);
54 
55  /// If the page was locked as read-only, you can change the status to write
56  /// by calling modify. Alternatively, you can abort the write to disk by
57  /// passing readwrite=false.
58  void modify(ut_FMemoryPage *mem, bool readwrite=true);
59 
60  /// Unlocking the page will release the memory associated with the page.
61  /// If the memory has been modified, the page will be written to disk
62  /// (unless the modified flag has been turned off).
63  void unlock(ut_FMemoryPage *mem);
64 
65  /// Query methods for fmalloc. In theory, you probably don't need these,
66  /// but it might be handy.
67  /// @{
68  /// The file may not be allocated until a page is actually written to disk,
69  /// so querying the file pointer may return a null pointer.
70  FILE *getFile() { return myFile; }
71  size_t getSize(ut_FMemoryPage *mem);
72  int64 getSeek(ut_FMemoryPage *mem);
73  /// This method will return a null ptr if there is no memory locked to the
74  /// current page. This can be used to find out the state of the page.
75  void *getLockedMemory(ut_FMemoryPage *mem);
76  /// @}
77 
78  /// This will garbage collect the disk file. All used pages will be
79  /// compacted to a better memory layout.
80  void compactTrash();
81 
82  /// Get the state of memory fragmentation. There's no metric defined for
83  /// this yet.
84  int getEntropy() const { return myEntropy; }
85 
86  /// Debug builds only to dump statistics on memory usage. Please see
87  /// UT_Memory for additional information.
88  void dumpStats();
89 
90 private:
91  // The free memory bins
92  ut_FMemoryPage *myBins[UT_FMALLOC_NBUCKETS];
93 
94  // Pointers to used memory blocks (required during compacting)
95  char *myPath;
96  FILE *myFile;
97 
98  // The entropy is a measure of how much fragmentation there is in the disk
99  // file. This can be used for garbage collection of the used pages.
100  int myEntropy;
101  int64 myFileSize;
102  int myBlockCount;
103 };
104 
105 #endif
GT_API const UT_StringHolder filename
#define UT_API
Definition: UT_API.h:14
#define UT_FMALLOC_NBUCKETS
Definition: UT_FMalloc.h:30
#define UT_NON_COPYABLE(CLASS)
Define deleted copy constructor and assignment operator inside a class.
long long int64
Definition: SYS_Types.h:116
GLsizeiptr size
Definition: glcorearb.h:664
int getEntropy() const
Definition: UT_FMalloc.h:84
FILE * getFile()
Definition: UT_FMalloc.h:70