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