root/src/dd_stack.c

Revision 10ff89cb385fb32bdb4f48a3b1a6d997d36c6bb6, 2.7 kB (checked in by Philip Herron <redbrain@…>, 2 years ago)

more math code

  • Property mode set to 100644
Line 
1/**
2 * builtin_stack.c -> Part of Crules Programming language
3 *
4 * Crules is the legal property of its developers. Please refer to the
5 * COPYRIGHT file distributed with this source distribution.
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
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
21#ifdef HAVE_CONFIG_H
22# include "config.h"
23#else
24# define CMAKE 1
25# include "config.h.cmake"
26#endif
27
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31
32#include <crules/crules.h>
33#include <crules/opcodes.h>
34#include <crules/symbols.h>
35#include <crules/objects.h>
36#include <crules/backend.h>
37#include <crules/runtime.h>
38#include <crules/garbage.h>
39#include <crules/operators.h>
40
41//threshold size alloctor to scale up faster
42#define threshold_alloc(x) (((x)+16)*3/2)
43
44void crl_dd_stack_push( crl_stack_t * stack, const void * obj )
45{
46  unsigned int idx= stack->length, tsize= 0;
47  void *pstack= NULL;
48
49  if( !stack || !obj )
50    crl_fatal("undefined stack or object!\n");
51
52  if( stack->length >= stack->size )
53    {
54      tsize= threshold_alloc( stack->size );
55      pstack= crl_realloc( stack->array, tsize * sizeof(void*) );
56
57      stack->array= pstack;
58      stack->size= tsize;
59    }
60
61  stack->array[idx]= obj;
62  idx++; stack->length= idx;
63}
64
65inline
66void* crl_dd_stack_pop( crl_stack_t *stack )
67{
68  void *obj;
69  if( !stack || stack->length <= 0 )
70    return NULL;
71
72  obj= stack->array[ (stack->length)-1 ];
73  stack->length--;
74
75  return obj;
76}
77
78inline
79void crl_dd_stack_init( crl_stack_t **stack )
80{
81  if( stack )
82    {
83      unsigned int len= threshold_alloc( 0 );
84      (*stack)->array= crl_calloc( len, sizeof(void*) );
85      (*stack)->size= len;
86      (*stack)->length= 0;
87    }
88  else {
89    crl_error("null stack refernce to a stack in stack_init!\n");
90  }
91}
92
93/**
94 * Free a stack object
95 *    WARNING: does not free the contents of the nodes!
96 **/
97inline
98void crl_dd_stack_free( crl_stack_t *stack )
99{
100  if( stack )
101    {
102      if( stack->array )
103        crl_free( stack->array );
104      crl_free(stack);
105    }
106}
107
108inline
109unsigned int crl_dd_stack_get_size( crl_stack_t *stack )
110{
111  unsigned int retval;
112  if( !stack )
113    retval= 0;
114  else
115    retval= stack->length;
116
117  return retval;
118}
Note: See TracBrowser for help on using the browser.