core/sm_memtable.cpp
author Borja Ferrer <faluco@alliedmods.net>
Fri Jan 05 00:42:22 2007 +0000 (2007-01-05)
changeset 268 d6fed4f05e07
parent 204 947d19d39772
child 293 83b5a2d33e3d
permissions -rw-r--r--
Fixed CPlayer init, it was called too early.
nothing important on the logger
     1 #include <string.h>
     2 #include <malloc.h>
     3 #include "sm_memtable.h"
     4 
     5 BaseMemTable::BaseMemTable(unsigned int init_size)
     6 {
     7 	membase = (unsigned char *)malloc(init_size);
     8 	size = init_size;
     9 	tail = 0;
    10 }
    11 
    12 BaseMemTable::~BaseMemTable()
    13 {
    14 	free(membase);
    15 	membase = NULL;
    16 }
    17 
    18 int BaseMemTable::CreateMem(unsigned int addsize, void **addr)
    19 {
    20 	int idx = (int)tail;
    21 
    22 	while (tail + addsize >= size)
    23 	{
    24 		size *= 2;
    25 		membase = (unsigned char *)realloc(membase, size);
    26 	}
    27 
    28 	tail += addsize;
    29 
    30 	if (addr)
    31 	{
    32 		*addr = (void *)&membase[idx];
    33 	}
    34 
    35 	return idx;
    36 }
    37 
    38 void *BaseMemTable::GetAddress(int index)
    39 {
    40 	if (index < 0 || (unsigned int)index >= tail)
    41 	{
    42 		return NULL;
    43 	}
    44 
    45 	return &membase[index];
    46 }
    47 
    48 void BaseMemTable::Reset()
    49 {
    50 	tail = 0;
    51 }
    52 
    53 BaseStringTable::BaseStringTable(unsigned int init_size) : m_table(init_size)
    54 {
    55 }
    56 
    57 BaseStringTable::~BaseStringTable()
    58 {
    59 }
    60 
    61 int BaseStringTable::AddString(const char *string)
    62 {
    63 	size_t len = strlen(string) + 1;
    64 	int idx;
    65 	char *addr;
    66 
    67 	idx = m_table.CreateMem(len, (void **)&addr);
    68 	strcpy(addr, string);
    69 
    70 	return idx;
    71 }
    72 
    73 const char *BaseStringTable::GetString(int str)
    74 {
    75 	return (const char *)m_table.GetAddress(str);
    76 }
    77 
    78 void BaseStringTable::Reset()
    79 {
    80 	m_table.Reset();
    81 }