plugins/include/adt_stack.inc
author David Anderson <dvander@alliedmods.net>
Fri Aug 15 05:22:26 2008 +0000 (2008-08-15)
changeset 2226 d10b38524be3
parent 2023 1bd756a9f8db
permissions -rw-r--r--
Landed sourcepawn-1.2. The big changes:

1) JIT compilation/optimization now occurs per-function, and only when functions are first used. We're now officially a whole-method JIT rather than an AOT compiler (albiet, still a simple JIT). This has two implications: Functions are now much better abstracted internally, and loading a plugin is now much less expensive. If a function contains calls to other functions, THOSE functions are only compiled when they're invoked as well.

2) I've removed debug mode. We always show full backtraces now, as there was a very cheap way to implement this which really cleaned up everything. This is great for a number of reasons -- there's less code, the JIT is better designed, we don't need to relocate debug tables, and best of all we no longer have to tell users to enable debug mode at their own expense.
dvander@1999
     1
/**
dvander@1999
     2
 * vim: set ts=4 :
dvander@1999
     3
 * =============================================================================
dvander@1999
     4
 * SourceMod (C)2004-2008 AlliedModders LLC.  All rights reserved.
dvander@1999
     5
 * =============================================================================
dvander@1999
     6
 *
dvander@1999
     7
 * This file is part of the SourceMod/SourcePawn SDK.
dvander@1999
     8
 *
dvander@1999
     9
 * This program is free software; you can redistribute it and/or modify it under
dvander@1999
    10
 * the terms of the GNU General Public License, version 3.0, as published by the
dvander@1999
    11
 * Free Software Foundation.
dvander@1999
    12
 * 
dvander@1999
    13
 * This program is distributed in the hope that it will be useful, but WITHOUT
dvander@1999
    14
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
dvander@1999
    15
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
dvander@1999
    16
 * details.
dvander@1999
    17
 *
dvander@1999
    18
 * You should have received a copy of the GNU General Public License along with
dvander@1999
    19
 * this program.  If not, see <http://www.gnu.org/licenses/>.
dvander@1999
    20
 *
dvander@1999
    21
 * As a special exception, AlliedModders LLC gives you permission to link the
dvander@1999
    22
 * code of this program (as well as its derivative works) to "Half-Life 2," the
dvander@1999
    23
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
dvander@1999
    24
 * by the Valve Corporation.  You must obey the GNU General Public License in
dvander@1999
    25
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
dvander@1999
    26
 * this exception to all derivative works.  AlliedModders LLC defines further
dvander@1999
    27
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
dvander@1999
    28
 * or <http://www.sourcemod.net/license.php>.
dvander@1999
    29
 *
dvander@1999
    30
 * Version: $Id$
dvander@1999
    31
 */
dvander@1999
    32
 
dvander@1999
    33
#if defined _adt_stack_included
dvander@1999
    34
 #endinput
dvander@1999
    35
#endif
dvander@1999
    36
#define _adt_stack_included
dvander@1999
    37
dvander@1999
    38
/**
dvander@1999
    39
 * Creates a stack structure.  A stack is a LIFO (last in, first out) 
dvander@1999
    40
 * vector (array) of items.  It has O(1) insertion and O(1) removal.
dvander@1999
    41
 *
dvander@1999
    42
 * Stacks have two operations: Push (adding an item) and Pop (removes 
dvander@1999
    43
 * items in reverse-push order).
dvander@1999
    44
 * 
dvander@1999
    45
 * The contents of the stack are uniform; i.e. storing a string and then 
dvander@1999
    46
 * retrieving it as an integer is NOT the same as StringToInt()!
dvander@1999
    47
 *
dvander@1999
    48
 * The "blocksize" determines how many cells each slot has; it cannot
dvander@1999
    49
 * be changed after creation.
dvander@1999
    50
 *
dvander@1999
    51
 * @param blocksize		The number of cells each entry in the stack can 
dvander@1999
    52
 *						hold.  For example, 32 cells is equivalent to:
dvander@1999
    53
 *						new Array[X][32]
dvander@1999
    54
 * @return				New stack Handle.
dvander@1999
    55
 */
dvander@1999
    56
native Handle:CreateStack(blocksize=1);
dvander@1999
    57
dvander@1999
    58
/**
dvander@2003
    59
 * Pushes a value onto the end of the stack, adding a new index.
dvander@1999
    60
 *
dvander@1999
    61
 * This may safely be used even if the stack has a blocksize
dvander@1999
    62
 * greater than 1.
dvander@1999
    63
 *
dvander@1999
    64
 * @param stack			Stack Handle.
dvander@1999
    65
 * @param value			Value to push.
dvander@1999
    66
 * @noreturn
dvander@1999
    67
 * @error				Invalid Handle or out of memory.
dvander@1999
    68
 */
dvander@1999
    69
native PushStackCell(Handle:stack, any:value);
dvander@1999
    70
dvander@1999
    71
/**
dvander@1999
    72
 * Pushes a string onto the end of a stack, truncating it if it is 
dvander@1999
    73
 * too big.
dvander@1999
    74
 *
dvander@1999
    75
 * @param stack			Stack Handle.
dvander@1999
    76
 * @param value			String to push.
dvander@1999
    77
 * @noreturn
dvander@1999
    78
 * @error				Invalid Handle or out of memory.
dvander@1999
    79
 */
dvander@1999
    80
native PushStackString(Handle:stack, const String:value[]);
dvander@1999
    81
dvander@1999
    82
/**
dvander@1999
    83
 * Pushes an array of cells onto the end of a stack.  The cells
dvander@1999
    84
 * are pushed as a block (i.e. the entire array takes up one stack slot),
dvander@1999
    85
 * rather than pushing each cell individually.
dvander@1999
    86
 *
dvander@1999
    87
 * @param stack			Stack Handle.
dvander@1999
    88
 * @param values		Block of values to copy.
dvander@1999
    89
 * @param size			If not set, the number of elements copied from the array
dvander@1999
    90
 *						will be equal to the blocksize.  If set higher than the 
dvander@1999
    91
 *						blocksize, the operation will be truncated.
dvander@1999
    92
 * @noreturn
dvander@1999
    93
 * @error				Invalid Handle or out of memory.
dvander@1999
    94
 */
ds@2023
    95
native PushStackArray(Handle:stack, const any:values[], size=-1);
dvander@1999
    96
dvander@1999
    97
/**
dvander@1999
    98
 * Pops a cell value from a stack.
dvander@1999
    99
 *
dvander@1999
   100
 * @param stack			Stack Handle.
dvander@1999
   101
 * @param value			Variable to store the value.
dvander@1999
   102
 * @param block			Optionally specify which block to read from
dvander@1999
   103
 *						(useful if the blocksize > 0).
dvander@1999
   104
 * @param asChar		Optionally read as a byte instead of a cell.
dvander@1999
   105
 * @return				True on success, false if the stack is empty.
dvander@1999
   106
 * @error				Invalid Handle.
dvander@1999
   107
 */
dvander@1999
   108
native bool:PopStackCell(Handle:stack, &any:value, block=0, bool:asChar=false);
dvander@1999
   109
dvander@1999
   110
/**
dvander@1999
   111
 * Pops a string value from a stack.
dvander@1999
   112
 *
dvander@1999
   113
 * @param stack			Stack Handle.
dvander@1999
   114
 * @param buffer		Buffer to store string.
dvander@1999
   115
 * @param maxlength		Maximum size of the buffer.
dvander@1999
   116
 * @return				True on success, false if the stack is empty.
dvander@1999
   117
 * @error				Invalid Handle.
dvander@1999
   118
 */
dvander@1999
   119
native bool:PopStackString(Handle:stack, String:buffer[], maxlength, &written=0);
dvander@1999
   120
dvander@1999
   121
/**
dvander@1999
   122
 * Pops an array of cells from a stack.
dvander@1999
   123
 *
dvander@1999
   124
 * @param stack			Stack Handle.
dvander@1999
   125
 * @param buffer		Buffer to store the array in.
dvander@1999
   126
 * @param size			If not set, assumes the buffer size is equal to the
dvander@1999
   127
 *						blocksize.  Otherwise, the size passed is used.
dvander@1999
   128
 * @return				True on success, false if the stack is empty.
dvander@1999
   129
 * @error				Invalid Handle.
dvander@1999
   130
 */
ds@2023
   131
native bool:PopStackArray(Handle:stack, any:buffer[], size=-1);
dvander@1999
   132
dvander@1999
   133
/**
dvander@1999
   134
 * Checks if a stack is empty.
dvander@1999
   135
 *
dvander@1999
   136
 * @param stack			Stack Handle.
dvander@1999
   137
 * @return				True if empty, false if not empty.
dvander@1999
   138
 * @error				Invalid Handle.
dvander@1999
   139
 */
dvander@1999
   140
native bool:IsStackEmpty(Handle:stack);
dvander@1999
   141
dvander@1999
   142
/**
dvander@1999
   143
 * Pops a value off a stack, ignoring it completely.
dvander@1999
   144
 *
dvander@1999
   145
 * @param stack			Stack Handle.
dvander@1999
   146
 * @return				True if something was popped, false otherwise.
dvander@1999
   147
 * @error				Invalid Handle.
dvander@1999
   148
 */
dvander@1999
   149
stock PopStack(Handle:stack)
dvander@1999
   150
{
dvander@1999
   151
	new value;
dvander@1999
   152
	
dvander@1999
   153
	return PopStackCell(stack, value);
dvander@1999
   154
}