extensions/sqlite/sm_memtable.cpp
author David Anderson <dvander@alliedmods.net>
Wed Aug 01 01:54:53 2007 +0000 (2007-08-01)
changeset 1226 478550a4d5ec
parent 1203 633c25ce6405
child 1330 ab94316bb52b
permissions -rw-r--r--
updated license headers for most extensions
updated sdk bases for most extensions
     1 /**
     2  * vim: set ts=4 :
     3  * ================================================================
     4  * SourceMod SQLite Extension
     5  * Copyright (C) 2004-2007 AlliedModders LLC.  All rights reserved.
     6  * ================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or 
     9  * modify it under the terms of the GNU General Public License, 
    10  * version 3.0, as published by the Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful,
    13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    15  * GNU General Public License for more details.
    16  *
    17  * You should have received a copy of the GNU General Public License
    18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to 
    21  * link the code of this program (as well as its derivative works) to 
    22  * "Half-Life 2," the "Source Engine," the "SourcePawn JIT," and any 
    23  * Game MODs that run on software by the Valve Corporation.  You must 
    24  * obey the GNU General Public License in all respects for all other 
    25  * code used. Additionally, AlliedModders LLC grants this exception 
    26  * to all derivative works. AlliedModders LLC defines further 
    27  * exceptions, found in LICENSE.txt (as of this writing, version 
    28  * JULY-31-2007), or <http://www.sourcemod.net/license.php>.
    29  *
    30  * Version: $Id$
    31  */
    32 
    33 #include <string.h>
    34 #include <malloc.h>
    35 #include "sm_memtable.h"
    36 
    37 BaseMemTable::BaseMemTable(unsigned int init_size)
    38 {
    39 	membase = (unsigned char *)malloc(init_size);
    40 	size = init_size;
    41 	tail = 0;
    42 }
    43 
    44 BaseMemTable::~BaseMemTable()
    45 {
    46 	free(membase);
    47 	membase = NULL;
    48 }
    49 
    50 int BaseMemTable::CreateMem(unsigned int addsize, void **addr)
    51 {
    52 	int idx = (int)tail;
    53 
    54 	while (tail + addsize >= size)
    55 	{
    56 		size *= 2;
    57 		membase = (unsigned char *)realloc(membase, size);
    58 	}
    59 
    60 	tail += addsize;
    61 
    62 	if (addr)
    63 	{
    64 		*addr = (void *)&membase[idx];
    65 	}
    66 
    67 	return idx;
    68 }
    69 
    70 void *BaseMemTable::GetAddress(int index)
    71 {
    72 	if (index < 0 || (unsigned int)index >= tail)
    73 	{
    74 		return NULL;
    75 	}
    76 
    77 	return &membase[index];
    78 }
    79 
    80 void BaseMemTable::Reset()
    81 {
    82 	tail = 0;
    83 }
    84 
    85 BaseStringTable::BaseStringTable(unsigned int init_size) : m_table(init_size)
    86 {
    87 }
    88 
    89 BaseStringTable::~BaseStringTable()
    90 {
    91 }
    92 
    93 int BaseStringTable::AddString(const char *string)
    94 {
    95 	size_t len = strlen(string) + 1;
    96 	int idx;
    97 	char *addr;
    98 
    99 	idx = m_table.CreateMem(len, (void **)&addr);
   100 	strcpy(addr, string);
   101 
   102 	return idx;
   103 }
   104 
   105 /*const char *BaseStringTable::GetString(int str)
   106 {
   107 	return (const char *)m_table.GetAddress(str);
   108 }*/
   109 
   110 void BaseStringTable::Reset()
   111 {
   112 	m_table.Reset();
   113 }