core/sm_memtable.cpp
author David Anderson <dvander@alliedmods.net>
Mon Oct 26 23:51:39 2009 -0700 (2009-10-26)
changeset 2832 bf5d37088b6d
parent 2471 194be299b700
permissions -rw-r--r--
Don't pass unconnected clients into OnClientCommand (bug 3732, r=fyren).
     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 #include <string.h>
    33 #include <stdlib.h>
    34 #include "sm_memtable.h"
    35 
    36 BaseMemTable::BaseMemTable(unsigned int init_size)
    37 {
    38 	membase = (unsigned char *)malloc(init_size);
    39 	size = init_size;
    40 	tail = 0;
    41 }
    42 
    43 BaseMemTable::~BaseMemTable()
    44 {
    45 	free(membase);
    46 	membase = NULL;
    47 }
    48 
    49 int BaseMemTable::CreateMem(unsigned int addsize, void **addr)
    50 {
    51 	int idx = (int)tail;
    52 
    53 	while (tail + addsize >= size)
    54 	{
    55 		size *= 2;
    56 		membase = (unsigned char *)realloc(membase, size);
    57 	}
    58 
    59 	tail += addsize;
    60 
    61 	if (addr)
    62 	{
    63 		*addr = (void *)&membase[idx];
    64 	}
    65 
    66 	return idx;
    67 }
    68 
    69 void *BaseMemTable::GetAddress(int index)
    70 {
    71 	if (index < 0 || (unsigned int)index >= tail)
    72 	{
    73 		return NULL;
    74 	}
    75 
    76 	return &membase[index];
    77 }
    78 
    79 void BaseMemTable::Reset()
    80 {
    81 	tail = 0;
    82 }
    83 
    84 BaseStringTable::BaseStringTable(unsigned int init_size) : m_table(init_size)
    85 {
    86 }
    87 
    88 BaseStringTable::~BaseStringTable()
    89 {
    90 }
    91 
    92 int BaseStringTable::AddString(const char *string)
    93 {
    94 	size_t len = strlen(string) + 1;
    95 	int idx;
    96 	char *addr;
    97 
    98 	idx = m_table.CreateMem(len, (void **)&addr);
    99 	strcpy(addr, string);
   100 
   101 	return idx;
   102 }
   103 
   104 /*const char *BaseStringTable::GetString(int str)
   105 {
   106 	return (const char *)m_table.GetAddress(str);
   107 }*/
   108 
   109 void BaseStringTable::Reset()
   110 {
   111 	m_table.Reset();
   112 }