SourceMod SDK  1.7
sm_memtable.h
1 
32 #ifndef _INCLUDE_SOURCEMOD_CORE_STRINGTABLE_H_
33 #define _INCLUDE_SOURCEMOD_CORE_STRINGTABLE_H_
34 
35 #include <stdlib.h>
36 #include <string.h>
37 
39 {
40 public:
41  BaseMemTable(unsigned int init_size)
42  {
43  membase = (unsigned char *)malloc(init_size);
44  size = init_size;
45  tail = 0;
46  }
47  ~BaseMemTable()
48  {
49  free(membase);
50  membase = NULL;
51  }
52 public:
58  int CreateMem(unsigned int addsize, void **addr)
59  {
60  int idx = (int)tail;
61 
62  while (tail + addsize >= size) {
63  size *= 2;
64  membase = (unsigned char *)realloc(membase, size);
65  }
66 
67  tail += addsize;
68  if (addr)
69  *addr = (void *)&membase[idx];
70 
71  return idx;
72  }
73 
78  void *GetAddress(int index)
79  {
80  if (index < 0 || (unsigned int)index >= tail)
81  return NULL;
82  return &membase[index];
83  }
84 
90  void Reset()
91  {
92  tail = 0;
93  }
94 
95  inline unsigned int GetMemUsage()
96  {
97  return size;
98  }
99 
100  inline unsigned int GetActualMemUsed()
101  {
102  return tail;
103  }
104 
105 private:
106  unsigned char *membase;
107  unsigned int size;
108  unsigned int tail;
109 };
110 
112 {
113 public:
114  BaseStringTable(unsigned int init_size) : m_table(init_size)
115  {
116  }
117 public:
121  int AddString(const char *string)
122  {
123  return AddString(string, strlen(string));
124  }
125 
129  int AddString(const char *string, size_t length)
130  {
131  size_t len = length + 1;
132  int idx;
133  char *addr;
134 
135  idx = m_table.CreateMem(len, (void **)&addr);
136  memcpy(addr, string, length + 1);
137  return idx;
138  }
139 
143  inline const char *GetString(int str)
144  {
145  return (const char *)m_table.GetAddress(str);
146  }
147 
153  void Reset()
154  {
155  m_table.Reset();
156  }
157 
162  {
163  return &m_table;
164  }
165 private:
166  BaseMemTable m_table;
167 };
168 
169 #endif //_INCLUDE_SOURCEMOD_CORE_STRINGTABLE_H_
170 
void * GetAddress(int index)
Definition: sm_memtable.h:78
BaseMemTable * GetMemTable()
Definition: sm_memtable.h:161
void Reset()
Definition: sm_memtable.h:90
Definition: sm_memtable.h:38
void Reset()
Definition: sm_memtable.h:153
Definition: sm_memtable.h:111
int AddString(const char *string)
Definition: sm_memtable.h:121
const char * GetString(int str)
Definition: sm_memtable.h:143
int AddString(const char *string, size_t length)
Definition: sm_memtable.h:129
int CreateMem(unsigned int addsize, void **addr)
Definition: sm_memtable.h:58