@@ -20,33 +20,31 @@ #import "ObjFWRT.h" #import "private.h" struct objc_sparsearray * -objc_sparsearray_new(uint8_t indexSize) +objc_sparsearray_new(uint8_t levels) { struct objc_sparsearray *sparsearray; - if ((sparsearray = calloc(1, sizeof(*sparsearray))) == NULL) + if ((sparsearray = calloc(1, sizeof(*sparsearray))) == NULL || + (sparsearray->data = calloc(1, sizeof(*sparsearray->data))) == NULL) OBJC_ERROR("Failed to allocate memory for sparse array!"); - if ((sparsearray->data = calloc(1, sizeof(*sparsearray->data))) == NULL) - OBJC_ERROR("Failed to allocate memory for sparse array!"); - - sparsearray->indexSize = indexSize; + sparsearray->levels = levels; return sparsearray; } void * objc_sparsearray_get(struct objc_sparsearray *sparsearray, uintptr_t idx) { struct objc_sparsearray_data *iter = sparsearray->data; - for (uint8_t i = 0; i < sparsearray->indexSize - 1; i++) { + for (uint8_t i = 0; i < sparsearray->levels - 1; i++) { uintptr_t j = - (idx >> ((sparsearray->indexSize - i - 1) * 8)) & 0xFF; + (idx >> ((sparsearray->levels - i - 1) * 8)) & 0xFF; if ((iter = iter->next[j]) == NULL) return NULL; } @@ -57,13 +55,13 @@ objc_sparsearray_set(struct objc_sparsearray *sparsearray, uintptr_t idx, void *value) { struct objc_sparsearray_data *iter = sparsearray->data; - for (uint8_t i = 0; i < sparsearray->indexSize - 1; i++) { + for (uint8_t i = 0; i < sparsearray->levels - 1; i++) { uintptr_t j = - (idx >> ((sparsearray->indexSize - i - 1) * 8)) & 0xFF; + (idx >> ((sparsearray->levels - i - 1) * 8)) & 0xFF; if (iter->next[j] == NULL) if ((iter->next[j] = calloc(1, sizeof(struct objc_sparsearray_data))) == NULL) OBJC_ERROR("Failed to allocate memory for " @@ -88,8 +86,8 @@ } void objc_sparsearray_free(struct objc_sparsearray *sparsearray) { - freeSparsearrayData(sparsearray->data, sparsearray->indexSize); + freeSparsearrayData(sparsearray->data, sparsearray->levels); free(sparsearray); }