00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef __SYS_Memory__
00020 #define __SYS_Memory__
00021
00022 #include "SYS_API.h"
00023 #include <stdlib.h>
00024 #include "SYS_Types.h"
00025 #include "SYS_Align.h"
00026 #include "SYS_AtomicInt.h"
00027 #include "SYS_AtomicIntImpl.h"
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070 class SYS_API SYS_MemoryUser {
00071 public:
00072 SYS_MemoryUser(const char *label)
00073 : myUsed(0)
00074 , myPeak(0)
00075 {
00076
00077 myLabel = label;
00078 }
00079
00080 SYS_MemoryUser(const SYS_MemoryUser &rhs)
00081 : myUsed((int)rhs.myUsed)
00082 , myPeak((int)rhs.myPeak)
00083 {
00084 myLabel = rhs.myLabel;
00085 }
00086
00087 const char *getLabel() const { return myLabel; }
00088 size_t getUsed() const { return (int)myUsed; }
00089 size_t getPeak() const { return (int)myPeak; }
00090
00091 void inc(size_t amount)
00092 {
00093 myPeak.maximum(myUsed.add(amount));
00094 }
00095 void dec(size_t amount)
00096 {
00097 myUsed.add(-amount);
00098 }
00099
00100 private:
00101 const char *myLabel;
00102 SYS_AtomicInt32 myUsed;
00103 SYS_AtomicInt32 myPeak;
00104 };
00105
00106 class SYS_API SYS_MemoryTable {
00107 public:
00108 SYS_MemoryTable(SYS_MemoryUser *user_list, int number_of_users)
00109 {
00110 myTable = user_list;
00111 mySize = number_of_users;
00112 }
00113
00114 SYS_MemoryUser &getUser(int index) { return myTable[index]; }
00115 const SYS_MemoryUser &getUser(int index) const { return myTable[index]; }
00116
00117 int entries() const { return mySize; }
00118 size_t getUsed() const;
00119 size_t getPeak() const;
00120
00121 void inc(int user, size_t amount)
00122 {
00123 myTable[user].inc(amount);
00124 }
00125 void dec(int user, size_t amount)
00126 {
00127 myTable[user].dec(amount);
00128 }
00129
00130 private:
00131 SYS_MemoryUser *myTable;
00132 int mySize;
00133 };
00134
00135 class SYS_API SYS_Memory {
00136 public:
00137
00138
00139
00140 static void *Malloc(SYS_MemoryTable &table,
00141 int which_entry,
00142 size_t amount)
00143 {
00144 if (amount)
00145 {
00146 table.inc(which_entry, amount);
00147 return SYSamalloc(amount);
00148 }
00149 return 0;
00150 }
00151
00152
00153
00154 static void *Calloc(SYS_MemoryTable &table,
00155 int which_entry,
00156 size_t amount)
00157 {
00158 if (amount)
00159 {
00160 table.inc(which_entry, amount);
00161 return SYSacalloc(1, amount);
00162 }
00163 return 0;
00164 }
00165
00166
00167
00168
00169
00170 static void *Realloc(SYS_MemoryTable &table,
00171 int which_entry,
00172 void *original_memory,
00173 size_t old_amount,
00174 size_t new_amount)
00175 {
00176 void *mem;
00177 if (new_amount)
00178 {
00179 if (original_memory)
00180 {
00181
00182
00183
00184 table.dec(which_entry, old_amount);
00185 table.inc(which_entry, new_amount);
00186 mem = SYSarealloc(original_memory, new_amount);
00187 }
00188 else
00189 {
00190 table.inc(which_entry, new_amount);
00191 mem = SYSamalloc(new_amount);
00192 }
00193 }
00194 else
00195 {
00196 mem = 0;
00197 if (original_memory)
00198 {
00199 table.dec(which_entry, old_amount);
00200 SYSafree(original_memory);
00201 }
00202 }
00203 return mem;
00204 }
00205
00206
00207
00208
00209
00210 static void Free(SYS_MemoryTable &table,
00211 int which_entry,
00212 void *memory,
00213 size_t amount)
00214 {
00215 if (memory)
00216 {
00217 table.dec(which_entry, amount);
00218 SYSafree(memory);
00219 }
00220 }
00221
00222
00223 static void acquire(SYS_MemoryTable &table,
00224 int which_entry,
00225 size_t amount)
00226 {
00227 table.inc(which_entry, amount);
00228 }
00229
00230 static void release(SYS_MemoryTable &table,
00231 int which_entry,
00232 size_t amount)
00233 {
00234 table.dec(which_entry, amount);
00235 }
00236 };
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249 #define SYS_MEMORY_SUBCLASS(STATIC, prefix, table) \
00250 STATIC void *prefix##Malloc(int i, size_t amount) \
00251 { return SYS_Memory::Malloc(table, i, amount); } \
00252 STATIC void *prefix##Calloc(int i, size_t amount) \
00253 { return SYS_Memory::Calloc(table, i, amount); } \
00254 STATIC void *prefix##Realloc(int i, void *m, size_t o, size_t n) \
00255 { return SYS_Memory::Realloc(table, i, m, o, n); } \
00256 STATIC void prefix##Free(int i, void *m, size_t amount) \
00257 { SYS_Memory::Free(table, i, m, amount); } \
00258 STATIC void prefix##acquire(int i, size_t amount) \
00259 { SYS_Memory::acquire(table, i, amount); } \
00260 STATIC void prefix##release(int i, size_t amount) \
00261 { SYS_Memory::release(table, i, amount); } \
00262 static inline SYS_MemoryTable &prefix##getTable() { return table; }
00263
00264
00265
00266
00267
00268 #define SYS_MEMPRINTSIZE 32
00269 SYS_API void SYSprintMemory(char buf[SYS_MEMPRINTSIZE], int64 memory,
00270 int field_width=-1);
00271
00272 #endif