Author: mturk Date: Wed Aug 26 16:08:41 2009 New Revision: 808083 URL: http://svn.apache.org/viewvc?rev=808083&view=rev Log: Add few handy array management utils
Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr_memory.h commons/sandbox/runtime/trunk/src/main/native/shared/memory.c Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr_memory.h URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr_memory.h?rev=808083&r1=808082&r2=808083&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/include/acr_memory.h (original) +++ commons/sandbox/runtime/trunk/src/main/native/include/acr_memory.h Wed Aug 26 16:08:41 2009 @@ -139,6 +139,55 @@ ACR_DECLARE(void *) ACR_SbhCalloc(JNIEnv *env, const char *file, int line, acr_sbh_t *sbh, acr_size_t size); +/** + * Apache's "replacement" for the malloc() function that throws + * thejava.lang.OutOfMemoryError in case of failure. + * @param env Current JNI environment. + * @param size Allocation size + * @param file Source file where the function was called + * @param line Source file line where the function was called + * @return Pointer to allocated memory. + */ +ACR_DECLARE(void *) ACR_Malloc(JNIEnv *env, const char *file, int line, + acr_size_t size); + +/** + * Allocate pointer array function that throws + * thejava.lang.OutOfMemoryError in case of failure. + * @param env Current JNI environment. + * @param size Allocation size + * @param file Source file where the function was called + * @param line Source file line where the function was called + * @return Pointer to allocated array of pointers. + * @note Allocated pointers are set to NULL. + */ +ACR_DECLARE(void **) ACR_Aalloc(JNIEnv *env, const char *file, int line, + acr_size_t size); + +/** + * Free the array of pointers calling free on all array members. + * @param env Current JNI environment. + * @param array Pointer array to free + * @param file Source file where the function was called + * @param line Source file line where the function was called. + * @note Function calls free on each non NULL array member and stops + * on first NULL pointer. This means that pointer array must not + * be interleaved with NULL pointers. + */ +ACR_DECLARE(void) ACR_Afree(JNIEnv *env, const char *file, int line, + void **array); + +/** + * Count the number of non NULL elemenst in the array. + * @param array Pointer array to use. + * @note Use this function only with arrays allocated with ACR_Aalloc + * or with the array that have terminating NULL pointer. If the + * array is not NULL terminated the function will go beyond array + * size leading to faulty results. + * In case NULL is provided for array the function returns zero. + */ +ACR_DECLARE(size_t) ACR_Asize(const void * const *array); + #ifdef __cplusplus } #endif Modified: commons/sandbox/runtime/trunk/src/main/native/shared/memory.c URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/memory.c?rev=808083&r1=808082&r2=808083&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/shared/memory.c (original) +++ commons/sandbox/runtime/trunk/src/main/native/shared/memory.c Wed Aug 26 16:08:41 2009 @@ -253,6 +253,60 @@ free(sbh); } +ACR_DECLARE(void **) ACR_Aalloc(JNIEnv *_E, const char *file, int line, + size_t size) +{ + void **arr = x_calloc((size + 1) * sizeof(void *)); + + if (!arr) { + int err = ACR_GET_OS_ERROR(); + if (_E == NULL) + _E = ACR_GetJNIEnv(); + if (IS_VALID_HANDLE(_E)) + ACR_ThrowException(_E, file, line, ACR_EX_ENOMEM, err); + } + return arr; +} + +ACR_DECLARE(void) ACR_Afree(JNIEnv *_E, const char *file, int line, + void **arr) +{ + void **ptr = arr; + if (IS_INVALID_HANDLE(arr)) { + if (_E == NULL) + _E = ACR_GetJNIEnv(); + if (IS_VALID_HANDLE(_E)) + ACR_ThrowException(_E, file, line, ACR_EX_ENULL, + ACR_EISNULL); + } + else { + while (*ptr != NULL) + free(*(ptr++)); + free(arr); + } +} + +ACR_DECLARE(size_t) ACR_Asize(const void * const *arr) +{ + const void * const *ptr = arr; + if (arr) { + size_t len = 0; + while (*ptr != NULL) { + ptr++; + len++; +#if defined (DEBUG) + if (len > (SIZE_T_MAX / 2)) { + /* This is probably ubterminated array + */ + return 0; + } +#endif + } + return len; + } + else + return 0; +} static int memory_pointer_cleanup(void *mem, size_t unused) {