• R/O
  • HTTP
  • SSH
  • HTTPS

CHNOSProject: Commit

CHNOSProjectのすべてのプロジェクトのソースのルート


Commit MetaInfo

Revisiónffb73332f17338f77ba58e14dadd13dcf9df4cfa (tree)
Tiempo2013-05-19 06:22:03
Autorhikarupsp <hikarupsp@user...>
Commiterhikarupsp

Log Message

referenceCountを導入。今のところ正常に動作している。メモリリークについても一部改善。69.67KB(1852/2023)>36.59KB(588/2163)

Cambiar Resumen

Diferencia incremental

Binary files a/CHNOSProject/AI003/AI003.xcodeproj/project.xcworkspace/xcuserdata/hikaru.xcuserdatad/UserInterfaceState.xcuserstate and b/CHNOSProject/AI003/AI003.xcodeproj/project.xcworkspace/xcuserdata/hikaru.xcuserdatad/UserInterfaceState.xcuserstate differ
--- a/CHNOSProject/AI003/AI003.xcodeproj/xcuserdata/hikaru.xcuserdatad/xcdebugger/Breakpoints.xcbkptlist
+++ b/CHNOSProject/AI003/AI003.xcodeproj/xcuserdata/hikaru.xcuserdatad/xcdebugger/Breakpoints.xcbkptlist
@@ -86,11 +86,11 @@
8686 ignoreCount = "0"
8787 continueAfterRunningActions = "No"
8888 filePath = "AI003/main.c"
89- timestampString = "390425099.955042"
89+ timestampString = "390602565.100723"
9090 startingColumnNumber = "9223372036854775807"
9191 endingColumnNumber = "9223372036854775807"
92- startingLineNumber = "272"
93- endingLineNumber = "272"
92+ startingLineNumber = "280"
93+ endingLineNumber = "280"
9494 landmarkName = "AI_Memory_AddRootWordData()"
9595 landmarkType = "7">
9696 </FileBreakpoint>
@@ -175,11 +175,11 @@
175175 ignoreCount = "0"
176176 continueAfterRunningActions = "No"
177177 filePath = "AI003/main.c"
178- timestampString = "390425099.955042"
178+ timestampString = "390602565.100723"
179179 startingColumnNumber = "9223372036854775807"
180180 endingColumnNumber = "9223372036854775807"
181- startingLineNumber = "107"
182- endingLineNumber = "107"
181+ startingLineNumber = "119"
182+ endingLineNumber = "119"
183183 landmarkName = "AI_Think_LearnWordFromInputString()"
184184 landmarkType = "7">
185185 </FileBreakpoint>
@@ -188,11 +188,11 @@
188188 ignoreCount = "0"
189189 continueAfterRunningActions = "No"
190190 filePath = "AI003/main.c"
191- timestampString = "390425099.955042"
191+ timestampString = "390602565.100723"
192192 startingColumnNumber = "9223372036854775807"
193193 endingColumnNumber = "9223372036854775807"
194- startingLineNumber = "123"
195- endingLineNumber = "123"
194+ startingLineNumber = "136"
195+ endingLineNumber = "136"
196196 landmarkName = "AI_Think_LearnWordFromInputString()"
197197 landmarkType = "7">
198198 </FileBreakpoint>
@@ -201,11 +201,11 @@
201201 ignoreCount = "0"
202202 continueAfterRunningActions = "No"
203203 filePath = "AI003/main.c"
204- timestampString = "390425099.955042"
204+ timestampString = "390602565.100723"
205205 startingColumnNumber = "9223372036854775807"
206206 endingColumnNumber = "9223372036854775807"
207- startingLineNumber = "128"
208- endingLineNumber = "128"
207+ startingLineNumber = "129"
208+ endingLineNumber = "129"
209209 landmarkName = "AI_Think_LearnWordFromInputString()"
210210 landmarkType = "7">
211211 </FileBreakpoint>
@@ -214,11 +214,11 @@
214214 ignoreCount = "0"
215215 continueAfterRunningActions = "No"
216216 filePath = "AI003/main.c"
217- timestampString = "390473631.009346"
217+ timestampString = "390602565.100723"
218218 startingColumnNumber = "9223372036854775807"
219219 endingColumnNumber = "9223372036854775807"
220- startingLineNumber = "117"
221- endingLineNumber = "117"
220+ startingLineNumber = "120"
221+ endingLineNumber = "120"
222222 landmarkName = "AI_Think_LearnWordFromInputString()"
223223 landmarkType = "7">
224224 </FileBreakpoint>
@@ -227,24 +227,11 @@
227227 ignoreCount = "0"
228228 continueAfterRunningActions = "No"
229229 filePath = "AI003/main.c"
230- timestampString = "390473685.266823"
230+ timestampString = "390602565.100723"
231231 startingColumnNumber = "9223372036854775807"
232232 endingColumnNumber = "9223372036854775807"
233- startingLineNumber = "108"
234- endingLineNumber = "108"
235- landmarkName = "AI_Think_LearnWordFromInputString()"
236- landmarkType = "7">
237- </FileBreakpoint>
238- <FileBreakpoint
239- shouldBeEnabled = "No"
240- ignoreCount = "0"
241- continueAfterRunningActions = "No"
242- filePath = "AI003/main.c"
243- timestampString = "390473859.806502"
244- startingColumnNumber = "9223372036854775807"
245- endingColumnNumber = "9223372036854775807"
246- startingLineNumber = "178"
247- endingLineNumber = "178"
233+ startingLineNumber = "186"
234+ endingLineNumber = "186"
248235 landmarkName = "AI_Think_SlideLookUpWordByHistory()"
249236 landmarkType = "7">
250237 </FileBreakpoint>
@@ -253,11 +240,11 @@
253240 ignoreCount = "0"
254241 continueAfterRunningActions = "No"
255242 filePath = "AI003/main.c"
256- timestampString = "390474101.310759"
243+ timestampString = "390602565.100723"
257244 startingColumnNumber = "9223372036854775807"
258245 endingColumnNumber = "9223372036854775807"
259- startingLineNumber = "184"
260- endingLineNumber = "184"
246+ startingLineNumber = "192"
247+ endingLineNumber = "192"
261248 landmarkName = "AI_Think_SlideLookUpWordByHistory()"
262249 landmarkType = "7">
263250 </FileBreakpoint>
--- a/CHNOSProject/AI003/AI003/core.h
+++ b/CHNOSProject/AI003/AI003/core.h
@@ -12,6 +12,7 @@
1212 #include "chnlib.h"
1313
1414 #define AI_CONFIG_FILE_NAME "config.txt"
15+#define AI_INPUT_HISTORY_MAX 10
1516
1617 typedef struct AI_WORKING_SET AI_WorkingSet;
1718 struct AI_WORKING_SET {
--- a/CHNOSProject/AI003/AI003/main.c
+++ b/CHNOSProject/AI003/AI003/main.c
@@ -64,12 +64,13 @@ int main(int argc, const char * argv[])
6464 } else if(CHNLIB_String_CompareStringWithCString(temp, "exit")){
6565 break;
6666 } else if(CHNLIB_String_CompareStringWithCString(temp, "wordlist")){
67- sorted = CHNLIB_UIPArray_SortInDescendingOrderByData32(WorkingSet.RootWordList);
67+ sorted = CHNLIB_UIPArray_GetSortedInDescendingOrderByData32(WorkingSet.RootWordList);
6868 i_max = CHNLIB_UIPArray_GetNumberOfDatas(sorted);
6969 for(i = 0; i < i_max; i++){
7070 printf("word%3d:%3d:%s\n", i, CHNLIB_UIPArray_GetData32ByIndex(sorted, i), CHNLIB_String_GetReferencePointerOfCString(CHNLIB_UIPArray_GetPointerByIndex(sorted, i)));
7171 }
72- CHNLIB_UIPArray_FreeOnlyArray(&sorted);
72+ //CHNLIB_UIPArray_FreeOnlyArray(&sorted);
73+ release(sorted);
7374 } else if(CHNLIB_String_CompareStringWithCString(temp, "readfile")){
7475 //::readfile:filename
7576 readfp = fopen(CHNLIB_String_GetReferencePointerOfCString(CHNLIB_UIPArray_GetPointerByIndex(separated, 4)), "rb");
@@ -81,8 +82,9 @@ int main(int argc, const char * argv[])
8182 }
8283 }
8384
84- //WorkingSet.SystemWordList0を使っているので、解放しないように注意
85- CHNLIB_UIPArray_FreeSelectedAll(&separated);
85+ ////WorkingSet.SystemWordList0を使っているので、解放しないように注意
86+ //CHNLIB_UIPArray_FreeSelectedAll(&separated);
87+ release(separated);
8688 }
8789
8890 if(!passthink){
@@ -90,8 +92,17 @@ int main(int argc, const char * argv[])
9092 //::readfile:AITestData_ja.txt
9193 AI_Think_LearnWordFromInputString(input);
9294
93- CHNLIB_UIPArray_AppendLast(&WorkingSet.InputHistory, CHNLIB_UIPArray_GetNumberOfDatas(WorkingSet.InputHistory), input);
95+ //入力履歴を追加
96+ i = CHNLIB_UIPArray_AppendLast(&WorkingSet.InputHistory, 0, input);
97+ if(i > AI_INPUT_HISTORY_MAX){
98+ //履歴の整理
99+ for(i = 0; i < AI_INPUT_HISTORY_MAX; i++){
100+ CHNLIB_UIPArray_RemoveByIndex(&WorkingSet.InputHistory, 0);
101+ }
102+ }
94103 }
104+
105+ release(input);
95106 }
96107
97108 return 0;
@@ -117,15 +128,11 @@ void AI_Think_LearnWordFromInputString(CHNLIB_String *input)
117128 tagIndex = AI_Memory_AddRootWordData(CHNLIB_UIPArray_GetPointerByIndex(candidateWordList, i));
118129
119130 CHNLIB_UIPArray_SetData32ByIndex(WorkingSet.RootWordList, tagIndex, CHNLIB_UIPArray_GetData32ByIndex(WorkingSet.RootWordList, tagIndex) + CHNLIB_UIPArray_GetData32ByIndex(candidateWordList, i));
120-
121- if(tagIndex <= maxExistingWordIndex){
122- //既存のタグなので、解放されるようにdata32をFalseにしておく。
123- CHNLIB_UIPArray_SetData32ByIndex(candidateWordList, i, False);
124- }
125- //新規追加されたタグについては0以上の値つまりTrueが設定されているはずなので、解放されない。
126131 }
127- //data32 == Falseのタグ、つまり既存のタグと同じだったもののみ解放する。
128- CHNLIB_UIPArray_FreeSelectedAll(&candidateWordList);
132+ //すでに存在したタグは追加されていないため、ReferenceCountが1のはずなので、解放される。
133+ //追加されたタグはReferenceCountが2のため、解放されない。
134+ release(candidateWordList);
135+ //CHNLIB_UIPArray_FreeSelectedAll(&candidateWordList);
129136
130137 return;
131138 }
@@ -175,7 +182,7 @@ CHNLIB_UIPArray *AI_Think_SlideLookUpWordByHistory(CHNLIB_String *input)
175182 }
176183 }
177184 if(candidatelength > 0){
178- CHNLIB_UIPArray_AppendLast_ProtectFromDuplication(&candidatewordlist, 0, CHNLIB_String_ExtractByLength(input, CHNLIB_UTF8_GetByteSizeFromLengthByCharacter(cstr_input, 0, k - 1), CHNLIB_UTF8_GetByteSizeFromLengthByCharacter(cstrp_input, 0, candidatelength - 1)), &AI_Memory_AddRootWordData_IsDuplicated);
185+ CHNLIB_UIPArray_AppendLast_ProtectFromDuplication(&candidatewordlist, 0, autorelease(CHNLIB_String_ExtractByLength(input, CHNLIB_UTF8_GetByteSizeFromLengthByCharacter(cstr_input, 0, k - 1), CHNLIB_UTF8_GetByteSizeFromLengthByCharacter(cstrp_input, 0, candidatelength - 1))), &AI_Memory_AddRootWordData_IsDuplicated);
179186 }
180187 CHNLIB_UTF8_GetNextUnicodeOfCharacter(cstrp_input, &cstrp_input);
181188 }
@@ -290,8 +297,8 @@ void AI_System_InitializeSystemWorkingSet(void)
290297 {
291298 //WorkingSetを初期化する。
292299 WorkingSet.SystemWordList0 = CHNLIB_UIPArray_Initialize();
293- CHNLIB_UIPArray_AppendLast(&WorkingSet.SystemWordList0, True, CHNLIB_String_Initialize(":"));
294- CHNLIB_UIPArray_AppendLast(&WorkingSet.SystemWordList0, True, CHNLIB_String_Initialize(","));
300+ CHNLIB_UIPArray_AppendLast(&WorkingSet.SystemWordList0, True, autorelease(CHNLIB_String_Initialize(":")));
301+ CHNLIB_UIPArray_AppendLast(&WorkingSet.SystemWordList0, True, autorelease(CHNLIB_String_Initialize(",")));
295302
296303 WorkingSet.InputHistory = CHNLIB_UIPArray_Initialize();
297304
@@ -310,6 +317,7 @@ void AI_System_LoadMemory(const char configfilename[])
310317 fclose(fp);
311318
312319 fp = fopen(CHNLIB_String_GetReferencePointerOfCString(currentfilename), "rb");
320+ release(currentfilename);
313321
314322 WorkingSet.RootWordList = CHNLIB_UIPArray_Initialize();
315323 for(i = 1; ;i++){
@@ -318,12 +326,9 @@ void AI_System_LoadMemory(const char configfilename[])
318326 break;
319327 }
320328 CHNLIB_UIPArray_AppendLast(&WorkingSet.RootWordList, i, line);
321- //CHNLIB_String_Free(line);リストに登録している実体なので解放の必要なし!
329+ release(line);
322330 }
323331 fclose(fp);
324- CHNLIB_String_Free(currentfilename);
325-
326- //CHNLIB_Debug_PrintStructureData(WorkingSet.RootWordList, 0);
327332
328333 return;
329334 }
Binary files a/CHNOSProject/CHNOSProject.xcodeproj/project.xcworkspace/xcuserdata/hikaru.xcuserdatad/UserInterfaceState.xcuserstate and b/CHNOSProject/CHNOSProject.xcodeproj/project.xcworkspace/xcuserdata/hikaru.xcuserdatad/UserInterfaceState.xcuserstate differ
--- a/CHNOSProject/CHNOSProject.xcodeproj/xcuserdata/hikaru.xcuserdatad/xcdebugger/Breakpoints.xcbkptlist
+++ b/CHNOSProject/CHNOSProject.xcodeproj/xcuserdata/hikaru.xcuserdatad/xcdebugger/Breakpoints.xcbkptlist
@@ -60,11 +60,11 @@
6060 ignoreCount = "0"
6161 continueAfterRunningActions = "No"
6262 filePath = "chn/chnlib.h"
63- timestampString = "384873562.138536"
63+ timestampString = "390601441.205558"
6464 startingColumnNumber = "9223372036854775807"
6565 endingColumnNumber = "9223372036854775807"
66- startingLineNumber = "118"
67- endingLineNumber = "118">
66+ startingLineNumber = "119"
67+ endingLineNumber = "119">
6868 </FileBreakpoint>
6969 <FileBreakpoint
7070 shouldBeEnabled = "No"
@@ -92,5 +92,18 @@
9292 landmarkName = "calc_value0()"
9393 landmarkType = "7">
9494 </FileBreakpoint>
95+ <FileBreakpoint
96+ shouldBeEnabled = "No"
97+ ignoreCount = "0"
98+ continueAfterRunningActions = "No"
99+ filePath = "libtest/libtest/main.c"
100+ timestampString = "390597364.50073"
101+ startingColumnNumber = "9223372036854775807"
102+ endingColumnNumber = "9223372036854775807"
103+ startingLineNumber = "28"
104+ endingLineNumber = "28"
105+ landmarkName = "main()"
106+ landmarkType = "7">
107+ </FileBreakpoint>
95108 </FileBreakpoints>
96109 </Bucket>
--- a/CHNOSProject/chn/chnlib.c
+++ b/CHNOSProject/chn/chnlib.c
@@ -95,6 +95,7 @@ void CHNLIB_StructureHeader_Initialize(CHNLIB_StructureHeader *header, uint type
9595 {
9696 //ライブラリ共通構造体ヘッダの初期化を行う。
9797 //各構造体のInitializeで呼ばれることを想定している。
98+ //referenceCountは1で初期化される(初期化を行った関数が所有すると考える)
9899 if(header == NULL){
99100 CHNLIB_ReportError("Null structure.", CHNLIB_DEBUG_ARGUMENTS);
100101 return;
@@ -102,6 +103,11 @@ void CHNLIB_StructureHeader_Initialize(CHNLIB_StructureHeader *header, uint type
102103
103104 header->signature = CHNLIB_STRUCTURE_SIGNATURE;
104105 header->typeid = typeid;
106+#ifdef DEBUG_MEMORY_REFERENCE_COUNT
107+ CHNLIB_Debug("Init with referenceCount = 1 [%p].", CHNLIB_DEBUG_ARGUMENTS, header);
108+#endif
109+ header->referenceCount = 1;
110+ header->destructor = NULL;
105111 return;
106112 }
107113
@@ -160,6 +166,83 @@ void CHNLIB_System_FreeMemory(void *p, const char filename[], int line, const ch
160166 return;
161167 }
162168
169+void CHNLIB_Retain(void **structure)
170+{
171+ //structureのreferenceCountをインクリメントする。
172+ //referenceCountの初期値は1
173+
174+ CHNLIB_StructureHeader *strhead;
175+
176+ if(structure == NULL || *structure == NULL){
177+ return;
178+ }
179+
180+ strhead = (CHNLIB_StructureHeader *)*structure;
181+ if(strhead->signature == CHNLIB_STRUCTURE_SIGNATURE){
182+#ifdef DEBUG_MEMORY_REFERENCE_COUNT
183+ CHNLIB_Debug("Retain [%p].", CHNLIB_DEBUG_ARGUMENTS, *structure);
184+#endif
185+ strhead->referenceCount++;
186+ }
187+
188+ return;
189+}
190+
191+void CHNLIB_Release(void **structure)
192+{
193+ //structureのreferenceCountをデクリメントする。
194+ //デクリメントした結果referenceCount==0となった時、そのstructureを解放する。
195+ //structureが内包しているオブジェクトについては、releaseを行う。
196+
197+ CHNLIB_StructureHeader *strhead;
198+
199+ if(structure == NULL || *structure == NULL){
200+ return;
201+ }
202+
203+ strhead = (CHNLIB_StructureHeader *)*structure;
204+ if(strhead->signature == CHNLIB_STRUCTURE_SIGNATURE){
205+ if(strhead->referenceCount <= 1){
206+ if(strhead->destructor == NULL){
207+#ifdef DEBUG_MEMORY_REFERENCE_COUNT
208+ CHNLIB_Debug("Release(with free)[%p].", CHNLIB_DEBUG_ARGUMENTS, *structure);
209+#endif
210+ free(*structure);
211+ } else{
212+ strhead->destructor(structure);
213+ }
214+ } else{
215+#ifdef DEBUG_MEMORY_REFERENCE_COUNT
216+ CHNLIB_Debug("Release[%p].", CHNLIB_DEBUG_ARGUMENTS, *structure);
217+#endif
218+ strhead->referenceCount--;
219+ *structure = NULL;
220+ }
221+ }
222+
223+ return;
224+}
225+
226+void *CHNLIB_AutoRelease(void *structure)
227+{
228+ //structureのreferenceCountを、destructせずに0にする。
229+ //オブジェクト確保後に自分の所有権を放棄して他の関数へ渡すときに利用する。
230+
231+ CHNLIB_StructureHeader *strhead;
232+
233+ if(structure != NULL){
234+ strhead = (CHNLIB_StructureHeader *)structure;
235+ if(strhead->signature == CHNLIB_STRUCTURE_SIGNATURE){
236+#ifdef DEBUG_MEMORY_REFERENCE_COUNT
237+ CHNLIB_Debug("Set AutoRelease [%p].", CHNLIB_DEBUG_ARGUMENTS, structure);
238+#endif
239+ strhead->referenceCount = 0;
240+ }
241+ }
242+
243+ return structure;
244+}
245+
163246 //
164247 //Internal functions
165248 //
--- a/CHNOSProject/chn/chnlib.h
+++ b/CHNOSProject/chn/chnlib.h
@@ -27,6 +27,7 @@
2727
2828 #ifdef DEBUG
2929 //#define DEBUG_MEMORY_ALLOCATION
30+//#define DEBUG_MEMORY_REFERENCE_COUNT
3031 #endif
3132
3233 //
@@ -42,6 +43,8 @@ struct CHNLIB_STRUCTURE_HEADER {
4243 //ライブラリ共通構造体ヘッダ
4344 uint signature;
4445 uint typeid;
46+ uint referenceCount;
47+ void (*destructor)(void **structrue);
4548 };
4649
4750 //
@@ -61,6 +64,10 @@ struct CHNLIB_STRUCTURE_HEADER {
6164
6265 #define CHNLIB_UIPArray_INDEX_NOTFOUND -1
6366
67+#define retain(structure) CHNLIB_Retain((void **) &structure)
68+#define release(structure) CHNLIB_Release((void **) &structure)
69+#define autorelease(structure) CHNLIB_AutoRelease(structure)
70+
6471 //
6572 //Declare library functions
6673 //
@@ -72,7 +79,9 @@ void CHNLIB_StructureHeader_Initialize(CHNLIB_StructureHeader *header, uint type
7279 uint CHNLIB_StructureHeader_GetTypeID(const void *structure);
7380 void *CHNLIB_System_AllocateMemory_Strict(int size, const char filename[], int line, const char funcname[]);
7481 void CHNLIB_System_FreeMemory(void *p, const char filename[], int line, const char funcname[]);
75-
82+void CHNLIB_Retain(void **structure);
83+void CHNLIB_Release(void **structure);
84+void *CHNLIB_AutoRelease(void *structure);
7685 //@chnlib00.c
7786 typedef struct CHNLIB_UIPARRAY CHNLIB_UIPArray;
7887 struct CHNLIB_UIP_ARRAY;
@@ -101,8 +110,8 @@ void *CHNLIB_UIPArray_GetPointerByData32(const CHNLIB_UIPArray *array, uint data
101110 int CHNLIB_UIPArray_GetIndexByData32(const CHNLIB_UIPArray *array, uint data32);
102111 //----配列のコピー
103112 CHNLIB_UIPArray *CHNLIB_UIPArray_CopyArray(const CHNLIB_UIPArray *source);
104-//----配列のソート
105-CHNLIB_UIPArray *CHNLIB_UIPArray_SortInDescendingOrderByData32(const CHNLIB_UIPArray *array);
113+//----配列のソートしたものを新たに取得
114+CHNLIB_UIPArray *CHNLIB_UIPArray_GetSortedInDescendingOrderByData32(const CHNLIB_UIPArray *array);
106115
107116 //@chnlib01.c
108117 typedef struct CHNLIB_STRING CHNLIB_String;
--- a/CHNOSProject/chn/chnlib00.c
+++ b/CHNOSProject/chn/chnlib00.c
@@ -26,6 +26,7 @@
2626 //Declare internal functions
2727 //
2828 CHNLIB_UIPArray *CHNLIB_UIPArray_Internal_Allocate(void);
29+void CHNLIB_UIPArray_Internal_Destruct(void **structure);
2930
3031 //
3132 //Define types
@@ -39,6 +40,7 @@ struct CHNLIB_UIPARRAY_INTERNAL_TAG {
3940
4041 struct CHNLIB_UIPARRAY {
4142 //UnsignedIntとPointerを格納するArray構造体
43+ //headerはすべてのpackにあるが、ReferenceCountはその特性上、先頭のものだけが有効である。
4244 CHNLIB_StructureHeader header;
4345
4446 int using_tags; //Number of using tags in this pack.
@@ -59,6 +61,7 @@ int CHNLIB_UIPArray_AppendLast(CHNLIB_UIPArray **array, uint data32, void *point
5961 {
6062 //戻り値:追加されたタグのIndex
6163 //Arrayの末尾にデータを追加する。
64+ //追加された要素のpointerの参照先はArrayが所有する(Retain)
6265 int index;
6366
6467 index = 0;
@@ -73,6 +76,8 @@ int CHNLIB_UIPArray_AppendLast(CHNLIB_UIPArray **array, uint data32, void *point
7376 (*array)->tag[(*array)->using_tags].pointer = pointer;
7477 (*array)->using_tags++;
7578
79+ CHNLIB_Retain(&pointer);
80+
7681 return index + (*array)->using_tags - 1;
7782 }
7883
@@ -98,7 +103,7 @@ int CHNLIB_UIPArray_RemoveByIndex(CHNLIB_UIPArray **array, int index)
98103 {
99104 //戻り値:削除が成功したか(0:成功,-1:指定された添字の要素は存在しない)
100105 //indexが指し示す要素を削除し、後続の要素は前へつめる。
101- //削除された要素に格納されていたデータに関しては関知しない。
106+ //削除された要素のpointerの参照先の所有を放棄する(Release)。
102107 int i;
103108
104109 for(; (index / CHNLIB_UIPArray_INTERNAL_PACKDATAS) > 0; index -= CHNLIB_UIPArray_INTERNAL_PACKDATAS){
@@ -108,11 +113,17 @@ int CHNLIB_UIPArray_RemoveByIndex(CHNLIB_UIPArray **array, int index)
108113 array = &(*array)->next;
109114 }
110115 if(*array == NULL){
116+ //大雑把に見て、配列の範囲外
111117 return -1;
112118 }
113119 if(index >= (*array)->using_tags){
120+ //詳細に見て、一つのパックの中で、配列の範囲外
114121 return -1;
115122 }
123+
124+ //参照先の所有を放棄
125+ CHNLIB_Release(&(*array)->tag[index].pointer);
126+
116127 for(i = index; i < (*array)->using_tags - 1; i++){
117128 (*array)->tag[i] = (*array)->tag[i + 1];
118129 }
@@ -120,8 +131,8 @@ int CHNLIB_UIPArray_RemoveByIndex(CHNLIB_UIPArray **array, int index)
120131 if((*array)->next != NULL){
121132 if((*array)->using_tags != CHNLIB_UIPArray_INTERNAL_PACKDATAS || (*array)->next->using_tags == 0){
122133 //タグ数とリンクの関係があわない。以下の状態になっている。
123- //次のパックがあるのに、間のパックが使い切られていない。
124- //次のパックがあるのに、次のパックに一つもデータがない。
134+ //-次のパックがあるのに、間のパックが使い切られていない。
135+ //-次のパックがあるのに、次のパックに一つもデータがない。
125136 CHNLIB_ReportError("Internal data error.", CHNLIB_DEBUG_ARGUMENTS);
126137 }
127138 (*array)->tag[CHNLIB_UIPArray_INTERNAL_PACKDATAS - 1] = (*array)->next->tag[0];
@@ -311,7 +322,7 @@ CHNLIB_UIPArray *CHNLIB_UIPArray_CopyArray(const CHNLIB_UIPArray *source)
311322 return destination;
312323 }
313324
314-CHNLIB_UIPArray *CHNLIB_UIPArray_SortInDescendingOrderByData32(const CHNLIB_UIPArray *array)
325+CHNLIB_UIPArray *CHNLIB_UIPArray_GetSortedInDescendingOrderByData32(const CHNLIB_UIPArray *array)
315326 {
316327 //タグをData32の値で降順(大きい方から小さい方)になるように整列した新たなArrayを返す。
317328 CHNLIB_UIPArray *sorted, *temp;
@@ -349,6 +360,8 @@ CHNLIB_UIPArray *CHNLIB_UIPArray_SortInDescendingOrderByData32(const CHNLIB_UIPA
349360 }
350361 }
351362
363+ release(temp);
364+
352365 return sorted;
353366 }
354367
@@ -364,8 +377,43 @@ CHNLIB_UIPArray *CHNLIB_UIPArray_Internal_Allocate(void)
364377 tag = (CHNLIB_UIPArray *)CHNLIB_System_AllocateMemory_Strict(sizeof(CHNLIB_UIPArray), CHNLIB_DEBUG_ARGUMENTS);
365378
366379 CHNLIB_StructureHeader_Initialize(&tag->header, CHNLIB_STRUCT_ID_UIPArray);
380+ tag->header.destructor = &CHNLIB_UIPArray_Internal_Destruct;
367381
368382 return tag;
369383
370384 }
371385
386+void CHNLIB_UIPArray_Internal_Destruct(void **structure)
387+{
388+ //デストラクタ(実際にRelease->freeされる時に呼ばれる)
389+ //方針:すべてのオブジェクトをReleaseした後、すべてのパックをFreeする。
390+ int i;
391+ CHNLIB_UIPArray *array;
392+
393+ if(structure == NULL){
394+ return;
395+ }
396+ if(CHNLIB_StructureHeader_GetTypeID(*structure) != CHNLIB_STRUCT_ID_UIPArray){
397+ return;
398+ }
399+
400+#ifdef DEBUG_MEMORY_REFERENCE_COUNT
401+ CHNLIB_Debug("Release(with free)[%p].", CHNLIB_DEBUG_ARGUMENTS, *structure);
402+#endif
403+
404+ array = *structure;
405+
406+ for(; array != NULL; array = array->next){
407+ for(i = 0; i < array->using_tags; i++){
408+ if(array->tag[i].pointer != NULL){
409+ CHNLIB_Release(&array->tag[i].pointer);
410+ }
411+ }
412+ }
413+
414+ CHNLIB_UIPArray_FreeOnlyArray((CHNLIB_UIPArray **)structure);
415+
416+ return;
417+}
418+
419+
--- a/CHNOSProject/chn/chnlib01.c
+++ b/CHNOSProject/chn/chnlib01.c
@@ -31,6 +31,7 @@ char *CHNLIB_String_Intenal_NullCString = "";
3131 //
3232
3333 CHNLIB_String *CHNLIB_String_Internal_Allocate(void);
34+void CHNLIB_String_Internal_Destruct(void **structure);
3435
3536 //
3637 //Define types
@@ -53,6 +54,7 @@ CHNLIB_String *CHNLIB_String_Initialize(const char str[])
5354 CHNLIB_String *strtag;
5455
5556 strtag = CHNLIB_String_Internal_Allocate();
57+ strtag->header.destructor = &CHNLIB_String_Internal_Destruct;
5658
5759 if(str != NULL){
5860 CHNLIB_String_SetStringFromCString(strtag, str);
@@ -357,4 +359,22 @@ CHNLIB_String *CHNLIB_String_Internal_Allocate(void)
357359 CHNLIB_StructureHeader_Initialize(&tag->header, CHNLIB_STRUCT_ID_String);
358360
359361 return tag;
360-}
\ No newline at end of file
362+}
363+
364+void CHNLIB_String_Internal_Destruct(void **structure)
365+{
366+ //デストラクタ(実際にRelease->freeされる時に呼ばれる)
367+ if(structure == NULL){
368+ return;
369+ }
370+
371+#ifdef DEBUG_MEMORY_REFERENCE_COUNT
372+ CHNLIB_Debug("Release(with free)[%p].", CHNLIB_DEBUG_ARGUMENTS, *structure);
373+#endif
374+
375+ CHNLIB_String_Free(*structure);
376+
377+ *structure = NULL;
378+
379+ return;
380+}
--- a/CHNOSProject/chn/chnlib02.c
+++ b/CHNOSProject/chn/chnlib02.c
@@ -62,7 +62,9 @@ int CHNLIB_UIPArray_GetSeparatedStringByUIPArray(CHNLIB_UIPArray **separated, co
6262 {
6363 //listにある文字列でsを分割し、その結果をseparatedに追加する。
6464 //listに存在する文字列は新しくメモリを確保せず、リスト中のStringタグへのポインタをseparatedに記録する。
65- //従ってseparatedを解放する際は、listのdata32をすべてTrueにした上で、CHNLIB_UIPArray_FreeSelectedAllを利用するべきである。
65+ ////従ってseparatedを解放する際は、listのdata32をすべてTrueにした上で、CHNLIB_UIPArray_FreeSelectedAllを利用するべきである。
66+ //referenceCountは、list中の文字列は2以上になり、listにない文字列は1となる。
67+ //従って、separatedをreleaseすれば、安全に解放できる。
6668 //listにない文字列はdata32==False(0)
6769 //listにある文字列はdata32==tag->data32
6870 int index, end, end_end, location;
@@ -79,14 +81,14 @@ int CHNLIB_UIPArray_GetSeparatedStringByUIPArray(CHNLIB_UIPArray **separated, co
7981 //もうリストの文字列はない
8082 if(end != end_end){
8183 //でもまだ文字列は残っている
82- CHNLIB_UIPArray_AppendLast(separated, False, CHNLIB_String_ExtractByLength(s, end, CHNLIB_MAX_STRING_LENGTH));
84+ CHNLIB_UIPArray_AppendLast(separated, False, autorelease(CHNLIB_String_ExtractByLength(s, end, CHNLIB_MAX_STRING_LENGTH)));
8385 }
8486 break;
8587 }
8688 //リストの文字列が見つかった
8789 if(location != 0){
8890 //リストの文字列の前に、リストにない文字列がある
89- CHNLIB_UIPArray_AppendLast(separated, False, CHNLIB_String_ExtractByLength(s, end, location));
91+ CHNLIB_UIPArray_AppendLast(separated, False, autorelease(CHNLIB_String_ExtractByLength(s, end, location)));
9092 end += location;
9193 }
9294 CHNLIB_UIPArray_AppendLast(separated, CHNLIB_UIPArray_GetData32ByIndex(list, index), CHNLIB_UIPArray_GetPointerByIndex(list, index));
@@ -116,7 +118,7 @@ int CHNLIB_UIPArray_GetSeparatedUTF8Character(CHNLIB_UIPArray **separated, const
116118 //終端文字
117119 break;
118120 }
119- CHNLIB_UIPArray_AppendLast(separated, u, CHNLIB_String_ExtractByLength(s, (int)(p - p_base), (int)(q - p)));
121+ CHNLIB_UIPArray_AppendLast(separated, u, autorelease(CHNLIB_String_ExtractByLength(s, (int)(p - p_base), (int)(q - p))));
120122 p = q;
121123 }
122124
--- a/CHNOSProject/chn/chnlib04.c
+++ b/CHNOSProject/chn/chnlib04.c
@@ -118,6 +118,7 @@ uint CHNLIB_UTF8_GetNextUnicodeOfCharacter(const char s[], const char **next)
118118 }
119119
120120 unicode = 0;
121+ mbsize = 0;
121122 for(i = 0; s[i] != '\0'; i++){
122123 n = CHNLIB_UTF8_GetCharacterType(s[i]);
123124 switch(n){
--- a/CHNOSProject/libtest/libtest/main.c
+++ b/CHNOSProject/libtest/libtest/main.c
@@ -11,28 +11,26 @@
1111
1212 int main(int argc, const char * argv[])
1313 {
14- CHNLIB_String *s, *t;
14+ //CHNLIB_String *s, *t;
1515 CHNLIB_UIPArray *separated;
16+ char str[1024];
17+ int i;
1618
1719 separated = CHNLIB_UIPArray_Initialize();
18- s = CHNLIB_String_Initialize("あいうえおかきくけこ");
19- t = CHNLIB_String_Initialize("た");
2020
21- CHNLIB_UIPArray_GetSeparatedUTF8Character(&separated, s);
21+ for(i = 0; i < 32; i++){
22+ snprintf(str, sizeof(str), "Number %d.", i);
23+ CHNLIB_UIPArray_AppendLast(&separated, i, autorelease(CHNLIB_String_Initialize(str)));
24+ }
2225
23- printf("%d bytes, %d characters\n", CHNLIB_String_GetLength(s), CHNLIB_UTF8_GetStringLengthByCharacter(CHNLIB_String_GetReferencePointerOfCString(s)));
24- printf("%d bytes, %d characters\n", CHNLIB_String_GetLength(t), CHNLIB_UTF8_GetStringLengthByCharacter(CHNLIB_String_GetReferencePointerOfCString(t)));
26+ CHNLIB_Debug_PrintStructureData(separated, 0);
27+
28+ //CHNLIB_Release((void **)&separated);
29+ release(separated);
2530
26- printf("match %d character(s).\n", CHNLIB_UTF8_CompareString_LeftHand(CHNLIB_String_GetReferencePointerOfCString(s), CHNLIB_String_GetReferencePointerOfCString(t)));
27- printf("match %d character(s).\n", CHNLIB_UTF8_CompareString_LeftHand(CHNLIB_String_GetReferencePointerOfCString(t), CHNLIB_String_GetReferencePointerOfCString(s)));
28- printf("match %d character(s).\n", CHNLIB_UTF8_CompareString_LeftHand(CHNLIB_String_GetReferencePointerOfCString(s), CHNLIB_String_GetReferencePointerOfCString(s)));
29- printf("match %d character(s).\n", CHNLIB_UTF8_CompareString_LeftHand(CHNLIB_String_GetReferencePointerOfCString(t), CHNLIB_String_GetReferencePointerOfCString(t)));
30-
31- printf("%d\n", CHNLIB_UTF8_GetCountOfContain(CHNLIB_String_GetReferencePointerOfCString(s), CHNLIB_String_GetReferencePointerOfCString(t)));
3231
3332 CHNLIB_Debug_PrintStructureData(separated, 0);
34- printf("%d\n", CHNLIB_UIPArray_RemoveByIndex(&separated, 0));
35- CHNLIB_Debug_PrintStructureData(separated, 0);
33+
3634 return 0;
3735 }
3836
Show on old repository browser