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