Back to index

php5  5.3.10
init.c
Go to the documentation of this file.
00001 /* init.c: bcmath library file. */
00002 /*
00003     Copyright (C) 1991, 1992, 1993, 1994, 1997 Free Software Foundation, Inc.
00004     Copyright (C) 2000 Philip A. Nelson
00005 
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Lesser General Public
00008     License as published by the Free Software Foundation; either
00009     version 2 of the License, or (at your option) any later version.
00010 
00011     This library is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     Lesser General Public License for more details.  (COPYING.LIB)
00015 
00016     You should have received a copy of the GNU Lesser General Public
00017     License along with this library; if not, write to:
00018 
00019       The Free Software Foundation, Inc.
00020       59 Temple Place, Suite 330
00021       Boston, MA 02111-1307 USA.
00022 
00023     You may contact the author by:
00024        e-mail:  philnelson@acm.org
00025       us-mail:  Philip A. Nelson
00026                 Computer Science Department, 9062
00027                 Western Washington University
00028                 Bellingham, WA 98226-9062
00029        
00030 *************************************************************************/
00031 
00032 #include <config.h>
00033 #include <stdio.h>
00034 #include <assert.h>
00035 #include <stdlib.h>
00036 #include <ctype.h>
00037 #include <stdarg.h>
00038 #include "bcmath.h"
00039 #include "private.h"
00040 
00041 #if SANDER_0
00042  bc_num _bc_Free_list = NULL;
00043 #endif
00044 
00045 /* new_num allocates a number and sets fields to known values. */
00046 
00047 bc_num
00048 _bc_new_num_ex (length, scale, persistent)
00049      int length, scale, persistent;
00050 {
00051   bc_num temp;
00052 
00053   /* PHP Change:  malloc() -> pemalloc(), removed free_list code */
00054   temp = (bc_num) safe_pemalloc (1, sizeof(bc_struct)+length, scale, persistent);
00055 #if 0
00056   if (_bc_Free_list != NULL) {
00057     temp = _bc_Free_list;
00058     _bc_Free_list = temp->n_next;
00059   } else {
00060     temp = (bc_num) pemalloc (sizeof(bc_struct), persistent);
00061     if (temp == NULL) bc_out_of_memory ();
00062   }
00063 #endif
00064   temp->n_sign = PLUS;
00065   temp->n_len = length;
00066   temp->n_scale = scale;
00067   temp->n_refs = 1;
00068   /* PHP Change:  malloc() -> pemalloc() */
00069   temp->n_ptr = (char *) safe_pemalloc (1, length, scale, persistent);
00070   if (temp->n_ptr == NULL) bc_out_of_memory();
00071   temp->n_value = temp->n_ptr;
00072   memset (temp->n_ptr, 0, length+scale);
00073   return temp;
00074 }
00075 
00076 
00077 /* "Frees" a bc_num NUM.  Actually decreases reference count and only
00078    frees the storage if reference count is zero. */
00079 
00080 void
00081 _bc_free_num_ex (num, persistent)
00082     bc_num *num;
00083     int persistent;
00084 {
00085   if (*num == NULL) return;
00086   (*num)->n_refs--;
00087   if ((*num)->n_refs == 0) {
00088     if ((*num)->n_ptr)
00089               /* PHP Change:  free() -> pefree(), removed free_list code */
00090       pefree ((*num)->n_ptr, persistent);
00091        pefree(*num, persistent);
00092 #if 0
00093     (*num)->n_next = _bc_Free_list;
00094     _bc_Free_list = *num;
00095 #endif
00096   }
00097   *num = NULL;
00098 }
00099 
00100 
00101 /* Intitialize the number package! */
00102 
00103 void
00104 bc_init_numbers (TSRMLS_D)
00105 {
00106   BCG(_zero_) = _bc_new_num_ex (1,0,1);
00107   BCG(_one_)  = _bc_new_num_ex (1,0,1);
00108   BCG(_one_)->n_value[0] = 1;
00109   BCG(_two_)  = _bc_new_num_ex (1,0,1);
00110   BCG(_two_)->n_value[0] = 2;
00111 }
00112 
00113 
00114 /* Make a copy of a number!  Just increments the reference count! */
00115 
00116 bc_num
00117 bc_copy_num (bc_num num)
00118 {
00119   num->n_refs++;
00120   return num;
00121 }
00122 
00123 
00124 /* Initialize a number NUM by making it a copy of zero. */
00125 
00126 void
00127 bc_init_num (bc_num *num TSRMLS_DC)
00128 {
00129   *num = bc_copy_num (BCG(_zero_));
00130 }
00131