core/sm_memtable.cpp
author David Anderson <dvander@alliedmods.net>
Mon Feb 26 09:21:08 2007 +0000 (2007-02-26)
changeset 539 fa7e8a232a18
parent 370 d3a484f98f6d
child 653 ef60870067ff
permissions -rw-r--r--
fixed stupid export
     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 #include <string.h>
    15 #include <malloc.h>
    16 #include "sm_memtable.h"
    17 
    18 BaseMemTable::BaseMemTable(unsigned int init_size)
    19 {
    20 	membase = (unsigned char *)malloc(init_size);
    21 	size = init_size;
    22 	tail = 0;
    23 }
    24 
    25 BaseMemTable::~BaseMemTable()
    26 {
    27 	free(membase);
    28 	membase = NULL;
    29 }
    30 
    31 int BaseMemTable::CreateMem(unsigned int addsize, void **addr)
    32 {
    33 	int idx = (int)tail;
    34 
    35 	while (tail + addsize >= size)
    36 	{
    37 		size *= 2;
    38 		membase = (unsigned char *)realloc(membase, size);
    39 	}
    40 
    41 	tail += addsize;
    42 
    43 	if (addr)
    44 	{
    45 		*addr = (void *)&membase[idx];
    46 	}
    47 
    48 	return idx;
    49 }
    50 
    51 void *BaseMemTable::GetAddress(int index)
    52 {
    53 	if (index < 0 || (unsigned int)index >= tail)
    54 	{
    55 		return NULL;
    56 	}
    57 
    58 	return &membase[index];
    59 }
    60 
    61 void BaseMemTable::Reset()
    62 {
    63 	tail = 0;
    64 }
    65 
    66 BaseStringTable::BaseStringTable(unsigned int init_size) : m_table(init_size)
    67 {
    68 }
    69 
    70 BaseStringTable::~BaseStringTable()
    71 {
    72 }
    73 
    74 int BaseStringTable::AddString(const char *string)
    75 {
    76 	size_t len = strlen(string) + 1;
    77 	int idx;
    78 	char *addr;
    79 
    80 	idx = m_table.CreateMem(len, (void **)&addr);
    81 	strcpy(addr, string);
    82 
    83 	return idx;
    84 }
    85 
    86 /*const char *BaseStringTable::GetString(int str)
    87 {
    88 	return (const char *)m_table.GetAddress(str);
    89 }*/
    90 
    91 void BaseStringTable::Reset()
    92 {
    93 	m_table.Reset();
    94 }