2013-08-02 13:12:24 -07:00
|
|
|
// Matt Wells, copyright Sep 2001
|
|
|
|
|
|
|
|
// . mostly just wrappers for most memory functions
|
|
|
|
// . allows us to constrain memory
|
|
|
|
// . also calls mlockall() on construction to avoid swapping out any mem
|
|
|
|
// . TODO: primealloc(int slotSize,int numSlots) :
|
|
|
|
// pre-allocs a table of these slots for faster mmalloc'ing
|
|
|
|
|
2016-03-08 22:14:30 +01:00
|
|
|
#ifndef GB_MEM_H
|
|
|
|
#define GB_MEM_H
|
2013-08-02 13:12:24 -07:00
|
|
|
|
|
|
|
#include <new>
|
2016-03-14 00:37:18 +01:00
|
|
|
#include <stddef.h> //for NULL
|
|
|
|
#include <inttypes.h>
|
2016-06-28 11:01:47 +02:00
|
|
|
#include "Sanity.h"
|
2016-06-20 19:29:10 +02:00
|
|
|
|
2013-08-02 13:12:24 -07:00
|
|
|
|
2014-08-28 12:55:02 -07:00
|
|
|
class SafeBuf;
|
2013-08-02 13:12:24 -07:00
|
|
|
|
|
|
|
|
|
|
|
class Mem {
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
Mem();
|
|
|
|
~Mem();
|
|
|
|
|
2016-04-28 13:47:33 +02:00
|
|
|
bool init ( );
|
2013-08-02 13:12:24 -07:00
|
|
|
|
2016-08-15 15:15:28 +02:00
|
|
|
void *gbmalloc ( size_t size , const char *note );
|
|
|
|
void *gbcalloc ( size_t size , const char *note);
|
|
|
|
void *gbrealloc ( void *oldPtr, size_t oldSize, size_t newSize, const char *note);
|
|
|
|
void gbfree(void *ptr, const char *note, size_t size, bool checksize);
|
|
|
|
char *dup ( const void *data , size_t dataSize , const char *note);
|
2016-07-28 17:19:14 +02:00
|
|
|
char *strdup ( const char *string, const char *note );
|
2013-08-02 13:12:24 -07:00
|
|
|
|
2014-11-10 14:45:11 -08:00
|
|
|
int32_t validate();
|
2013-08-02 13:12:24 -07:00
|
|
|
|
|
|
|
// this one does not include new/delete mem, only *alloc()/free() mem
|
2016-08-15 15:15:28 +02:00
|
|
|
size_t getUsedMem() const;
|
2013-08-02 13:12:24 -07:00
|
|
|
// the max mem ever alloced
|
2016-08-15 15:15:28 +02:00
|
|
|
size_t getMaxAlloced() const { return m_maxAlloced; }
|
|
|
|
size_t getMaxAlloc () const { return m_maxAlloc; }
|
2016-08-09 22:53:57 +02:00
|
|
|
const char *getMaxAllocBy() const { return m_maxAllocBy; }
|
2013-08-02 13:12:24 -07:00
|
|
|
// the max mem we can use!
|
2016-08-15 15:15:28 +02:00
|
|
|
size_t getMaxMem() const;
|
2013-08-02 13:12:24 -07:00
|
|
|
|
2016-08-09 22:53:57 +02:00
|
|
|
int32_t getNumAllocated() const { return m_numAllocated; }
|
2013-08-02 13:12:24 -07:00
|
|
|
|
2016-08-09 22:53:57 +02:00
|
|
|
int64_t getNumTotalAllocated() const { return m_numTotalAllocated; }
|
2016-08-08 12:57:00 +02:00
|
|
|
|
|
|
|
float getUsedMemPercentage() const;
|
2016-08-08 14:13:24 +02:00
|
|
|
int32_t getOOMCount() const { return m_outOfMems; }
|
|
|
|
uint32_t getMemTableSize() const { return m_memtablesize; }
|
|
|
|
int64_t getFreeMem() const;
|
|
|
|
void setMemTableSize(uint32_t sz) { m_memtablesize = sz; }
|
|
|
|
|
|
|
|
void incrementOOMCount() { m_outOfMems++; }
|
2013-08-02 13:12:24 -07:00
|
|
|
|
|
|
|
// who underan/overran their buffers?
|
2016-07-21 15:13:17 +02:00
|
|
|
int printBreeches () ;
|
2013-08-02 13:12:24 -07:00
|
|
|
// print mem usage stats
|
|
|
|
int printMem ( ) ;
|
|
|
|
|
2016-08-15 15:15:28 +02:00
|
|
|
void addMem(void *mem, size_t size, const char *note, char isnew);
|
|
|
|
bool rmMem(void *mem, size_t size, const char *note);
|
|
|
|
bool lblMem(void *mem, size_t size, const char *note);
|
|
|
|
void addnew(void *ptr, size_t size, const char *note);
|
|
|
|
void delnew(void *ptr, size_t size, const char *note);
|
2013-08-02 13:12:24 -07:00
|
|
|
|
|
|
|
bool printMemBreakdownTable(SafeBuf* sb,
|
|
|
|
char *lightblue,
|
|
|
|
char *darkblue);
|
|
|
|
|
2016-08-15 15:15:28 +02:00
|
|
|
size_t m_maxAlloced; // at any one time
|
|
|
|
size_t m_maxAlloc; // the biggest single alloc ever done
|
2013-08-02 13:12:24 -07:00
|
|
|
const char *m_maxAllocBy; // the biggest single alloc ever done
|
|
|
|
|
2016-08-08 14:13:24 +02:00
|
|
|
private:
|
2016-08-09 22:57:43 +02:00
|
|
|
int32_t getMemSlot(void *mem);
|
|
|
|
|
2013-08-02 13:12:24 -07:00
|
|
|
// currently used mem (estimate)
|
2016-08-15 15:15:28 +02:00
|
|
|
size_t m_used;
|
2013-08-02 13:12:24 -07:00
|
|
|
|
2014-12-16 16:22:50 -08:00
|
|
|
// count how many allocs/news failed
|
|
|
|
int32_t m_outOfMems;
|
|
|
|
|
2014-11-10 14:45:11 -08:00
|
|
|
int32_t m_numAllocated;
|
2014-10-30 13:36:39 -06:00
|
|
|
int64_t m_numTotalAllocated;
|
2014-11-10 14:45:11 -08:00
|
|
|
uint32_t m_memtablesize;
|
2016-07-28 16:33:43 +02:00
|
|
|
|
|
|
|
int printBreeches_unlocked();
|
2016-08-09 22:57:43 +02:00
|
|
|
int printBreech(int32_t i);
|
2013-08-02 13:12:24 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
extern class Mem g_mem;
|
|
|
|
|
2016-08-15 15:15:28 +02:00
|
|
|
static inline void *mmalloc(size_t size, const char *note) {
|
2016-08-15 14:46:49 +02:00
|
|
|
return g_mem.gbmalloc(size, note);
|
|
|
|
}
|
|
|
|
|
2016-08-15 15:15:28 +02:00
|
|
|
static inline void *mcalloc(size_t size, const char *note) {
|
2016-08-15 14:46:49 +02:00
|
|
|
return g_mem.gbcalloc(size, note);
|
|
|
|
}
|
|
|
|
|
2016-08-15 15:15:28 +02:00
|
|
|
static inline void *mrealloc(void *oldPtr, size_t oldSize, size_t newSize, const char *note) {
|
2016-08-15 14:46:49 +02:00
|
|
|
return g_mem.gbrealloc(oldPtr, oldSize, newSize, note);
|
|
|
|
}
|
|
|
|
|
2016-08-15 15:15:28 +02:00
|
|
|
static inline void mfree(void *ptr, size_t size, const char *note) {
|
2016-08-15 14:46:49 +02:00
|
|
|
return g_mem.gbfree(ptr, note, size, true);
|
|
|
|
}
|
|
|
|
|
2016-08-15 15:15:28 +02:00
|
|
|
static inline char *mdup(const void *data, size_t dataSize, const char *note) {
|
2016-08-15 14:46:49 +02:00
|
|
|
return g_mem.dup(data, dataSize, note);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline char *mstrdup(const char *string, const char *note) {
|
|
|
|
return g_mem.strdup(string, note);
|
|
|
|
}
|
|
|
|
|
2016-08-15 15:15:28 +02:00
|
|
|
static inline void mnew(void *ptr, size_t size, const char *note) {
|
2016-08-15 14:46:49 +02:00
|
|
|
return g_mem.addnew(ptr, size, note);
|
|
|
|
}
|
|
|
|
|
2016-08-15 15:15:28 +02:00
|
|
|
static inline void mdelete(void *ptr, size_t size, const char *note) {
|
2016-08-15 14:46:49 +02:00
|
|
|
return g_mem.delnew(ptr, size, note);
|
|
|
|
}
|
|
|
|
|
2016-08-15 15:15:28 +02:00
|
|
|
static inline bool relabel(void *ptr, size_t size, const char *note) {
|
2016-08-15 14:46:49 +02:00
|
|
|
return g_mem.lblMem(ptr, size, note);
|
|
|
|
}
|
2013-08-02 13:12:24 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
2016-03-08 22:14:30 +01:00
|
|
|
#endif // GB_MEM_H
|