extensions/sqlite/sm_memtable.h
changeset 1203 633c25ce6405
child 1226 478550a4d5ec
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/extensions/sqlite/sm_memtable.h	Sun Jul 29 01:15:31 2007 +0000
     1.3 @@ -0,0 +1,97 @@
     1.4 +/**
     1.5 + * vim: set ts=4 :
     1.6 + * ===============================================================
     1.7 + * SourceMod SQLite Driver Extension
     1.8 + * Copyright (C) 2004-2007 AlliedModders LLC. All rights reserved.
     1.9 + * ===============================================================
    1.10 + *
    1.11 + * This program is free software; you can redistribute it and/or
    1.12 + * modify it under the terms of the GNU General Public License
    1.13 + * as published by the Free Software Foundation; either version 2
    1.14 + * of the License, or (at your option) any later version.
    1.15 + * This program is distributed in the hope that it will be useful,
    1.16 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.17 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.18 + * GNU General Public License for more details.
    1.19 + * 
    1.20 + * You should have received a copy of the GNU General Public License
    1.21 + * along with this program; if not, write to the Free Software
    1.22 + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
    1.23 + *
    1.24 + * Version: $Id$
    1.25 + */
    1.26 +
    1.27 +#ifndef _INCLUDE_SOURCEMOD_CORE_STRINGTABLE_H_
    1.28 +#define _INCLUDE_SOURCEMOD_CORE_STRINGTABLE_H_
    1.29 +
    1.30 +class BaseMemTable
    1.31 +{
    1.32 +public:
    1.33 +	BaseMemTable(unsigned int init_size);
    1.34 +	~BaseMemTable();
    1.35 +public:
    1.36 +	/**
    1.37 +	 * Allocates 'size' bytes of memory.
    1.38 +	 * Optionally outputs the address through 'addr'.
    1.39 +	 * Returns an index >= 0 on success, < 0 on failure.
    1.40 +	 */
    1.41 +	int CreateMem(unsigned int size, void **addr);
    1.42 +
    1.43 +	/**
    1.44 +	 * Given an index into the memory table, returns its address.
    1.45 +	 * Returns NULL if invalid.
    1.46 +	 */
    1.47 +	void *GetAddress(int index);
    1.48 +
    1.49 +	/**
    1.50 +	 * Scraps the memory table.  For caching purposes, the memory 
    1.51 +	 * is not freed, however subsequent calls to CreateMem() will 
    1.52 +	 * begin at the first index again.
    1.53 +	 */
    1.54 +	void Reset();
    1.55 +
    1.56 +
    1.57 +private:
    1.58 +	unsigned char *membase;
    1.59 +	unsigned int size;
    1.60 +	unsigned int tail;
    1.61 +};
    1.62 +
    1.63 +class BaseStringTable
    1.64 +{
    1.65 +public:
    1.66 +	BaseStringTable(unsigned int init_size);
    1.67 +	~BaseStringTable();
    1.68 +public:
    1.69 +	/** 
    1.70 +	 * Adds a string to the string table and returns its index.
    1.71 +	 */
    1.72 +	int AddString(const char *string);
    1.73 +
    1.74 +	/**
    1.75 +	 * Given an index into the string table, returns the associated string.
    1.76 +	 */
    1.77 +	inline const char *GetString(int str)
    1.78 +	{
    1.79 +		return (const char *)m_table.GetAddress(str);
    1.80 +	}
    1.81 +
    1.82 +	/**
    1.83 +	 * Scraps the string table.  For caching purposes, the memory
    1.84 +	 * is not freed, however subsequent calls to AddString() will 
    1.85 +	 * begin at the first index again.
    1.86 +	 */
    1.87 +	void Reset();
    1.88 +
    1.89 +	/**
    1.90 +	 * Returns the parent BaseMemTable that this string table uses.
    1.91 +	 */
    1.92 +	inline BaseMemTable *GetMemTable()
    1.93 +	{
    1.94 +		return &m_table;
    1.95 +	}
    1.96 +private:
    1.97 +	BaseMemTable m_table;
    1.98 +};
    1.99 +
   1.100 +#endif //_INCLUDE_SOURCEMOD_CORE_STRINGTABLE_H_