64 lines
1,008 B
C++
64 lines
1,008 B
C++
|
|
||
|
#include "array.h"
|
||
|
|
||
|
#include <stdlib.h>
|
||
|
#include <assert.h>
|
||
|
|
||
|
typedef struct
|
||
|
{
|
||
|
int itemcount;
|
||
|
int itemsize;
|
||
|
} ArrayHeader;
|
||
|
|
||
|
void
|
||
|
ArrayResize(void** array, int itemcount)
|
||
|
{
|
||
|
ArrayHeader* head = (ArrayHeader*)*array - 1;
|
||
|
|
||
|
assert(*array);
|
||
|
|
||
|
head->itemcount = itemcount;
|
||
|
|
||
|
head = realloc(head, head->itemcount * head->itemsize + sizeof(ArrayHeader));
|
||
|
|
||
|
assert(head);
|
||
|
|
||
|
*array = head;
|
||
|
}
|
||
|
|
||
|
int
|
||
|
ArrayItemSize(void* array)
|
||
|
{
|
||
|
return ((ArrayHeader*)array)[-1].itemsize;
|
||
|
}
|
||
|
|
||
|
int
|
||
|
ArrayItemCount(void* array)
|
||
|
{
|
||
|
return ((ArrayHeader*)array)[-1].itemcount;
|
||
|
}
|
||
|
|
||
|
void*
|
||
|
ArrayCreate(int itemcount, int itemsize)
|
||
|
{
|
||
|
ArrayHeader* array = malloc(itemcount * itemsize + sizeof(ArrayHeader));
|
||
|
|
||
|
assert(array);
|
||
|
|
||
|
array->itemcount = itemcount;
|
||
|
array->itemsize = itemsize;
|
||
|
|
||
|
return (ArrayHeader*)array + 1;
|
||
|
}
|
||
|
|
||
|
void
|
||
|
ArrayDelete(void* array)
|
||
|
{
|
||
|
assert(array);
|
||
|
|
||
|
ArrayResize(array, 0);
|
||
|
|
||
|
free((ArrayHeader*)array - 1);
|
||
|
}
|
||
|
|