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