core/sm_memtable.h
author Nicholas Hastings <skamonkey@gmail.com>
Sat Aug 10 12:22:09 2013 -0400 (2013-08-10)
changeset 3981 af3681eade3f
parent 3003 489c2b05a1b2
child 4096 1ed7fe4e8f7e
permissions -rw-r--r--
Debug build fixes.
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or modify it under
     9  * the terms of the GNU General Public License, version 3.0, as published by the
    10  * Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful, but WITHOUT
    13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    15  * details.
    16  *
    17  * You should have received a copy of the GNU General Public License along with
    18  * this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to link the
    21  * code of this program (as well as its derivative works) to "Half-Life 2," the
    22  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    23  * by the Valve Corporation.  You must obey the GNU General Public License in
    24  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    25  * this exception to all derivative works.  AlliedModders LLC defines further
    26  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    27  * or <http://www.sourcemod.net/license.php>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #ifndef _INCLUDE_SOURCEMOD_CORE_STRINGTABLE_H_
    33 #define _INCLUDE_SOURCEMOD_CORE_STRINGTABLE_H_
    34 
    35 #include <stdlib.h>
    36 #include <string.h>
    37 
    38 class BaseMemTable
    39 {
    40 public:
    41 	BaseMemTable(unsigned int init_size)
    42 	{
    43 		membase = (unsigned char *)malloc(init_size);
    44 		size = init_size;
    45 		tail = 0;
    46 	}
    47 	~BaseMemTable()
    48 	{
    49 		free(membase);
    50 		membase = NULL;
    51 	}
    52 public:
    53 	/**
    54 	 * Allocates 'size' bytes of memory.
    55 	 * Optionally outputs the address through 'addr'.
    56 	 * Returns an index >= 0 on success, < 0 on failure.
    57 	 */
    58 	int CreateMem(unsigned int addsize, void **addr)
    59 	{
    60 		int idx = (int)tail;
    61 
    62 		while (tail + addsize >= size) {
    63 			size *= 2;
    64 			membase = (unsigned char *)realloc(membase, size);
    65 		}
    66 
    67 		tail += addsize;
    68 		if (addr)
    69 			*addr = (void *)&membase[idx];
    70 
    71 		return idx;
    72 	}
    73 
    74 	/**
    75 	 * Given an index into the memory table, returns its address.
    76 	 * Returns NULL if invalid.
    77 	 */
    78 	void *GetAddress(int index)
    79 	{
    80 		if (index < 0 || (unsigned int)index >= tail)
    81 			return NULL;
    82 		return &membase[index];
    83 	}
    84 
    85 	/**
    86 	 * Scraps the memory table.  For caching purposes, the memory 
    87 	 * is not freed, however subsequent calls to CreateMem() will 
    88 	 * begin at the first index again.
    89 	 */
    90 	void Reset()
    91 	{
    92 		tail = 0;
    93 	}
    94 
    95 	inline unsigned int GetMemUsage()
    96 	{
    97 		return size;
    98 	}
    99 
   100 	inline unsigned int GetActualMemUsed()
   101 	{
   102 		return tail;
   103 	}
   104 
   105 private:
   106 	unsigned char *membase;
   107 	unsigned int size;
   108 	unsigned int tail;
   109 };
   110 
   111 class BaseStringTable
   112 {
   113 public:
   114 	BaseStringTable(unsigned int init_size) : m_table(init_size)
   115 	{
   116 	}
   117 public:
   118 	/** 
   119 	 * Adds a string to the string table and returns its index.
   120 	 */
   121 	int AddString(const char *string)
   122 	{
   123 		size_t len = strlen(string) + 1;
   124 		int idx;
   125 		char *addr;
   126 
   127 		idx = m_table.CreateMem(len, (void **)&addr);
   128 		strcpy(addr, string);
   129 
   130 		return idx;
   131 	}
   132 
   133 	/**
   134 	 * Given an index into the string table, returns the associated string.
   135 	 */
   136 	inline const char *GetString(int str)
   137 	{
   138 		return (const char *)m_table.GetAddress(str);
   139 	}
   140 
   141 	/**
   142 	 * Scraps the string table.  For caching purposes, the memory
   143 	 * is not freed, however subsequent calls to AddString() will 
   144 	 * begin at the first index again.
   145 	 */
   146 	void Reset()
   147 	{
   148 		m_table.Reset();
   149 	}
   150 
   151 	/**
   152 	 * Returns the parent BaseMemTable that this string table uses.
   153 	 */
   154 	inline BaseMemTable *GetMemTable()
   155 	{
   156 		return &m_table;
   157 	}
   158 private:
   159 	BaseMemTable m_table;
   160 };
   161 
   162 #endif //_INCLUDE_SOURCEMOD_CORE_STRINGTABLE_H_
   163