core/sm_memtable.h
author David Anderson <dvander@alliedmods.net>
Fri Mar 02 08:34:13 2007 +0000 (2007-03-02)
changeset 558 fc0eec032efb
parent 404 673ccd0935ba
child 653 ef60870067ff
permissions -rw-r--r--
added state change function
     1 /**
     2  * ===============================================================
     3  * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
     4  * ===============================================================
     5  *
     6  * This file is not open source and may not be copied without explicit
     7  * written permission of AlliedModders LLC.  This file may not be redistributed 
     8  * in whole or significant part.
     9  * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
    10  *
    11  * Version: $Id$
    12  */
    13 
    14 #ifndef _INCLUDE_SOURCEMOD_CORE_STRINGTABLE_H_
    15 #define _INCLUDE_SOURCEMOD_CORE_STRINGTABLE_H_
    16 
    17 class BaseMemTable
    18 {
    19 public:
    20 	BaseMemTable(unsigned int init_size);
    21 	~BaseMemTable();
    22 public:
    23 	/**
    24 	 * Allocates 'size' bytes of memory.
    25 	 * Optionally outputs the address through 'addr'.
    26 	 * Returns an index >= 0 on success, < 0 on failure.
    27 	 */
    28 	int CreateMem(unsigned int size, void **addr);
    29 
    30 	/**
    31 	 * Given an index into the memory table, returns its address.
    32 	 * Returns NULL if invalid.
    33 	 */
    34 	void *GetAddress(int index);
    35 
    36 	/**
    37 	 * Scraps the memory table.  For caching purposes, the memory 
    38 	 * is not freed, however subsequent calls to CreateMem() will 
    39 	 * begin at the first index again.
    40 	 */
    41 	void Reset();
    42 
    43 
    44 private:
    45 	unsigned char *membase;
    46 	unsigned int size;
    47 	unsigned int tail;
    48 };
    49 
    50 class BaseStringTable
    51 {
    52 public:
    53 	BaseStringTable(unsigned int init_size);
    54 	~BaseStringTable();
    55 public:
    56 	/** 
    57 	 * Adds a string to the string table and returns its index.
    58 	 */
    59 	int AddString(const char *string);
    60 
    61 	/**
    62 	 * Given an index into the string table, returns the associated string.
    63 	 */
    64 	inline const char *GetString(int str)
    65 	{
    66 		return (const char *)m_table.GetAddress(str);
    67 	}
    68 
    69 	/**
    70 	 * Scraps the string table.  For caching purposes, the memory
    71 	 * is not freed, however subsequent calls to AddString() will 
    72 	 * begin at the first index again.
    73 	 */
    74 	void Reset();
    75 
    76 	/**
    77 	 * Returns the parent BaseMemTable that this string table uses.
    78 	 */
    79 	inline BaseMemTable *GetMemTable()
    80 	{
    81 		return &m_table;
    82 	}
    83 private:
    84 	BaseMemTable m_table;
    85 };
    86 
    87 #endif //_INCLUDE_SOURCEMOD_CORE_STRINGTABLE_H_