Adjust sourcize.c to use _snprintf if using MSVC.
[SquirrelJME.git] / nanocoat / src / cleanup.c
blob47d352c5b00e723759c0c7b53602e0898c5e49eb
1 /* -*- Mode: C; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // SquirrelJME
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the Mozilla Public License Version 2.0.
7 // See license.mkd for licensing and copyright information.
8 // -------------------------------------------------------------------------*/
10 #include "sjme/cleanup.h"
11 #include "sjme/nvm/rom.h"
12 #include "sjme/nvm/stringPool.h"
13 #include "sjme/nvm/classy.h"
15 /** Simple free. */
16 #define SJME_CLEANUP_FREE(w) \
17 do { if ((w) != NULL) \
18 { \
19 if (sjme_error_is(error = sjme_alloc_free((w)))) \
20 return sjme_error_default(error); \
22 (w) = NULL; \
23 } } while(0)
25 /** Simple close. */
26 #define SJME_CLEANUP_CLOSE(x) \
27 do { if ((x) != NULL) \
28 { \
29 if (sjme_error_is(error = sjme_closeable_close( \
30 SJME_AS_CLOSEABLE((x))))) \
31 return sjme_error_default(error); \
33 (x) = NULL; \
34 } } while(0)
36 /** Cleanup of list. */
37 #define SJME_CLEANUP_LIST(y) \
38 do { if ((y) != NULL) \
39 { \
40 for (i = 0, n = (y)->length; i < n; i++) \
41 SJME_CLEANUP_CLOSE((y)->elements[i]); \
43 SJME_CLEANUP_FREE(y); \
44 } } while(0)
46 static sjme_errorCode sjme_class_classInfoClose(
47 sjme_attrInNotNull sjme_closeable closeable)
49 sjme_errorCode error;
50 sjme_class_info info;
51 sjme_jint i, n;
53 /* Recover. */
54 info = (sjme_class_info)closeable;
55 if (info == NULL)
56 return SJME_ERROR_NULL_ARGUMENTS;
58 SJME_CLEANUP_CLOSE(info->pool);
59 SJME_CLEANUP_CLOSE(info->name);
60 SJME_CLEANUP_CLOSE(info->superName);
61 SJME_CLEANUP_LIST(info->interfaceNames);
62 SJME_CLEANUP_LIST(info->fields);
63 SJME_CLEANUP_LIST(info->methods);
65 /* Success! */
66 return SJME_ERROR_NONE;
70 static sjme_errorCode sjme_class_codeInfoClose(
71 sjme_attrInNotNull sjme_closeable closeable)
73 sjme_errorCode error;
74 sjme_class_codeInfo info;
75 sjme_jint i, n;
77 /* Recover. */
78 info = (sjme_class_codeInfo)closeable;
79 if (info == NULL)
80 return SJME_ERROR_NULL_ARGUMENTS;
82 SJME_CLEANUP_CLOSE(info->inMethod);
83 SJME_CLEANUP_FREE(info->exceptions);
85 /* Success! */
86 return SJME_ERROR_NONE;
89 static sjme_errorCode sjme_class_constantPoolClose(
90 sjme_attrInNotNull sjme_closeable closeable)
92 sjme_errorCode error;
93 sjme_class_poolInfo info;
94 sjme_class_poolEntry* entry;
95 sjme_jint i, n;
97 /* Recover. */
98 info = (sjme_class_poolInfo)closeable;
99 if (info == NULL)
100 return SJME_ERROR_NULL_ARGUMENTS;
102 /* Cleanup any pool entries. */
103 if (info->pool != NULL)
104 for (i = 0, n = info->pool->length; i < n; i++)
106 entry = &info->pool->elements[i];
107 switch (entry->type)
109 case SJME_CLASS_POOL_TYPE_CLASS:
110 SJME_CLEANUP_CLOSE(entry->classRef.descriptor);
111 break;
113 case SJME_CLASS_POOL_TYPE_STRING:
114 SJME_CLEANUP_CLOSE(entry->constString.value);
115 break;
117 case SJME_CLASS_POOL_TYPE_NAME_AND_TYPE:
118 SJME_CLEANUP_CLOSE(entry->nameAndType.descriptor);
119 SJME_CLEANUP_CLOSE(entry->nameAndType.name);
120 break;
122 case SJME_CLASS_POOL_TYPE_UTF:
123 SJME_CLEANUP_CLOSE(entry->utf.utf);
124 break;
128 /* Free list. */
129 SJME_CLEANUP_FREE(info->pool);
131 /* Success! */
132 return SJME_ERROR_NONE;
135 static sjme_errorCode sjme_class_fieldInfoClose(
136 sjme_attrInNotNull sjme_closeable closeable)
138 sjme_errorCode error;
139 sjme_class_fieldInfo info;
140 sjme_jint i, n;
142 /* Recover. */
143 info = (sjme_class_fieldInfo)closeable;
144 if (info == NULL)
145 return SJME_ERROR_NULL_ARGUMENTS;
147 SJME_CLEANUP_CLOSE(info->name);
148 SJME_CLEANUP_CLOSE(info->type);
150 /* Success! */
151 return SJME_ERROR_NONE;
154 static sjme_errorCode sjme_class_methodInfoClose(
155 sjme_attrInNotNull sjme_closeable closeable)
157 sjme_errorCode error;
158 sjme_class_methodInfo info;
159 sjme_jint i, n;
161 /* Recover. */
162 info = (sjme_class_methodInfo)closeable;
163 if (info == NULL)
164 return SJME_ERROR_NULL_ARGUMENTS;
166 SJME_CLEANUP_CLOSE(info->name);
167 SJME_CLEANUP_CLOSE(info->type);
168 SJME_CLEANUP_CLOSE(info->code);
170 /* Success! */
171 return SJME_ERROR_NONE;
174 static sjme_errorCode sjme_desc_identifierClose(
175 sjme_attrInNotNull sjme_closeable closeable)
177 sjme_errorCode error;
178 sjme_desc_identifier info;
180 /* Recover. */
181 info = (sjme_desc_identifier)closeable;
182 if (info == NULL)
183 return SJME_ERROR_NULL_ARGUMENTS;
185 SJME_CLEANUP_CLOSE(info->whole);
187 /* Success! */
188 return SJME_ERROR_NONE;
191 static sjme_errorCode sjme_rom_libraryClose(
192 sjme_attrInNotNull sjme_closeable closeable)
194 sjme_errorCode error;
195 sjme_rom_library inLibrary;
197 /* Recover library. */
198 inLibrary = (sjme_rom_library)closeable;
199 if (inLibrary == NULL)
200 return SJME_ERROR_NULL_ARGUMENTS;
202 /* Delete the prefix if there is one. */
203 if (inLibrary->prefix != NULL)
205 /* Free it. */
206 if (sjme_error_is(error = sjme_alloc_free(
207 (sjme_pointer)inLibrary->prefix)))
208 return sjme_error_default(error);
210 /* Clear. */
211 inLibrary->prefix = NULL;
214 /* Delete name if there is one. */
215 if (inLibrary->name != NULL)
217 /* Free it. */
218 if (sjme_error_is(error = sjme_alloc_free(
219 (sjme_pointer)inLibrary->name)))
220 return sjme_error_default(error);
222 /* Clear. */
223 inLibrary->name = NULL;
226 /* Forward close handler. */
227 if (inLibrary->functions->close != NULL)
228 return inLibrary->functions->close(inLibrary);
230 /* Success! */
231 return SJME_ERROR_NONE;
234 static sjme_errorCode sjme_rom_suiteClose(
235 sjme_attrInNotNull sjme_closeable closeable)
237 if (closeable == NULL)
238 return SJME_ERROR_NULL_ARGUMENTS;
240 sjme_todo("Impl?");
241 return sjme_error_notImplemented(0);
244 static sjme_errorCode sjme_stringPool_close(
245 sjme_attrInNullable sjme_closeable closeable)
247 sjme_errorCode error;
248 sjme_stringPool stringPool;
249 sjme_jint i, n;
250 sjme_list_sjme_stringPool_string* strings;
251 sjme_stringPool_string target;
253 /* Recover pool. */
254 stringPool = (sjme_stringPool)closeable;
255 if (stringPool == NULL)
256 return SJME_ERROR_NULL_ARGUMENTS;
258 /* Loaded string cleanup. */
259 SJME_CLEANUP_LIST(stringPool->strings);
261 /* Success! */
262 return SJME_ERROR_NONE;
265 static sjme_errorCode sjme_stringPool_stringClose(
266 sjme_attrInNotNull sjme_closeable closeable)
268 sjme_errorCode error;
269 sjme_stringPool_string info;
271 /* Recover. */
272 info = (sjme_stringPool_string)closeable;
273 if (info == NULL)
274 return SJME_ERROR_NULL_ARGUMENTS;
276 /* Success! */
277 return SJME_ERROR_NONE;
280 /* ------------------------------------------------------------------------ */
282 sjme_errorCode sjme_nvm_allocR(
283 sjme_attrInNotNull sjme_alloc_pool* inPool,
284 sjme_attrInPositiveNonZero sjme_jint allocSize,
285 sjme_attrInValue sjme_nvm_structType inType,
286 sjme_attrOutNotNull sjme_nvm_common* outCommon
287 SJME_DEBUG_ONLY_COMMA SJME_DEBUG_DECL_FILE_LINE_FUNC_OPTIONAL)
289 sjme_errorCode error;
290 sjme_closeable_closeHandlerFunc handler;
291 sjme_nvm_common result;
293 if (inPool == NULL || outCommon == NULL)
294 return SJME_ERROR_NULL_ARGUMENTS;
296 if (inType <= SJME_NVM_STRUCT_UNKNOWN ||
297 inType >= SJME_NVM_NUM_STRUCT)
298 return SJME_ERROR_INVALID_ARGUMENT;
300 /* Which handler is used? */
301 handler = NULL;
302 switch (inType)
304 case SJME_NVM_STRUCT_CLASS_INFO:
305 handler = sjme_class_classInfoClose;
306 break;
308 case SJME_NVM_STRUCT_CODE:
309 handler = sjme_class_codeInfoClose;
310 break;
312 case SJME_NVM_STRUCT_FIELD_INFO:
313 handler = sjme_class_fieldInfoClose;
314 break;
316 case SJME_NVM_STRUCT_IDENTIFIER:
317 handler = sjme_desc_identifierClose;
318 break;
320 case SJME_NVM_STRUCT_METHOD_INFO:
321 handler = sjme_class_methodInfoClose;
322 break;
324 case SJME_NVM_STRUCT_POOL:
325 handler = sjme_class_constantPoolClose;
326 break;
328 case SJME_NVM_STRUCT_ROM_LIBRARY:
329 handler = sjme_rom_libraryClose;
330 break;
332 case SJME_NVM_STRUCT_ROM_SUITE:
333 handler = sjme_rom_suiteClose;
334 break;
336 case SJME_NVM_STRUCT_STRING_POOL:
337 handler = sjme_stringPool_close;
338 break;
340 case SJME_NVM_STRUCT_STRING_POOL_STRING:
341 handler = sjme_stringPool_stringClose;
342 break;
344 default:
345 sjme_todo("Impl? %d", inType);
346 return sjme_error_notImplemented(0);
349 /* Allocate result. */
350 result = NULL;
351 #if defined(SJME_CONFIG_DEBUG)
352 if (sjme_error_is(error = sjme_closeable_allocR(inPool,
353 allocSize, handler, SJME_JNI_TRUE,
354 SJME_AS_CLOSEABLEP(&result), file, line, func)) ||
355 result == NULL)
356 #else
357 if (sjme_error_is(error = sjme_closeable_alloc(inPool,
358 allocSize, handler, SJME_JNI_TRUE,
359 SJME_AS_CLOSEABLEP(&result))) || result == NULL)
360 #endif
361 return sjme_error_default(error);
363 /* Set fields. */
364 result->type = inType;
366 /* Success! */
367 *outCommon = result;
368 return SJME_ERROR_NONE;