Files

137 lines
3.7 KiB
C
Raw Permalink Normal View History

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>
#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
class SafeBuf;
2013-08-02 13:12:24 -07:00
class Mem {
public:
Mem();
~Mem();
bool init ( );
2013-08-02 13:12:24 -07: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
size_t getUsedMem() const;
2013-08-02 13:12:24 -07:00
// the max mem ever alloced
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!
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; }
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?
int printBreeches () ;
2013-08-02 13:12:24 -07:00
// print mem usage stats
int printMem ( ) ;
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);
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)
size_t m_used;
2013-08-02 13:12:24 -07: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;
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;
static inline void *mmalloc(size_t size, const char *note) {
return g_mem.gbmalloc(size, note);
}
static inline void *mcalloc(size_t size, const char *note) {
return g_mem.gbcalloc(size, note);
}
static inline void *mrealloc(void *oldPtr, size_t oldSize, size_t newSize, const char *note) {
return g_mem.gbrealloc(oldPtr, oldSize, newSize, note);
}
static inline void mfree(void *ptr, size_t size, const char *note) {
return g_mem.gbfree(ptr, note, size, true);
}
static inline char *mdup(const void *data, size_t dataSize, const char *note) {
return g_mem.dup(data, dataSize, note);
}
static inline char *mstrdup(const char *string, const char *note) {
return g_mem.strdup(string, note);
}
static inline void mnew(void *ptr, size_t size, const char *note) {
return g_mem.addnew(ptr, size, note);
}
static inline void mdelete(void *ptr, size_t size, const char *note) {
return g_mem.delnew(ptr, size, note);
}
static inline bool relabel(void *ptr, size_t size, const char *note) {
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