1 /*
2  * $Id: localalloc.c,v 1.2 2001/06/14 18:16:16 ura Exp $
3  */
4 
5 /*
6  * FreeWnn is a network-extensible Kana-to-Kanji conversion system.
7  * This file is part of FreeWnn.
8  *
9  * Copyright OMRON Corporation. 1987, 1988, 1989, 1990, 1991, 1992, 1999
10  * Copyright 1991 by Massachusetts Institute of Technology
11  *
12  * Author: OMRON SOFTWARE Co., Ltd. <freewnn@rd.kyoto.omronsoft.co.jp>
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2, or (at your option)
17  * any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with GNU Emacs; see the file COPYING.  If not, write to the
26  * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27  *
28  * Commentary:
29  *
30  * Change log:
31  *      '99/04/01       �ܴ֡�μ <honma@nasu-net.or.jp>
32  *              size �� sizeof(char *) �������ܤ��ڤ�夲�롣
33  *
34  * Last modified date: 20,Mar.1999
35  *
36  * Code:
37  *
38  */
39 
40 /*
41  * memory allocation routines
42  */
43 #include <stdio.h>
44 #include "commonhd.h"
45 #include "sdefine.h"
46 #ifdef  XJUTIL
47 #include "xjutil.h"
48 #include "sxheader.h"
49 #include "xext.h"
50 #else /* XJUTIL */
51 #include "xim.h"
52 #include "sheader.h"
53 #include "ext.h"
54 #endif /* XJUTIL */
55 
56 extern char *malloc (), *realloc (), *calloc ();
57 #ifdef ALLOC_DEBUG
58 #define DEBUG_TBL_SIZE  10000
59 typedef struct _alloc_debug_struct
60 {
61   char *ptr;
62   int size;
63 }
64 debug_tbl;
65 
66 debug_tbl alloc_tbl[DEBUG_TBL_SIZE];
67 static int debug_initialized = 0;
68 
69 void
initialize_debug()70 initialize_debug ()
71 {
72   register int i;
73 
74   for (i = 0; i < DEBUG_TBL_SIZE; i++)
75     {
76       alloc_tbl[i].ptr = NULL;
77       alloc_tbl[i].size = 0;
78     }
79   debug_initialized = 1;
80 }
81 
82 static void
register_debug(ptr,size)83 register_debug (ptr, size)
84      char *ptr;
85      int size;
86 {
87   register int i;
88 
89   for (i = 0; i < DEBUG_TBL_SIZE; i++)
90     {
91       if (alloc_tbl[i].ptr == NULL)
92         {
93           alloc_tbl[i].ptr = ptr;
94           alloc_tbl[i].size = size;
95           return;
96         }
97     }
98   print_out ("alloc_table over flow");
99 }
100 
101 void
unregister_debug(ptr)102 unregister_debug (ptr)
103      char *ptr;
104 {
105   register int i;
106 
107   for (i = 0; i < DEBUG_TBL_SIZE; i++)
108     {
109       if (alloc_tbl[i].ptr == ptr)
110         {
111           alloc_tbl[i].ptr = NULL;
112           alloc_tbl[i].size = 0;
113           return;
114         }
115     }
116   print_out1 ("illegal calling of free ptr = %x", ptr);
117 }
118 
119 #endif
120 
121 char *
Malloc(size)122 Malloc (size)
123      unsigned size;
124 {
125   char *ptr;
126   if (size == 0)
127     return (NULL);
128   size += (sizeof (char *) - (size % sizeof (char *)));
129   if ((ptr = malloc (size)) == NULL)
130     {
131 #ifdef ALLOC_DEBUG
132       print_out1 ("alloc failed with size = %d", size);
133 #endif
134       return (NULL);
135     }
136 #ifdef ALLOC_DEBUG
137   if (!debug_initialized)
138     {
139       initialize_debug ();
140     }
141   register_debug (ptr, size);
142 #endif
143   return (ptr);
144 }
145 
146 #ifdef nodef                    /* should not use Realloc, because realloc may broke
147                                    old data if it fails allocation of new area */
148 char *
Realloc(ptr,size)149 Realloc (ptr, size)
150      char *ptr;
151      unsigned size;
152 {
153   if (size == 0)
154     return (ptr);
155   size += (sizeof (char *) - (size % sizeof (char *)));
156   if (ptr == NULL)
157     return (Malloc (size));
158 #ifdef ALLOC_DEBUG
159   unregister_debug (ptr);
160 #endif
161   if ((ptr = realloc (ptr, size)) == NULL)
162     {
163       return (NULL);
164     }
165 #ifdef ALLOC_DEBUG
166   register_debug (ptr, size);
167 #endif
168   return (ptr);
169 }
170 #endif
171 
172 char *
Calloc(num,size)173 Calloc (num, size)
174      unsigned num, size;
175 {
176   char *ptr;
177   if (size == 0)
178     return (NULL);
179   size += (sizeof (char *) - (size % sizeof (char *)));
180   if ((ptr = calloc (num, size)) == NULL)
181     {
182       return (NULL);
183     }
184 #ifdef ALLOC_DEBUG
185   if (!debug_initialized)
186     {
187       initialize_debug ();
188     }
189   register_debug (ptr, size);
190 #endif
191   return (ptr);
192 }
193 
194 void
Free(ptr)195 Free (ptr)
196      char *ptr;
197 {
198   if (ptr != NULL)
199     free (ptr);
200 #ifdef ALLOC_DEBUG
201   unregister_debug (ptr);
202 #endif
203 }
204 
205 
206 char *
alloc_and_copy(src)207 alloc_and_copy (src)
208      register char *src;
209 {
210   register char *ptr;
211   register unsigned int n;
212 
213   if (src == NULL)
214     return (NULL);
215   if ((ptr = Malloc ((n = strlen (src)) + 1)) == NULL)
216     {
217       malloc_error ("allocation of work area");
218       return (NULL);
219     }
220   bcopy (src, ptr, n);
221   ptr[n] = '\0';
222   return (ptr);
223 }
224