[Opensrf-commits] r1617 - in trunk: include/opensrf src/libopensrf

svn at svn.open-ils.org svn at svn.open-ils.org
Mon Jan 12 00:35:13 EST 2009


Author: scottmk
Date: 2009-01-12 00:35:10 -0500 (Mon, 12 Jan 2009)
New Revision: 1617

Modified:
   trunk/include/opensrf/string_array.h
   trunk/src/libopensrf/string_array.c
Log:
1. Replaced the innards of an osrfStringArray to include an
osrfList instead of a pointer to an osrfList.  This change
eliminates a layer of malloc and free when creating and
destroying an osrfStringArray.  It also eliminates a layer
of indirection when performing other operations.

2. Eliminated the prototype for string_array_get_total_size(),
since no such function exists.

3. Added the const qualifier to various function parameters.


Modified: trunk/include/opensrf/string_array.h
===================================================================
--- trunk/include/opensrf/string_array.h	2009-01-11 22:11:34 UTC (rev 1616)
+++ trunk/include/opensrf/string_array.h	2009-01-12 05:35:10 UTC (rev 1617)
@@ -13,32 +13,27 @@
 
 #define STRING_ARRAY_MAX_SIZE 4096
 
-#define OSRF_STRING_ARRAY_FREE(arr)\
-    if(arr) {osrfListFree(arr->list); free(arr);}
-        
+#define OSRF_STRING_ARRAY_FREE(arr) osrfListFree( (osrfList*) (arr) )
 
-struct string_array_struct {
-    osrfList* list;
-    int size;
-};
-typedef struct string_array_struct osrfStringArray;
+typedef struct {
+    osrfList list;
+	int size;    // redundant with osrfList.size
+} osrfStringArray;
 
-osrfStringArray* osrfNewStringArray(int size);
+osrfStringArray* osrfNewStringArray( int size );
 
-void osrfStringArrayAdd(osrfStringArray*, char* str);
+void osrfStringArrayAdd( osrfStringArray*, const char* str );
 
-char* osrfStringArrayGetString(osrfStringArray* arr, int index);
+char* osrfStringArrayGetString( osrfStringArray* arr, int index );
 
 /* returns true if this array contains the given string */
-int osrfStringArrayContains( osrfStringArray* arr, char* string );
+int osrfStringArrayContains(
+	const osrfStringArray* arr, const char* string );
 
-void osrfStringArrayFree(osrfStringArray*);
+void osrfStringArrayFree( osrfStringArray* );
 
-/* total size of all included strings */
-int string_array_get_total_size(osrfStringArray* arr);
+void osrfStringArrayRemove( osrfStringArray* arr, const char* str );
 
-void osrfStringArrayRemove( osrfStringArray* arr, char* str);
-
 #ifdef __cplusplus
 }
 #endif

Modified: trunk/src/libopensrf/string_array.c
===================================================================
--- trunk/src/libopensrf/string_array.c	2009-01-11 22:11:34 UTC (rev 1616)
+++ trunk/src/libopensrf/string_array.c	2009-01-12 05:35:10 UTC (rev 1617)
@@ -5,36 +5,66 @@
 		osrfLogError( OSRF_LOG_MARK, "osrfNewStringArray size is too large");
 
 	osrfStringArray* arr;
-	OSRF_MALLOC( arr, sizeof(osrfStringArray));
-    arr->list = osrfNewListSize(size);
-    osrfListSetDefaultFree(arr->list);
+	OSRF_MALLOC(arr, sizeof(osrfStringArray));
 	arr->size = 0;
+
+	// Initialize list
+	arr->list.size = 0;
+	arr->list.freeItem = NULL;
+	if( size <= 0 )
+		arr->list.arrsize = 16;
+	else
+		arr->list.arrsize = size;
+	OSRF_MALLOC( arr->list.arrlist, arr->list.arrsize * sizeof(void*) );
+
+	// Nullify all pointers in the array
+
+	int i;
+	for( i = 0; i < arr->list.arrsize; ++i )
+		arr->list.arrlist[ i ] = NULL;
+
+	osrfListSetDefaultFree(&arr->list);
 	return arr;
 }
 
-
-void osrfStringArrayAdd(osrfStringArray* arr, char* str) {
-	if(arr == NULL || str == NULL ) return;
-	if( arr->size > STRING_ARRAY_MAX_SIZE ) 
-		osrfLogError( OSRF_LOG_MARK, "osrfStringArrayAdd size is too large");
-    osrfListPush(arr->list, strdup(str));
-    arr->size = arr->list->size;
+void osrfStringArrayAdd( osrfStringArray* arr, const char* string ) {
+	if(arr == NULL || string == NULL ) return;
+	if( arr->list.size > STRING_ARRAY_MAX_SIZE )
+		osrfLogError( OSRF_LOG_MARK, "osrfStringArrayAdd size is too large" );
+    osrfListPush(&arr->list, strdup(string));
+    arr->size = arr->list.size;
 }
 
-char* osrfStringArrayGetString(osrfStringArray* arr, int index) {
+char* osrfStringArrayGetString( osrfStringArray* arr, int index ) {
     if(!arr) return NULL;
-    return OSRF_LIST_GET_INDEX(arr->list, index);
+	return OSRF_LIST_GET_INDEX(&arr->list, index);
 }
 
 void osrfStringArrayFree(osrfStringArray* arr) {
-    OSRF_STRING_ARRAY_FREE(arr);
+
+	// This function is a sleazy hack designed to avoid the
+	// need to duplicate the code in osrfListFree().  It
+	// works because:
+	//
+	// 1. The osrfList is the first member of an
+	//    osrfStringArray.  C guarantees that a pointer
+	//    to the one is also a pointer to the other.
+	//
+	// 2. The rest of the osrfStringArray owns no memory
+	//    and requires no special attention when freeing.
+	//
+	// If these facts ever cease to be true, we'll have to
+	// revisit this function.
+	
+	osrfListFree( (osrfList*) arr );
 }
 
-int osrfStringArrayContains( osrfStringArray* arr, char* string ) {
+int osrfStringArrayContains(
+	const osrfStringArray* arr, const char* string ) {
 	if(!(arr && string)) return 0;
 	int i;
 	for( i = 0; i < arr->size; i++ ) {
-        char* str = OSRF_LIST_GET_INDEX(arr->list, i);
+        char* str = OSRF_LIST_GET_INDEX(&arr->list, i);
 		if(str && !strcmp(str, string)) 
             return 1;
 	}
@@ -42,16 +72,16 @@
 	return 0;
 }
 
-void osrfStringArrayRemove( osrfStringArray* arr, char* tstr) {
+void osrfStringArrayRemove( osrfStringArray* arr, const char* tstr ) {
 	if(!(arr && tstr)) return;
 	int i;
     char* str;
 
 	for( i = 0; i < arr->size; i++ ) {
         /* find and remove the string */
-        str = OSRF_LIST_GET_INDEX(arr->list, i);
+        str = OSRF_LIST_GET_INDEX(&arr->list, i);
 		if(str && !strcmp(str, tstr)) {
-            osrfListRemove(arr->list, i);
+            osrfListRemove(&arr->list, i);
 			break;
 		}
 	}
@@ -59,16 +89,14 @@
     /* disable automatic item freeing on delete and shift
      * items up in the array to fill in the gap
      */
-    arr->list->freeItem = NULL;
+    arr->list.freeItem = NULL;
 	for( ; i < arr->size - 1; i++ ) 
-        osrfListSet(arr->list, OSRF_LIST_GET_INDEX(arr->list, i+1) , i);
+        osrfListSet(&arr->list, OSRF_LIST_GET_INDEX(&arr->list, i+1), i);
 
     /* remove the last item since it was shifted up */
-    osrfListRemove(arr->list, i);
+    osrfListRemove(&arr->list, i);
 
     /* re-enable automatic item freeing in delete */
-    osrfListSetDefaultFree(arr->list);
+    osrfListSetDefaultFree(&arr->list);
 	arr->size--;
 }
-
-



More information about the opensrf-commits mailing list