core/sm_memtable.h
author David Anderson <dvander@alliedmods.net>
Sat Nov 29 16:58:59 2008 -0600 (2008-11-29)
changeset 2437 64492591f381
parent 2289 44045ead61cd
child 3003 489c2b05a1b2
permissions -rw-r--r--
Triggered build, something funky happened....
dvander@370
     1
/**
ds@653
     2
 * vim: set ts=4 :
ds@1330
     3
 * =============================================================================
dvander@1231
     4
 * SourceMod
ds@1947
     5
 * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
ds@1330
     6
 * =============================================================================
dvander@370
     7
 *
ds@1330
     8
 * This program is free software; you can redistribute it and/or modify it under
ds@1330
     9
 * the terms of the GNU General Public License, version 3.0, as published by the
ds@1330
    10
 * Free Software Foundation.
dvander@1231
    11
 * 
ds@1330
    12
 * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1330
    13
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1330
    14
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
ds@1330
    15
 * details.
dvander@1231
    16
 *
ds@1330
    17
 * You should have received a copy of the GNU General Public License along with
ds@1330
    18
 * this program.  If not, see <http://www.gnu.org/licenses/>.
dvander@1231
    19
 *
ds@1330
    20
 * As a special exception, AlliedModders LLC gives you permission to link the
ds@1330
    21
 * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1330
    22
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1330
    23
 * by the Valve Corporation.  You must obey the GNU General Public License in
ds@1330
    24
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
ds@1330
    25
 * this exception to all derivative works.  AlliedModders LLC defines further
ds@1330
    26
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1330
    27
 * or <http://www.sourcemod.net/license.php>.
dvander@370
    28
 *
dvander@370
    29
 * Version: $Id$
dvander@370
    30
 */
dvander@370
    31
dvander@203
    32
#ifndef _INCLUDE_SOURCEMOD_CORE_STRINGTABLE_H_
dvander@203
    33
#define _INCLUDE_SOURCEMOD_CORE_STRINGTABLE_H_
dvander@203
    34
dvander@203
    35
class BaseMemTable
dvander@203
    36
{
dvander@203
    37
public:
dvander@203
    38
	BaseMemTable(unsigned int init_size);
dvander@203
    39
	~BaseMemTable();
dvander@203
    40
public:
dvander@203
    41
	/**
dvander@203
    42
	 * Allocates 'size' bytes of memory.
dvander@203
    43
	 * Optionally outputs the address through 'addr'.
dvander@203
    44
	 * Returns an index >= 0 on success, < 0 on failure.
dvander@203
    45
	 */
dvander@203
    46
	int CreateMem(unsigned int size, void **addr);
dvander@203
    47
dvander@203
    48
	/**
dvander@203
    49
	 * Given an index into the memory table, returns its address.
dvander@203
    50
	 * Returns NULL if invalid.
dvander@203
    51
	 */
dvander@203
    52
	void *GetAddress(int index);
dvander@203
    53
dvander@203
    54
	/**
dvander@203
    55
	 * Scraps the memory table.  For caching purposes, the memory 
dvander@203
    56
	 * is not freed, however subsequent calls to CreateMem() will 
dvander@203
    57
	 * begin at the first index again.
dvander@203
    58
	 */
dvander@203
    59
	void Reset();
dvander@404
    60
dvander@1766
    61
	inline unsigned int GetMemUsage()
dvander@1766
    62
	{
dvander@1766
    63
		return size;
dvander@1766
    64
	}
dvander@404
    65
pred@2142
    66
	inline unsigned int GetActualMemUsed()
pred@2142
    67
	{
pred@2142
    68
		return tail;
pred@2142
    69
	}
pred@2142
    70
dvander@203
    71
private:
dvander@203
    72
	unsigned char *membase;
dvander@203
    73
	unsigned int size;
dvander@203
    74
	unsigned int tail;
dvander@203
    75
};
dvander@203
    76
dvander@203
    77
class BaseStringTable
dvander@203
    78
{
dvander@203
    79
public:
dvander@203
    80
	BaseStringTable(unsigned int init_size);
dvander@203
    81
	~BaseStringTable();
dvander@203
    82
public:
dvander@203
    83
	/** 
dvander@203
    84
	 * Adds a string to the string table and returns its index.
dvander@203
    85
	 */
dvander@203
    86
	int AddString(const char *string);
dvander@203
    87
dvander@203
    88
	/**
dvander@203
    89
	 * Given an index into the string table, returns the associated string.
dvander@203
    90
	 */
dvander@293
    91
	inline const char *GetString(int str)
dvander@293
    92
	{
dvander@293
    93
		return (const char *)m_table.GetAddress(str);
dvander@293
    94
	}
dvander@203
    95
dvander@203
    96
	/**
dvander@203
    97
	 * Scraps the string table.  For caching purposes, the memory
dvander@203
    98
	 * is not freed, however subsequent calls to AddString() will 
dvander@203
    99
	 * begin at the first index again.
dvander@203
   100
	 */
dvander@203
   101
	void Reset();
dvander@203
   102
dvander@203
   103
	/**
dvander@203
   104
	 * Returns the parent BaseMemTable that this string table uses.
dvander@203
   105
	 */
dvander@203
   106
	inline BaseMemTable *GetMemTable()
dvander@203
   107
	{
dvander@203
   108
		return &m_table;
dvander@203
   109
	}
dvander@203
   110
private:
dvander@203
   111
	BaseMemTable m_table;
dvander@203
   112
};
dvander@203
   113
dvander@203
   114
#endif //_INCLUDE_SOURCEMOD_CORE_STRINGTABLE_H_