tizen 2.4 release
[external/clips.git] / src / utility.h
1    /*******************************************************/
2    /*      "C" Language Integrated Production System      */
3    /*                                                     */
4    /*             CLIPS Version 6.30  08/22/14            */
5    /*                                                     */
6    /*                 UTILITY HEADER FILE                 */
7    /*******************************************************/
8
9 /*************************************************************/
10 /* Purpose: Provides a set of utility functions useful to    */
11 /*   other modules. Primarily these are the functions for    */
12 /*   handling periodic garbage collection and appending      */
13 /*   string data.                                            */
14 /*                                                           */
15 /* Principal Programmer(s):                                  */
16 /*      Gary D. Riley                                        */
17 /*                                                           */
18 /* Contributing Programmer(s):                               */
19 /*                                                           */
20 /* Revision History:                                         */
21 /*                                                           */
22 /*      6.24: Renamed BOOLEAN macro type to intBool.         */
23 /*                                                           */
24 /*      6.30: Changed integer type/precision.                */
25 /*                                                           */
26 /*            Changed garbage collection algorithm.          */
27 /*                                                           */
28 /*            Added CopyString, DeleteString,                */
29 /*            InsertInString,and EnlargeString functions.    */
30 /*                                                           */
31 /*            Used genstrncpy function instead of strncpy    */
32 /*            function.                                      */
33 /*                                                           */
34 /*            Support for typed EXTERNAL_ADDRESS.            */
35 /*                                                           */
36 /*            Support for tracked memory (allows memory to   */
37 /*            be freed if CLIPS is exited while executing).  */
38 /*                                                           */
39 /*            Added UTF-8 routines.                          */
40 /*                                                           */
41 /*            Added const qualifiers to remove C++           */
42 /*            deprecation warnings.                          */
43 /*                                                           */
44 /*            Converted API macros to function calls.        */
45 /*                                                           */
46 /*************************************************************/
47
48 #ifndef _H_utility
49 #define _H_utility
50
51 #ifndef _H_evaluatn
52 #include "evaluatn.h"
53 #endif
54
55 #ifdef LOCALE
56 #undef LOCALE
57 #endif
58
59 struct callFunctionItem
60   {
61    const char *name;
62    void (*func)(void *);
63    int priority;
64    struct callFunctionItem *next;
65    short int environmentAware;
66    void *context;
67   };
68
69 struct callFunctionItemWithArg
70   {
71    const char *name;
72    void (*func)(void *,void *);
73    int priority;
74    struct callFunctionItemWithArg *next;
75    short int environmentAware;
76    void *context;
77   };
78
79 struct trackedMemory
80   {
81    void *theMemory;
82    struct trackedMemory *next;
83    struct trackedMemory *prev;
84    size_t memSize;
85   };
86
87 struct garbageFrame
88   {
89    short dirty;
90    short topLevel;
91    struct garbageFrame *priorFrame;
92    struct ephemeron *ephemeralSymbolList;
93    struct ephemeron *ephemeralFloatList;
94    struct ephemeron *ephemeralIntegerList;
95    struct ephemeron *ephemeralBitMapList;
96    struct ephemeron *ephemeralExternalAddressList;
97    struct multifield *ListOfMultifields;
98    struct multifield *LastMultifield;
99   };
100
101 #define UTILITY_DATA 55
102
103 struct utilityData
104   {
105    struct callFunctionItem *ListOfCleanupFunctions;
106    struct callFunctionItem *ListOfPeriodicFunctions;
107    short GarbageCollectionLocks;
108    short PeriodicFunctionsEnabled;
109    short YieldFunctionEnabled;
110    void (*YieldTimeFunction)(void);
111    struct trackedMemory *trackList;
112    struct garbageFrame MasterGarbageFrame;
113    struct garbageFrame *CurrentGarbageFrame;
114   };
115
116 #define UtilityData(theEnv) ((struct utilityData *) GetEnvironmentData(theEnv,UTILITY_DATA))
117
118   /* Is c the start of a utf8 sequence? */
119 #define IsUTF8Start(ch) (((ch) & 0xC0) != 0x80)
120 #define IsUTF8MultiByteStart(ch) ((((unsigned char) ch) >= 0xC0) && (((unsigned char) ch) <= 0xF7))
121 #define IsUTF8MultiByteContinuation(ch) ((((unsigned char) ch) >= 0x80) && (((unsigned char) ch) <= 0xBF))
122
123 #ifdef _UTILITY_SOURCE_
124 #define LOCALE
125 #else
126 #define LOCALE extern
127 #endif
128
129    LOCALE void                           InitializeUtilityData(void *);
130    LOCALE intBool                        AddCleanupFunction(void *,const char *,void (*)(void *),int);
131    LOCALE intBool                        EnvAddPeriodicFunction(void *,const char *,void (*)(void *),int);
132    LOCALE intBool                        AddPeriodicFunction(const char *,void (*)(void),int);
133    LOCALE intBool                        RemoveCleanupFunction(void *,const char *);
134    LOCALE intBool                        EnvRemovePeriodicFunction(void *,const char *);
135    LOCALE char                          *CopyString(void *,const char *);
136    LOCALE void                           DeleteString(void *,char *);
137    LOCALE const char                    *AppendStrings(void *,const char *,const char *);
138    LOCALE const char                    *StringPrintForm(void *,const char *);
139    LOCALE char                          *AppendToString(void *,const char *,char *,size_t *,size_t *);
140    LOCALE char                          *InsertInString(void *,const char *,size_t,char *,size_t *,size_t *);
141    LOCALE char                          *AppendNToString(void *,const char *,char *,size_t,size_t *,size_t *);
142    LOCALE char                          *EnlargeString(void *,size_t,char *,size_t *,size_t *);
143    LOCALE char                          *ExpandStringWithChar(void *,int,char *,size_t *,size_t *,size_t);
144    LOCALE struct callFunctionItem       *AddFunctionToCallList(void *,const char *,int,void (*)(void *),
145                                                                struct callFunctionItem *,intBool);
146    LOCALE struct callFunctionItem       *AddFunctionToCallListWithContext(void *,const char *,int,void (*)(void *),
147                                                                           struct callFunctionItem *,intBool,void *);
148    LOCALE struct callFunctionItem       *RemoveFunctionFromCallList(void *,const char *,
149                                                              struct callFunctionItem *,
150                                                              int *);
151    LOCALE void                           DeallocateCallList(void *,struct callFunctionItem *);
152    LOCALE struct callFunctionItemWithArg *AddFunctionToCallListWithArg(void *,const char *,int,void (*)(void *, void *),
153                                                                        struct callFunctionItemWithArg *,intBool);
154    LOCALE struct callFunctionItemWithArg *AddFunctionToCallListWithArgWithContext(void *,const char *,int,void (*)(void *, void *),
155                                                                                   struct callFunctionItemWithArg *,intBool,void *);
156    LOCALE struct callFunctionItemWithArg *RemoveFunctionFromCallListWithArg(void *,const char *,
157                                                                             struct callFunctionItemWithArg *,
158                                                                             int *);
159    LOCALE void                           DeallocateCallListWithArg(void *,struct callFunctionItemWithArg *);
160    LOCALE unsigned long                  ItemHashValue(void *,unsigned short,void *,unsigned long);
161    LOCALE void                           YieldTime(void *);
162    LOCALE void                           EnvIncrementGCLocks(void *);
163    LOCALE void                           EnvDecrementGCLocks(void *);
164    LOCALE short                          EnablePeriodicFunctions(void *,short);
165    LOCALE short                          EnableYieldFunction(void *,short);
166    LOCALE struct trackedMemory          *AddTrackedMemory(void *,void *,size_t);
167    LOCALE void                           RemoveTrackedMemory(void *,struct trackedMemory *);
168    LOCALE void                           UTF8Increment(const char *,size_t *);
169    LOCALE size_t                         UTF8Offset(const char *,size_t);
170    LOCALE size_t                         UTF8Length(const char *);
171    LOCALE size_t                         UTF8CharNum(const char *,size_t);
172    LOCALE void                           RestorePriorGarbageFrame(void *,struct garbageFrame *,struct garbageFrame *,struct dataObject *);
173    LOCALE void                           CallCleanupFunctions(void *);
174    LOCALE void                           CallPeriodicTasks(void *);
175    LOCALE void                           CleanCurrentGarbageFrame(void *,struct dataObject *);
176
177 #if ALLOW_ENVIRONMENT_GLOBALS
178
179    LOCALE void                           IncrementGCLocks(void);
180    LOCALE void                           DecrementGCLocks(void);
181    LOCALE intBool                        RemovePeriodicFunction(const char *);
182
183 #endif /* ALLOW_ENVIRONMENT_GLOBALS */
184
185 #endif /* _H_utility */
186
187
188
189