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