|
@@ -7,23 +7,23 @@
|
|
|
|
|
|
static long long allocatedBytes = 0;
|
|
static long long allocatedBytes = 0;
|
|
|
|
|
|
-void asInit(Arrays* as) {
|
|
|
|
|
|
+void asInit(SnuviArrays* as) {
|
|
as->capacity = 0;
|
|
as->capacity = 0;
|
|
as->usedStart = -1;
|
|
as->usedStart = -1;
|
|
as->freeStart = -1;
|
|
as->freeStart = -1;
|
|
as->data = NULL;
|
|
as->data = NULL;
|
|
}
|
|
}
|
|
|
|
|
|
-void asDelete(Arrays* as) {
|
|
|
|
|
|
+void asDelete(SnuviArrays* as) {
|
|
for(int i = 0; i < as->capacity; i++) {
|
|
for(int i = 0; i < as->capacity; i++) {
|
|
allocatedBytes -= as->data[i].size;
|
|
allocatedBytes -= as->data[i].size;
|
|
free(as->data[i].data);
|
|
free(as->data[i].data);
|
|
}
|
|
}
|
|
- allocatedBytes -= as->capacity * sizeof(Array);
|
|
|
|
|
|
+ allocatedBytes -= as->capacity * sizeof(SnuviArray);
|
|
free(as->data);
|
|
free(as->data);
|
|
}
|
|
}
|
|
|
|
|
|
-static void aInitArray(Array* a, int previous, int next) {
|
|
|
|
|
|
+static void aInitArray(SnuviArray* a, int previous, int next) {
|
|
a->size = 0;
|
|
a->size = 0;
|
|
a->length = 0;
|
|
a->length = 0;
|
|
a->next = next;
|
|
a->next = next;
|
|
@@ -31,7 +31,7 @@ static void aInitArray(Array* a, int previous, int next) {
|
|
a->data = NULL;
|
|
a->data = NULL;
|
|
}
|
|
}
|
|
|
|
|
|
-static void asInitArrays(Arrays* as, int start) {
|
|
|
|
|
|
+static void asInitArrays(SnuviArrays* as, int start) {
|
|
int end = as->capacity - 1;
|
|
int end = as->capacity - 1;
|
|
aInitArray(as->data + start, -1, start + 1);
|
|
aInitArray(as->data + start, -1, start + 1);
|
|
for(int i = start + 1; i < end; i++) {
|
|
for(int i = start + 1; i < end; i++) {
|
|
@@ -41,14 +41,14 @@ static void asInitArrays(Arrays* as, int start) {
|
|
as->freeStart = start;
|
|
as->freeStart = start;
|
|
}
|
|
}
|
|
|
|
|
|
-static bool asEnsureCapacity(Arrays* as) {
|
|
|
|
|
|
+static bool asEnsureCapacity(SnuviArrays* as) {
|
|
if(as->freeStart != -1) {
|
|
if(as->freeStart != -1) {
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
int oldCapacity = as->capacity;
|
|
int oldCapacity = as->capacity;
|
|
int capacity = (as->data == NULL) ? 4 : as->capacity * 2;
|
|
int capacity = (as->data == NULL) ? 4 : as->capacity * 2;
|
|
- int oldBytes = sizeof(Array) * oldCapacity;
|
|
|
|
- int bytes = sizeof(Array) * capacity;
|
|
|
|
|
|
+ int oldBytes = sizeof(SnuviArray) * oldCapacity;
|
|
|
|
+ int bytes = sizeof(SnuviArray) * capacity;
|
|
if(bytes < 0 || allocatedBytes - oldBytes + bytes > MAX_ALLOCATED_BYTES) {
|
|
if(bytes < 0 || allocatedBytes - oldBytes + bytes > MAX_ALLOCATED_BYTES) {
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
@@ -59,17 +59,17 @@ static bool asEnsureCapacity(Arrays* as) {
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
-static void asPrintDebug(Arrays* as) {
|
|
|
|
|
|
+static void asPrintDebug(SnuviArrays* as) {
|
|
printf("Free: %d, Used: %d\n", as->freeStart, as->usedStart);
|
|
printf("Free: %d, Used: %d\n", as->freeStart, as->usedStart);
|
|
for(int i = 0; i < as->capacity; i++) {
|
|
for(int i = 0; i < as->capacity; i++) {
|
|
- Array* a = as->data + i;
|
|
|
|
|
|
+ SnuviArray* a = as->data + i;
|
|
printf("%d: %s, size: %d, next: %d, previous: %d\n", i,
|
|
printf("%d: %s, size: %d, next: %d, previous: %d\n", i,
|
|
a->data == NULL ? "null" : "valid", a->size, a->next,
|
|
a->data == NULL ? "null" : "valid", a->size, a->next,
|
|
a->previous);
|
|
a->previous);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-int asAllocate(Arrays* as, int typeSize, int length) {
|
|
|
|
|
|
+int asAllocate(SnuviArrays* as, int typeSize, int length) {
|
|
(void)asPrintDebug;
|
|
(void)asPrintDebug;
|
|
if(asEnsureCapacity(as)) {
|
|
if(asEnsureCapacity(as)) {
|
|
return -1;
|
|
return -1;
|
|
@@ -83,7 +83,7 @@ int asAllocate(Arrays* as, int typeSize, int length) {
|
|
allocatedBytes += bytes;
|
|
allocatedBytes += bytes;
|
|
|
|
|
|
int index = as->freeStart;
|
|
int index = as->freeStart;
|
|
- Array* array = as->data + index;
|
|
|
|
|
|
+ SnuviArray* array = as->data + index;
|
|
|
|
|
|
as->freeStart = array->next;
|
|
as->freeStart = array->next;
|
|
if(array->next != -1) {
|
|
if(array->next != -1) {
|
|
@@ -102,14 +102,14 @@ int asAllocate(Arrays* as, int typeSize, int length) {
|
|
return index;
|
|
return index;
|
|
}
|
|
}
|
|
|
|
|
|
-Array* asGet(Arrays* as, int p) {
|
|
|
|
|
|
+SnuviArray* asGet(SnuviArrays* as, int p) {
|
|
if(p < 0 || p >= as->capacity || as->data[p].size == 0) {
|
|
if(p < 0 || p >= as->capacity || as->data[p].size == 0) {
|
|
return NULL;
|
|
return NULL;
|
|
}
|
|
}
|
|
return as->data + p;
|
|
return as->data + p;
|
|
}
|
|
}
|
|
|
|
|
|
-void asDeleteArray(Arrays* as, Array* a, int p) {
|
|
|
|
|
|
+void asDeleteArray(SnuviArrays* as, SnuviArray* a, int p) {
|
|
if(a->previous != -1) {
|
|
if(a->previous != -1) {
|
|
as->data[a->previous].next = a->next;
|
|
as->data[a->previous].next = a->next;
|
|
} else {
|
|
} else {
|