2014-08-04 14:49:44 +04:00
|
|
|
// vim: set ts=4 sw=4 tw=99 noet:
|
|
|
|
//
|
|
|
|
// AMX Mod X, based on AMX Mod by Aleksander Naszko ("OLO").
|
|
|
|
// Copyright (C) The AMX Mod X Development Team.
|
|
|
|
//
|
|
|
|
// This software is licensed under the GNU General Public License, version 3 or higher.
|
|
|
|
// Additional exceptions apply. For full license details, see LICENSE.txt or visit:
|
|
|
|
// https://alliedmods.net/amxmodx-license
|
|
|
|
|
|
|
|
//
|
|
|
|
// SQLite Module
|
|
|
|
//
|
|
|
|
|
2006-06-03 23:52:21 +04:00
|
|
|
#include <string.h>
|
2014-08-10 19:24:38 +04:00
|
|
|
#include <sh_stack.h>
|
|
|
|
#include <am-vector.h>
|
2006-06-03 23:52:21 +04:00
|
|
|
#include "sqlite_header.h"
|
|
|
|
|
|
|
|
struct QHandle
|
|
|
|
{
|
|
|
|
void *_ptr;
|
|
|
|
FREEHANDLE _func;
|
|
|
|
HandleType type;
|
|
|
|
bool isfree;
|
|
|
|
};
|
|
|
|
|
2014-08-10 19:24:38 +04:00
|
|
|
ke::Vector<QHandle *> g_Handles;
|
2006-06-03 23:52:21 +04:00
|
|
|
CStack<unsigned int> g_FreeHandles;
|
|
|
|
|
|
|
|
unsigned int MakeHandle(void *ptr, HandleType type, FREEHANDLE f)
|
|
|
|
{
|
|
|
|
unsigned int num;
|
|
|
|
QHandle *h;
|
|
|
|
|
|
|
|
if (g_FreeHandles.size())
|
|
|
|
{
|
|
|
|
num = g_FreeHandles.front();
|
|
|
|
g_FreeHandles.pop();
|
|
|
|
h = g_Handles[num];
|
|
|
|
} else {
|
|
|
|
h = new QHandle;
|
2014-08-10 19:24:38 +04:00
|
|
|
g_Handles.append(h);
|
|
|
|
num = static_cast<unsigned int>(g_Handles.length()) - 1;
|
2006-06-03 23:52:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
h->_ptr = ptr;
|
|
|
|
h->type = type;
|
|
|
|
h->_func = f;
|
|
|
|
h->isfree = false;
|
|
|
|
|
|
|
|
return num + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *GetHandle(unsigned int num, HandleType type)
|
|
|
|
{
|
|
|
|
if (num == 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
num--;
|
2014-08-10 19:24:38 +04:00
|
|
|
if (num >= g_Handles.length())
|
2006-06-03 23:52:21 +04:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
QHandle *h = g_Handles[num];
|
|
|
|
if (h->isfree || (h->type != type))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return h->_ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FreeHandle(unsigned int num)
|
|
|
|
{
|
|
|
|
if (num == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
unsigned int _num = num;
|
|
|
|
|
|
|
|
num--;
|
2014-08-10 19:24:38 +04:00
|
|
|
if (num >= g_Handles.length())
|
2006-06-03 23:52:21 +04:00
|
|
|
return false;
|
|
|
|
|
|
|
|
QHandle *h = g_Handles[num];
|
|
|
|
if (h->isfree)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
h->_func(h->_ptr, _num);
|
|
|
|
h->_ptr = NULL;
|
|
|
|
h->_func = NULL;
|
|
|
|
h->isfree = true;
|
|
|
|
|
|
|
|
g_FreeHandles.push(num);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FreeAllHandles(HandleType type)
|
|
|
|
{
|
|
|
|
QHandle *q;
|
2014-08-10 19:24:38 +04:00
|
|
|
for (size_t i = 0; i < g_Handles.length(); i++)
|
2006-06-03 23:52:21 +04:00
|
|
|
{
|
|
|
|
q = g_Handles[i];
|
|
|
|
if (q && !q->isfree && q->type == type)
|
|
|
|
{
|
|
|
|
FreeHandle((unsigned int)i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FreeHandleTable()
|
|
|
|
{
|
|
|
|
QHandle *q;
|
2014-08-10 19:24:38 +04:00
|
|
|
for (size_t i = 0; i < g_Handles.length(); i++)
|
2006-06-03 23:52:21 +04:00
|
|
|
{
|
|
|
|
q = g_Handles[i];
|
|
|
|
if (q && !q->isfree)
|
|
|
|
FreeHandle((unsigned int)i);
|
|
|
|
}
|
|
|
|
g_Handles.clear();
|
|
|
|
while (!g_FreeHandles.empty())
|
|
|
|
g_FreeHandles.pop();
|
|
|
|
}
|