HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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 
25 #include <stdio.h>
26 #include <SYS/SYS_Types.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 
37  /// Allocate memory from the disk. This creates a memory block which can
38  /// be accessed at a later time by locking the page in memory.
39  ut_FMemoryPage *fmalloc(size_t size);
40 
41  /// Free a block of memory. This allows the block to be re-used. If a
42  /// locked page is freed, it's contents will not be written to disk
43  /// regardless of it's read/write status.
44  void ffree(ut_FMemoryPage *mem);
45 
46  /// Lock the memory in RAM. If the page is marked as read-write, when it's
47  /// unlocked, it will be re-flushed to disk (regardless of whether the
48  /// block was actually modified). If it's not flagged as read-write, the
49  /// in-core memory can be modified without changing the state of the disk
50  /// contents.
51  void *lock(ut_FMemoryPage *mem, bool readwrite=true);
52 
53  /// If the page was locked as read-only, you can change the status to write
54  /// by calling modify. Alternatively, you can abort the write to disk by
55  /// passing readwrite=false.
56  void modify(ut_FMemoryPage *mem, bool readwrite=true);
57 
58  /// Unlocking the page will release the memory associated with the page.
59  /// If the memory has been modified, the page will be written to disk
60  /// (unless the modified flag has been turned off).
61  void unlock(ut_FMemoryPage *mem);
62 
63  /// Query methods for fmalloc. In theory, you probably don't need these,
64  /// but it might be handy.
65  /// @{
66  /// The file may not be allocated until a page is actually written to disk,
67  /// so querying the file pointer may return a null pointer.
68  FILE *getFile() { return myFile; }
69  size_t getSize(ut_FMemoryPage *mem);
70  int64 getSeek(ut_FMemoryPage *mem);
71  /// This method will return a null ptr if there is no memory locked to the
72  /// current page. This can be used to find out the state of the page.
73  void *getLockedMemory(ut_FMemoryPage *mem);
74  /// @}
75 
76  /// This will garbage collect the disk file. All used pages will be
77  /// compacted to a better memory layout.
78  void compactTrash();
79 
80  /// Get the state of memory fragmentation. There's no metric defined for
81  /// this yet.
82  int getEntropy() const { return myEntropy; }
83 
84  /// Debug builds only to dump statistics on memory usage. Please see
85  /// UT_Memory for additional information.
86  void dumpStats();
87 
88 private:
89  // The free memory bins
90  ut_FMemoryPage *myBins[UT_FMALLOC_NBUCKETS];
91 
92  // Pointers to used memory blocks (required during compacting)
93  char *myPath;
94  FILE *myFile;
95 
96  // The entropy is a measure of how much fragmentation there is in the disk
97  // file. This can be used for garbage collection of the used pages.
98  int myEntropy;
99  int64 myFileSize;
100  int myBlockCount;
101 };
102 
103 #endif
#define UT_API
Definition: UT_API.h:12
GLsizeiptr size
Definition: glcorearb.h:663
long long int64
Definition: SYS_Types.h:106
#define UT_FMALLOC_NBUCKETS
Definition: UT_FMalloc.h:30
int getEntropy() const
Definition: UT_FMalloc.h:82
FILE * getFile()
Definition: UT_FMalloc.h:68