1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/*******************************************************/
/* "C" Language Integrated Production System */
/* */
/* CLIPS Version 6.40 10/11/17 */
/* */
/* MEMORY MODULE */
/*******************************************************/
/*************************************************************/
/* Purpose: Memory allocation routines. */
/* */
/* Principal Programmer(s): */
/* Gary D. Riley */
/* */
/* Contributing Programmer(s): */
/* Brian L. Dantes */
/* */
/* Revision History: */
/* */
/* 6.24: Removed HaltExecution check from the */
/* EnvReleaseMem function. DR0863 */
/* */
/* Renamed BOOLEAN macro type to intBool. */
/* */
/* Corrected code to remove compiler warnings. */
/* */
/* 6.30: Removed conditional code for unsupported */
/* compilers/operating systems. */
/* */
/* Changed integer type/precision. */
/* */
/* Removed genlongalloc/genlongfree functions. */
/* */
/* Added get_mem and rtn_mem macros. */
/* */
/* Converted API macros to function calls. */
/* */
/* Removed deallocating message parameter from */
/* EnvReleaseMem. */
/* */
/* Removed support for BLOCK_MEMORY. */
/* */
/* 6.40: Pragma once and other inclusion changes. */
/* */
/* Added support for booleans with <stdbool.h>. */
/* */
/* Removed use of void pointers for specific */
/* data structures. */
/* */
/* ALLOW_ENVIRONMENT_GLOBALS no longer supported. */
/* */
/*************************************************************/
#include <stdio.h>
#include "setup.h"
#include "constant.h"
#include "envrnmnt.h"
#include "memalloc.h"
#include "prntutil.h"
#include "router.h"
#include "utility.h"
#include <stdlib.h>
#if WIN_MVC
#include <malloc.h>
#endif
#define STRICT_ALIGN_SIZE sizeof(double)
#define SpecialMalloc(sz) malloc((STD_SIZE) sz)
#define SpecialFree(ptr) free(ptr)
/********************************************/
/* InitializeMemory: Sets up memory tables. */
/********************************************/
void InitializeMemory(
Environment *theEnv)
{
AllocateEnvironmentData(theEnv,MEMORY_DATA,sizeof(struct memoryData),NULL);
MemoryData(theEnv)->OutOfMemoryCallback = DefaultOutOfMemoryFunction;
#if (MEM_TABLE_SIZE > 0)
MemoryData(theEnv)->MemoryTable = (struct memoryPtr **)
malloc((STD_SIZE) (sizeof(struct memoryPtr *) * MEM_TABLE_SIZE));
if (MemoryData(theEnv)->MemoryTable == NULL)
{
PrintErrorID(theEnv,"MEMORY",1,true);
WriteString(theEnv,STDERR,"Out of memory.\n");
ExitRouter(theEnv,EXIT_FAILURE);
}
else
{
int i;
for (i = 0; i < MEM_TABLE_SIZE; i++) MemoryData(theEnv)->MemoryTable[i] = NULL;
}
#else // MEM_TABLE_SIZE == 0
MemoryData(theEnv)->MemoryTable = NULL;
#endif
}
/***************************************************/
/* genalloc: A generic memory allocation function. */
/***************************************************/
void *genalloc(
Environment *theEnv,
size_t size)
{
void *memPtr;
memPtr = malloc(size);
if (memPtr == NULL)
{
ReleaseMem(theEnv,(long long) ((size * 5 > 4096) ? size * 5 : 4096));
memPtr = malloc(size);
if (memPtr == NULL)
{
ReleaseMem(theEnv,-1);
memPtr = malloc(size);
while (memPtr == NULL)
{
if ((*MemoryData(theEnv)->OutOfMemoryCallback)(theEnv,size))
return NULL;
memPtr = malloc(size);
}
}
}
MemoryData(theEnv)->MemoryAmount += size;
MemoryData(theEnv)->MemoryCalls++;
return memPtr;
}
/***********************************************/
/* DefaultOutOfMemoryFunction: Function called */
/* when the KB runs out of memory. */
/***********************************************/
bool DefaultOutOfMemoryFunction(
Environment *theEnv,
size_t size)
{
#if MAC_XCD
#pragma unused(size)
#endif
PrintErrorID(theEnv,"MEMORY",1,true);
WriteString(theEnv,STDERR,"Out of memory.\n");
ExitRouter(theEnv,EXIT_FAILURE);
return true;
}
/**********************************************************/
/* SetOutOfMemoryFunction: Allows the function which is */
/* called when the KB runs out of memory to be changed. */
/**********************************************************/
OutOfMemoryFunction *SetOutOfMemoryFunction(
Environment *theEnv,
OutOfMemoryFunction *functionPtr)
{
OutOfMemoryFunction *tmpPtr;
tmpPtr = MemoryData(theEnv)->OutOfMemoryCallback;
MemoryData(theEnv)->OutOfMemoryCallback = functionPtr;
return tmpPtr;
}
/****************************************************/
/* genfree: A generic memory deallocation function. */
/****************************************************/
void genfree(
Environment *theEnv,
void *waste,
size_t size)
{
free(waste);
MemoryData(theEnv)->MemoryAmount -= size;
MemoryData(theEnv)->MemoryCalls--;
}
/******************************************************/
/* genrealloc: Simple (i.e. dumb) version of realloc. */
/******************************************************/
void *genrealloc(
Environment *theEnv,
void *oldaddr,
size_t oldsz,
size_t newsz)
{
char *newaddr;
unsigned i;
size_t limit;
newaddr = ((newsz != 0) ? (char *) gm2(theEnv,newsz) : NULL);
if (oldaddr != NULL)
{
limit = (oldsz < newsz) ? oldsz : newsz;
for (i = 0 ; i < limit ; i++)
{ newaddr[i] = ((char *) oldaddr)[i]; }
for ( ; i < newsz; i++)
{ newaddr[i] = '\0'; }
rm(theEnv,oldaddr,oldsz);
}
return((void *) newaddr);
}
/********************************/
/* MemUsed: C access routine */
/* for the mem-used command. */
/********************************/
long long MemUsed(
Environment *theEnv)
{
return MemoryData(theEnv)->MemoryAmount;
}
/***********************************/
/* MemRequests: C access routine */
/* for the mem-requests command. */
/***********************************/
long long MemRequests(
Environment *theEnv)
{
return MemoryData(theEnv)->MemoryCalls;
}
/***************************************/
/* UpdateMemoryUsed: Allows the amount */
/* of memory used to be updated. */
/***************************************/
long long UpdateMemoryUsed(
Environment *theEnv,
long long value)
{
MemoryData(theEnv)->MemoryAmount += value;
return MemoryData(theEnv)->MemoryAmount;
}
/*******************************************/
/* UpdateMemoryRequests: Allows the number */
/* of memory requests to be updated. */
/*******************************************/
long long UpdateMemoryRequests(
Environment *theEnv,
long long value)
{
MemoryData(theEnv)->MemoryCalls += value;
return MemoryData(theEnv)->MemoryCalls;
}
/**********************************/
/* ReleaseMem: C access routine */
/* for the release-mem command. */
/**********************************/
long long ReleaseMem(
Environment *theEnv,
long long maximum)
{
struct memoryPtr *tmpPtr, *memPtr;
unsigned int i;
long long returns = 0;
long long amount = 0;
for (i = (MEM_TABLE_SIZE - 1) ; i >= sizeof(char *) ; i--)
{
YieldTime(theEnv);
memPtr = MemoryData(theEnv)->MemoryTable[i];
while (memPtr != NULL)
{
tmpPtr = memPtr->next;
genfree(theEnv,memPtr,i);
memPtr = tmpPtr;
amount += i;
returns++;
if ((returns % 100) == 0)
{ YieldTime(theEnv); }
}
MemoryData(theEnv)->MemoryTable[i] = NULL;
if ((amount > maximum) && (maximum > 0))
{ return amount; }
}
return amount;
}
/*****************************************************/
/* gm1: Allocates memory and sets all bytes to zero. */
/*****************************************************/
void *gm1(
Environment *theEnv,
size_t size)
{
struct memoryPtr *memPtr;
char *tmpPtr;
size_t i;
if ((size < sizeof(char *)) ||
(size >= MEM_TABLE_SIZE))
{
tmpPtr = (char *) genalloc(theEnv,size);
for (i = 0 ; i < size ; i++)
{ tmpPtr[i] = '\0'; }
return((void *) tmpPtr);
}
memPtr = (struct memoryPtr *) MemoryData(theEnv)->MemoryTable[size];
if (memPtr == NULL)
{
tmpPtr = (char *) genalloc(theEnv,size);
for (i = 0 ; i < size ; i++)
{ tmpPtr[i] = '\0'; }
return((void *) tmpPtr);
}
MemoryData(theEnv)->MemoryTable[size] = memPtr->next;
tmpPtr = (char *) memPtr;
for (i = 0 ; i < size ; i++)
{ tmpPtr[i] = '\0'; }
return ((void *) tmpPtr);
}
/*****************************************************/
/* gm2: Allocates memory and does not initialize it. */
/*****************************************************/
void *gm2(
Environment *theEnv,
size_t size)
{
#if (MEM_TABLE_SIZE > 0)
struct memoryPtr *memPtr;
#endif
if (size < sizeof(char *))
{ return genalloc(theEnv,size); }
#if (MEM_TABLE_SIZE > 0)
if (size >= MEM_TABLE_SIZE) return genalloc(theEnv,size);
memPtr = (struct memoryPtr *) MemoryData(theEnv)->MemoryTable[size];
if (memPtr == NULL)
{ return genalloc(theEnv,size); }
MemoryData(theEnv)->MemoryTable[size] = memPtr->next;
return ((void *) memPtr);
#else
return genalloc(theEnv,size);
#endif
}
/****************************************/
/* rm: Returns a block of memory to the */
/* maintained pool of free memory. */
/****************************************/
void rm(
Environment *theEnv,
void *str,
size_t size)
{
#if (MEM_TABLE_SIZE > 0)
struct memoryPtr *memPtr;
#endif
if (size == 0)
{
SystemError(theEnv,"MEMORY",1);
ExitRouter(theEnv,EXIT_FAILURE);
return;
}
if (size < sizeof(char *))
{
genfree(theEnv,str,size);
return;
}
#if (MEM_TABLE_SIZE > 0)
if (size >= MEM_TABLE_SIZE)
{
genfree(theEnv,str,size);
return;
}
memPtr = (struct memoryPtr *) str;
memPtr->next = MemoryData(theEnv)->MemoryTable[size];
MemoryData(theEnv)->MemoryTable[size] = memPtr;
#else
genfree(theEnv,str,size);
#endif
}
/***************************************************/
/* PoolSize: Returns number of bytes in free pool. */
/***************************************************/
unsigned long PoolSize(
Environment *theEnv)
{
unsigned long cnt = 0;
#if (MEM_TABLE_SIZE > 0)
int i;
struct memoryPtr *memPtr;
for (i = sizeof(char *) ; i < MEM_TABLE_SIZE ; i++)
{
memPtr = MemoryData(theEnv)->MemoryTable[i];
while (memPtr != NULL)
{
cnt += (unsigned long) i;
memPtr = memPtr->next;
}
}
#endif
return(cnt);
}
/***************************************************************/
/* ActualPoolSize : Returns number of bytes DOS requires to */
/* store the free pool. This routine is functionally */
/* equivalent to pool_size on anything other than the IBM-PC */
/***************************************************************/
unsigned long ActualPoolSize(
Environment *theEnv)
{
return(PoolSize(theEnv));
}
/*****************************************/
/* SetConserveMemory: Allows the setting */
/* of the memory conservation flag. */
/*****************************************/
bool SetConserveMemory(
Environment *theEnv,
bool value)
{
bool ov;
ov = MemoryData(theEnv)->ConserveMemory;
MemoryData(theEnv)->ConserveMemory = value;
return ov;
}
/****************************************/
/* GetConserveMemory: Returns the value */
/* of the memory conservation flag. */
/****************************************/
bool GetConserveMemory(
Environment *theEnv)
{
return MemoryData(theEnv)->ConserveMemory;
}
/**************************/
/* genmemcpy: */
/**************************/
void genmemcpy(
char *dst,
char *src,
unsigned long size)
{
unsigned long i;
for (i = 0L ; i < size ; i++)
dst[i] = src[i];
}