core/logic/sm_memtable.h
changeset 3004 e3ac0dc6d9a4
parent 3003 489c2b05a1b2
child 4096 1ed7fe4e8f7e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/core/logic/sm_memtable.h	Fri May 14 19:43:53 2010 -0700
     1.3 @@ -0,0 +1,163 @@
     1.4 +/**
     1.5 + * vim: set ts=4 :
     1.6 + * =============================================================================
     1.7 + * SourceMod
     1.8 + * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     1.9 + * =============================================================================
    1.10 + *
    1.11 + * This program is free software; you can redistribute it and/or modify it under
    1.12 + * the terms of the GNU General Public License, version 3.0, as published by the
    1.13 + * Free Software Foundation.
    1.14 + * 
    1.15 + * This program is distributed in the hope that it will be useful, but WITHOUT
    1.16 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    1.17 + * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    1.18 + * details.
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License along with
    1.21 + * this program.  If not, see <http://www.gnu.org/licenses/>.
    1.22 + *
    1.23 + * As a special exception, AlliedModders LLC gives you permission to link the
    1.24 + * code of this program (as well as its derivative works) to "Half-Life 2," the
    1.25 + * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    1.26 + * by the Valve Corporation.  You must obey the GNU General Public License in
    1.27 + * all respects for all other code used.  Additionally, AlliedModders LLC grants
    1.28 + * this exception to all derivative works.  AlliedModders LLC defines further
    1.29 + * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    1.30 + * or <http://www.sourcemod.net/license.php>.
    1.31 + *
    1.32 + * Version: $Id$
    1.33 + */
    1.34 +
    1.35 +#ifndef _INCLUDE_SOURCEMOD_CORE_STRINGTABLE_H_
    1.36 +#define _INCLUDE_SOURCEMOD_CORE_STRINGTABLE_H_
    1.37 +
    1.38 +#include <stdlib.h>
    1.39 +#include <string.h>
    1.40 +
    1.41 +class BaseMemTable
    1.42 +{
    1.43 +public:
    1.44 +	BaseMemTable(unsigned int init_size)
    1.45 +	{
    1.46 +		membase = (unsigned char *)malloc(init_size);
    1.47 +		size = init_size;
    1.48 +		tail = 0;
    1.49 +	}
    1.50 +	~BaseMemTable()
    1.51 +	{
    1.52 +		free(membase);
    1.53 +		membase = NULL;
    1.54 +	}
    1.55 +public:
    1.56 +	/**
    1.57 +	 * Allocates 'size' bytes of memory.
    1.58 +	 * Optionally outputs the address through 'addr'.
    1.59 +	 * Returns an index >= 0 on success, < 0 on failure.
    1.60 +	 */
    1.61 +	int CreateMem(unsigned int addsize, void **addr)
    1.62 +	{
    1.63 +		int idx = (int)tail;
    1.64 +
    1.65 +		while (tail + addsize >= size) {
    1.66 +			size *= 2;
    1.67 +			membase = (unsigned char *)realloc(membase, size);
    1.68 +		}
    1.69 +
    1.70 +		tail += addsize;
    1.71 +		if (addr)
    1.72 +			*addr = (void *)&membase[idx];
    1.73 +
    1.74 +		return idx;
    1.75 +	}
    1.76 +
    1.77 +	/**
    1.78 +	 * Given an index into the memory table, returns its address.
    1.79 +	 * Returns NULL if invalid.
    1.80 +	 */
    1.81 +	void *GetAddress(int index)
    1.82 +	{
    1.83 +		if (index < 0 || (unsigned int)index >= tail)
    1.84 +			return NULL;
    1.85 +		return &membase[index];
    1.86 +	}
    1.87 +
    1.88 +	/**
    1.89 +	 * Scraps the memory table.  For caching purposes, the memory 
    1.90 +	 * is not freed, however subsequent calls to CreateMem() will 
    1.91 +	 * begin at the first index again.
    1.92 +	 */
    1.93 +	void Reset()
    1.94 +	{
    1.95 +		tail = 0;
    1.96 +	}
    1.97 +
    1.98 +	inline unsigned int GetMemUsage()
    1.99 +	{
   1.100 +		return size;
   1.101 +	}
   1.102 +
   1.103 +	inline unsigned int GetActualMemUsed()
   1.104 +	{
   1.105 +		return tail;
   1.106 +	}
   1.107 +
   1.108 +private:
   1.109 +	unsigned char *membase;
   1.110 +	unsigned int size;
   1.111 +	unsigned int tail;
   1.112 +};
   1.113 +
   1.114 +class BaseStringTable
   1.115 +{
   1.116 +public:
   1.117 +	BaseStringTable(unsigned int init_size) : m_table(init_size)
   1.118 +	{
   1.119 +	}
   1.120 +public:
   1.121 +	/** 
   1.122 +	 * Adds a string to the string table and returns its index.
   1.123 +	 */
   1.124 +	int AddString(const char *string)
   1.125 +	{
   1.126 +		size_t len = strlen(string) + 1;
   1.127 +		int idx;
   1.128 +		char *addr;
   1.129 +
   1.130 +		idx = m_table.CreateMem(len, (void **)&addr);
   1.131 +		strcpy(addr, string);
   1.132 +
   1.133 +		return idx;
   1.134 +	}
   1.135 +
   1.136 +	/**
   1.137 +	 * Given an index into the string table, returns the associated string.
   1.138 +	 */
   1.139 +	inline const char *GetString(int str)
   1.140 +	{
   1.141 +		return (const char *)m_table.GetAddress(str);
   1.142 +	}
   1.143 +
   1.144 +	/**
   1.145 +	 * Scraps the string table.  For caching purposes, the memory
   1.146 +	 * is not freed, however subsequent calls to AddString() will 
   1.147 +	 * begin at the first index again.
   1.148 +	 */
   1.149 +	void Reset()
   1.150 +	{
   1.151 +		m_table.Reset();
   1.152 +	}
   1.153 +
   1.154 +	/**
   1.155 +	 * Returns the parent BaseMemTable that this string table uses.
   1.156 +	 */
   1.157 +	inline BaseMemTable *GetMemTable()
   1.158 +	{
   1.159 +		return &m_table;
   1.160 +	}
   1.161 +private:
   1.162 +	BaseMemTable m_table;
   1.163 +};
   1.164 +
   1.165 +#endif //_INCLUDE_SOURCEMOD_CORE_STRINGTABLE_H_
   1.166 +